FoxIntermediateBackend.Mod 557 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181
  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;
  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. NonPointer = -1; (* special pointer values *)
  53. NoType = 0; (* special type info values *)
  54. LhsIsPointer = 0; (* for the operator kind *)
  55. RhsIsPointer = 1;
  56. (* priority values, lower means higher priority *)
  57. EntryPriority=-4;
  58. FirstPriority=-3;
  59. InitPriority=-2;
  60. ExitPriority=-1;
  61. BasePointerTypeSize = 5;
  62. BaseArrayTypeSize = BasePointerTypeSize + 3;
  63. LengthOffset = BasePointerTypeSize + 0;
  64. DataOffset = BasePointerTypeSize + 1;
  65. DescriptorOffset = BasePointerTypeSize + 2;
  66. BaseRecordTypeSize = BasePointerTypeSize + 2;
  67. ActionOffset = BasePointerTypeSize + 0;
  68. MonitorOffset = BasePointerTypeSize + 1;
  69. BaseObjectTypeSize = BaseRecordTypeSize;
  70. ActionTypeSize = 3;
  71. MonitorTypeSize = 7;
  72. ProcessorOffset = BaseObjectTypeSize + 1;
  73. StackLimitOffset* = BaseObjectTypeSize + 3;
  74. QuantumOffset = BaseObjectTypeSize + 4;
  75. (* flags for optimizations with small matricies and vectors (Alexey Morozov) *)
  76. SmallMatrixFlag = 3; (* flag for identification of a small matrix *)
  77. SmallVectorFlag = 3; (* flag for identification of a small vector *)
  78. Size2Flag = 4; (* size = 2 *)
  79. Size3Flag = 5; (* size = 3 *)
  80. Size4Flag = 6; (* size = 4 *)
  81. Size5Flag = 7; (* size = 5 *)
  82. Size6Flag = 8; (* size = 6 *)
  83. Size7Flag = 9; (* size = 7 *)
  84. Size8Flag = 10; (* size = 8 *)
  85. ReflectionSupport = TRUE;
  86. (* Solution for identifying procedure descriptors on the stack and for being able to differentiate "old school" stack frames from the underlying operating system stack frames:
  87. push a procedure desriptor plus one to where the BP pointer would be located. The misalignment of the procedure descriptor makes it possible to identify that it is not
  88. a base pointer but a procedure descriptor. The base pointer itself is in such cases located at BP + address size.
  89. *)
  90. TYPE
  91. Position=SyntaxTree.Position;
  92. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  93. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  94. Operand = RECORD
  95. mode: SHORTINT;
  96. op: IntermediateCode.Operand;
  97. tag: IntermediateCode.Operand;
  98. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  99. dimOffset: LONGINT;
  100. END;
  101. Fixup= POINTER TO RECORD
  102. pc: LONGINT;
  103. nextFixup: Fixup;
  104. END;
  105. WriteBackCall = POINTER TO RECORD
  106. call: SyntaxTree.ProcedureCallDesignator;
  107. next: WriteBackCall;
  108. END;
  109. Label= OBJECT
  110. VAR
  111. fixups: Fixup;
  112. section: IntermediateCode.Section;
  113. pc: LONGINT;
  114. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  115. BEGIN
  116. SELF.section := section; pc := -1;
  117. END InitLabel;
  118. PROCEDURE Resolve(pc: LONGINT);
  119. VAR at: LONGINT;
  120. BEGIN
  121. SELF.pc := pc;
  122. WHILE(fixups # NIL) DO
  123. at := fixups.pc;
  124. section.PatchAddress(at,pc);
  125. fixups := fixups.nextFixup;
  126. END;
  127. END Resolve;
  128. PROCEDURE AddFixup(at: LONGINT);
  129. VAR fixup: Fixup;
  130. BEGIN
  131. ASSERT(pc=-1);
  132. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  133. END AddFixup;
  134. END Label;
  135. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  136. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  137. VAR
  138. backend: IntermediateBackend;
  139. implementationVisitor: ImplementationVisitor;
  140. meta: MetaDataGenerator;
  141. system: Global.System;
  142. currentScope: SyntaxTree.Scope;
  143. module: Sections.Module;
  144. moduleSelf: SyntaxTree.Variable;
  145. dump: BOOLEAN;
  146. forceModuleBody: BOOLEAN;
  147. addressType: IntermediateCode.Type;
  148. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  149. BEGIN
  150. currentScope := NIL; module := NIL; moduleSelf := NIL;
  151. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  152. SELF.dump := dump;
  153. SELF.backend := backend;
  154. SELF.forceModuleBody := forceModuleBody;
  155. addressType := IntermediateCode.GetType(system,system.addressType)
  156. END Init;
  157. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  158. BEGIN
  159. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  160. END Error;
  161. PROCEDURE Type(x: SyntaxTree.Type);
  162. BEGIN
  163. x.Accept(SELF);
  164. END Type;
  165. (** types **)
  166. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  167. BEGIN (* no code emission *) END VisitBasicType;
  168. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  169. BEGIN (* no code emission *) END VisitCharacterType;
  170. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  171. BEGIN (* no code emission *) END VisitIntegerType;
  172. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  173. BEGIN (* no code emission *) END VisitFloatType;
  174. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  175. BEGIN (* no code emission *) END VisitComplexType;
  176. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  177. VAR type: SyntaxTree.Type;
  178. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  179. type := x.resolved;
  180. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  181. meta.CheckTypeDeclaration(type);
  182. END;
  183. END VisitQualifiedType;
  184. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  185. BEGIN (* no code emission *) END VisitStringType;
  186. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  187. BEGIN (* no code emission *)
  188. END VisitArrayRangeType;
  189. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  190. BEGIN (* no code emission *) END VisitArrayType;
  191. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  192. BEGIN (* no code emission *) END VisitPortType;
  193. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  194. BEGIN
  195. meta.CheckTypeDeclaration(x);
  196. END VisitMathArrayType;
  197. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  198. BEGIN
  199. meta.CheckTypeDeclaration(x);
  200. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  201. END VisitPointerType;
  202. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  203. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  204. BEGIN (* no code emission *)
  205. meta.CheckTypeDeclaration(x);
  206. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  207. IF x.pointerType.typeDeclaration # NIL THEN
  208. td := x.pointerType.typeDeclaration
  209. ELSE
  210. td := x.typeDeclaration
  211. END;
  212. Global.GetSymbolName(td,name);
  213. (* code section for object *)
  214. END;
  215. Scope(x.recordScope);
  216. END VisitRecordType;
  217. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  218. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  219. BEGIN
  220. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  221. WHILE (this # NIL) & (this.identifier# id) DO
  222. this := this.nextModifier;
  223. END;
  224. RETURN this # NIL
  225. END HasFlag;
  226. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  227. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  228. BEGIN
  229. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  230. capabilities := {};
  231. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  232. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  233. backend.SetCapabilities(capabilities);
  234. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  235. Scope(x.cellScope);
  236. END;
  237. END VisitCellType;
  238. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  239. BEGIN (* no code emission *) END VisitProcedureType;
  240. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  241. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  242. END VisitEnumerationType;
  243. (* symbols *)
  244. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  245. BEGIN
  246. Procedure(x);
  247. END VisitProcedure;
  248. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  249. BEGIN
  250. Procedure(x);
  251. END VisitOperator;
  252. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  253. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  254. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  255. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  256. BEGIN
  257. type := type.resolved;
  258. IF type IS SyntaxTree.RecordType THEN
  259. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  260. ELSIF (type IS SyntaxTree.ArrayType) THEN
  261. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  262. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  263. END;
  264. ELSIF type IS SyntaxTree.MathArrayType THEN
  265. WITH type: SyntaxTree.MathArrayType DO
  266. IF type.form = SyntaxTree.Open THEN
  267. RETURN TRUE
  268. ELSIF type.form = SyntaxTree.Static THEN
  269. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  270. END;
  271. END;
  272. END;
  273. RETURN FALSE
  274. END TypeNeedsInitialization;
  275. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  276. VAR variable: SyntaxTree.Variable;
  277. BEGIN
  278. variable := scope.firstVariable;
  279. WHILE variable # NIL DO
  280. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  281. IF variable.initializer # NIL THEN RETURN TRUE END;
  282. variable := variable.nextVariable;
  283. END;
  284. RETURN FALSE
  285. END ScopeNeedsInitialization;
  286. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  287. BEGIN
  288. size := offset - lastUpdated;
  289. IF size > 0 THEN
  290. irv.Emit(Reserve(x.position,size));
  291. END;
  292. irv.Emit(Data(x.position, op));
  293. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  294. END SingleInitialize;
  295. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  296. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  297. BEGIN
  298. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  299. WITH type: SyntaxTree.RecordType DO
  300. baseType := type.baseType;
  301. IF baseType # NIL THEN
  302. baseType := baseType.resolved;
  303. IF baseType IS SyntaxTree.PointerType THEN
  304. baseType := baseType(SyntaxTree.PointerType).pointerBase
  305. END;
  306. Initialize(baseType,NIL, offset);
  307. END;
  308. variable := type.recordScope.firstVariable;
  309. WHILE variable # NIL DO
  310. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  311. variable := variable.nextVariable
  312. END;
  313. | type: SyntaxTree.ArrayType DO
  314. IF type.form = SyntaxTree.Static THEN
  315. baseType := type.arrayBase;
  316. IF TypeNeedsInitialization(baseType) THEN
  317. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  318. FOR i := 0 TO type.staticLength-1 DO
  319. Initialize(baseType,NIL,offset+i*size);
  320. END;
  321. END;
  322. END;
  323. | type: SyntaxTree.MathArrayType DO
  324. IF type.form = SyntaxTree.Open THEN
  325. dim := DynamicDim(type);
  326. baseType := SemanticChecker.ArrayBase(type,dim);
  327. imm := IntermediateCode.Immediate(addressType,dim);
  328. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  329. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  330. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  331. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  332. (* flags remain empty (=0) for open array *)
  333. ELSIF type.form = SyntaxTree.Static THEN
  334. baseType := type.arrayBase;
  335. IF TypeNeedsInitialization(baseType) THEN
  336. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  337. ASSERT(type.staticLength < 1024*1024*1024);
  338. FOR i := 0 TO type.staticLength-1 DO
  339. Initialize(baseType,NIL,offset+i*size);
  340. END;
  341. END;
  342. END;
  343. ELSE
  344. IF initializer # NIL THEN
  345. implementationVisitor.Evaluate(initializer, op);
  346. SingleInitialize(op.op, offset);
  347. END;
  348. END;
  349. END Initialize;
  350. BEGIN
  351. IF x.externalName # NIL THEN RETURN END;
  352. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  353. (* code section for variable *)
  354. Global.GetSymbolSegmentedName(x,name);
  355. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  356. irv.SetExported(IsExported(x));
  357. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  358. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  359. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  360. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  361. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  362. FOR i := 0 TO DynamicDim(x.type)-1 DO
  363. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  364. END;
  365. ELSE
  366. lastUpdated:= 0;
  367. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  368. Initialize(x.type, x.initializer, 0);
  369. END;
  370. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  371. IF size > 0 THEN
  372. irv.Emit(Reserve(x.position,size));
  373. END;
  374. IF ~x.fixed THEN
  375. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  376. ELSE
  377. align := x.alignment;
  378. END;
  379. irv.SetPositionOrAlignment(x.fixed, align);
  380. meta.CheckTypeDeclaration(x.type);
  381. END;
  382. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  383. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  384. END;
  385. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  386. END VisitVariable;
  387. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  388. BEGIN
  389. VisitVariable(x)
  390. END VisitProperty;
  391. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  392. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  393. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  394. BEGIN
  395. ASSERT(currentScope IS SyntaxTree.CellScope);
  396. Global.GetSymbolSegmentedName(x,name);
  397. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  398. irv.SetExported(IsExported(x));
  399. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  400. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  401. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  403. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  404. FOR i := 0 TO DynamicDim(x.type)-1 DO
  405. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  406. END;
  407. ELSE
  408. lastUpdated:= 0;
  409. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  410. IF size > 0 THEN
  411. irv.Emit(Reserve(x.position,size));
  412. END;
  413. IF ~x.fixed THEN
  414. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  415. ELSE
  416. align := x.alignment;
  417. END;
  418. irv.SetPositionOrAlignment(x.fixed, align);
  419. meta.CheckTypeDeclaration(x.type);
  420. END;
  421. END VisitParameter;
  422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  423. BEGIN
  424. Type(x.declaredType); (* => code in objects *)
  425. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  426. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  427. END;
  428. END VisitTypeDeclaration;
  429. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  430. BEGIN
  431. IF (SyntaxTree.Public * x.access # {}) THEN
  432. implementationVisitor.VisitConstant(x);
  433. END;
  434. END VisitConstant;
  435. PROCEDURE Scope(x: SyntaxTree.Scope);
  436. VAR procedure: SyntaxTree.Procedure;
  437. constant: SyntaxTree.Constant;
  438. variable: SyntaxTree.Variable;
  439. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  440. cell: SyntaxTree.CellType;
  441. parameter: SyntaxTree.Parameter;
  442. property: SyntaxTree.Property;
  443. BEGIN
  444. prevScope := currentScope;
  445. currentScope := x;
  446. (* constants treated in implementation visitor *)
  447. WITH x: SyntaxTree.CellScope DO
  448. cell := x.ownerCell;
  449. parameter := cell.firstParameter;
  450. WHILE parameter # NIL DO
  451. VisitParameter(parameter);
  452. parameter := parameter.nextParameter;
  453. END;
  454. property := cell.firstProperty;
  455. WHILE property # NIL DO
  456. VisitProperty(property);
  457. property := property.nextProperty;
  458. END;
  459. ELSE
  460. END;
  461. typeDeclaration := x.firstTypeDeclaration;
  462. WHILE typeDeclaration # NIL DO
  463. VisitTypeDeclaration(typeDeclaration);
  464. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  465. END;
  466. variable := x.firstVariable;
  467. WHILE variable # NIL DO
  468. VisitVariable(variable);
  469. variable := variable.nextVariable;
  470. END;
  471. procedure := x.firstProcedure;
  472. WHILE procedure # NIL DO
  473. VisitProcedure(procedure);
  474. procedure := procedure.nextProcedure;
  475. END;
  476. constant := x.firstConstant;
  477. WHILE constant # NIL DO
  478. VisitConstant(constant);
  479. constant := constant.nextConstant;
  480. END;
  481. currentScope := prevScope;
  482. END Scope;
  483. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  484. VAR parameter: SyntaxTree.Parameter;
  485. BEGIN
  486. parameter := first;
  487. WHILE parameter # NIL DO
  488. VisitParameter(parameter);
  489. parameter := parameter.nextParameter;
  490. END;
  491. END Parameters;
  492. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  493. VAR scope: SyntaxTree.ProcedureScope;
  494. prevScope: SyntaxTree.Scope;
  495. inline, finalizer: BOOLEAN;
  496. procedureType: SyntaxTree.ProcedureType;
  497. pc: LONGINT;
  498. stackSize: LONGINT;
  499. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  500. null,size,src,dest,fp,res: IntermediateCode.Operand;
  501. cc: LONGINT;
  502. cellType: SyntaxTree.CellType;
  503. registerNumber: LONGINT;
  504. registerParameter: Backend.Registers;
  505. registerParameters: LONGINT;
  506. registerClass: IntermediateCode.RegisterClass;
  507. type: IntermediateCode.Type;
  508. formalParameter: SyntaxTree.Parameter;
  509. recordType: SyntaxTree.RecordType;
  510. isModuleBody: BOOLEAN;
  511. parametersSize: LONGINT;
  512. PROCEDURE Signature;
  513. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  514. BEGIN
  515. procedureType := x.type(SyntaxTree.ProcedureType);
  516. returnType := procedureType.returnType;
  517. IF returnType # NIL THEN
  518. meta.CheckTypeDeclaration(returnType)
  519. END;
  520. parameter := procedureType.firstParameter;
  521. WHILE parameter # NIL DO
  522. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  523. parameter := parameter.nextParameter;
  524. END;
  525. END Signature;
  526. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  527. VAR result: BOOLEAN;
  528. BEGIN
  529. result := FALSE;
  530. IF x = SyntaxTree.invalidExpression THEN
  531. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  532. result := TRUE;
  533. value := x.resolved(SyntaxTree.IntegerValue).value;
  534. ELSE
  535. Error(x.position,"expression is not an integer constant");
  536. END;
  537. RETURN result;
  538. END CheckIntegerValue;
  539. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  540. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  541. BEGIN
  542. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  543. WHILE (this # NIL) & (this.identifier # id) DO
  544. this := this.nextModifier;
  545. END;
  546. IF this # NIL THEN
  547. IF this.expression = NIL THEN
  548. Error(this.position,"expected expression value");
  549. ELSIF CheckIntegerValue(this.expression,value) THEN
  550. END;
  551. RETURN TRUE
  552. ELSE RETURN FALSE
  553. END;
  554. END HasValue;
  555. CONST DefaultDataMemorySize=512;
  556. BEGIN
  557. IF x.externalName # NIL THEN RETURN END;
  558. (*
  559. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  560. *)
  561. (* code section for this procedure *)
  562. scope := x.procedureScope;
  563. prevScope := currentScope;
  564. currentScope := scope;
  565. procedureType := x.type(SyntaxTree.ProcedureType);
  566. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  567. implementationVisitor.temporaries.Init;
  568. implementationVisitor.usedRegisters := NIL;
  569. implementationVisitor.registerUsageCount.Init;
  570. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  571. IF (scope.body # NIL) & (x.isInline) THEN
  572. inline := TRUE;
  573. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  574. ir.SetExported(IsExported(x));
  575. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  576. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  577. IF backend.cellsAreObjects THEN
  578. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  579. ir.SetExported(IsExported(x));
  580. ELSE
  581. RETURN; (* cellnet cannot be compiled for final static hardware *)
  582. END;
  583. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  584. inline := FALSE;
  585. AddBodyCallStub(x);
  586. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  587. ir.SetExported(IsExported(x));
  588. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  589. inline := FALSE;
  590. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  591. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  592. AddBodyCallStub(x);
  593. AddStackAllocation(x,stackSize);
  594. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  595. ir.SetExported(IsExported(x));
  596. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  597. inline := FALSE;
  598. Parameters(procedureType.firstParameter);
  599. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  600. ir.SetExported(IsExported(x));
  601. ELSE
  602. inline := FALSE;
  603. IF x.isEntry OR x.isExit THEN
  604. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  605. ir.SetExported(TRUE);
  606. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  607. ELSE
  608. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  609. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  610. END;
  611. END;
  612. cc := procedureType.callingConvention;
  613. IF cc = SyntaxTree.WinAPICallingConvention THEN
  614. parametersSize := ProcedureParametersSize(backend.system,x);
  615. ELSE
  616. parametersSize := 0;
  617. END;
  618. IF scope.body # NIL THEN
  619. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  620. registerNumber := 0;
  621. IF ~inline THEN
  622. IF scope.lastVariable = NIL THEN
  623. stackSize := 0
  624. ELSE
  625. stackSize := scope.lastVariable.offsetInBits;
  626. IF stackSize <0 THEN stackSize := -stackSize END;
  627. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  628. END;
  629. (*
  630. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  632. *)
  633. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  634. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  635. END;
  636. pc := ir.pc-1;
  637. (*
  638. ir.Emit(Nop(position)); (* placeholder for fill *)
  639. *)
  640. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  641. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  642. IF registerParameter = NIL THEN registerParameters := 0
  643. ELSE registerParameters := LEN(registerParameter)
  644. END;
  645. formalParameter := procedureType.lastParameter;
  646. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  647. IF ~PassInRegister(formalParameter) THEN
  648. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  649. ELSE
  650. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  651. type := GetType(system, formalParameter.type);
  652. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  653. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  654. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  655. implementationVisitor.ReleaseIntermediateOperand(src);
  656. INC(registerNumber);
  657. formalParameter := formalParameter.prevParameter;
  658. END;
  659. END;
  660. END;
  661. END;
  662. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  663. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  664. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  665. IF scope.lastVariable # NIL THEN
  666. stackSize := scope.lastVariable.offsetInBits;
  667. IF stackSize <0 THEN stackSize := -stackSize END;
  668. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  669. END;
  670. END;
  671. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  672. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  673. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  674. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  675. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  676. ir.EmitAt(pc,Push(size));
  677. implementationVisitor.StaticCallOperand(result,procedure);
  678. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  679. END;
  680. *)
  681. END;
  682. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  683. IF stackSize > 0 THEN
  684. IF (stackSize MOD system.addressSize = 0) THEN
  685. null := IntermediateCode.Immediate(addressType,0);
  686. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  687. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  688. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  689. ELSE
  690. null := IntermediateCode.Immediate(int8,0);
  691. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  692. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  693. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  694. END;
  695. (*! should potentially be called by backend -- enter might initialize
  696. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  697. *)
  698. END;
  699. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  700. parametersSize := ProcedureParametersSize(backend.system,x);
  701. ELSE
  702. parametersSize := 0;
  703. END;
  704. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  705. finalizer := FALSE;
  706. IF backend.cooperative & x.isFinalizer THEN
  707. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  708. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  709. GetRecordTypeName(recordType,name);
  710. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  711. END;
  712. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  713. IF backend.cooperative THEN
  714. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  715. IF implementationVisitor.profile & ~isModuleBody THEN
  716. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  717. END;
  718. END;
  719. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  720. IF finalizer THEN
  721. IF backend.hasLinkRegister THEN
  722. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  723. END;
  724. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  725. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  726. ir.Emit(Br(x.position,dest));
  727. ELSE
  728. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  729. END;
  730. ELSE
  731. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  732. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  733. END;
  734. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  735. IF backend.cooperative THEN
  736. IF HasPointers (scope) THEN
  737. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  738. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  739. ir.Emit(Result(x.position, res));
  740. ir.Emit(Push(x.position, res));
  741. implementationVisitor.ResetVariables(scope);
  742. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  743. ir.Emit(Pop(x.position, res));
  744. ir.Emit(Return(x.position, res));
  745. ELSE
  746. implementationVisitor.ResetVariables(scope);
  747. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  748. END;
  749. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  750. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  751. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  752. ir.Emit(Result(x.position, res));
  753. ir.Emit(Push(x.position, res));
  754. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  755. ir.Emit(Pop(x.position, res));
  756. ir.Emit(Return(x.position, res));
  757. ELSE
  758. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  759. END;
  760. END;
  761. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  762. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  763. ELSE
  764. ir.Emit(Nop(x.position));
  765. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  766. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  767. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  768. END;
  769. END;
  770. END
  771. END;
  772. ELSE (* force body for procedures *)
  773. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  774. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  775. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  776. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  777. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  778. END;
  779. Scope(scope);
  780. Signature;
  781. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  782. currentScope := prevScope;
  783. END Procedure;
  784. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  785. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  786. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  787. BEGIN
  788. ASSERT (bodyProcedure # NIL);
  789. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  790. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  791. procedure.SetScope(bodyProcedure.scope);
  792. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  793. procedure.SetAccess(SyntaxTree.Hidden);
  794. Global.GetSymbolSegmentedName (procedure,name);
  795. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  796. ir.SetExported(TRUE);
  797. ir.SetPriority(InitPriority);
  798. Global.GetSymbolSegmentedName (bodyProcedure,name);
  799. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  800. implementationVisitor.currentScope := module.module.moduleScope;
  801. implementationVisitor.section := ir;
  802. implementationVisitor.PushSelfPointer();
  803. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  804. ELSIF backend.preregisterStatic THEN
  805. implementationVisitor.currentScope := module.module.moduleScope;
  806. implementationVisitor.section := ir;
  807. implementationVisitor.PushSelfPointer();
  808. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  809. ELSE
  810. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  811. ir.Emit(Call(bodyProcedure.position,op, 0));
  812. END;
  813. END AddBodyCallStub;
  814. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  815. VAR name: Basic.SegmentedName;
  816. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  817. BEGIN
  818. Global.GetSymbolSegmentedName (symbol,name);
  819. Basic.RemoveSuffix(name);
  820. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  821. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  822. ir.SetExported(TRUE);
  823. ir.SetPriority(FirstPriority);
  824. IntermediateCode.InitImmediate(op,addressType,initStack);
  825. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  826. END AddStackAllocation;
  827. (** entry function to visit a complete module *)
  828. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  829. VAR
  830. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  831. hasDynamicOperatorDeclarations: BOOLEAN;
  832. operator: SyntaxTree.Operator;
  833. import: SyntaxTree.Import;
  834. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  835. BEGIN
  836. type := type.resolved;
  837. IF type IS SyntaxTree.RecordType THEN
  838. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  839. ELSIF (type IS SyntaxTree.ArrayType) THEN
  840. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  841. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  842. END;
  843. ELSIF type IS SyntaxTree.MathArrayType THEN
  844. WITH type: SyntaxTree.MathArrayType DO
  845. IF type.form = SyntaxTree.Open THEN
  846. RETURN TRUE
  847. ELSIF type.form = SyntaxTree.Static THEN
  848. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  849. END;
  850. END;
  851. END;
  852. RETURN FALSE
  853. END TypeNeedsInitialization;
  854. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  855. VAR variable: SyntaxTree.Variable;
  856. BEGIN
  857. variable := scope.firstVariable;
  858. WHILE variable # NIL DO
  859. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  860. IF variable.initializer # NIL THEN RETURN TRUE END;
  861. variable := variable.nextVariable;
  862. END;
  863. RETURN FALSE
  864. END ScopeNeedsInitialization;
  865. BEGIN
  866. ASSERT(x # NIL); ASSERT(module # NIL);
  867. SELF.module := module;
  868. (* add import names to the generated Sections.Module *)
  869. import := x.moduleScope.firstImport;
  870. WHILE import # NIL DO
  871. import.module.GetName(idstr);
  872. module.imports.AddName(idstr);
  873. import := import.nextImport
  874. END;
  875. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  876. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  877. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  878. moduleSelf.SetType(system.anyType);
  879. moduleSelf.SetScope(x.moduleScope);
  880. moduleSelf.SetUntraced(TRUE);
  881. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  882. ir.SetExported(TRUE);
  883. IntermediateCode.InitImmediate(op,addressType,0);
  884. ir.Emit(Data(Basic.invalidPosition,op));
  885. END;
  886. implementationVisitor.module := module;
  887. implementationVisitor.moduleScope := x.moduleScope;
  888. implementationVisitor.moduleSelf := moduleSelf;
  889. implementationVisitor.canBeLoaded := TRUE;
  890. meta.SetModule(module);
  891. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  892. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  893. END;
  894. IF backend.profile THEN
  895. EnsureBodyProcedure(x.moduleScope);
  896. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  897. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  898. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  899. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  900. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  901. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  902. Global.GetModuleName(module.module,idstr);
  903. implementationVisitor.ProfilerAddModule(idstr);
  904. implementationVisitor.numberProcedures := 0;
  905. END;
  906. implementationVisitor.profile := backend.profile;
  907. (* check if there is at least one dynamic operator locally defined *)
  908. hasDynamicOperatorDeclarations := FALSE;
  909. operator := x.moduleScope.firstOperator;
  910. WHILE operator # NIL DO
  911. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  912. operator := operator.nextOperator
  913. END;
  914. (* add operator initialization code section *)
  915. IF hasDynamicOperatorDeclarations THEN
  916. EnsureBodyProcedure(x.moduleScope);
  917. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  918. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  919. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  920. END;
  921. Scope(x.moduleScope);
  922. IF hasDynamicOperatorDeclarations THEN
  923. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  924. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  925. END;
  926. IF backend.profile THEN
  927. implementationVisitor.ProfilerPatchInit;
  928. END;
  929. END Module;
  930. END DeclarationVisitor;
  931. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  932. RegisterUsageCount*=OBJECT
  933. VAR used: UsedArray; count: LONGINT;
  934. PROCEDURE &Init;
  935. VAR i: LONGINT;
  936. BEGIN
  937. count := 0;
  938. IF used = NIL THEN NEW(used,64); END;
  939. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  940. END Init;
  941. PROCEDURE Grow;
  942. VAR new: UsedArray; size,i: LONGINT;
  943. BEGIN
  944. size := LEN(used)*2;
  945. NEW(new,size);
  946. FOR i := 0 TO LEN(used)-1 DO
  947. new[i].count := used[i].count;
  948. new[i].type := used[i].type;
  949. new[i].map := used[i].map
  950. END;
  951. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  952. used := new
  953. END Grow;
  954. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  955. BEGIN
  956. INC(count);
  957. IF count = LEN(used) THEN Grow END;
  958. used[count].type := type;
  959. used[count].class := class;
  960. used[count].map := count;
  961. RETURN count;
  962. END Next;
  963. PROCEDURE IncUse(register: LONGINT);
  964. BEGIN
  965. INC(used[register].count);
  966. (*
  967. IF (register = 1) & (count > 30) THEN
  968. D.TraceBack;
  969. END;
  970. *)
  971. END IncUse;
  972. PROCEDURE DecUse(register: LONGINT);
  973. BEGIN
  974. DEC(used[register].count);
  975. END DecUse;
  976. PROCEDURE Map(register: LONGINT): LONGINT;
  977. VAR map : LONGINT;
  978. BEGIN
  979. IF register > 0 THEN
  980. map := used[register].map;
  981. WHILE register # map DO register := map; map := used[register].map END;
  982. END;
  983. RETURN register
  984. END Map;
  985. PROCEDURE Use(register: LONGINT): LONGINT;
  986. BEGIN
  987. IF register< LEN(used) THEN
  988. RETURN used[register].count
  989. ELSE
  990. RETURN 0
  991. END
  992. END Use;
  993. END RegisterUsageCount;
  994. RegisterEntry = POINTER TO RECORD
  995. prev,next: RegisterEntry;
  996. register: LONGINT;
  997. registerClass: IntermediateCode.RegisterClass;
  998. type: IntermediateCode.Type;
  999. END;
  1000. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1001. Variables = OBJECT (Basic.List)
  1002. VAR
  1003. inUse: VariableUse;
  1004. registerIndex: LONGINT;
  1005. PROCEDURE & Init;
  1006. VAR i: LONGINT;
  1007. BEGIN
  1008. InitList(16);
  1009. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1010. registerIndex := 1024;
  1011. END Init;
  1012. PROCEDURE GetUsage(VAR use: VariableUse);
  1013. BEGIN
  1014. use := inUse;
  1015. END GetUsage;
  1016. PROCEDURE SetUsage(CONST use: VariableUse);
  1017. BEGIN
  1018. inUse := use;
  1019. END SetUsage;
  1020. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1021. VAR any: ANY;
  1022. BEGIN
  1023. any := Get(i);;
  1024. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1025. END GetVariable;
  1026. PROCEDURE Occupy(pos: LONGINT);
  1027. BEGIN
  1028. INCL(inUse[pos DIV 32], pos MOD 32);
  1029. END Occupy;
  1030. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1031. BEGIN
  1032. Occupy(Length());
  1033. Add(v);
  1034. END AddVariable;
  1035. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1036. VAR var : SyntaxTree.Variable;
  1037. BEGIN
  1038. FOR pos := 0 TO Length()-1 DO
  1039. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1040. var := GetVariable(pos);
  1041. IF type.SameType(var.type) THEN
  1042. Occupy(pos); RETURN var
  1043. END;
  1044. END;
  1045. END;
  1046. pos := Length();
  1047. RETURN NIL
  1048. END GetFreeVariable;
  1049. END Variables;
  1050. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  1051. SymbolMapper = OBJECT
  1052. VAR
  1053. first: SymbolMap;
  1054. PROCEDURE & Init;
  1055. BEGIN
  1056. first := NIL;
  1057. END Init;
  1058. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  1059. VAR new: SymbolMap;
  1060. BEGIN
  1061. NEW(new); new.this := this; new.to := to; new.tag := tag;
  1062. new.next := first; first := new;
  1063. END Add;
  1064. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1065. VAR s: SymbolMap;
  1066. BEGIN
  1067. s := first;
  1068. WHILE (s # NIL) & (s.this # this) DO
  1069. s := s.next
  1070. END;
  1071. RETURN s
  1072. END Get;
  1073. END SymbolMapper;
  1074. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1075. VAR
  1076. system: Global.System;
  1077. section: IntermediateCode.Section;
  1078. module: Sections.Module;
  1079. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1080. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1081. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1082. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1083. checker: SemanticChecker.Checker;
  1084. backend: IntermediateBackend;
  1085. meta: MetaDataGenerator;
  1086. position: Position;
  1087. moduleSelf: SyntaxTree.Variable;
  1088. (* variables for hand over of variables / temporary state *)
  1089. currentScope: SyntaxTree.Scope;
  1090. constantDeclaration : SyntaxTree.Symbol;
  1091. result: Operand; (* result of the most recent expression / statement *)
  1092. destination: IntermediateCode.Operand;
  1093. arrayDestinationTag: IntermediateCode.Operand;
  1094. arrayDestinationDimension:LONGINT;
  1095. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1096. conditional: BOOLEAN;
  1097. trueLabel, falseLabel, exitLabel: Label;
  1098. locked: BOOLEAN;
  1099. (*
  1100. usedRegisters: Registers;
  1101. *)
  1102. registerUsageCount: RegisterUsageCount;
  1103. usedRegisters: RegisterEntry;
  1104. (* useful operands and types *)
  1105. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1106. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1107. commentPrintout: Printout.Printer;
  1108. dump: Streams.Writer;
  1109. tagsAvailable : BOOLEAN;
  1110. supportedInstruction: SupportedInstructionProcedure;
  1111. supportedImmediate: SupportedImmediateProcedure;
  1112. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1113. emitLabels: BOOLEAN;
  1114. runtimeModuleName : SyntaxTree.IdentifierString;
  1115. newObjectFile: BOOLEAN;
  1116. indexCounter: LONGINT;
  1117. profile: BOOLEAN;
  1118. profileId, profileInit: IntermediateCode.Section;
  1119. profileInitPatchPosition: LONGINT;
  1120. numberProcedures: LONGINT;
  1121. procedureResultDesignator : SyntaxTree.Designator;
  1122. operatorInitializationCodeSection: IntermediateCode.Section;
  1123. fingerPrinter: FingerPrinter.FingerPrinter;
  1124. temporaries: Variables;
  1125. canBeLoaded : BOOLEAN;
  1126. currentIsInline: BOOLEAN;
  1127. currentMapper: SymbolMapper;
  1128. currentInlineExit: Label;
  1129. moduleBodySection: IntermediateCode.Section;
  1130. NeedDescriptor : BOOLEAN;
  1131. cooperativeSwitches: BOOLEAN;
  1132. lastSwitchPC: LONGINT;
  1133. isUnchecked: BOOLEAN;
  1134. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1135. newObjectFile: BOOLEAN);
  1136. BEGIN
  1137. SELF.system := system;
  1138. SELF.runtimeModuleName := runtime;
  1139. currentScope := NIL;
  1140. hiddenPointerType := NIL;
  1141. delegatePointerType := NIL;
  1142. awaitProcCounter := 0;
  1143. labelId := 0; constId := 0; labelId := 0;
  1144. SELF.checker := checker;
  1145. SELF.backend := backend;
  1146. position := Basic.invalidPosition;
  1147. conditional := FALSE;
  1148. locked := FALSE;
  1149. InitOperand(result,ModeUndefined);
  1150. addressType := IntermediateCode.GetType(system,system.addressType);
  1151. setType := IntermediateCode.GetType(system,system.setType);
  1152. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1153. byteType := IntermediateCode.GetType(system, system.byteType);
  1154. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1155. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1156. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1157. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1158. nil := IntermediateCode.Immediate(addressType,0);
  1159. one := IntermediateCode.Immediate(addressType,1);
  1160. IntermediateCode.InitOperand(destination);
  1161. tagsAvailable := TRUE;
  1162. supportedInstruction := supportedInstructionProcedure;
  1163. supportedImmediate := supportedImmediateProcedure;
  1164. inData := FALSE;
  1165. SELF.emitLabels := emitLabels;
  1166. IntermediateCode.InitOperand(arrayDestinationTag);
  1167. bool := IntermediateCode.GetType(system,system.booleanType);
  1168. IntermediateCode.InitImmediate(false,bool,0);
  1169. IntermediateCode.InitImmediate(true,bool,1);
  1170. SELF.newObjectFile := newObjectFile;
  1171. indexCounter := 0;
  1172. NEW(registerUsageCount);
  1173. usedRegisters := NIL;
  1174. procedureResultDesignator := NIL;
  1175. NEW(fingerPrinter, system);
  1176. NEW(temporaries);
  1177. currentIsInline := FALSE;
  1178. NeedDescriptor := FALSE;
  1179. isUnchecked := backend.noRuntimeChecks;
  1180. END Init;
  1181. TYPE Context = RECORD
  1182. section: IntermediateCode.Section;
  1183. registerUsageCount: RegisterUsageCount;
  1184. usedRegisters: RegisterEntry;
  1185. END;
  1186. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1187. VAR context: Context;
  1188. BEGIN
  1189. context.section := section;
  1190. context.registerUsageCount := registerUsageCount;
  1191. context.usedRegisters := usedRegisters;
  1192. section := new;
  1193. NEW(registerUsageCount);
  1194. usedRegisters := NIL;
  1195. RETURN context;
  1196. END SwitchContext;
  1197. PROCEDURE ReturnToContext(context: Context);
  1198. BEGIN
  1199. section := context.section;
  1200. registerUsageCount := context.registerUsageCount;
  1201. usedRegisters := context.usedRegisters;
  1202. END ReturnToContext;
  1203. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1204. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1205. BEGIN
  1206. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1207. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1208. END;
  1209. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1210. section.SetExported(IsExported(syntaxTreeSymbol));
  1211. RETURN section
  1212. END NewSection;
  1213. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1214. VAR new: LONGINT;
  1215. BEGIN
  1216. new := registerUsageCount.Next(type,class);
  1217. UseRegister(new);
  1218. RETURN new
  1219. END AcquireRegister;
  1220. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1221. VAR
  1222. fingerPrint: SyntaxTree.FingerPrint;
  1223. fingerPrintString: ARRAY 32 OF CHAR;
  1224. BEGIN
  1225. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1226. string := "[";
  1227. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1228. Strings.Append(string, fingerPrintString);
  1229. Strings.Append(string, "]");
  1230. END GetFingerprintString;
  1231. (** get the name for the code section that represens a certain symbol
  1232. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1233. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1234. VAR string: ARRAY 32 OF CHAR;
  1235. BEGIN
  1236. Global.GetSymbolSegmentedName(symbol, name);
  1237. (* if the symbol is an operator, then append the fingerprint to the name *)
  1238. IF symbol IS SyntaxTree.Operator THEN
  1239. GetFingerprintString(symbol, string);
  1240. Basic.AppendToSegmentedName(name,string);
  1241. END
  1242. END GetCodeSectionNameForSymbol;
  1243. (** get the name for the code section that represens a certain symbol
  1244. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1245. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1246. VAR string: ARRAY 32 OF CHAR;
  1247. BEGIN
  1248. Global.GetSymbolNameInScope(symbol, scope, name);
  1249. (* if the symbol is an operator, then append the fingerprint to the name *)
  1250. IF symbol IS SyntaxTree.Operator THEN
  1251. GetFingerprintString(symbol, string);
  1252. Strings.Append(name, string);
  1253. END
  1254. END GetCodeSectionNameForSymbolInScope;
  1255. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1256. BEGIN
  1257. IF dump # NIL THEN
  1258. dump.String("enter "); dump.String(s); dump.Ln;
  1259. END;
  1260. END TraceEnter;
  1261. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1262. BEGIN
  1263. IF dump # NIL THEN
  1264. dump.String("exit "); dump.String(s); dump.Ln;
  1265. END;
  1266. END TraceExit;
  1267. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1268. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1269. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1270. VAR i: LONGINT;
  1271. BEGIN
  1272. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1273. IF op.rule # NIL THEN
  1274. FOR i := 0 TO LEN(op.rule)-1 DO
  1275. CheckRegister(op.rule[i])
  1276. END;
  1277. END;
  1278. END CheckRegister;
  1279. BEGIN
  1280. CheckRegister(instruction.op1);
  1281. CheckRegister(instruction.op2);
  1282. CheckRegister(instruction.op3);
  1283. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1284. ELSE section.Emit(instruction);
  1285. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1286. END;
  1287. END Emit;
  1288. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1289. BEGIN
  1290. IF backend.cooperative THEN
  1291. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1292. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1293. ELSE
  1294. Emit(Trap(position,trapNo));
  1295. END;
  1296. END EmitTrap;
  1297. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1298. VAR name: Basic.SegmentedName;
  1299. VAR op1, op2, reg: IntermediateCode.Operand;
  1300. VAR call, nocall: Label;
  1301. VAR parametersSize: LONGINT;
  1302. VAR prevSection: IntermediateCode.Section;
  1303. VAR prevDump: Streams.Writer;
  1304. VAR body: SyntaxTree.Body;
  1305. VAR procedureType: SyntaxTree.ProcedureType;
  1306. BEGIN
  1307. IF procedure # NIL THEN
  1308. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1309. ELSE procedureType := NIL;
  1310. END;
  1311. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1312. prevSection := SELF.section;
  1313. SELF.section := section;
  1314. prevDump := dump;
  1315. dump := section.comments;
  1316. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1317. IF backend.hasLinkRegister THEN
  1318. Emit(Push(Basic.invalidPosition, lr));
  1319. END;
  1320. Emit(Push(Basic.invalidPosition,fp));
  1321. END;
  1322. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1323. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1324. GetCodeSectionNameForSymbol(procedure, name);
  1325. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1326. ELSE
  1327. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1328. END;
  1329. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1330. Emit(Push(Basic.invalidPosition,op1));
  1331. Emit(Mov(Basic.invalidPosition,fp, sp));
  1332. body := procedure.procedureScope.body;
  1333. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1334. NEW(call, section);
  1335. NEW(nocall, section);
  1336. reg := NewRegisterOperand(addressType);
  1337. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1338. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1339. BrltL(call, sp, reg);
  1340. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1341. BrgeL(nocall, sp, op2);
  1342. call.Resolve(section.pc);
  1343. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1344. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1345. Emit(Push(Basic.invalidPosition, op2));
  1346. Emit(Push(Basic.invalidPosition, reg));
  1347. ReleaseIntermediateOperand(reg);
  1348. CallThis(position, "Activities","ExpandStack",2);
  1349. Emit(Result(Basic.invalidPosition, sp));
  1350. nocall.Resolve(section.pc);
  1351. END;
  1352. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1353. IF procedure # NIL THEN
  1354. body := procedure.procedureScope.body;
  1355. ELSE
  1356. body := NIL;
  1357. END;
  1358. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1359. Emit(Push(Basic.invalidPosition, one)) ;
  1360. procedureType.SetParametersOffset(1);
  1361. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1362. END;
  1363. Emit(Mov(Basic.invalidPosition, fp, sp));
  1364. END;
  1365. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1366. SELF.section := prevSection;
  1367. dump := prevDump;
  1368. END EmitEnter;
  1369. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1370. VAR op1,op2: IntermediateCode.Operand;
  1371. VAR instruction: IntermediateCode.Instruction;
  1372. BEGIN
  1373. IntermediateCode.InitNumber(op1,callconv);
  1374. IntermediateCode.InitNumber(op2,varSize);
  1375. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1376. RETURN instruction
  1377. END Enter;
  1378. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1379. VAR op1: IntermediateCode.Operand;
  1380. VAR instruction: IntermediateCode.Instruction;
  1381. BEGIN
  1382. IntermediateCode.InitNumber(op1,callconv);
  1383. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1384. RETURN instruction
  1385. END Leave;
  1386. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1387. VAR prevSection: IntermediateCode.Section;
  1388. VAR op2, size: IntermediateCode.Operand;
  1389. VAR body: SyntaxTree.Body;
  1390. BEGIN
  1391. prevSection := SELF.section;
  1392. SELF.section := section;
  1393. Emit(Leave(position, callconv));
  1394. IF procedure # NIL THEN
  1395. body := procedure.procedureScope.body;
  1396. ELSE
  1397. body := NIL;
  1398. END;
  1399. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1400. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1401. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1402. Emit(Add(position, sp, fp, op2));
  1403. ELSE
  1404. Emit(Mov(position, sp, fp));
  1405. END;
  1406. Emit(Pop(position, fp));
  1407. END;
  1408. SELF.section := prevSection;
  1409. END EmitLeave;
  1410. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1411. VAR m: SymbolMap;
  1412. BEGIN
  1413. position := x.position;
  1414. IF currentIsInline THEN
  1415. m := currentMapper.Get(x);
  1416. IF m # NIL THEN
  1417. (*
  1418. Printout.Info("mapping from", x);
  1419. Printout.Info("mapping to ", m.to);
  1420. *)
  1421. m.to.Accept(SELF);
  1422. op := result;
  1423. IF m.tag # NIL THEN
  1424. ReleaseIntermediateOperand(result.tag);
  1425. m.tag.Accept(SELF);
  1426. op.tag := result.op;
  1427. ReleaseIntermediateOperand(result.tag);
  1428. END;
  1429. RETURN
  1430. END;
  1431. END;
  1432. x.Accept(SELF);
  1433. op := result;
  1434. END Symbol;
  1435. PROCEDURE Expression(x: SyntaxTree.Expression);
  1436. BEGIN
  1437. position := x.position;
  1438. constantDeclaration := NIL;
  1439. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1440. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1441. END;
  1442. IF x.resolved # NIL THEN
  1443. x.resolved.Accept(SELF)
  1444. ELSE
  1445. x.Accept(SELF)
  1446. END;
  1447. (* check this, was commented out in ActiveCells3 *)
  1448. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1449. Error(x.position, "unsupported modifier");
  1450. END;
  1451. END Expression;
  1452. (*
  1453. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1454. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1455. BEGIN
  1456. temporaries.GetUsage(current);
  1457. FOR i := 0 TO LEN(current)-1 DO
  1458. set := current[i] - previous[i];
  1459. IF set # {} THEN
  1460. FOR j := 0 TO MAX(SET)-1 DO
  1461. IF j IN set THEN
  1462. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1463. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1464. Symbol(variable, op);
  1465. MakeMemory(tmp,op.op,addressType,0);
  1466. ReleaseOperand(op);
  1467. Emit(Mov(position,tmp, nil ) );
  1468. ReleaseIntermediateOperand(tmp);
  1469. END;
  1470. END;
  1471. END;
  1472. END;
  1473. END;
  1474. END ResetUsedTemporaries;
  1475. *)
  1476. PROCEDURE Statement(x: SyntaxTree.Statement);
  1477. VAR use: VariableUse;
  1478. BEGIN
  1479. temporaries.GetUsage(use);
  1480. position := x.position;
  1481. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1482. IF commentPrintout # NIL THEN
  1483. commentPrintout.Statement(x);
  1484. dump.Ln;
  1485. (*dump.Update;*)
  1486. END;
  1487. x.Accept(SELF);
  1488. (*
  1489. CheckRegistersFree();
  1490. *)
  1491. (*ResetUsedTemporaries(use);*)
  1492. temporaries.SetUsage(use);
  1493. END Statement;
  1494. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1495. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1496. *)
  1497. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1498. BEGIN
  1499. ASSERT(op.mode # IntermediateCode.Undefined);
  1500. IF op.mode = IntermediateCode.ModeMemory THEN
  1501. ReuseCopy(res,op);
  1502. ELSE
  1503. res := op;
  1504. UseIntermediateOperand(res);
  1505. END;
  1506. IntermediateCode.AddOffset(res,offset);
  1507. IntermediateCode.MakeMemory(res,type);
  1508. END MakeMemory;
  1509. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1510. VAR mem: IntermediateCode.Operand;
  1511. BEGIN
  1512. MakeMemory(mem,res,type,offset);
  1513. ReleaseIntermediateOperand(res);
  1514. res := mem;
  1515. END ToMemory;
  1516. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1517. VAR mem: IntermediateCode.Operand;
  1518. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1519. componentType: SyntaxTree.Type;
  1520. BEGIN
  1521. type := type.resolved;
  1522. IF operand.mode = ModeReference THEN
  1523. IF type IS SyntaxTree.RangeType THEN
  1524. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1525. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1526. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1527. ReleaseOperand(operand);
  1528. operand.op := firstOp;
  1529. operand.tag := lastOp;
  1530. operand.extra := stepOp;
  1531. ELSIF type IS SyntaxTree.ComplexType THEN
  1532. componentType := type(SyntaxTree.ComplexType).componentType;
  1533. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1534. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1535. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1536. ReleaseOperand(operand);
  1537. operand.op := firstOp;
  1538. operand.tag := lastOp
  1539. ELSE
  1540. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1541. ReleaseIntermediateOperand(operand.op);
  1542. operand.op := mem;
  1543. END;
  1544. operand.mode := ModeValue;
  1545. END;
  1546. ASSERT(operand.mode = ModeValue);
  1547. END LoadValue;
  1548. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1549. VAR prevConditional: BOOLEAN;
  1550. BEGIN
  1551. prevConditional := conditional;
  1552. conditional := FALSE;
  1553. InitOperand(result, ModeUndefined);
  1554. Expression(x);
  1555. op := result;
  1556. LoadValue(op,x.type.resolved);
  1557. conditional := prevConditional;
  1558. END Evaluate;
  1559. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1560. VAR prevConditional: BOOLEAN;
  1561. BEGIN
  1562. prevConditional := conditional;
  1563. conditional := FALSE;
  1564. InitOperand(result,ModeUndefined);
  1565. Expression(x);
  1566. op := result;
  1567. (*
  1568. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1569. *)
  1570. conditional := prevConditional;
  1571. END Designate;
  1572. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1573. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1574. BEGIN
  1575. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1576. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1577. conditional := TRUE;
  1578. trueLabel := trueL; falseLabel := falseL;
  1579. Expression(x);
  1580. trueL := trueLabel; falseL := falseLabel;
  1581. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1582. END Condition;
  1583. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1584. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1585. BEGIN
  1586. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1587. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1588. RETURN op
  1589. END NewRegisterOperand;
  1590. PROCEDURE UnuseRegister(register: LONGINT);
  1591. BEGIN
  1592. IF (register > 0) THEN
  1593. register := registerUsageCount.Map(register);
  1594. registerUsageCount.DecUse(register);
  1595. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1596. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1597. END;
  1598. IF registerUsageCount.Use(register)=0 THEN
  1599. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1600. Warning(position, "register cannot be removed");
  1601. END;
  1602. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1603. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1604. END;
  1605. ELSIF registerUsageCount.Use(register)<0 THEN
  1606. Warning(position, "register removed too often");
  1607. IF dump # NIL THEN
  1608. dump.String("register removed too often"); dump.Ln; dump.Update;
  1609. END;
  1610. D.TraceBack;
  1611. END;
  1612. END;
  1613. END UnuseRegister;
  1614. PROCEDURE UseRegister(register: LONGINT);
  1615. BEGIN
  1616. IF (register > 0) THEN
  1617. register := registerUsageCount.Map(register);
  1618. registerUsageCount.IncUse(register);
  1619. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1620. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1621. END;
  1622. IF registerUsageCount.Use(register)=1 THEN
  1623. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1624. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1625. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1626. END;
  1627. END;
  1628. END;
  1629. END UseRegister;
  1630. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1631. BEGIN
  1632. UnuseRegister(op.register)
  1633. END ReleaseIntermediateOperand;
  1634. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1635. BEGIN
  1636. UseRegister(op.register)
  1637. END UseIntermediateOperand;
  1638. PROCEDURE ReleaseOperand(CONST op: Operand);
  1639. BEGIN
  1640. UnuseRegister(op.op.register);
  1641. UnuseRegister(op.tag.register);
  1642. UnuseRegister(op.extra.register);
  1643. END ReleaseOperand;
  1644. (* save registers marked in array "markedRegisters" to the stack
  1645. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1646. *)
  1647. PROCEDURE SaveRegisters();
  1648. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1649. BEGIN
  1650. entry := usedRegisters;
  1651. WHILE entry # NIL DO
  1652. type := registerUsageCount.used[entry.register].type;
  1653. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1654. Emit(Push(position,op));
  1655. entry := entry.next;
  1656. END;
  1657. END SaveRegisters;
  1658. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1659. BEGIN
  1660. saved := usedRegisters;
  1661. usedRegisters := NIL;
  1662. END ReleaseUsedRegisters;
  1663. (** remove parameter registers from used queue *)
  1664. PROCEDURE ReleaseParameterRegisters;
  1665. VAR entry,prev,next: RegisterEntry;
  1666. BEGIN
  1667. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1668. WHILE entry # NIL DO
  1669. next := entry.next;
  1670. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1671. registerUsageCount.DecUse(entry.register);
  1672. ASSERT(registerUsageCount.Use(entry.register)=0);
  1673. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1674. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1675. END;
  1676. ELSIF prev = NIL THEN
  1677. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1678. ELSE
  1679. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1680. END;
  1681. entry := next;
  1682. END;
  1683. END ReleaseParameterRegisters;
  1684. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1685. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1686. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1687. BEGIN
  1688. entry := saved;
  1689. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1690. entry := prev;
  1691. WHILE entry # NIL DO
  1692. prev := entry.prev;
  1693. type := entry.type;
  1694. class := entry.registerClass;
  1695. IntermediateCode.InitRegister(op,type,class,entry.register);
  1696. (*
  1697. new := registerUsageCount.Next(type,class);
  1698. registerUsageCount.Remap(entry.register,new);
  1699. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1700. dump.String("remap register "); dump.Int(entry.register,1);
  1701. dump.String("to "); dump.Int(new,1);
  1702. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1703. END;
  1704. entry.register := new;
  1705. *)
  1706. Emit(Pop(position,op));
  1707. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1708. entry := prev;
  1709. END;
  1710. (*
  1711. usedRegisters := saved;
  1712. *)
  1713. END RestoreRegisters;
  1714. PROCEDURE CheckRegistersFree;
  1715. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1716. BEGIN
  1717. IF usedRegisters # NIL THEN
  1718. r := usedRegisters;
  1719. WHILE r # NIL DO
  1720. warning := "register ";
  1721. Strings.AppendInt(warning, r.register);
  1722. Strings.Append(warning, " not released.");
  1723. Warning(position,warning);
  1724. r := r .next;
  1725. END;
  1726. END;
  1727. FOR i := 0 TO registerUsageCount.count-1 DO
  1728. IF registerUsageCount.used[i].count < 0 THEN
  1729. warning := "register ";
  1730. Strings.AppendInt(warning, i);
  1731. Strings.Append(warning, " unused too often.");
  1732. Warning(position,warning);
  1733. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1734. warning := "register ";
  1735. Strings.AppendInt(warning, i);
  1736. Strings.Append(warning, " not unused often enough.");
  1737. Warning(position,warning);
  1738. END;
  1739. END;
  1740. END CheckRegistersFree;
  1741. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1742. Otherwise allocate a new register.
  1743. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1744. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1745. BEGIN
  1746. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1747. UseIntermediateOperand(result);
  1748. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1749. UseIntermediateOperand(result);
  1750. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1751. END;
  1752. END Reuse2;
  1753. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1754. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1755. If not then allocate a new register.
  1756. Does NOT necessarily keep the content of src1 or src2 in result!
  1757. *)
  1758. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1759. BEGIN
  1760. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1761. UseIntermediateOperand(result);
  1762. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1765. result := alternative; alternative := emptyOperand;
  1766. UseIntermediateOperand(result);
  1767. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1768. END;
  1769. END Reuse2a;
  1770. (* like reuse2 but only one source *)
  1771. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1772. BEGIN
  1773. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1774. UseIntermediateOperand(result);
  1775. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1776. END;
  1777. END Reuse1;
  1778. (* like reuse2a but only one source *)
  1779. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1780. BEGIN
  1781. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1782. UseIntermediateOperand(result);
  1783. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1784. UseIntermediateOperand(result);
  1785. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1786. END;
  1787. END Reuse1a;
  1788. (* like reuse1 but guarantees that content of src1 is in result *)
  1789. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1790. BEGIN
  1791. IF ReusableRegister(src1) THEN
  1792. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1793. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1794. UseIntermediateOperand(result);
  1795. ELSE
  1796. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1797. Emit(Mov(position,result,src1));
  1798. END
  1799. END ReuseCopy;
  1800. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1801. BEGIN
  1802. IF ReusableRegister(src) THEN
  1803. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1804. ELSE
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1806. Emit(Mov(position,result,src));
  1807. ReleaseIntermediateOperand(src);
  1808. END
  1809. END TransferToRegister;
  1810. (** labels and branches **)
  1811. PROCEDURE NewLabel(): Label;
  1812. VAR label: Label;
  1813. BEGIN
  1814. NEW(label,section); RETURN label;
  1815. END NewLabel;
  1816. PROCEDURE SetLabel(label: Label);
  1817. BEGIN label.Resolve(section.pc);
  1818. END SetLabel;
  1819. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1820. BEGIN
  1821. ASSERT(label # NIL);
  1822. IF label.pc < 0 THEN (* label not yet set *)
  1823. label.AddFixup(section.pc);
  1824. END;
  1825. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1826. END LabelOperand;
  1827. PROCEDURE BrL(label: Label);
  1828. BEGIN
  1829. Emit(Br(position,LabelOperand(label)));
  1830. END BrL;
  1831. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1832. BEGIN
  1833. Emit(Brge(position,LabelOperand(label),left,right));
  1834. END BrgeL;
  1835. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1836. BEGIN
  1837. Emit(Brlt(position,LabelOperand(label),left,right));
  1838. END BrltL;
  1839. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1840. BEGIN
  1841. Emit(Breq(position,LabelOperand(label),left,right));
  1842. END BreqL;
  1843. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1844. BEGIN
  1845. Emit(Brne(position,LabelOperand(label),left,right));
  1846. END BrneL;
  1847. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1848. VAR new: IntermediateCode.Operand;
  1849. BEGIN
  1850. IF Trace THEN TraceEnter("Convert") END;
  1851. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1852. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1853. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1854. & (operand.offset = 0)
  1855. THEN
  1856. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1857. Emit(Conv(position,new,operand));
  1858. ELSE
  1859. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1860. Emit(Conv(position,new,operand));
  1861. ReleaseIntermediateOperand(operand);
  1862. END;
  1863. operand := new;
  1864. 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
  1865. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1866. ELSE
  1867. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1868. Emit(Conv(position,new,operand));
  1869. ReleaseIntermediateOperand(operand);
  1870. operand := new;
  1871. END;
  1872. IF Trace THEN TraceExit("Convert") END;
  1873. END Convert;
  1874. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1875. VAR exit: Label;
  1876. BEGIN
  1877. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1878. exit := NewLabel();
  1879. br(exit,left,right);
  1880. EmitTrap(position,trapNo);
  1881. SetLabel(exit);
  1882. END TrapC;
  1883. (** expressions *)
  1884. (** emit necessary runtime check for set elements **)
  1885. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1886. VAR max: IntermediateCode.Operand;
  1887. BEGIN
  1888. IF isUnchecked THEN RETURN END;
  1889. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1890. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1891. TrapC(BrgeL, max, o, SetElementTrap);
  1892. END;
  1893. END CheckSetElement;
  1894. (** the set that a range represents **)
  1895. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1896. VAR
  1897. operand: Operand;
  1898. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1899. BEGIN
  1900. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1901. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1902. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1903. one := IntermediateCode.Immediate(setType, 1);
  1904. Evaluate(x, operand);
  1905. Convert(operand.op, setType);
  1906. Convert(operand.tag, setType);
  1907. CheckSetElement(operand.op);
  1908. CheckSetElement(operand.tag);
  1909. (* create mask for lower bound
  1910. i.e. shift 11111111 to the left by the value of the lower bound
  1911. *)
  1912. Reuse1(temp, operand.op);
  1913. Emit(Shl(position,temp, allBits, operand.op));
  1914. ReleaseIntermediateOperand(operand.op);
  1915. operand.op := temp;
  1916. (* create mask for upper bound
  1917. i.e. shift 11111111 to the right by the difference between the
  1918. upper bound and the maximum number of set elements
  1919. *)
  1920. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1921. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1922. Reuse1(temp, operand.tag);
  1923. ELSE
  1924. Reuse1(temp, operand.tag);
  1925. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1926. Emit(Sub(position,temp, size, operand.tag));
  1927. END;
  1928. Emit(Shr(position,temp, allBits, operand.tag));
  1929. ReleaseIntermediateOperand(operand.tag);
  1930. operand.tag := temp;
  1931. Reuse2(resultingSet, operand.op, operand.tag);
  1932. (* intersect the two masks *)
  1933. Emit(And(position,resultingSet, operand.op, operand.tag));
  1934. ReleaseOperand(operand);
  1935. RETURN resultingSet
  1936. END SetFromRange;
  1937. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1938. VAR
  1939. res, operand: Operand;
  1940. temp, one, noBits, dest: IntermediateCode.Operand;
  1941. expression: SyntaxTree.Expression;
  1942. i: LONGINT;
  1943. BEGIN
  1944. IF Trace THEN TraceEnter("VisitSet") END;
  1945. dest := destination;
  1946. destination := emptyOperand;
  1947. noBits := IntermediateCode.Immediate(setType, 0);
  1948. one := IntermediateCode.Immediate(setType, 1);
  1949. (* start off with the empty set *)
  1950. InitOperand(res, ModeValue);
  1951. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1952. Emit(Mov(position,res.op, noBits));
  1953. FOR i := 0 TO x.elements.Length() - 1 DO
  1954. expression := x.elements.GetExpression(i);
  1955. IF expression IS SyntaxTree.RangeExpression THEN
  1956. (* range of set elements *)
  1957. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1958. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1959. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1960. ReleaseIntermediateOperand(temp)
  1961. ELSE
  1962. (* singelton element *)
  1963. Evaluate(expression, operand);
  1964. Convert(operand.op, setType);
  1965. CheckSetElement(operand.op);
  1966. (* create subset containing single element *)
  1967. Reuse1(temp, operand.op);
  1968. Emit(Shl(position,temp, one, operand.op));
  1969. ReleaseOperand(operand);
  1970. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1971. ReleaseIntermediateOperand(temp);
  1972. END
  1973. END;
  1974. result := res;
  1975. destination := dest;
  1976. IF Trace THEN TraceExit("VisitSet") END;
  1977. END VisitSet;
  1978. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1979. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1980. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1981. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1982. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1983. element: SyntaxTree.IntegerValue;
  1984. BEGIN
  1985. numberElements := x.elements.Length();
  1986. expression := index.parameters.GetExpression(dim);
  1987. element := expression(SyntaxTree.IntegerValue);
  1988. FOR i := 0 TO numberElements-1 DO
  1989. expression := x.elements.GetExpression(i);
  1990. element.SetValue(i);
  1991. IF expression IS SyntaxTree.MathArrayExpression THEN
  1992. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1993. ELSE
  1994. Assign(index,expression);
  1995. END;
  1996. END;
  1997. END RecursiveAssignment;
  1998. BEGIN
  1999. variable := GetTemporaryVariable(x.type, FALSE);
  2000. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2001. designator.SetType(variable.type);
  2002. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2003. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2004. FOR i := 0 TO dim-1 DO
  2005. element := SyntaxTree.NewIntegerValue(x.position,0);
  2006. element.SetType(system.longintType);
  2007. index.parameters.AddExpression(element);
  2008. END;
  2009. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2010. RecursiveAssignment(x,0);
  2011. Expression(designator);
  2012. END VisitMathArrayExpression;
  2013. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2014. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2015. BEGIN
  2016. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2017. dest := destination; destination := emptyOperand;
  2018. IF x.operator = Scanner.Not THEN
  2019. IF conditional THEN
  2020. Condition(x.left,falseLabel,trueLabel)
  2021. ELSE
  2022. Evaluate(x.left,operand);
  2023. InitOperand(result,ModeValue);
  2024. Reuse1a(result.op,operand.op,dest);
  2025. Emit(Xor(position,result.op,operand.op,true));
  2026. ReleaseOperand(operand);
  2027. END;
  2028. ELSIF x.operator = Scanner.Minus THEN
  2029. Evaluate(x.left,operand);
  2030. InitOperand(result,ModeValue);
  2031. Reuse1a(result.op,operand.op,dest);
  2032. type := x.left.type.resolved;
  2033. IF type IS SyntaxTree.SetType THEN
  2034. Emit(Not(position,result.op,operand.op));
  2035. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2036. Reuse1(result.tag,operand.tag);
  2037. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2038. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2039. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2040. Emit(Neg(position,result.op,operand.op));
  2041. ELSE HALT(200)
  2042. END;
  2043. ReleaseOperand(operand);
  2044. ELSIF x.operator = Scanner.Address THEN
  2045. Designate(x.left,operand);
  2046. operand.mode := ModeValue;
  2047. t0 := x.left.type.resolved;
  2048. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2049. ReleaseIntermediateOperand(operand.op);
  2050. operand.op := operand.tag;
  2051. IntermediateCode.InitOperand(operand.tag);
  2052. END;
  2053. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2054. result := operand;
  2055. ELSE HALT(100)
  2056. END;
  2057. destination := dest;
  2058. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2059. END VisitUnaryExpression;
  2060. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2061. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2062. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2063. BEGIN
  2064. type := type.resolved;
  2065. originalType := type;
  2066. IF type IS SyntaxTree.PointerType THEN
  2067. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2068. END;
  2069. IF type IS SyntaxTree.ObjectType THEN
  2070. BrL(trueL);
  2071. ELSE
  2072. ASSERT(type IS SyntaxTree.RecordType);
  2073. (*
  2074. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2075. *)
  2076. ReuseCopy(left,tag);
  2077. right := TypeDescriptorAdr(type);
  2078. IF ~newObjectFile THEN
  2079. IntermediateCode.MakeMemory(right,addressType);
  2080. END;
  2081. IF backend.cooperative THEN
  2082. repeatL := NewLabel();
  2083. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2084. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2085. END;
  2086. SetLabel(repeatL);
  2087. BreqL(trueL,left,right);
  2088. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2089. BrneL(repeatL,left,nil);
  2090. ELSIF meta.simple THEN
  2091. level := type(SyntaxTree.RecordType).Level();
  2092. (* get type desc tag of level relative to base tag *)
  2093. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2094. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2095. IntermediateCode.MakeMemory(left,addressType);
  2096. BreqL(trueL,left,right);
  2097. ELSE
  2098. level := type(SyntaxTree.RecordType).Level();
  2099. (* get type desc tag of level relative to base tag *)
  2100. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2101. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2102. IntermediateCode.MakeMemory(left,addressType);
  2103. BreqL(trueL,left,right);
  2104. END;
  2105. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2106. BrL(falseL);
  2107. END;
  2108. END TypeTest;
  2109. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2110. BEGIN
  2111. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2112. IF dump # NIL THEN
  2113. dump.String(s); dump.Ln;
  2114. END;
  2115. END Error;
  2116. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2117. BEGIN
  2118. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2119. IF dump # NIL THEN
  2120. dump.String(s); dump.Ln; dump.Update;
  2121. END;
  2122. END Warning;
  2123. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2124. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2125. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2126. operand:Operand;
  2127. BEGIN
  2128. previousSection := section;
  2129. Global.GetModuleName(mod,name);
  2130. Strings.Append(name,".@Trace");
  2131. Basic.ToSegmentedName(name, pooledName);
  2132. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2133. IF dump # NIL THEN dump := section.comments END;
  2134. IF backend.hasLinkRegister THEN
  2135. Emit(Push(Basic.invalidPosition, lr));
  2136. END;
  2137. variable := mod.moduleScope.firstVariable;
  2138. WHILE variable # NIL DO
  2139. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2140. Symbol(variable, operand);
  2141. register := operand.op;
  2142. CallTraceMethod(register, variable.type);
  2143. ReleaseIntermediateOperand(register);
  2144. END;
  2145. variable := variable.nextVariable;
  2146. END;
  2147. IF backend.hasLinkRegister THEN
  2148. Emit(Pop(Basic.invalidPosition, lr));
  2149. END;
  2150. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2151. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2152. Emit(Br(position,op));
  2153. INC(statCoopTraceModule, section.pc);
  2154. section := previousSection;
  2155. IF dump # NIL THEN dump := section.comments END;
  2156. END CreateTraceModuleMethod;
  2157. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2158. BEGIN
  2159. Emit (Push(position, dst));
  2160. Emit (Push(position, src));
  2161. CallThis(position,"GarbageCollector","Assign", 2);
  2162. END CallAssignPointer;
  2163. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2164. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2165. BEGIN
  2166. IF SemanticChecker.IsPointerType (type) THEN
  2167. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2168. ELSIF type.IsRecordType() THEN
  2169. Emit (Push(position,dst));
  2170. Emit (Push(position,src));
  2171. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2172. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2173. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2174. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2175. ELSIF IsStaticArray(type) THEN
  2176. size := StaticArrayNumElements(type);
  2177. base := StaticArrayBaseType(type);
  2178. IF base.IsRecordType() THEN
  2179. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2180. Emit (Push(position, dst));
  2181. Emit (Push(position, src));
  2182. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2183. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2184. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2185. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2186. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2187. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2188. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2189. Emit (Push(position, dst));
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, src));
  2192. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2193. ELSE
  2194. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2195. Emit (Push(position, dst));
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, src));
  2198. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2199. ASSERT(StaticArrayBaseType(type).IsPointer());
  2200. END;
  2201. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2202. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2203. Emit (Push(position, dst));
  2204. Emit (Push(position, src));
  2205. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2206. ELSE HALT(100); (* missing ? *)
  2207. END;
  2208. END CallAssignMethod;
  2209. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2210. VAR name: Basic.SegmentedName;
  2211. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2212. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2213. context: Context;
  2214. BEGIN
  2215. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2216. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2217. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2218. GetRecordTypeName (recordType,name);
  2219. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2220. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2221. IF dump # NIL THEN dump := section.comments END;
  2222. IF backend.hasLinkRegister THEN
  2223. Emit(Push(Basic.invalidPosition, lr));
  2224. END;
  2225. variable := recordType.recordScope.firstVariable;
  2226. WHILE variable # NIL DO
  2227. IF variable.NeedsTrace() THEN
  2228. dst := NewRegisterOperand (addressType);
  2229. src := NewRegisterOperand (addressType);
  2230. Emit (Mov(position, dst, parameter1));
  2231. Emit (Mov(position, src, parameter2));
  2232. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2233. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2234. CallAssignMethod(dst, src, variable.type);
  2235. ReleaseIntermediateOperand(src);
  2236. ReleaseIntermediateOperand(dst);
  2237. END;
  2238. variable := variable.nextVariable;
  2239. END;
  2240. recordBase := recordType.GetBaseRecord();
  2241. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2242. IF backend.hasLinkRegister THEN
  2243. Emit(Pop(Basic.invalidPosition, lr));
  2244. END;
  2245. GetRecordTypeName (recordBase,name);
  2246. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2247. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2248. Emit(Br(position,op));
  2249. ELSE
  2250. Emit(Exit(position,0,0, 0));
  2251. END;
  2252. IF ~recordType.isObject THEN
  2253. GetRecordTypeName (recordType,name);
  2254. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2255. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2256. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2257. NEW(registerUsageCount);
  2258. usedRegisters := NIL;
  2259. dst := NewRegisterOperand (addressType);
  2260. src := NewRegisterOperand (addressType);
  2261. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2262. Emit(Mov(position, dst, parameter1));
  2263. Emit(Mov(position, src, parameter2));
  2264. label := NewLabel();
  2265. SetLabel(label);
  2266. Emit(Push(position, dst));
  2267. Emit(Push(position, src));
  2268. GetRecordTypeName (recordType,name);
  2269. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2270. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2271. Emit(Call(position, op, 0));
  2272. Emit(Pop(position, src));
  2273. Emit(Pop(position, dst));
  2274. Emit(Add(position, dst, dst, ofs));
  2275. Emit(Add(position, src, src, ofs));
  2276. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2277. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2278. Emit(Exit(position,0,0, 0));
  2279. END;
  2280. INC(statCoopAssignProcedure, section.pc);
  2281. ReturnToContext(context);
  2282. IF dump # NIL THEN dump := section.comments END;
  2283. END CreateAssignProcedure;
  2284. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2285. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2286. BEGIN
  2287. IF IsUnsafePointer (type) THEN
  2288. skip := NewLabel();
  2289. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2290. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2291. BreqL (skip, op, nil);
  2292. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2293. SetLabel (skip);
  2294. ELSIF SemanticChecker.IsPointerType (type) THEN
  2295. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2296. CallThis(position,"GarbageCollector","Mark", 1);
  2297. ELSIF type.IsRecordType() THEN
  2298. Emit (Push(position,register));
  2299. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2300. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2301. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2302. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2303. ELSIF IsStaticArray(type) THEN
  2304. size := StaticArrayNumElements(type);
  2305. base := StaticArrayBaseType(type);
  2306. IF base.IsRecordType() THEN
  2307. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2308. Emit (Push(position, register));
  2309. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2310. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2311. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2312. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2313. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2314. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2315. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2316. Emit (Push(position, register));
  2317. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2318. ELSE
  2319. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2320. Emit (Push(position, register));
  2321. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2322. ASSERT(base.IsPointer());
  2323. END;
  2324. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2325. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2326. CallThis(position,"GarbageCollector","Mark", 1);
  2327. ELSE HALT(100); (* missing ? *)
  2328. END;
  2329. END CallTraceMethod;
  2330. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2331. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2332. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2333. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2334. BEGIN
  2335. previousSection := section;
  2336. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2337. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2338. GetRecordTypeName (recordType,name);
  2339. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2340. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2341. IF dump # NIL THEN dump := section.comments END;
  2342. IF backend.hasLinkRegister THEN
  2343. Emit(Push(Basic.invalidPosition, lr));
  2344. END;
  2345. variable := recordType.recordScope.firstVariable;
  2346. WHILE variable # NIL DO
  2347. IF variable.NeedsTrace() THEN
  2348. register := NewRegisterOperand (addressType);
  2349. Emit (Mov(position,register,parameter1));
  2350. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2351. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2352. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2353. END;
  2354. CallTraceMethod(register, variable.type);
  2355. ReleaseIntermediateOperand(register);
  2356. END;
  2357. variable := variable.nextVariable;
  2358. END;
  2359. recordBase := recordType.GetBaseRecord();
  2360. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2361. recordBase := recordBase.GetBaseRecord();
  2362. END;
  2363. IF recordBase # NIL THEN
  2364. IF backend.hasLinkRegister THEN
  2365. Emit(Pop(Basic.invalidPosition, lr));
  2366. END;
  2367. GetRecordTypeName (recordBase,name);
  2368. IF HasExplicitTraceMethod (recordBase) THEN
  2369. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2370. ELSE
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2372. END;
  2373. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2374. Emit(Br(position,op));
  2375. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2376. Emit(Exit(position,0,0,0));
  2377. ELSE
  2378. IF backend.hasLinkRegister THEN
  2379. Emit(Pop(Basic.invalidPosition, lr));
  2380. END;
  2381. IF recordType.isObject THEN
  2382. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2383. ELSE
  2384. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2385. END;
  2386. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2387. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2388. Emit(Br(position,op));
  2389. END;
  2390. IF ~recordType.isObject THEN
  2391. GetRecordTypeName (recordType,name);
  2392. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2393. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2394. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2395. NEW(registerUsageCount);
  2396. usedRegisters := NIL;
  2397. IF dump # NIL THEN dump := section.comments END;
  2398. register := NewRegisterOperand (addressType);
  2399. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2400. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2401. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2402. END;
  2403. Emit (Push(position,register));
  2404. GetRecordTypeName (recordType,name);
  2405. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2406. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2407. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2408. ReleaseIntermediateOperand(register);
  2409. Emit(Exit(position,0,0,0));
  2410. GetRecordTypeName (recordType,name);
  2411. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2412. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2413. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2414. NEW(registerUsageCount);
  2415. usedRegisters := NIL;
  2416. register := NewRegisterOperand (addressType);
  2417. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2418. Emit(Mov(position, register, parameter1));
  2419. label := NewLabel();
  2420. SetLabel(label);
  2421. Emit(Push(position, register));
  2422. GetRecordTypeName (recordType,name);
  2423. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2424. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2425. Emit(Call(position, op, 0));
  2426. Emit(Pop(position, register));
  2427. Emit(Add(position, register, register, ofs));
  2428. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2429. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2430. Emit(Exit(position,0,0,0));
  2431. END;
  2432. INC(statCoopTraceMethod, section.pc);
  2433. ReturnToContext(context);
  2434. IF dump # NIL THEN dump := section.comments END;
  2435. END CreateTraceMethod;
  2436. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2437. VAR name: Basic.SegmentedName;
  2438. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2439. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2440. context: Context;
  2441. BEGIN
  2442. IF recordType.isObject THEN RETURN END;
  2443. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2444. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2445. GetRecordTypeName (recordType,name);
  2446. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2447. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2448. IF dump # NIL THEN dump := section.comments END;
  2449. IF backend.hasLinkRegister THEN
  2450. Emit(Push(Basic.invalidPosition, lr));
  2451. END;
  2452. variable := recordType.recordScope.firstVariable;
  2453. WHILE variable # NIL DO
  2454. IF variable.NeedsTrace() THEN
  2455. dst := NewRegisterOperand (addressType);
  2456. Emit (Mov(position, dst, parameter1));
  2457. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2458. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2459. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2460. END;
  2461. CallResetProcedure(dst, nil, variable.type);
  2462. ReleaseIntermediateOperand(dst);
  2463. END;
  2464. variable := variable.nextVariable;
  2465. END;
  2466. recordBase := recordType.GetBaseRecord();
  2467. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2468. IF backend.hasLinkRegister THEN
  2469. Emit(Pop(Basic.invalidPosition, lr));
  2470. END;
  2471. GetRecordTypeName (recordBase,name);
  2472. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2473. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2474. Emit(Br(position,op));
  2475. ELSE
  2476. Emit(Exit(position,0,0, 0));
  2477. END;
  2478. GetRecordTypeName (recordType,name);
  2479. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2480. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2481. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2482. NEW(registerUsageCount);
  2483. usedRegisters := NIL;
  2484. dst := NewRegisterOperand (addressType);
  2485. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2486. Emit(Mov(position, dst, parameter1));
  2487. label := NewLabel();
  2488. SetLabel(label);
  2489. Emit(Push(position, dst));
  2490. GetRecordTypeName (recordType,name);
  2491. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2492. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2493. Emit(Call(position, op, 0));
  2494. Emit(Pop(position, dst));
  2495. Emit(Add(position, dst, dst, ofs));
  2496. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2497. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2498. Emit(Exit(position,0,0, 0));
  2499. INC(statCoopResetProcedure, section.pc);
  2500. ReturnToContext(context);
  2501. IF dump # NIL THEN dump := section.comments END;
  2502. END CreateResetProcedure;
  2503. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2504. VAR name: Basic.SegmentedName; context: Context;
  2505. BEGIN
  2506. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2507. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2508. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2509. IF dump # NIL THEN dump := section.comments END;
  2510. IF backend.hasLinkRegister THEN
  2511. Emit(Push(Basic.invalidPosition, lr));
  2512. END;
  2513. Emit(Push(position,fp));
  2514. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2515. ResetVariables(scope);
  2516. Emit(Pop(position,fp));
  2517. Emit(Exit(position,0,0, 0));
  2518. ReturnToContext(context);
  2519. IF dump # NIL THEN dump := section.comments END;
  2520. END CreateResetMethod;
  2521. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2522. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2523. BEGIN
  2524. IF SemanticChecker.IsPointerType (type) THEN
  2525. Emit (Push(position, dest));
  2526. CallThis(position,"GarbageCollector","Reset", 1);
  2527. ELSIF type.IsRecordType() THEN
  2528. Emit (Push(position, dest));
  2529. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2530. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2531. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2532. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2533. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2534. size := GetArrayLength(type, tag);
  2535. base := ArrayBaseType(type);
  2536. IF base.IsRecordType() THEN
  2537. Emit (Push(position, size));
  2538. Emit (Push(position, dest));
  2539. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2540. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2541. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2542. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2543. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2544. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2545. Emit (Push(position, size));
  2546. Emit (Push(position, dest));
  2547. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2548. ELSE
  2549. Emit (Push(position, size));
  2550. Emit (Push(position, dest));
  2551. CallThis(position,"GarbageCollector","ResetArray", 2);
  2552. ASSERT(ArrayBaseType(type).IsPointer());
  2553. END;
  2554. ReleaseIntermediateOperand(size);
  2555. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2556. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2557. Emit (Push(position, dest));
  2558. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2559. ELSE HALT(100); (* missing ? *)
  2560. END;
  2561. END CallResetProcedure;
  2562. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2563. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2564. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2565. VAR operand: Operand;
  2566. BEGIN
  2567. Symbol (symbol, operand);
  2568. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2569. ReleaseOperand(operand);
  2570. END Reset;
  2571. BEGIN
  2572. previousScope := currentScope;
  2573. currentScope := scope;
  2574. pc := section.pc;
  2575. variable := scope.firstVariable;
  2576. WHILE variable # NIL DO
  2577. IF variable.NeedsTrace() THEN
  2578. Reset (variable);
  2579. END;
  2580. variable := variable.nextVariable;
  2581. END;
  2582. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2583. WHILE parameter # NIL DO
  2584. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2585. Reset (parameter);
  2586. END;
  2587. parameter := parameter.nextParameter;
  2588. END;
  2589. INC(statCoopResetVariables, section.pc - pc);
  2590. currentScope := previousScope;
  2591. END ResetVariables;
  2592. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2593. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2594. VAR op: IntermediateCode.Operand; context: Context;
  2595. BEGIN
  2596. previousSection := section;
  2597. GetCodeSectionNameForSymbol(procedure, name);
  2598. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2599. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2600. IF dump # NIL THEN dump := section.comments END;
  2601. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2602. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2603. Emit(Data(position,op));
  2604. Emit(Data(position,nil));
  2605. IF HasPointers (procedure.procedureScope) THEN
  2606. GetCodeSectionNameForSymbol(procedure, name);
  2607. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2608. ELSE
  2609. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2610. END;
  2611. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2612. Emit(Data(position,op));
  2613. ReturnToContext(context);
  2614. IF dump # NIL THEN dump := section.comments END;
  2615. END CreateProcedureDescriptor;
  2616. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2617. VAR import: SyntaxTree.Import;
  2618. s: Basic.MessageString;
  2619. selfName: SyntaxTree.IdentifierString;
  2620. BEGIN
  2621. moduleScope.ownerModule.GetName(selfName);
  2622. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2623. module := moduleScope.ownerModule
  2624. ELSE
  2625. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2626. IF import = NIL THEN
  2627. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2628. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2629. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2630. s := "Module ";
  2631. Strings.Append(s,moduleName);
  2632. Strings.Append(s," cannot be imported.");
  2633. IF force THEN
  2634. Error(position,s);
  2635. ELSIF canBeLoaded THEN
  2636. Strings.Append(s, "=> no dynamic linking.");
  2637. Warning(position, s);
  2638. canBeLoaded := FALSE;
  2639. END;
  2640. RETURN FALSE
  2641. ELSE
  2642. SELF.module.imports.AddName(moduleName)
  2643. END;
  2644. ELSIF import.module = NIL THEN (* already tried *)
  2645. RETURN FALSE
  2646. END;
  2647. module := import.module;
  2648. END;
  2649. RETURN TRUE
  2650. END AddImport;
  2651. (* needed for old binary object file format*)
  2652. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2653. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2654. BEGIN
  2655. IF AddImport(moduleName,module,TRUE) THEN
  2656. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2657. IF procedure = NIL THEN
  2658. s := "Instruction not supported on target, emulation procedure ";
  2659. Strings.Append(s,moduleName);
  2660. Strings.Append(s,".");
  2661. Strings.Append(s,procedureName);
  2662. Strings.Append(s," not present");
  2663. Error(position,s);
  2664. ELSE
  2665. StaticCallOperand(r,procedure);
  2666. ReleaseOperand(r);
  2667. fp := GetFingerprint(procedure);
  2668. END;
  2669. END;
  2670. END EnsureSymbol;
  2671. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2672. VAR exit: Label; trueL,falseL: Label;
  2673. BEGIN
  2674. trueL := NewLabel();
  2675. falseL := NewLabel();
  2676. exit := NewLabel();
  2677. Condition(x,trueL,falseL);
  2678. InitOperand(result,ModeValue);
  2679. SetLabel(trueL);
  2680. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2681. Emit(Mov(position,result.op,true));
  2682. BrL(exit);
  2683. SetLabel(falseL);
  2684. Emit(MovReplace(position,result.op,false));
  2685. SetLabel(exit);
  2686. END ConditionToValue;
  2687. PROCEDURE ValueToCondition(VAR op: Operand);
  2688. BEGIN
  2689. LoadValue(op,system.booleanType);
  2690. BrneL(trueLabel,op.op, false);
  2691. ReleaseOperand(op);
  2692. BrL(falseLabel);
  2693. END ValueToCondition;
  2694. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2695. VAR size: LONGINT;
  2696. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2697. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2698. BEGIN
  2699. ASSERT(type.form = SyntaxTree.Open);
  2700. baseType := type.arrayBase.resolved;
  2701. IF IsOpenArray(baseType) THEN
  2702. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2703. ELSE
  2704. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2705. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2706. END;
  2707. len := tag;
  2708. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2709. IntermediateCode.MakeMemory(len,addressType);
  2710. UseIntermediateOperand(len);
  2711. Reuse2(res,sizeOperand,len);
  2712. Emit(Mul(position,res,sizeOperand,len));
  2713. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2714. RETURN res
  2715. END GetArraySize;
  2716. BEGIN
  2717. type := type.resolved;
  2718. IF IsOpenArray(type) THEN
  2719. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2720. RETURN tag
  2721. ELSE
  2722. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2723. END;
  2724. ELSE
  2725. size := ToMemoryUnits(system,system.SizeOf(type));
  2726. RETURN IntermediateCode.Immediate(addressType,size)
  2727. END;
  2728. END GetDynamicSize;
  2729. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2730. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2731. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2732. BEGIN
  2733. ASSERT(type.form = SyntaxTree.Open);
  2734. baseType := type.arrayBase.resolved;
  2735. IF IsOpenArray(baseType) THEN
  2736. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2737. ELSE
  2738. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2739. END;
  2740. len := tag;
  2741. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2742. IntermediateCode.MakeMemory(len,addressType);
  2743. UseIntermediateOperand(len);
  2744. Reuse2(res,sizeOperand,len);
  2745. Emit(Mul(position,res,sizeOperand,len));
  2746. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2747. RETURN res
  2748. END GetLength;
  2749. BEGIN
  2750. type := type.resolved;
  2751. IF IsOpenArray(type) THEN
  2752. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2753. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2754. ELSIF type IS SyntaxTree.StringType THEN
  2755. RETURN tag;
  2756. ELSE
  2757. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2758. END;
  2759. END GetArrayLength;
  2760. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2761. VAR result: IntermediateCode.Operand;
  2762. BEGIN
  2763. IF backend.cooperative THEN
  2764. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2765. ELSE
  2766. MakeMemory(result, tag, addressType, 0);
  2767. END;
  2768. RETURN result
  2769. END GetSizeFromTag;
  2770. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2771. VAR parameter: SyntaxTree.Parameter;
  2772. BEGIN
  2773. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2774. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2775. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2776. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2777. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. END;
  2779. RETURN GetDynamicSize(e.type, tag);
  2780. END GetArrayOfBytesSize;
  2781. (*
  2782. to find imported symbol. not needed ?
  2783. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2784. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2785. BEGIN
  2786. IF AddImport(moduleName,importedModule,FALSE) THEN
  2787. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2788. RETURN symbol
  2789. ELSE
  2790. RETURN NIL
  2791. END
  2792. END SymbolByName;
  2793. *)
  2794. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2795. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2796. BEGIN
  2797. IF AddImport(moduleName,runtimeModule,force) THEN
  2798. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2799. IF procedure = NIL THEN
  2800. s := "Procedure ";
  2801. Strings.Append(s,moduleName);
  2802. Strings.Append(s,".");
  2803. Strings.Append(s,procedureName);
  2804. Strings.Append(s," not present");
  2805. Error(position,s);
  2806. RETURN FALSE
  2807. ELSE
  2808. RETURN TRUE
  2809. END;
  2810. ELSE RETURN FALSE
  2811. END;
  2812. END GetRuntimeProcedure;
  2813. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2814. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2815. s: Basic.MessageString;
  2816. BEGIN
  2817. Basic.InitSegmentedName(name);
  2818. name[0] := Basic.MakeString(moduleName);
  2819. name[1] := Basic.MakeString(typeName);
  2820. name[2] := -1;
  2821. IF AddImport(moduleName,importedModule, FALSE) THEN
  2822. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2823. IF symbol = NIL THEN
  2824. s := "type ";
  2825. Strings.Append(s,moduleName);
  2826. Strings.Append(s,".");
  2827. Strings.Append(s,typeName);
  2828. Strings.Append(s," not present");
  2829. Error(position,s);
  2830. END;
  2831. ELSE symbol := NIL;
  2832. END;
  2833. IF importedModule = moduleScope.ownerModule THEN
  2834. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2835. ELSE
  2836. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. END;
  2838. RETURN symbol
  2839. END GetTypeDescriptor;
  2840. (* 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. *)
  2841. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2842. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2843. pooledName: Basic.SegmentedName; size: LONGINT;
  2844. BEGIN
  2845. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2846. StaticCallOperand(result,procedure);
  2847. IF numberParameters < 0 THEN
  2848. size := ProcedureParametersSize(system,procedure);
  2849. ELSE
  2850. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2851. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2852. Error(position,"runtime call parameter count mismatch");
  2853. END;
  2854. END;
  2855. Emit(Call(position, result.op, size));
  2856. ReleaseOperand(result);
  2857. ELSE (* only static linking possible *)
  2858. ASSERT(numberParameters >= 0);
  2859. Basic.InitSegmentedName(pooledName);
  2860. pooledName[0] := Basic.MakeString(moduleName);
  2861. pooledName[1] := Basic.MakeString(procedureName);
  2862. pooledName[2] := -1;
  2863. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2864. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2865. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2866. END;
  2867. END CallThisChecked;
  2868. (* 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. *)
  2869. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2870. BEGIN
  2871. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2872. END CallThis;
  2873. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2874. VAR
  2875. left,right: Operand;
  2876. leftSize, rightSize: IntermediateCode.Operand;
  2877. saved: RegisterEntry;
  2878. reg: IntermediateCode.Operand;
  2879. procedureName: SyntaxTree.IdentifierString;
  2880. BEGIN
  2881. procedureName := "CompareString";
  2882. SaveRegisters();ReleaseUsedRegisters(saved);
  2883. Designate(leftExpression,left);
  2884. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2885. Emit(Push(position,leftSize));
  2886. ReleaseIntermediateOperand(leftSize);
  2887. Emit(Push(position,left.op));
  2888. ReleaseOperand(left);
  2889. Designate(rightExpression,right);
  2890. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2891. Emit(Push(position,rightSize));
  2892. ReleaseIntermediateOperand(rightSize);
  2893. Emit(Push(position,right.op));
  2894. ReleaseOperand(right);
  2895. IF backend.cooperative THEN
  2896. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2897. ELSE
  2898. CallThis(position,runtimeModuleName,procedureName, 4);
  2899. END;
  2900. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2901. Emit(Result(position,reg));
  2902. (*
  2903. AcquireThisRegister(int8,IntermediateCode.Result);
  2904. *)
  2905. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2906. (*
  2907. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2908. *)
  2909. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2910. ReleaseIntermediateOperand(reg);
  2911. BrL(falseLabel);
  2912. END CompareString;
  2913. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2914. VAR
  2915. left,right: Operand;
  2916. leftSize, rightSize: IntermediateCode.Operand;
  2917. saved: RegisterEntry;
  2918. procedureName: SyntaxTree.IdentifierString;
  2919. BEGIN
  2920. procedureName := "CopyString";
  2921. SaveRegisters();ReleaseUsedRegisters(saved);
  2922. Designate(leftExpression,left);
  2923. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2924. Emit(Push(position,leftSize));
  2925. ReleaseIntermediateOperand(leftSize);
  2926. Emit(Push(position,left.op));
  2927. ReleaseOperand(left);
  2928. Designate(rightExpression,right);
  2929. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2930. Emit(Push(position,rightSize));
  2931. ReleaseIntermediateOperand(rightSize);
  2932. Emit(Push(position,right.op));
  2933. ReleaseOperand(right);
  2934. IF backend.cooperative THEN
  2935. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2936. ELSE
  2937. CallThis(position,runtimeModuleName,procedureName,4);
  2938. END;
  2939. RestoreRegisters(saved);
  2940. END CopyString;
  2941. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2942. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2943. leftType,rightType: SyntaxTree.Type;
  2944. leftExpression,rightExpression : SyntaxTree.Expression;
  2945. componentType: IntermediateCode.Type;
  2946. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2947. size: LONGINT;
  2948. BEGIN
  2949. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2950. dest := destination; destination := emptyOperand;
  2951. leftType := x.left.type.resolved;
  2952. rightType := x.right.type.resolved;
  2953. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2954. CASE x.operator OF
  2955. Scanner.Or:
  2956. (* shortcut evaluation of left OR right *)
  2957. IF ~conditional THEN ConditionToValue(x);
  2958. ELSE
  2959. next := NewLabel();
  2960. Condition(x.left,trueLabel,next);
  2961. SetLabel(next);
  2962. Condition(x.right,trueLabel,falseLabel);
  2963. END;
  2964. |Scanner.And:
  2965. (* shortcut evaluation of left & right *)
  2966. IF ~conditional THEN ConditionToValue(x);
  2967. ELSE
  2968. next := NewLabel();
  2969. Condition(x.left,next,falseLabel);
  2970. SetLabel(next);
  2971. Condition(x.right,trueLabel,falseLabel);
  2972. END;
  2973. |Scanner.Is:
  2974. IF ~conditional THEN ConditionToValue(x);
  2975. ELSE
  2976. (* get type desc tag *)
  2977. IF IsPointerToRecord(leftType,recordType) THEN
  2978. Evaluate(x.left,left);
  2979. Dereference(left,recordType,IsUnsafePointer(leftType))
  2980. ELSE
  2981. Designate(x.left,left);
  2982. END;
  2983. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2984. ReleaseOperand(left);
  2985. END;
  2986. |Scanner.Plus:
  2987. Evaluate(x.left,left);
  2988. Evaluate(x.right,right);
  2989. IF leftType IS SyntaxTree.SetType THEN
  2990. InitOperand(result,ModeValue);
  2991. Reuse2a(result.op,left.op,right.op,dest);
  2992. Emit(Or(position,result.op,left.op,right.op));
  2993. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2994. InitOperand(result,ModeValue);
  2995. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2996. Reuse2(result.tag,left.tag,right.tag);
  2997. Emit(Add(position,result.op,left.op,right.op));
  2998. Emit(Add(position,result.tag,left.tag,right.tag))
  2999. ELSE
  3000. InitOperand(result,ModeValue);
  3001. Reuse2a(result.op,left.op,right.op,dest);
  3002. Emit(Add(position,result.op,left.op,right.op));
  3003. END;
  3004. ReleaseOperand(left); ReleaseOperand(right);
  3005. |Scanner.Minus:
  3006. Evaluate(x.left,left);
  3007. Evaluate(x.right,right);
  3008. IF leftType IS SyntaxTree.SetType THEN
  3009. InitOperand(result,ModeValue);
  3010. Reuse1(result.op,right.op);
  3011. Emit(Not(position,result.op,right.op));
  3012. ReleaseOperand(right);
  3013. Emit(And(position,result.op,result.op,left.op));
  3014. ReleaseOperand(left);
  3015. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3016. InitOperand(result,ModeValue);
  3017. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3018. Reuse2(result.tag,left.tag,right.tag);
  3019. Emit(Sub(position,result.op,left.op,right.op));
  3020. Emit(Sub(position,result.tag,left.tag,right.tag));
  3021. ReleaseOperand(left); ReleaseOperand(right)
  3022. ELSE
  3023. InitOperand(result,ModeValue);
  3024. Reuse2a(result.op,left.op,right.op,dest);
  3025. Emit(Sub(position,result.op,left.op,right.op));
  3026. ReleaseOperand(left); ReleaseOperand(right);
  3027. END;
  3028. |Scanner.Times:
  3029. Evaluate(x.left,left);
  3030. Evaluate(x.right,right);
  3031. IF leftType IS SyntaxTree.SetType THEN
  3032. InitOperand(result,ModeValue);
  3033. Reuse2a(result.op,left.op,right.op,dest);
  3034. Emit(And(position,result.op,left.op,right.op));
  3035. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3036. InitOperand(result, ModeValue);
  3037. componentType := left.op.type;
  3038. (* TODO: review this *)
  3039. (*
  3040. result.op = left.op * right.op - left.tag * right.tag
  3041. result.tag = left.tag * right.op + left.op * right.tag
  3042. *)
  3043. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3044. Emit(Mul(position,result.op, left.op, right.op));
  3045. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3046. Emit(Mul(position,tempReg, left.tag, right.tag));
  3047. Emit(Sub(position,result.op, result.op, tempReg));
  3048. Reuse2(result.tag, left.tag, right.op);
  3049. Emit(Mul(position,result.tag, left.tag, right.op));
  3050. Emit(Mul(position,tempReg, left.op, right.tag));
  3051. Emit(Add(position,result.tag, result.tag, tempReg));
  3052. ReleaseIntermediateOperand(tempReg)
  3053. ELSE
  3054. InitOperand(result,ModeValue);
  3055. Reuse2a(result.op,left.op,right.op,dest);
  3056. Emit(Mul(position,result.op,left.op,right.op));
  3057. END;
  3058. ReleaseOperand(left); ReleaseOperand(right);
  3059. |Scanner.Div:
  3060. Evaluate(x.left,left);
  3061. Evaluate(x.right,right);
  3062. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3063. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3064. IF ~isUnchecked THEN
  3065. exit := NewLabel();
  3066. BrltL(exit,zero,right.op);
  3067. EmitTrap(position,NegativeDivisorTrap);
  3068. SetLabel(exit);
  3069. END;
  3070. END;
  3071. InitOperand(result,ModeValue);
  3072. Reuse2a(result.op,left.op,right.op,dest);
  3073. Emit(Div(position,result.op,left.op,right.op));
  3074. ReleaseOperand(left); ReleaseOperand(right);
  3075. |Scanner.Mod:
  3076. Evaluate(x.left,left);
  3077. Evaluate(x.right,right);
  3078. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3079. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3080. IF ~isUnchecked THEN
  3081. exit := NewLabel();
  3082. BrltL(exit,zero,right.op);
  3083. EmitTrap(position,NegativeDivisorTrap);
  3084. SetLabel(exit);
  3085. END;
  3086. END;
  3087. InitOperand(result,ModeValue);
  3088. Reuse2a(result.op,left.op,right.op,dest);
  3089. Emit(Mod(position,result.op,left.op,right.op));
  3090. ReleaseOperand(left); ReleaseOperand(right);
  3091. |Scanner.Slash:
  3092. Evaluate(x.left,left);
  3093. Evaluate(x.right,right);
  3094. IF leftType IS SyntaxTree.SetType THEN
  3095. InitOperand(result,ModeValue);
  3096. Reuse2a(result.op,left.op,right.op,dest);
  3097. Emit(Xor(position,result.op,left.op,right.op));
  3098. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3099. InitOperand(result,ModeValue);
  3100. componentType := left.op.type;
  3101. (* review this *)
  3102. (*
  3103. divisor = right.op * right.op + right.tag * right.tag
  3104. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3105. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3106. *)
  3107. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3108. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3109. Emit(Mul(position,tempReg, right.op, right.op));
  3110. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3111. Emit(Add(position,tempReg, tempReg, tempReg2));
  3112. result.op := tempReg2;
  3113. Emit(Mul(position,result.op, left.op, right.op));
  3114. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3115. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3116. Emit(Add(position,result.op, result.op, tempReg2));
  3117. Emit(Div(position,result.op, result.op, tempReg));
  3118. Reuse2(result.tag, left.tag, right.op);
  3119. Emit(Mul(position,result.tag, left.tag, right.op));
  3120. Emit(Mul(position,tempReg2, left.op, right.tag));
  3121. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3122. Emit(Div(position,result.tag, result.tag, tempReg));
  3123. ReleaseIntermediateOperand(tempReg);
  3124. ReleaseIntermediateOperand(tempReg2)
  3125. ELSE
  3126. InitOperand(result,ModeValue);
  3127. Reuse2a(result.op,left.op,right.op,dest);
  3128. Emit(Div(position,result.op,left.op,right.op));
  3129. END;
  3130. ReleaseOperand(left); ReleaseOperand(right);
  3131. |Scanner.Equal:
  3132. IF ~conditional THEN ConditionToValue(x);
  3133. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3134. CompareString(BreqL,x.left,x.right);
  3135. ELSE
  3136. Evaluate(x.left,left);
  3137. Evaluate(x.right,right);
  3138. IF leftType IS SyntaxTree.RangeType THEN
  3139. ASSERT(rightType IS SyntaxTree.RangeType);
  3140. BrneL(falseLabel, left.op, right.op); (* first *)
  3141. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3142. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3143. ReleaseOperand(left); ReleaseOperand(right);
  3144. BrL(trueLabel)
  3145. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3146. BrneL(falseLabel, left.op, right.op); (* first *)
  3147. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3148. ReleaseOperand(left); ReleaseOperand(right);
  3149. BrL(trueLabel)
  3150. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3151. (* TODO: review this *)
  3152. BrneL(falseLabel, left.op, right.op); (* real part *)
  3153. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3154. ReleaseOperand(left); ReleaseOperand(right);
  3155. BrL(trueLabel)
  3156. ELSE
  3157. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3158. ReleaseOperand(left); ReleaseOperand(right);
  3159. BrL(trueLabel);
  3160. END;
  3161. END;
  3162. |Scanner.LessEqual:
  3163. IF ~conditional THEN ConditionToValue(x);
  3164. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3165. CompareString(BrgeL,x.right,x.left);
  3166. ELSE
  3167. Evaluate(x.left,left);
  3168. Evaluate(x.right,right);
  3169. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3170. Reuse1(temp.op,right.op);
  3171. Emit(And(position,temp.op,left.op,right.op));
  3172. ReleaseOperand(right);
  3173. BreqL(trueLabel,temp.op,left.op);
  3174. BrL(falseLabel);
  3175. ReleaseOperand(temp);ReleaseOperand(left);
  3176. ELSE
  3177. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3178. ReleaseOperand(left); ReleaseOperand(right);
  3179. BrL(trueLabel);
  3180. END;
  3181. END;
  3182. |Scanner.Less:
  3183. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3184. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3185. leftExpression.SetType(system.booleanType);
  3186. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3187. rightExpression.SetType(system.booleanType);
  3188. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3189. leftExpression.SetType(system.booleanType);
  3190. Expression(leftExpression);
  3191. ELSIF ~conditional THEN ConditionToValue(x);
  3192. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3193. CompareString(BrltL,x.left,x.right);
  3194. ELSE
  3195. Evaluate(x.left,left);
  3196. Evaluate(x.right,right);
  3197. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3198. ReleaseOperand(left); ReleaseOperand(right);
  3199. BrL(trueLabel);
  3200. END;
  3201. |Scanner.Greater:
  3202. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3203. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3204. leftExpression.SetType(system.booleanType);
  3205. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3206. rightExpression.SetType(system.booleanType);
  3207. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3208. leftExpression.SetType(system.booleanType);
  3209. Expression(leftExpression);
  3210. ELSIF ~conditional THEN ConditionToValue(x);
  3211. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3212. CompareString(BrltL,x.right,x.left);
  3213. ELSE
  3214. Evaluate(x.left,left);
  3215. Evaluate(x.right,right);
  3216. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3217. ReleaseOperand(left); ReleaseOperand(right);
  3218. BrL(trueLabel);
  3219. END;
  3220. |Scanner.GreaterEqual:
  3221. IF ~conditional THEN ConditionToValue(x);
  3222. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3223. CompareString(BrgeL,x.left,x.right);
  3224. ELSE
  3225. Evaluate(x.left,left);
  3226. Evaluate(x.right,right);
  3227. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3228. Reuse1(temp.op,left.op);
  3229. Emit(And(position,temp.op,left.op,right.op));
  3230. ReleaseOperand(left);
  3231. BreqL(trueLabel, temp.op,right.op);
  3232. ReleaseOperand(temp); ReleaseOperand(right);
  3233. BrL(falseLabel);
  3234. ELSE
  3235. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3236. ReleaseOperand(left); ReleaseOperand(right);
  3237. BrL(trueLabel);
  3238. END;
  3239. END;
  3240. |Scanner.Unequal:
  3241. IF ~conditional THEN ConditionToValue(x);
  3242. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3243. CompareString(BrneL,x.left,x.right);
  3244. ELSE
  3245. Evaluate(x.left,left);
  3246. Evaluate(x.right,right);
  3247. IF leftType IS SyntaxTree.RangeType THEN
  3248. ASSERT(rightType IS SyntaxTree.RangeType);
  3249. BrneL(trueLabel, left.op, right.op); (* first *)
  3250. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3251. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3252. ReleaseOperand(left); ReleaseOperand(right);
  3253. BrL(falseLabel)
  3254. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3255. BrneL(trueLabel, left.op, right.op); (* first *)
  3256. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3257. ReleaseOperand(left); ReleaseOperand(right);
  3258. BrL(falseLabel)
  3259. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3260. (* TODO: review this *)
  3261. BrneL(trueLabel, left.op, right.op); (* real part *)
  3262. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3263. ReleaseOperand(left); ReleaseOperand(right);
  3264. BrL(falseLabel)
  3265. ELSE
  3266. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3267. ReleaseOperand(left); ReleaseOperand(right);
  3268. BrL(trueLabel);
  3269. END;
  3270. END;
  3271. |Scanner.In:
  3272. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3273. Evaluate(x.left,left);
  3274. Evaluate(x.right,right);
  3275. Convert(left.op,setType);
  3276. ReuseCopy(temp.op,right.op);
  3277. Emit(Shr(position,temp.op,temp.op,left.op));
  3278. ReleaseOperand(right); ReleaseOperand(left);
  3279. IntermediateCode.InitImmediate(one,setType,1);
  3280. Emit(And(position,temp.op,temp.op,one));
  3281. IF conditional THEN
  3282. IntermediateCode.InitImmediate(zero,setType,0);
  3283. BrneL(trueLabel,temp.op,zero);
  3284. ReleaseOperand(temp);
  3285. BrL(falseLabel);
  3286. ELSE
  3287. Convert(temp.op,bool);
  3288. result.mode := ModeValue;
  3289. result.op := temp.op;
  3290. result.tag := nil; (* may be left over from calls to evaluate *)
  3291. END;
  3292. ELSE
  3293. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3294. IF x.operator = Scanner.Questionmarks THEN
  3295. leftExpression := x.left;
  3296. rightExpression := x.right;
  3297. ELSE
  3298. leftExpression := x.right;
  3299. rightExpression := x.left;
  3300. END;
  3301. Evaluate(leftExpression, left);
  3302. Emit(Push(position,left.op));
  3303. ReleaseOperand(left);
  3304. Designate(rightExpression, right);
  3305. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3306. IF ~backend.cellsAreObjects THEN
  3307. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3308. END;
  3309. Emit(Push(position,right.op));
  3310. ReleaseOperand(right);
  3311. IF backend.cellsAreObjects THEN
  3312. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3313. ELSE
  3314. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3315. END;
  3316. InitOperand(result, ModeValue);
  3317. result.op := NewRegisterOperand(bool);
  3318. Emit(Result(position,result.op));
  3319. IF conditional THEN
  3320. IntermediateCode.InitImmediate(zero,setType,0);
  3321. BrneL(trueLabel,result.op,zero);
  3322. ReleaseOperand(result);
  3323. BrL(falseLabel);
  3324. END;
  3325. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3326. leftExpression := x.left;
  3327. rightExpression := x.right;
  3328. Evaluate(leftExpression, left);
  3329. Emit(Push(position,left.op));
  3330. ReleaseOperand(left);
  3331. Evaluate(rightExpression, right);
  3332. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3333. IF ~backend.cellsAreObjects THEN
  3334. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3335. END;
  3336. Emit(Push(position,right.op));
  3337. ReleaseOperand(right);
  3338. IF backend.cellsAreObjects THEN
  3339. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3340. ELSE
  3341. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3342. END;
  3343. InitOperand(result, ModeValue);
  3344. result.op := NewRegisterOperand(bool);
  3345. Emit(Result(position,result.op));
  3346. IF conditional THEN
  3347. IntermediateCode.InitImmediate(zero,setType,0);
  3348. BrneL(trueLabel,result.op,zero);
  3349. ReleaseOperand(result);
  3350. BrL(falseLabel);
  3351. END;
  3352. ELSE
  3353. HALT(100);
  3354. END;
  3355. END;
  3356. destination := dest;
  3357. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3358. END VisitBinaryExpression;
  3359. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3360. VAR localResult, operand: Operand;
  3361. BEGIN
  3362. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3363. InitOperand(localResult, ModeValue);
  3364. ASSERT(x.first # NIL);
  3365. Evaluate(x.first, operand);
  3366. localResult.op := operand.op;
  3367. ReleaseOperand(operand);
  3368. UseIntermediateOperand(localResult.op);
  3369. ASSERT(x.last # NIL);
  3370. Evaluate(x.last, operand);
  3371. localResult.tag := operand.op;
  3372. ReleaseOperand(operand);
  3373. UseIntermediateOperand(localResult.tag);
  3374. IF x.step # NIL THEN
  3375. Evaluate(x.step, operand);
  3376. localResult.extra := operand.op;
  3377. ReleaseOperand(operand);
  3378. UseIntermediateOperand(localResult.extra);
  3379. END;
  3380. result := localResult;
  3381. IF Trace THEN TraceExit("VisitRangeExpression") END
  3382. END VisitRangeExpression;
  3383. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3384. BEGIN
  3385. HALT(100); (* should never be evaluated *)
  3386. END VisitTensorRangeExpression;
  3387. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3388. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3389. BEGIN
  3390. IF Trace THEN TraceEnter("VisitConversion") END;
  3391. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3392. dest := destination; destination := emptyOperand;
  3393. Evaluate(x.expression,old);
  3394. InitOperand(result,ModeValue);
  3395. result.op := old.op;
  3396. ASSERT(result.op.mode # 0);
  3397. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3398. (* convert TO a complex number *)
  3399. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3400. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3401. ASSERT(result.op.mode # 0);
  3402. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3403. (* convert FROM a complex number TO a complex number*)
  3404. result.tag := old.tag;
  3405. ASSERT(result.tag.mode # 0);
  3406. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3407. ASSERT(result.tag.mode # 0)
  3408. ELSE
  3409. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3410. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3411. END
  3412. ELSE
  3413. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3414. ASSERT(result.op.mode # 0);
  3415. result.tag := old.tag; (*! probably never used *)
  3416. END;
  3417. destination := dest;
  3418. IF Trace THEN TraceExit("VisitConversion") END;
  3419. END VisitConversion;
  3420. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3421. BEGIN
  3422. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3423. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3424. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3425. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3426. END VisitTypeDeclaration;
  3427. (** designators (expressions) *)
  3428. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3429. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3430. BEGIN
  3431. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3432. IF x.left # NIL THEN Expression(x.left) END;
  3433. Symbol(x.symbol,result);
  3434. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3435. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3436. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3437. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3438. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3439. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3440. END;
  3441. END;
  3442. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3443. END VisitSymbolDesignator;
  3444. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3445. BEGIN
  3446. IF isUnchecked THEN RETURN END;
  3447. IF tagsAvailable THEN
  3448. TrapC(BrltL,index,length,IndexCheckTrap);
  3449. END;
  3450. END BoundCheck;
  3451. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3452. VAR d: IntermediateCode.Operand;
  3453. BEGIN
  3454. IF isUnchecked THEN RETURN END;
  3455. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3456. TrapC(op,dim,d,ArraySizeTrap);
  3457. ReleaseIntermediateOperand(d);
  3458. END DimensionCheck;
  3459. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3460. VAR
  3461. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3462. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3463. expression: SyntaxTree.Expression;
  3464. resultingType, leftType, baseType: SyntaxTree.Type;
  3465. skipLabel1: Label;
  3466. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3467. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3468. variableOp: Operand;
  3469. variable: SyntaxTree.Variable;
  3470. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3471. VAR expression: SyntaxTree.Expression;
  3472. BEGIN
  3473. (* count the number of indices, ranges and tensorRanges in the index list *)
  3474. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3475. FOR i := 0 TO parameters.Length() - 1 DO
  3476. expression := parameters.GetExpression(i);
  3477. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3478. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3479. ELSE INC(indexCount)
  3480. END
  3481. END;
  3482. END CountIndices;
  3483. BEGIN
  3484. ASSERT(tagsAvailable);
  3485. resultingType := x.type.resolved; (* resulting type *)
  3486. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3487. InitOperand(localResult, ModeReference);
  3488. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3489. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3490. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3491. (* a globally defined array destination tag is available -> use and invalidate it*)
  3492. localResult.tag := arrayDestinationTag;
  3493. IntermediateCode.InitOperand(arrayDestinationTag)
  3494. ELSE
  3495. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3496. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3497. Symbol(variable, variableOp);
  3498. ReuseCopy(localResult.tag, variableOp.op);
  3499. ReleaseOperand(variableOp);
  3500. END
  3501. END;
  3502. indexListSize := x.parameters.Length();
  3503. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3504. ASSERT(tensorRangeCount <= 1);
  3505. (* designate the array to be indexed over, perform tensor range check if known *)
  3506. Designate(x.left, array);
  3507. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3508. Dereference(array, leftType,FALSE);
  3509. IF tensorRangeCount=0 THEN
  3510. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3511. END
  3512. END;
  3513. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3514. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3515. srcDimOffset := -indexListSize;
  3516. destDimOffset := -rangeCount
  3517. ELSE
  3518. srcDimOffset := 0;
  3519. destDimOffset := 0
  3520. END;
  3521. indexDim := 0;
  3522. (* use address of source array as basis *)
  3523. (*
  3524. ReuseCopy(localResult.op, array.op);
  3525. *)
  3526. localResult.op := array.op;
  3527. UseIntermediateOperand(localResult.op);
  3528. (* go through the index list *)
  3529. FOR i := 0 TO indexListSize - 1 DO
  3530. expression := x.parameters.GetExpression(i);
  3531. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3532. (* nothing to do *)
  3533. ELSE
  3534. (* determine which dimension of source array is currently looked at *)
  3535. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3536. (* get the memory operand pointing to array descriptor dimension *)
  3537. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3538. (* make a reusable register from it *)
  3539. ReuseCopy(srcDim, tmp);
  3540. ReleaseIntermediateOperand(tmp);
  3541. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3542. ELSE
  3543. srcDim := IntermediateCode.Immediate(int32, i)
  3544. END;
  3545. (* get length and increment of source array for current dimension *)
  3546. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3547. Convert(sourceLength.op, sizeType);
  3548. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3549. Convert(sourceIncrement.op, sizeType);
  3550. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3551. ReleaseIntermediateOperand(srcDim);
  3552. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3553. (* SINGLE INDEX *)
  3554. Evaluate(expression, index);
  3555. ReleaseIntermediateOperand(index.tag);
  3556. index.tag := emptyOperand;
  3557. Convert(index.op, sizeType);
  3558. (* lower bound check *)
  3559. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3560. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3561. ELSIF isUnchecked THEN (* do nothing *)
  3562. ELSE
  3563. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3564. END;
  3565. (* upper bound check *)
  3566. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3567. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3568. ELSIF isUnchecked THEN (* do nothing *)
  3569. ELSE
  3570. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3571. END;
  3572. ReleaseOperand(sourceLength);
  3573. Convert(index.op, addressType);
  3574. summand := index.op;
  3575. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3576. (* RANGE OF INDICES *)
  3577. Evaluate(expression, range);
  3578. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3579. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3580. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3581. ReleaseOperand(range);
  3582. Convert(firstIndex, sizeType);
  3583. Convert(lastIndex, sizeType);
  3584. Convert(stepSize, sizeType);
  3585. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3586. if it is 'MAX(LONGINT)' *)
  3587. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3588. TransferToRegister(lastIndex, lastIndex);
  3589. skipLabel1 := NewLabel();
  3590. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3591. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3592. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3593. SetLabel(skipLabel1)
  3594. END;
  3595. (* check if step size is valid *)
  3596. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3597. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3598. ELSIF isUnchecked THEN (* do nothing *)
  3599. ELSE
  3600. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3601. END;
  3602. (* check lower bound check *)
  3603. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3604. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3605. ELSIF isUnchecked THEN (* do nothing *)
  3606. ELSE
  3607. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3608. END;
  3609. (* check upper bound check *)
  3610. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3611. (* statically open range: nothing to do *)
  3612. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3613. ASSERT(staticLastIndex < staticSourceLength)
  3614. ELSIF isUnchecked THEN (* do nothing *)
  3615. ELSE
  3616. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3617. END;
  3618. (* determine length of target array for current dimension *)
  3619. (* 1. incorporate last index: *)
  3620. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3621. (* last index is static *)
  3622. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3623. targetLength := sourceLength.op
  3624. ELSE
  3625. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3626. END;
  3627. UseIntermediateOperand(targetLength);
  3628. ELSE
  3629. (* targetLength := lastIndex + 1
  3630. Reuse1(targetLength, lastIndex);
  3631. *)
  3632. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3633. END;
  3634. ReleaseOperand(sourceLength);
  3635. ReleaseIntermediateOperand(lastIndex);
  3636. (* 2. incorporate first index: *)
  3637. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3638. (* first index and current target length are static *)
  3639. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3640. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3641. (* first index = 0: nothing to do *)
  3642. ELSE
  3643. (* targetLength := targetLength - firstIndex *)
  3644. TransferToRegister(targetLength, targetLength);
  3645. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3646. END;
  3647. (* clip negative lengths to 0 *)
  3648. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3649. IF staticTargetLength < 0 THEN
  3650. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3651. END
  3652. ELSE
  3653. skipLabel1 := NewLabel();
  3654. TransferToRegister(targetLength, targetLength);
  3655. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3656. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3657. SetLabel(skipLabel1)
  3658. END;
  3659. (* 3. incorporate index step size: *)
  3660. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3661. (*step size and current target length are static *)
  3662. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3663. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3664. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3665. (* step size = 1: nothing to do *)
  3666. ELSE
  3667. (* emit code for this:
  3668. targetLength := (targetLength-1) DIV stepSize +1;
  3669. *)
  3670. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3671. DivInt(targetLength, targetLength, stepSize);
  3672. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3673. END;
  3674. (* determine increment of target array for current dimension *)
  3675. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3676. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3677. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3678. (* step size = 1 *)
  3679. targetIncrement := sourceIncrement.op;
  3680. UseIntermediateOperand(targetIncrement)
  3681. ELSE
  3682. (* targetIncrement := sourceIncrement * stepSize *)
  3683. Reuse1(targetIncrement, stepSize);
  3684. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3685. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3686. END;
  3687. ReleaseIntermediateOperand(stepSize);
  3688. (* write length and increment of target array to descriptor *)
  3689. IF destDimOffset < 0 THEN
  3690. (* determine which dimension of target array is currently looked at *)
  3691. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3692. TransferToRegister(destDim, tmp);
  3693. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3694. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3695. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3696. ReleaseIntermediateOperand(destDim)
  3697. ELSE
  3698. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3699. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3700. END;
  3701. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3702. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3703. INC(indexDim);
  3704. Convert(firstIndex, addressType);
  3705. TransferToRegister(summand, firstIndex);
  3706. ELSE HALT(100);
  3707. END;
  3708. (*
  3709. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3710. *)
  3711. Convert(sourceIncrement.op, addressType);
  3712. Convert(summand, addressType);
  3713. MulInt(summand, summand, sourceIncrement.op);
  3714. ReleaseIntermediateOperand(sourceIncrement.op);
  3715. AddInt(localResult.op, localResult.op, summand);
  3716. ReleaseIntermediateOperand(summand);
  3717. END
  3718. END;
  3719. result := localResult;
  3720. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3721. ReleaseIntermediateOperand(result.tag);
  3722. result.tag := TypeDescriptorAdr(resultingType);
  3723. IF ~newObjectFile THEN
  3724. IntermediateCode.MakeMemory(result.tag,addressType);
  3725. END;
  3726. ELSIF IsDelegate(resultingType) THEN
  3727. ReleaseIntermediateOperand(result.tag);
  3728. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3729. UseIntermediateOperand(result.tag);
  3730. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3731. ReleaseIntermediateOperand(result.tag);
  3732. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3733. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3734. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3735. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3736. (* finalize target array descriptor *)
  3737. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3738. (* write lengths and increments of target array for remaining dimensions *)
  3739. i := indexListSize;
  3740. WHILE indexDim < targetArrayDimensionality DO
  3741. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3742. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3743. ReleaseOperand(sourceLength);
  3744. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3745. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3746. ReleaseOperand(sourceIncrement);
  3747. INC(i); INC(indexDim);
  3748. END;
  3749. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3750. tmp := nil;
  3751. ELSE
  3752. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3753. END;
  3754. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3755. ReleaseIntermediateOperand(tmp);
  3756. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3757. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3758. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3759. ELSE
  3760. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3761. END;
  3762. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3763. ReleaseIntermediateOperand(tmp);
  3764. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3765. (* write dimensionality *)
  3766. IF targetArrayDimensionality # 0 THEN
  3767. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3768. END;
  3769. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3770. END;
  3771. ReleaseOperand(array);
  3772. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3773. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3774. END;
  3775. END MathIndexDesignator;
  3776. (* get the length of an array , trying to make use of static information *)
  3777. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3778. VAR res: IntermediateCode.Operand; size: LONGINT;
  3779. BEGIN
  3780. type := type.resolved;
  3781. IF type IS SyntaxTree.ArrayType THEN
  3782. WITH type: SyntaxTree.ArrayType DO
  3783. IF type.form = SyntaxTree.Static THEN
  3784. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3785. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3786. Evaluate(type.length, op);
  3787. ReleaseIntermediateOperand(op.tag);
  3788. RETURN op.op;*)
  3789. ELSE
  3790. res := tag;
  3791. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3792. IntermediateCode.MakeMemory(res,addressType);
  3793. UseIntermediateOperand(res);
  3794. RETURN res
  3795. END
  3796. END;
  3797. ELSE
  3798. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3799. RETURN IntermediateCode.Immediate(addressType,size);
  3800. END;
  3801. END ArrayLength;
  3802. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3803. BEGIN
  3804. IF IsImmediate(x) THEN
  3805. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3806. ELSE
  3807. IF ~ReusableRegister(res) THEN
  3808. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3809. ELSE
  3810. UseIntermediateOperand(res);
  3811. END;
  3812. Emit(Mov(position,res,x))
  3813. END;
  3814. END CopyInt;
  3815. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3816. BEGIN
  3817. ReleaseIntermediateOperand(res);
  3818. IF IsImmediate(x) & IsImmediate(y) THEN
  3819. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3820. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3821. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3822. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3823. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3824. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3825. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3826. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3827. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3828. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3829. UseIntermediateOperand(res);
  3830. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3831. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3832. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3833. UseIntermediateOperand(res);
  3834. ELSE
  3835. IF ~ReusableRegister(res) THEN
  3836. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3837. ELSE
  3838. UseIntermediateOperand(res);
  3839. END;
  3840. IF IsImmediate(x) & (x.intValue = 0) THEN
  3841. Emit(Mov(position,res,y))
  3842. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3843. Emit(Mov(position,res,x))
  3844. ELSE
  3845. Emit(Add(position,res, x, y));
  3846. END;
  3847. END;
  3848. END AddInt;
  3849. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3850. BEGIN
  3851. ReleaseIntermediateOperand(res);
  3852. IF IsImmediate(x) & IsImmediate(y) THEN
  3853. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3854. ELSE
  3855. IF ~ReusableRegister(res) THEN
  3856. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3857. ELSE UseIntermediateOperand(res);
  3858. END;
  3859. IF IsImmediate(x) & (x.intValue = 1) THEN
  3860. Emit(Mov(position,res,y))
  3861. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3862. Emit(Mov(position,res,x))
  3863. ELSE
  3864. Emit(Mul(position,res, x, y));
  3865. END;
  3866. END;
  3867. END MulInt;
  3868. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3869. BEGIN
  3870. ReleaseIntermediateOperand(res);
  3871. IF IsImmediate(x) & IsImmediate(y) THEN
  3872. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3873. ELSE
  3874. IF ~ReusableRegister(res) THEN
  3875. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3876. ELSE UseIntermediateOperand(res);
  3877. END;
  3878. IF IsImmediate(x) & (x.intValue = 1) THEN
  3879. Emit(Mov(position,res,y))
  3880. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3881. Emit(Mov(position,res,x))
  3882. ELSE
  3883. Emit(Div(position,res, x, y));
  3884. END;
  3885. END;
  3886. END DivInt;
  3887. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3888. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3889. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3890. BEGIN
  3891. type := x.left.type.resolved;
  3892. IF type IS SyntaxTree.StringType THEN
  3893. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3894. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3895. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3896. type := atype;
  3897. x.left.SetType(type);
  3898. END;
  3899. IntermediateCode.InitImmediate(res,addressType,0);
  3900. maxDim := x.parameters.Length()-1;
  3901. (*
  3902. computation rule:
  3903. a: ARRAY X,Y,Z OF Element with size S
  3904. a[i,j,k] -->
  3905. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3906. *)
  3907. FOR i := 0 TO maxDim DO
  3908. e := x.parameters.GetExpression(i);
  3909. Evaluate(e,index);
  3910. Convert(index.op,addressType);
  3911. AddInt(res, res, index.op);
  3912. IF i = 0 THEN
  3913. (*
  3914. ReuseCopy(res, index.op);
  3915. *)
  3916. Designate(x.left,array);
  3917. type := x.left.type.resolved;
  3918. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3919. Dereference(array, type, FALSE);
  3920. END;
  3921. (*
  3922. ELSE AddInt(res, res, index.op);
  3923. *)
  3924. END;
  3925. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3926. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3927. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3928. BoundCheck(index.op, length);
  3929. END;
  3930. ReleaseIntermediateOperand(length);
  3931. END;
  3932. ReleaseOperand(index);
  3933. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3934. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3935. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3936. MulInt(res,res,length);
  3937. END;
  3938. ReleaseIntermediateOperand(length);
  3939. END;
  3940. (* remaining open dimensions -- compute address *)
  3941. i := maxDim+1;
  3942. IF type IS SyntaxTree.ArrayType THEN
  3943. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3944. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3945. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3946. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3947. MulInt(res,res,length);
  3948. END;
  3949. ReleaseIntermediateOperand(length);
  3950. INC(i);
  3951. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3952. END;
  3953. END;
  3954. IF (type IS SyntaxTree.ArrayType) THEN
  3955. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3956. size := StaticSize(system, type);
  3957. IF size # 1 THEN
  3958. length := IntermediateCode.Immediate(addressType,size);
  3959. MulInt(res,res,length);
  3960. END;
  3961. ELSE
  3962. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3963. IF size # 1 THEN
  3964. length := IntermediateCode.Immediate(addressType,size);
  3965. MulInt(res,res,length);
  3966. END;
  3967. END;
  3968. END;
  3969. AddInt(res,res,array.op);
  3970. InitOperand(result,ModeReference);
  3971. result.op := res;
  3972. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3973. ReleaseIntermediateOperand(result.tag);
  3974. result.tag := TypeDescriptorAdr(type);
  3975. IF ~newObjectFile THEN
  3976. IntermediateCode.MakeMemory(result.tag,addressType);
  3977. END
  3978. ELSIF IsDelegate(type) THEN
  3979. ReleaseIntermediateOperand(result.tag);
  3980. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3981. UseIntermediateOperand(result.tag);
  3982. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3983. ReleaseIntermediateOperand(result.tag);
  3984. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3985. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3986. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3987. END;
  3988. ReleaseOperand(array);
  3989. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3990. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3991. END;
  3992. END IndexDesignator;
  3993. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3994. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3995. BEGIN
  3996. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3997. dest := destination; destination := emptyOperand;
  3998. type := x.left.type.resolved;
  3999. IF type IS SyntaxTree.MathArrayType THEN
  4000. MathIndexDesignator(x);
  4001. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4002. IndexDesignator(x);
  4003. END;
  4004. destination := dest;
  4005. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4006. END VisitIndexDesignator;
  4007. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4008. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4009. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4010. procedure: SyntaxTree.Procedure;
  4011. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4012. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4013. BEGIN
  4014. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4015. parameters := expression.parameters;
  4016. moduleName := "FoxArrayBase";
  4017. procedureName := "CopyDescriptor";
  4018. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4019. SaveRegisters();ReleaseUsedRegisters(saved);
  4020. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4021. IF procedure = NIL THEN
  4022. s := "procedure ";
  4023. Strings.Append(s,moduleName);
  4024. Strings.Append(s,".");
  4025. Strings.Append(s,procedureName);
  4026. Strings.Append(s," not present");
  4027. Error(position,s);
  4028. ELSE
  4029. (* push address of temporary variable *)
  4030. Symbol(variable,destOperand);
  4031. Emit(Push(position,destOperand.op));
  4032. ReleaseOperand(destOperand);
  4033. (* push src *)
  4034. Evaluate(expression.left,srcOperand);
  4035. (*
  4036. Dereference(srcOperand,expression.type.resolved);
  4037. Emit(Push(position,srcOperand.tag));
  4038. *)
  4039. Emit(Push(position,srcOperand.op));
  4040. ReleaseOperand(srcOperand);
  4041. tensorFound := FALSE;
  4042. FOR i := 0 TO parameters.Length()-1 DO
  4043. e := parameters.GetExpression(i);
  4044. IF e IS SyntaxTree.TensorRangeExpression THEN
  4045. tensorFound := TRUE;
  4046. ELSIF e IS SyntaxTree.RangeExpression THEN
  4047. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4048. ELSE
  4049. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4050. END;
  4051. END;
  4052. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4053. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4054. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4055. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4056. StaticCallOperand(procOp,procedure);
  4057. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4058. ReleaseOperand(procOp);
  4059. END;
  4060. RestoreRegisters(saved);
  4061. END;
  4062. RETURN variable
  4063. END PrepareTensorDescriptor;
  4064. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4065. VAR
  4066. type, descriptorType, baseType: SyntaxTree.Type;
  4067. operand, tmpOperand, variableOp, variable2Op: Operand;
  4068. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4069. variable, variable2: SyntaxTree.Variable;
  4070. dim, i, size: LONGINT;
  4071. (* TODO: needed? *)
  4072. oldArrayDestinationTag: IntermediateCode.Operand;
  4073. oldArrayDestinationDimension: LONGINT;
  4074. position: Position;
  4075. saved: RegisterEntry;
  4076. arrayFlags: SET;
  4077. m, n: LONGINT;
  4078. PROCEDURE Pass(op: IntermediateCode.Operand);
  4079. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4080. BEGIN
  4081. IF numberRegister >= 0 THEN
  4082. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4083. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4084. Emit(Mov(position,parameterRegister, op));
  4085. ELSE
  4086. Emit(Push(position,op))
  4087. END
  4088. END Pass;
  4089. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4090. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4091. BEGIN
  4092. formalType := formalType.resolved; actualType := actualType.resolved;
  4093. IF IsOpenArray(formalType)THEN
  4094. IF actualType IS SyntaxTree.StringType THEN
  4095. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4096. RETURN;
  4097. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4098. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4099. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4100. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4101. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4102. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4103. ELSE
  4104. tmp := baseReg;
  4105. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4106. IntermediateCode.MakeMemory(tmp,addressType);
  4107. Pass((tmp));
  4108. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4109. END;
  4110. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4111. END;
  4112. END PushArrayLens;
  4113. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4114. BEGIN
  4115. CASE size OF
  4116. |2: INCL(flags,Size2Flag);
  4117. |3: INCL(flags,Size3Flag);
  4118. |4: INCL(flags,Size4Flag);
  4119. |5: INCL(flags,Size5Flag);
  4120. |6: INCL(flags,Size6Flag);
  4121. |7: INCL(flags,Size7Flag);
  4122. |8: INCL(flags,Size8Flag);
  4123. END;
  4124. END SetSmallArraySizeFlag;
  4125. BEGIN
  4126. IF Trace THEN TraceEnter("PushParameter") END;
  4127. position := expression.position;
  4128. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4129. type := expression.type.resolved;
  4130. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4131. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4132. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4133. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4134. );
  4135. (* TODO: needed? *)
  4136. oldArrayDestinationTag := arrayDestinationTag;
  4137. oldArrayDestinationDimension := arrayDestinationDimension;
  4138. IF IsArrayOfSystemByte(parameter.type) THEN
  4139. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4140. Designate(expression,operand);
  4141. ELSE
  4142. Evaluate(expression, tmpOperand);
  4143. variable := GetTemporaryVariable(expression.type, FALSE);
  4144. Symbol(variable, operand);
  4145. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4146. Emit(Mov(position,tmp, tmpOperand.op));
  4147. ReleaseOperand(tmpOperand);
  4148. END;
  4149. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4150. ReleaseIntermediateOperand(operand.tag);
  4151. operand.tag := tmp;
  4152. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4153. Pass((operand.tag));
  4154. END;
  4155. Pass((operand.op));
  4156. ELSIF IsOpenArray(parameter.type) THEN
  4157. Designate(expression,operand);
  4158. baseReg := operand.tag;
  4159. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4160. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4161. END;
  4162. Pass((operand.op)); (* address of the array *)
  4163. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4164. (* case 1 *)
  4165. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4166. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4167. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4168. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4169. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4170. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4171. arrayDestinationTag := sp;
  4172. (* case 1b *)
  4173. IF expression IS SyntaxTree.IndexDesignator THEN
  4174. (*
  4175. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4176. descriptorType := GetMathArrayDescriptorType(dim);
  4177. variable := GetTemporaryVariable(descriptorType,expression.position);
  4178. Symbol(variable,variableOp);
  4179. arrayDestinationTag := variableOp.op;
  4180. *)
  4181. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4182. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4183. arrayDestinationDimension := dim;
  4184. Designate(expression,operand);
  4185. (* case 1a *)
  4186. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4187. Designate(expression,operand);
  4188. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4189. i := 0;
  4190. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4191. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4192. INC(i);
  4193. END;
  4194. type := expression.type.resolved;
  4195. WHILE (i<dim) DO (* remaining static dimensions *)
  4196. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4197. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4198. ReleaseOperand(tmpOperand);
  4199. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4200. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4201. ReleaseOperand(tmpOperand);
  4202. INC(i);
  4203. END;
  4204. dimOp := IntermediateCode.Immediate(addressType,dim);
  4205. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4206. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4207. (* case 1d *)
  4208. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4209. Designate(expression,operand);
  4210. Dereference(operand,type.resolved,FALSE);
  4211. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4212. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4213. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4214. (* case 1f *)
  4215. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4216. Designate(expression,operand);
  4217. FOR i := 0 TO dim-1 DO
  4218. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4219. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4220. ReleaseOperand(tmpOperand);
  4221. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4222. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4223. ReleaseOperand(tmpOperand);
  4224. END;
  4225. (*******
  4226. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4227. *)
  4228. arrayFlags := {StaticFlag};
  4229. IF dim = 1 THEN
  4230. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4231. ReleaseOperand(tmpOperand);
  4232. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4233. m := LONGINT(tmpOperand.op.intValue);
  4234. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4235. ELSIF dim = 2 THEN
  4236. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4237. ReleaseOperand(tmpOperand);
  4238. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4239. m := LONGINT(tmpOperand.op.intValue);
  4240. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4241. ReleaseOperand(tmpOperand);
  4242. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4243. n := LONGINT(tmpOperand.op.intValue);
  4244. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4245. INCL(arrayFlags,SmallMatrixFlag);
  4246. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4247. END;
  4248. END;
  4249. (*******)
  4250. dimOp := IntermediateCode.Immediate(addressType,dim);
  4251. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4252. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4253. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4254. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4255. baseType := SemanticChecker.ArrayBase(type,dim);
  4256. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4257. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4258. ELSE HALT(100);
  4259. END;
  4260. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4261. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4262. (* case 2b *)
  4263. IF expression IS SyntaxTree.IndexDesignator THEN
  4264. descriptorType := GetMathArrayDescriptorType(dim);
  4265. variable := GetTemporaryVariable(descriptorType, FALSE);
  4266. Symbol(variable,variableOp);
  4267. arrayDestinationTag := variableOp.op;
  4268. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4269. arrayDestinationDimension := dim;
  4270. NeedDescriptor := TRUE;
  4271. Designate(expression,operand);
  4272. Pass((operand.tag));
  4273. NeedDescriptor := FALSE;
  4274. (* case 2a *)
  4275. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4276. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4277. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4278. INC(i);
  4279. END;
  4280. IF i = dim THEN
  4281. Designate(expression,operand);
  4282. Pass((operand.tag));
  4283. ELSE (* open-static *)
  4284. type := expression.type.resolved;
  4285. descriptorType := GetMathArrayDescriptorType(dim);
  4286. variable := GetTemporaryVariable(descriptorType, FALSE);
  4287. Symbol(variable,variableOp);
  4288. arrayDestinationTag := variableOp.op;
  4289. Designate(expression,operand);
  4290. FOR i := 0 TO dim-1 DO
  4291. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4292. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4293. ReleaseOperand(tmpOperand);
  4294. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4295. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4296. ReleaseOperand(tmpOperand);
  4297. END;
  4298. dimOp := IntermediateCode.Immediate(addressType,dim);
  4299. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4300. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4301. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4302. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4303. baseType := SemanticChecker.ArrayBase(type,dim);
  4304. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4305. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4306. Pass((arrayDestinationTag));
  4307. END;
  4308. (* case 2d *)
  4309. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4310. Designate(expression,operand);
  4311. Dereference(operand,type.resolved,FALSE);
  4312. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4313. Pass((operand.tag));
  4314. (* case 2f *)
  4315. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4316. descriptorType := GetMathArrayDescriptorType(dim);
  4317. variable := GetTemporaryVariable(descriptorType, FALSE);
  4318. Symbol(variable,variableOp);
  4319. arrayDestinationTag := variableOp.op;
  4320. Designate(expression,operand);
  4321. FOR i := 0 TO dim-1 DO
  4322. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4323. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4324. ReleaseOperand(tmpOperand);
  4325. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4326. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4327. ReleaseOperand(tmpOperand);
  4328. END;
  4329. (*
  4330. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4331. *)
  4332. arrayFlags := {StaticFlag};
  4333. IF dim = 1 THEN
  4334. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4335. ReleaseOperand(tmpOperand);
  4336. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4337. m := LONGINT(tmpOperand.op.intValue);
  4338. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4339. ELSIF dim = 2 THEN
  4340. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4341. ReleaseOperand(tmpOperand);
  4342. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4343. m := LONGINT(tmpOperand.op.intValue);
  4344. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4345. ReleaseOperand(tmpOperand);
  4346. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4347. n := LONGINT(tmpOperand.op.intValue);
  4348. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4349. INCL(arrayFlags,SmallMatrixFlag);
  4350. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4351. END;
  4352. END;
  4353. (*******)
  4354. dimOp := IntermediateCode.Immediate(addressType,dim);
  4355. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4356. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4357. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4358. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4359. baseType := SemanticChecker.ArrayBase(type,dim);
  4360. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4361. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4362. Pass((arrayDestinationTag));
  4363. ELSE HALT(100);
  4364. END;
  4365. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4366. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4367. (* case 3b *)
  4368. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4369. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4370. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4371. Symbol(variable,variableOp);
  4372. LoadValue(variableOp,system.addressType);
  4373. ELSE
  4374. descriptorType := GetMathArrayDescriptorType(dim);
  4375. variable := GetTemporaryVariable(descriptorType, FALSE);
  4376. Symbol(variable,variableOp);
  4377. END;
  4378. arrayDestinationTag := variableOp.op;
  4379. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4380. arrayDestinationDimension := 0;
  4381. Designate(expression,operand);
  4382. Pass((operand.tag));
  4383. (* case 3a *)
  4384. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4385. i := 0;
  4386. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4387. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4388. INC(i);
  4389. END;
  4390. IF i = dim THEN
  4391. Designate(expression,operand);
  4392. Pass((operand.tag));
  4393. ELSE (* open-static *)
  4394. type := expression.type.resolved;
  4395. descriptorType := GetMathArrayDescriptorType(dim);
  4396. variable := GetTemporaryVariable(descriptorType, FALSE);
  4397. Symbol(variable,variableOp);
  4398. arrayDestinationTag := variableOp.op;
  4399. Designate(expression,operand);
  4400. FOR i := 0 TO dim-1 DO
  4401. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4402. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4403. ReleaseOperand(tmpOperand);
  4404. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4405. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4406. ReleaseOperand(tmpOperand);
  4407. END;
  4408. dimOp := IntermediateCode.Immediate(addressType,dim);
  4409. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4410. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4411. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4412. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4413. baseType := SemanticChecker.ArrayBase(type,dim);
  4414. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4415. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4416. Pass((arrayDestinationTag));
  4417. END;
  4418. (* case 3d *)
  4419. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4420. Designate(expression,operand);
  4421. Dereference(operand,type.resolved,FALSE);
  4422. (*
  4423. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4424. *)
  4425. Pass((operand.tag));
  4426. (* case 3f *)
  4427. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4428. descriptorType := GetMathArrayDescriptorType(dim);
  4429. variable := GetTemporaryVariable(descriptorType, FALSE);
  4430. Symbol(variable,variableOp);
  4431. arrayDestinationTag := variableOp.op;
  4432. Designate(expression,operand);
  4433. IF operand.op.type.length >1 THEN (* vector register *)
  4434. variable2 := GetTemporaryVariable(type, FALSE);
  4435. Symbol(variable2, variable2Op);
  4436. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4437. Emit(Mov(position,tmp, operand.op));
  4438. ReleaseOperand(operand);
  4439. Symbol(variable2, operand);
  4440. END;
  4441. FOR i := 0 TO dim-1 DO
  4442. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4443. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4444. ReleaseOperand(tmpOperand);
  4445. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4446. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4447. ReleaseOperand(tmpOperand);
  4448. END;
  4449. dimOp := IntermediateCode.Immediate(addressType,dim);
  4450. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4451. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4452. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4453. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4454. baseType := SemanticChecker.ArrayBase(type,dim);
  4455. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4456. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4457. Pass((arrayDestinationTag));
  4458. ELSE HALT(100);
  4459. END;
  4460. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4461. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4462. (* case 4b *)
  4463. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4464. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4465. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4466. Symbol(variable,variableOp);
  4467. LoadValue(variableOp,system.addressType);
  4468. ELSE
  4469. descriptorType := GetMathArrayDescriptorType(dim);
  4470. variable := GetTemporaryVariable(descriptorType, FALSE);
  4471. Symbol(variable,variableOp);
  4472. END;
  4473. arrayDestinationTag := variableOp.op;
  4474. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4475. arrayDestinationDimension := 0;
  4476. Designate(expression,operand);
  4477. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4478. Symbol(variable,variableOp);
  4479. ELSE
  4480. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4481. Symbol(variable,variableOp);
  4482. MakeMemory(tmp,variableOp.op,addressType,0);
  4483. Emit(Mov(position,tmp,operand.tag));
  4484. ReleaseIntermediateOperand(tmp);
  4485. END;
  4486. Pass((variableOp.op));
  4487. ReleaseOperand(variableOp);
  4488. (* case 4a *)
  4489. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4490. i := 0;
  4491. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4492. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4493. INC(i);
  4494. END;
  4495. IF i = dim THEN
  4496. Designate(expression,operand);
  4497. arrayDestinationTag := operand.tag;
  4498. ELSE (* open-static *)
  4499. type := expression.type.resolved;
  4500. descriptorType := GetMathArrayDescriptorType(dim);
  4501. variable := GetTemporaryVariable(descriptorType, FALSE);
  4502. Symbol(variable,variableOp);
  4503. arrayDestinationTag := variableOp.op;
  4504. Designate(expression,operand);
  4505. FOR i := 0 TO dim-1 DO
  4506. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4507. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4508. ReleaseOperand(tmpOperand);
  4509. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4510. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4511. ReleaseOperand(tmpOperand);
  4512. END;
  4513. dimOp := IntermediateCode.Immediate(addressType,dim);
  4514. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4515. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4516. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4517. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4518. baseType := SemanticChecker.ArrayBase(type,dim);
  4519. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4520. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4521. END;
  4522. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4523. Symbol(variable,variableOp);
  4524. MakeMemory(tmp,variableOp.op,addressType,0);
  4525. Emit(Mov(position,tmp,arrayDestinationTag));
  4526. ReleaseIntermediateOperand(tmp);
  4527. Pass((variableOp.op));
  4528. ReleaseOperand(variableOp);
  4529. (* case 4d *)
  4530. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4531. Designate(expression,operand);
  4532. (*
  4533. Dereference(operand,type.resolved,FALSE);
  4534. *)
  4535. (*
  4536. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4537. *)
  4538. Pass((operand.op));
  4539. (* case 4f *)
  4540. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4541. descriptorType := GetMathArrayDescriptorType(dim);
  4542. variable := GetTemporaryVariable(descriptorType, FALSE);
  4543. Symbol(variable,variableOp);
  4544. arrayDestinationTag := variableOp.op;
  4545. Designate(expression,operand);
  4546. FOR i := 0 TO dim-1 DO
  4547. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4548. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4549. ReleaseOperand(tmpOperand);
  4550. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4551. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4552. ReleaseOperand(tmpOperand);
  4553. END;
  4554. dimOp := IntermediateCode.Immediate(addressType,dim);
  4555. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4556. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4557. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4558. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4559. baseType := SemanticChecker.ArrayBase(type,dim);
  4560. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4561. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4562. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4563. Symbol(variable,variableOp);
  4564. MakeMemory(tmp,variableOp.op,addressType,0);
  4565. Emit(Mov(position,tmp,arrayDestinationTag));
  4566. ReleaseIntermediateOperand(tmp);
  4567. Pass((variableOp.op));
  4568. ReleaseOperand(variableOp);
  4569. ELSE HALT(100);
  4570. END;
  4571. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4572. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4573. Designate(expression,operand);
  4574. IF operand.op.type.length > 1 THEN
  4575. Emit(Push(position, operand.op));
  4576. ReleaseOperand(operand);
  4577. ELSE
  4578. size := system.SizeOf(type);
  4579. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4580. size := ToMemoryUnits(system,size);
  4581. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4582. arrayDestinationTag := sp;
  4583. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4584. END;
  4585. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4586. Designate(expression,operand);
  4587. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4588. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4589. Symbol(variable,variableOp);
  4590. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4591. Emit(Mov(position,tmp,operand.op));
  4592. Emit(Push(position,variableOp.op));
  4593. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4594. Pass((operand.op));
  4595. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4596. END;
  4597. ELSE HALT(200)
  4598. END;
  4599. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4600. IF parameter.kind = SyntaxTree.VarParameter THEN
  4601. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4602. Designate(expression, operand);
  4603. Pass((operand.op));
  4604. ELSE
  4605. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4606. Evaluate(expression, operand);
  4607. Pass((operand.extra)); (* step *)
  4608. Pass((operand.tag)); (* last *)
  4609. Pass((operand.op)) (* first *)
  4610. END
  4611. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4612. IF parameter.kind = SyntaxTree.VarParameter THEN
  4613. Designate(expression, operand);
  4614. Pass((operand.op));
  4615. ELSE
  4616. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4617. Evaluate(expression, operand);
  4618. Pass((operand.tag)); (* real part *)
  4619. Pass((operand.op)) (* imaginary part *)
  4620. END
  4621. ELSE
  4622. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4623. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4624. Designate(expression,operand);
  4625. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4626. (* stack allocation *)
  4627. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4628. (*! parameter alignment to be discussed ... *)
  4629. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4630. size := type(SyntaxTree.StringType).length;
  4631. END;
  4632. IF backend.cooperative & parameter.NeedsTrace() THEN
  4633. dst := NewRegisterOperand (addressType);
  4634. Emit(Mov(position,dst, sp));
  4635. IntermediateCode.InitImmediate(null, byteType, 0);
  4636. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4637. ReleaseIntermediateOperand(dst);
  4638. SaveRegisters();ReleaseUsedRegisters(saved);
  4639. (* register dst has been freed before SaveRegisters already *)
  4640. CallAssignMethod(dst, operand.op, parameter.type);
  4641. RestoreRegisters(saved);
  4642. END;
  4643. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4644. ELSIF IsOpenArray(parameter.type) THEN
  4645. Designate(expression,operand);
  4646. baseReg := operand.tag;
  4647. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4648. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4649. END;
  4650. Pass((operand.op)); (* address of the array *)
  4651. ELSIF IsDelegate(parameter.type) THEN
  4652. Evaluate(expression,operand);
  4653. IF backend.cooperative & parameter.NeedsTrace() THEN
  4654. Emit(Push(position, nil));
  4655. dst := NewRegisterOperand (addressType);
  4656. Emit(Mov(position,dst, sp));
  4657. ReleaseIntermediateOperand(dst);
  4658. SaveRegisters();ReleaseUsedRegisters(saved);
  4659. Emit(Push(position, dst));
  4660. (* register dst has been freed before SaveRegisters already *)
  4661. Emit(Push(position, operand.tag));
  4662. CallThis(position,"GarbageCollector","Assign",2);
  4663. RestoreRegisters(saved);
  4664. ELSE
  4665. Pass((operand.tag));
  4666. END;
  4667. Pass((operand.op));
  4668. ELSE
  4669. Evaluate(expression,operand);
  4670. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4671. Emit(Push(position, nil));
  4672. dst := NewRegisterOperand (addressType);
  4673. Emit(Mov(position,dst, sp));
  4674. ReleaseIntermediateOperand(dst);
  4675. SaveRegisters();ReleaseUsedRegisters(saved);
  4676. Emit(Push(position, dst));
  4677. (* register dst has been freed before SaveRegisters already *)
  4678. Emit(Push(position, operand.op));
  4679. CallThis(position,"GarbageCollector","Assign",2);
  4680. RestoreRegisters(saved);
  4681. ELSE
  4682. Pass((operand.op));
  4683. END;
  4684. END;
  4685. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4686. Evaluate(expression,operand);
  4687. Pass((operand.op));
  4688. ELSE (* var parameter *)
  4689. Designate(expression,operand);
  4690. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4691. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4692. Pass((operand.tag));
  4693. END;
  4694. END;
  4695. Pass((operand.op));
  4696. END;
  4697. END;
  4698. (* TODO: needed? *)
  4699. arrayDestinationTag := oldArrayDestinationTag;
  4700. arrayDestinationDimension := oldArrayDestinationDimension;
  4701. IF needsParameterBackup THEN
  4702. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4703. ReuseCopy(parameterBackup, operand.op)
  4704. END;
  4705. ReleaseOperand(operand);
  4706. IF Trace THEN TraceExit("PushParameter") END;
  4707. END PushParameter;
  4708. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4709. VAR prevConditional: BOOLEAN;
  4710. BEGIN
  4711. prevConditional := conditional;
  4712. conditional := FALSE;
  4713. 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
  4714. Expression(x.result); (* use register *)
  4715. END;
  4716. Statement(x.statement);
  4717. conditional := prevConditional;
  4718. IF x.result # NIL THEN Expression(x.result) END;
  4719. 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
  4720. ReleaseIntermediateOperand(result.op);
  4721. END;
  4722. END VisitStatementDesignator;
  4723. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4724. VAR
  4725. procedure: SyntaxTree.Procedure;
  4726. procedureType: SyntaxTree.ProcedureType;
  4727. wasInline: BOOLEAN;
  4728. actualParameters: SyntaxTree.ExpressionList;
  4729. formalParameter: SyntaxTree.Parameter;
  4730. actualParameter: SyntaxTree.Expression;
  4731. i: LONGINT;
  4732. localVariable: SyntaxTree.Variable;
  4733. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4734. src, dest: Operand;
  4735. prevInlineExit : Label;
  4736. prevMapper: SymbolMapper;
  4737. tooComplex: BOOLEAN;
  4738. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4739. BEGIN
  4740. IF e = NIL THEN RETURN TRUE
  4741. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4742. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4743. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4744. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4745. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4746. ELSE RETURN FALSE
  4747. END;
  4748. END SimpleExpression;
  4749. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4750. BEGIN
  4751. RETURN checker.CanPassInRegister(type)
  4752. END FitsInRegister;
  4753. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4754. VAR
  4755. variable: SyntaxTree.Variable;
  4756. variableDesignator: SyntaxTree.Designator;
  4757. BEGIN
  4758. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4759. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4760. variableDesignator.SetType(type);
  4761. RETURN variableDesignator
  4762. END GetTemp;
  4763. BEGIN
  4764. wasInline := currentIsInline;
  4765. prevInlineExit := currentInlineExit;
  4766. prevMapper := currentMapper;
  4767. currentInlineExit := NewLabel();
  4768. tooComplex := FALSE;
  4769. NEW(currentMapper);
  4770. currentIsInline := TRUE;
  4771. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4772. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4773. formalParameter := procedureType.firstParameter;
  4774. actualParameters := x.parameters;
  4775. i := 0;
  4776. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4777. actualParameter := actualParameters.GetExpression(i);
  4778. IF actualParameter.resolved # NIL THEN
  4779. actualParameter := actualParameter.resolved
  4780. END;
  4781. (*
  4782. if expression is simple and can be passed immediately
  4783. or if type fits in register then we can proceed
  4784. otherwise we escape to ordinary procedure call.
  4785. *)
  4786. (* cases where the expression can be mapped identically *)
  4787. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4788. currentMapper.Add(formalParameter, actualParameter, NIL);
  4789. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4790. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4791. (*
  4792. Assign(variableDesignator, actualParameter);
  4793. *)
  4794. Evaluate(actualParameter, src);
  4795. Designate(variableDesignator, dest);
  4796. Emit(Mov(x.position, dest.op, src.op));
  4797. ReleaseOperand(dest);
  4798. ReleaseOperand(src);
  4799. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4800. ELSE tooComplex := TRUE
  4801. END;
  4802. (*
  4803. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4804. currentMapper.Add(formalParameter, actualParameter, NIL);
  4805. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4806. variableDesignator := GetTemp(system.addressType, FALSE);
  4807. Designate(actualParameter, src);
  4808. Designate(variableDesignator, dest);
  4809. IntermediateCode.MakeMemory(dest.op,addressType);
  4810. Emit(Mov(x.position, dest.op, src.op));
  4811. ReleaseOperand(dest);
  4812. IF src.tag.mode # IntermediateCode.Undefined THEN
  4813. tagDesignator := GetTemp(system.addressType, FALSE);
  4814. Designate(tagDesignator, dest);
  4815. IntermediateCode.MakeMemory(dest.op,addressType);
  4816. Emit(Mov(x.position, dest.op, src.op));
  4817. END;
  4818. ReleaseOperand(dest); ReleaseOperand(src);
  4819. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4820. END;
  4821. *)
  4822. formalParameter := formalParameter.nextParameter;
  4823. INC(i);
  4824. END;
  4825. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4826. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4827. currentMapper.Add(NIL, returnDesignator, NIL);
  4828. END;
  4829. localVariable := procedure.procedureScope.firstVariable;
  4830. WHILE ~tooComplex & (localVariable # NIL) DO
  4831. variableDesignator := GetTemp(localVariable.type, FALSE);
  4832. currentMapper.Add(localVariable, variableDesignator, NIL);
  4833. localVariable := localVariable.nextVariable;
  4834. END;
  4835. IF ~tooComplex THEN
  4836. VisitStatementBlock(procedure.procedureScope.body);
  4837. SetLabel(currentInlineExit);
  4838. IF procedureType.returnType # NIL THEN
  4839. Designate(returnDesignator, result);
  4840. IF conditional THEN
  4841. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4842. ValueToCondition(result)
  4843. END;
  4844. END;
  4845. END;
  4846. currentMapper := prevMapper;
  4847. currentInlineExit := prevInlineExit;
  4848. currentIsInline := wasInline;
  4849. RETURN ~tooComplex
  4850. END InlineProcedureCall;
  4851. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4852. VAR
  4853. parameters: SyntaxTree.ExpressionList;
  4854. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4855. designator: SyntaxTree.Designator;
  4856. procedureType: SyntaxTree.ProcedureType;
  4857. formalParameter: SyntaxTree.Parameter;
  4858. operand, returnValue: Operand;
  4859. reg, size, mask, dest: IntermediateCode.Operand;
  4860. saved: RegisterEntry;
  4861. symbol: SyntaxTree.Symbol;
  4862. variable: SyntaxTree.Variable;
  4863. i, parametersSize, returnTypeSize : LONGINT;
  4864. structuredReturnType: BOOLEAN;
  4865. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4866. tempVariableDesignator: SyntaxTree.Designator;
  4867. gap, alignment: LONGINT; (*fld*)
  4868. (* TODO: remove unnecessary backup variables *)
  4869. oldResult: Operand;
  4870. oldCurrentScope: SyntaxTree.Scope;
  4871. oldArrayDestinationTag: IntermediateCode.Operand;
  4872. oldArrayDestinationDimension: LONGINT;
  4873. oldConstantDeclaration: SyntaxTree.Symbol;
  4874. oldDestination: IntermediateCode.Operand;
  4875. oldCurrentLoop: Label;
  4876. oldConditional: BOOLEAN;
  4877. oldTrueLabel, oldFalseLabel: Label;
  4878. oldLocked: BOOLEAN;
  4879. usedRegisters,oldUsedRegisters: RegisterEntry;
  4880. return: IntermediateCode.Operand;
  4881. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4882. arg: IntermediateCode.Operand;
  4883. dummy: IntermediateCode.Operand;
  4884. recordType: SyntaxTree.RecordType;
  4885. operatorSelectionProcedureOperand: Operand;
  4886. operatorSelectionProcedure: SyntaxTree.Procedure;
  4887. fingerPrint: SyntaxTree.FingerPrint;
  4888. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4889. identifierNumber: LONGINT;
  4890. parameterRegister: Backend.Registers;
  4891. parameterRegisters: LONGINT;
  4892. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4893. procedure: SyntaxTree.Procedure;
  4894. PROCEDURE BackupGlobalState;
  4895. BEGIN
  4896. oldResult := result;
  4897. oldCurrentScope := currentScope;
  4898. oldArrayDestinationTag := arrayDestinationTag;
  4899. oldArrayDestinationDimension := arrayDestinationDimension;
  4900. oldConstantDeclaration := constantDeclaration;
  4901. oldDestination := destination;
  4902. oldCurrentLoop := currentLoop;
  4903. oldConditional := conditional;
  4904. oldTrueLabel := trueLabel;
  4905. oldFalseLabel := falseLabel;
  4906. oldLocked := locked;
  4907. oldUsedRegisters := usedRegisters
  4908. END BackupGlobalState;
  4909. PROCEDURE RestoreGlobalState;
  4910. BEGIN
  4911. result := oldResult;
  4912. currentScope := oldCurrentScope;
  4913. arrayDestinationTag := oldArrayDestinationTag;
  4914. arrayDestinationDimension := oldArrayDestinationDimension;
  4915. constantDeclaration := oldConstantDeclaration;
  4916. destination := oldDestination;
  4917. currentLoop := oldCurrentLoop;
  4918. conditional := oldConditional;
  4919. trueLabel := oldTrueLabel;
  4920. falseLabel := oldFalseLabel;
  4921. locked := oldLocked;
  4922. usedRegisters := oldUsedRegisters
  4923. END RestoreGlobalState;
  4924. (** do preparations before parameter push for array-structured object types (ASOTs):
  4925. if ASOT is passed as VAR parameter:
  4926. - allocate temporary variable of math array type
  4927. - copy contents of ASOT to be passed to temporary variable
  4928. - use temporary variable as the actual parameter instead
  4929. - 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)
  4930. **)
  4931. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4932. VAR
  4933. expression: SyntaxTree.Expression;
  4934. BEGIN
  4935. IF actualParameter IS SyntaxTree.Designator THEN
  4936. designator := actualParameter(SyntaxTree.Designator);
  4937. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4938. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4939. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4940. ASSERT(checker # NIL);
  4941. checker.SetCurrentScope(currentScope);
  4942. (* allocate temporary variable *)
  4943. ASSERT(actualParameter.type # NIL);
  4944. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4945. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4946. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4947. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4948. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4949. ASSERT(tempVariableDesignator.type # NIL);
  4950. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4951. (* copy math array stored in actual parameter to temporary variable *)
  4952. BackupGlobalState;
  4953. AssignMathArray(tempVariableDesignator, actualParameter);
  4954. RestoreGlobalState;
  4955. (* use temporary variable as actual parameter instead of the original one *)
  4956. actualParameter := tempVariableDesignator;
  4957. (* create write-back call and store it in linked list *)
  4958. (* create new list entry *)
  4959. IF firstWriteBackCall = NIL THEN
  4960. NEW(firstWriteBackCall);
  4961. currentWriteBackCall := firstWriteBackCall
  4962. ELSE
  4963. ASSERT(currentWriteBackCall # NIL);
  4964. NEW(currentWriteBackCall.next);
  4965. currentWriteBackCall := currentWriteBackCall.next
  4966. END;
  4967. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4968. ASSERT(expression.type = NIL);
  4969. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4970. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4971. (* prepare writeback for any other "normal" indexer *)
  4972. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4973. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4974. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4975. Assign(tempVariableDesignator, actualParameter);
  4976. actualParameter := tempVariableDesignator;
  4977. IF firstWriteBackCall = NIL THEN
  4978. NEW(firstWriteBackCall);
  4979. currentWriteBackCall := firstWriteBackCall
  4980. ELSE
  4981. ASSERT(currentWriteBackCall # NIL);
  4982. NEW(currentWriteBackCall.next);
  4983. currentWriteBackCall := currentWriteBackCall.next
  4984. END;
  4985. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  4986. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4987. END
  4988. END
  4989. END PrepareParameter;
  4990. BEGIN
  4991. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4992. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4993. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4994. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4995. IF InlineProcedureCall(x) THEN
  4996. RETURN
  4997. ELSE
  4998. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4999. END
  5000. END;
  5001. END;
  5002. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5003. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5004. dest := destination; destination := emptyOperand;
  5005. SaveRegisters();ReleaseUsedRegisters(saved);
  5006. parameters := x.parameters;
  5007. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5008. (* an operator is called *)
  5009. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5010. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5011. (* check if a dynamic operator call should be performed *)
  5012. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5013. ELSE
  5014. isCallOfDynamicOperator := FALSE
  5015. END;
  5016. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5017. Emit(Push(position, ap));
  5018. END;
  5019. alignment := procedureType.stackAlignment;
  5020. IF alignment > 1 THEN
  5021. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5022. Emit(Mov(position,reg, sp));
  5023. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5024. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5025. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5026. Emit(And(position,sp, sp, mask));
  5027. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5028. Emit(Push(position,reg));
  5029. (*
  5030. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5031. Emit(Mov(position,mem,reg));
  5032. *)
  5033. ReleaseIntermediateOperand(reg);
  5034. END;
  5035. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5036. (* align stack to 16-byte boundary *)
  5037. IntermediateCode.InitImmediate(mask,addressType,-16);
  5038. Emit(And(position,sp, sp, mask));
  5039. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5040. IF gap # 0 THEN
  5041. IntermediateCode.InitImmediate(size,addressType,gap);
  5042. Emit(Sub(position,sp,sp,size))
  5043. END;
  5044. END;
  5045. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5046. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5047. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5048. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5049. ELSE
  5050. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5051. END;
  5052. Evaluate(x.left, operand);
  5053. IF symbol IS SyntaxTree.Procedure THEN
  5054. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5055. Emit(Push(position,operand.tag));
  5056. ELSIF (procedureType.isDelegate) THEN
  5057. Emit(Push(position,operand.tag));
  5058. END;
  5059. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5060. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5061. Emit(Push(position,operand.tag));
  5062. END;
  5063. ELSE HALT(200);
  5064. END;
  5065. ReleaseIntermediateOperand(operand.tag);
  5066. operand.tag := emptyOperand;
  5067. (* determine if a structured return type is needed *)
  5068. structuredReturnType := StructuredReturnType(procedureType);
  5069. IF structuredReturnType THEN
  5070. IF resultDesignator # NIL THEN
  5071. d := resultDesignator;
  5072. ELSE
  5073. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5074. variable.SetUntraced(procedureType.hasUntracedReturn);
  5075. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5076. d.SetType(variable.type);
  5077. END;
  5078. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5079. Designate(d,returnValue);
  5080. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5081. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5082. Emit(Push(position,size));
  5083. Emit(Push(position,returnValue.op));
  5084. ReleaseOperand(returnValue);
  5085. ELSE
  5086. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5087. END;
  5088. END;
  5089. firstWriteBackCall := NIL; (* reset write-back call list *)
  5090. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5091. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5092. IF parameterRegister = NIL THEN parameterRegisters := 0
  5093. ELSE parameterRegisters := LEN(parameterRegister)
  5094. END;
  5095. passByRegister := parameterRegisters > 0;
  5096. registerNumber := 0;
  5097. formalParameter := procedureType.lastParameter;
  5098. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5099. actualParameter := parameters.GetExpression(i);
  5100. PrepareParameter(actualParameter, formalParameter);
  5101. IF passByRegister & (i < parameterRegisters) THEN
  5102. IF ~PassInRegister(formalParameter) THEN
  5103. Error(actualParameter.position,"cannot be passed by register")
  5104. ELSE
  5105. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5106. END;
  5107. INC(registerNumber);
  5108. ELSE
  5109. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5110. END;
  5111. formalParameter := formalParameter.prevParameter;
  5112. END;
  5113. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5114. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5115. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5116. END;
  5117. ELSE
  5118. hasDynamicOperands := FALSE;
  5119. formalParameter := procedureType.firstParameter;
  5120. FOR i := 0 TO parameters.Length() - 1 DO
  5121. actualParameter := parameters.GetExpression(i);
  5122. IF formalParameter # NIL THEN (* TENTATIVE *)
  5123. PrepareParameter(actualParameter, formalParameter);
  5124. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5125. ASSERT(i < 2);
  5126. hasDynamicOperands := TRUE;
  5127. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5128. ELSE
  5129. IF passByRegister & (registerNumber > 0) THEN
  5130. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5131. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5132. END;
  5133. passByRegister := FALSE;
  5134. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5135. END;
  5136. formalParameter := formalParameter.nextParameter;
  5137. END;
  5138. END;
  5139. END;
  5140. IF symbol IS SyntaxTree.Procedure THEN
  5141. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5142. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5143. Emit(Push(position,reg));
  5144. ReleaseIntermediateOperand(reg);
  5145. END;
  5146. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5147. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5148. parametersSize := ParametersSize(system,procedureType,FALSE);
  5149. END;
  5150. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5151. (*
  5152. dynamic operator overloading:
  5153. first push parameters, regularly:
  5154. [self]
  5155. par1
  5156. par2
  5157. then push parameters for GetOperator
  5158. identifier
  5159. ptr1
  5160. tag
  5161. ptr2
  5162. tag
  5163. call GetOperatorRuntimeProc
  5164. call Operator
  5165. *)
  5166. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5167. (* push ID *)
  5168. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5169. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5170. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5171. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5172. formalParameter := procedureType.firstParameter;
  5173. FOR i := 0 TO parameters.Length() - 1 DO
  5174. IF formalParameter.access # SyntaxTree.Hidden THEN
  5175. ASSERT(i < 2);
  5176. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5177. (* push pointer *)
  5178. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5179. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5180. (* add dereference *)
  5181. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5182. (*! better: do refer to stack above than using parameter backups !!*)
  5183. ReleaseIntermediateOperand(parameterBackups[i]);
  5184. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5185. END;
  5186. Emit(Push(position,parameterBackups[i]));
  5187. ReleaseIntermediateOperand(parameterBackups[i]);
  5188. (* push typetag *)
  5189. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5190. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5191. arg := TypeDescriptorAdr(recordType);
  5192. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5193. Emit(Push(position,arg));
  5194. ELSE
  5195. (* push 'NonPointer' *)
  5196. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5197. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5198. (* push fingerprint *)
  5199. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5200. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5201. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5202. END
  5203. END;
  5204. formalParameter := formalParameter.nextParameter
  5205. END;
  5206. (* for unary operators: complete the information for the second parameter *)
  5207. IF procedureType.numberParameters < 2 THEN
  5208. (* push 'NonPointer' *)
  5209. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5210. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5211. (* push 'NoType' *)
  5212. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5213. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5214. END;
  5215. (* call operator selection procedure *)
  5216. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5217. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5218. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5219. ReleaseOperand(operatorSelectionProcedureOperand);
  5220. (* use the address that the operator selection procedure returned as the target address of the call *)
  5221. InitOperand(operand, ModeValue);
  5222. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5223. Emit(Result(position,operand.op))
  5224. END
  5225. END;
  5226. ReleaseParameterRegisters();
  5227. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5228. Emit(Call(position,operand.op,0));
  5229. ELSE
  5230. Emit(Call(position,operand.op,parametersSize));
  5231. END;
  5232. ReleaseOperand(operand);
  5233. IF procedureType.noReturn THEN
  5234. EmitTrap(position,NoReturnTrap);
  5235. END;
  5236. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5237. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5238. Emit(Result(position,return));
  5239. END;
  5240. (* === return parameter space === *)
  5241. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5242. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5243. IF parametersSize < 32 THEN
  5244. (* allocated space for all parameter registers *)
  5245. parametersSize := 32
  5246. END;
  5247. size := IntermediateCode.Immediate(addressType,parametersSize);
  5248. Emit(Add(position,sp,sp,size))
  5249. END;
  5250. IF SysvABI(procedureType.callingConvention) THEN
  5251. IF passByRegister THEN
  5252. IF parameters.Length() > parameterRegisters THEN
  5253. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5254. ELSE
  5255. parametersSize := 0
  5256. END;
  5257. ELSE
  5258. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5259. INC( parametersSize, (-parametersSize) MOD 16 )
  5260. END;
  5261. IF parametersSize > 0 THEN
  5262. size := IntermediateCode.Immediate(addressType,parametersSize);
  5263. Emit(Add(position,sp,sp,size))
  5264. END;
  5265. END;
  5266. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5267. IF structuredReturnType THEN
  5268. (* stack pointer rewinding done by callee
  5269. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5270. Emit(Add(position,sp,sp,size));
  5271. *)
  5272. RestoreRegisters(saved);
  5273. InitOperand(result,ModeReference);
  5274. Symbol(variable,result);
  5275. ELSE
  5276. RestoreRegisters(saved);
  5277. InitOperand(result,ModeValue);
  5278. result.op := return;
  5279. END;
  5280. END;
  5281. IF alignment > 1 THEN
  5282. Emit(Pop(position,sp));
  5283. END;
  5284. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5285. Emit(Pop(position, ap));
  5286. END;
  5287. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5288. ValueToCondition(result);
  5289. END;
  5290. destination := dest;
  5291. (* perform all write-back calls in the list *)
  5292. BackupGlobalState;
  5293. currentWriteBackCall := firstWriteBackCall;
  5294. WHILE currentWriteBackCall # NIL DO
  5295. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5296. currentWriteBackCall := currentWriteBackCall.next
  5297. END;
  5298. RestoreGlobalState;
  5299. (* TENATIVE *)
  5300. (*
  5301. IF isOperatorCall THEN
  5302. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5303. END;
  5304. *)
  5305. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5306. END VisitProcedureCallDesignator;
  5307. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5308. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5309. td: SyntaxTree.Symbol;
  5310. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5311. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5312. BEGIN
  5313. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5314. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5315. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5316. variable.SetType(system.anyType);
  5317. scope.AddVariable(variable);
  5318. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5319. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5320. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5321. typeDeclaration.SetScope(module.module.moduleScope);
  5322. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5323. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5324. END;
  5325. RETURN hiddenPointerType;
  5326. END GetHiddenPointerType;
  5327. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5328. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5329. BEGIN
  5330. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5331. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5332. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5333. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5334. scope.AddVariable(variable);
  5335. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5336. variable.SetType(system.anyType);
  5337. scope.AddVariable(variable);
  5338. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5339. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5340. typeDeclaration.SetDeclaredType(delegatePointerType);
  5341. typeDeclaration.SetScope(module.module.moduleScope);
  5342. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5343. delegatePointerType.SetState(SyntaxTree.Resolved);
  5344. END;
  5345. RETURN delegatePointerType
  5346. END GetDelegateType;
  5347. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5348. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5349. such as in VAR a: RECORD ... END;
  5350. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5351. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5352. *)
  5353. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5354. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5355. BEGIN (* no code emission *)
  5356. source := NIL;
  5357. x := x.resolved;
  5358. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5359. x := GetHiddenPointerType();
  5360. ELSIF IsDelegate(x) THEN
  5361. x := GetDelegateType();
  5362. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5363. ELSE HALT(200);
  5364. END;
  5365. td := x.typeDeclaration;
  5366. IF td = NIL THEN
  5367. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5368. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5369. ASSERT(td # NIL);
  5370. END;
  5371. IF newObjectFile THEN
  5372. GetCodeSectionNameForSymbol(td,name);
  5373. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5374. meta.CheckTypeDeclaration(x);
  5375. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5376. ELSE
  5377. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5378. END;
  5379. IF backend.cooperative OR meta.simple THEN
  5380. offset := 0;
  5381. ELSE
  5382. IF x IS SyntaxTree.CellType THEN
  5383. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5384. IF baseRecord = NIL THEN
  5385. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5386. ELSE
  5387. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5388. END;
  5389. ELSE
  5390. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5391. END;
  5392. END;
  5393. ELSE
  5394. offset := 0;
  5395. source := module.allSections.FindBySymbol(td); (*TODO*)
  5396. IF source = NIL THEN
  5397. null := 0;
  5398. GetCodeSectionNameForSymbol(td,name);
  5399. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5400. Basic.SuffixSegmentedName (name, module.module.name);
  5401. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5402. IntermediateCode.InitImmediate(op,addressType,0);
  5403. source(IntermediateCode.Section).Emit(Data(position,op));
  5404. source.SetReferenced(FALSE)
  5405. ELSE
  5406. name := source.name;
  5407. END;
  5408. END;
  5409. RETURN td
  5410. END GetBackendType;
  5411. BEGIN
  5412. (*td := t.typeDeclaration;*)
  5413. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5414. (*
  5415. IF t IS SyntaxTree.PointerType THEN
  5416. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5417. ELSE
  5418. source := GetBackendType(t);
  5419. END;
  5420. *)
  5421. IF newObjectFile THEN
  5422. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5423. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5424. IntermediateCode.SetOffset(res,offset);
  5425. ELSE
  5426. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5427. END;
  5428. (*
  5429. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5430. make memory should be used when tag is used, not earlier
  5431. *)
  5432. RETURN res
  5433. END TypeDescriptorAdr;
  5434. (*
  5435. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5436. VAR result: IntermediateCode.Operand;
  5437. BEGIN
  5438. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5439. operand.tag := TypeDescriptorAdr(operand.type);
  5440. IntermediateCode.MakeMemory(operand.tag,addressType);
  5441. UseIntermediateOperand(operand.tag);
  5442. END;
  5443. END MakeTypeTag;
  5444. *)
  5445. PROCEDURE ProfilerInit;
  5446. VAR reg: IntermediateCode.Operand;
  5447. BEGIN
  5448. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5449. Emit(Call(position,reg,0));
  5450. END ProfilerInit;
  5451. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5452. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5453. BEGIN
  5454. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5455. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5456. THEN
  5457. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5458. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5459. Emit(Push(position,reg));
  5460. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5461. Emit(Push(position,reg));
  5462. StaticCallOperand(result,procedure);
  5463. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5464. ReleaseOperand(result);
  5465. END;
  5466. END ProfilerEnterExit;
  5467. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5468. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5469. BEGIN
  5470. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5471. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5472. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5473. profileInit.Emit(Push(position,reg));
  5474. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5475. profileInit.Emit(Push(position,reg));
  5476. NEW(string, LEN(name)); COPY(name, string^);
  5477. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5478. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5479. sv.SetType(type);
  5480. Designate(sv,result);
  5481. profileInit.Emit(Push(position,result.tag));
  5482. profileInit.Emit(Push(position,result.op));
  5483. StaticCallOperand(result,procedure);
  5484. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5485. ReleaseOperand(result);
  5486. END;
  5487. END ProfilerAddProcedure;
  5488. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5489. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5490. BEGIN
  5491. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5492. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5493. profileInit.Emit(Push(position,reg));
  5494. profileInitPatchPosition := profileInit.pc;
  5495. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5496. NEW(string, LEN(name)); COPY(name, string^);
  5497. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5498. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5499. sv.SetType(type);
  5500. Designate(sv,result);
  5501. profileInit.Emit(Push(position,result.tag));
  5502. profileInit.Emit(Push(position,result.op));
  5503. StaticCallOperand(result,procedure);
  5504. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5505. ReleaseOperand(result);
  5506. END;
  5507. END ProfilerAddModule;
  5508. PROCEDURE ProfilerPatchInit;
  5509. VAR reg: IntermediateCode.Operand;
  5510. BEGIN
  5511. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5512. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5513. EmitLeave(profileInit,position,NIL,0);
  5514. profileInit.Emit(Exit(position,0,0,0));
  5515. END ProfilerPatchInit;
  5516. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5517. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5518. VAR
  5519. id: LONGINT;
  5520. leftType, rightType: SyntaxTree.Type;
  5521. procedureType: SyntaxTree.ProcedureType;
  5522. runtimeProcedure: SyntaxTree.Procedure;
  5523. runtimeProcedureOperand, operatorOperand: Operand;
  5524. kind: SET;
  5525. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5526. VAR
  5527. arg: IntermediateCode.Operand;
  5528. recordType: SyntaxTree.RecordType;
  5529. fingerPrint: SyntaxTree.FingerPrint;
  5530. BEGIN
  5531. IF type = NIL THEN
  5532. (* no type: push 'NoType' *)
  5533. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5534. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5535. ELSIF IsStrictlyPointerToRecord(type) THEN
  5536. (* pointer to record type: push typetag *)
  5537. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5538. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5539. arg := TypeDescriptorAdr(recordType);
  5540. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5541. ELSE
  5542. (* non-pointer to record type: push fingerprint *)
  5543. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5544. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5545. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5546. END;
  5547. operatorInitializationCodeSection.Emit(Push(position,arg))
  5548. END PushTypeInfo;
  5549. BEGIN
  5550. ASSERT(operatorInitializationCodeSection # NIL);
  5551. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5552. procedureType := operator.type(SyntaxTree.ProcedureType);
  5553. (* determine types *)
  5554. leftType := procedureType.firstParameter.type;
  5555. IF procedureType.numberParameters = 2 THEN
  5556. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5557. rightType := procedureType.firstParameter.nextParameter.type;
  5558. ELSE
  5559. rightType := NIL
  5560. END;
  5561. (* determine operator kind *)
  5562. IF IsStrictlyPointerToRecord(leftType) THEN
  5563. kind := {LhsIsPointer}
  5564. ELSE
  5565. kind := {}
  5566. END;
  5567. IF IsStrictlyPointerToRecord(rightType) THEN
  5568. kind := kind + {RhsIsPointer}
  5569. END;
  5570. IF kind # {} THEN (* TODO: to be removed later on *)
  5571. (* at least one of the types is a pointer to record *)
  5572. (* emit a code that registers this specific operator in the runtime *)
  5573. dump := operatorInitializationCodeSection.comments;
  5574. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5575. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5576. (* push ID *)
  5577. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5578. id := Global.GetSymbol(module.module.case, operator.name);
  5579. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5580. (* push kind *)
  5581. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5582. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5583. (* push type infos *)
  5584. PushTypeInfo(leftType);
  5585. PushTypeInfo(rightType);
  5586. (* push operator address *)
  5587. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5588. StaticCallOperand(operatorOperand, operator);
  5589. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5590. ReleaseOperand(operatorOperand);
  5591. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5592. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5593. ReleaseOperand(runtimeProcedureOperand)
  5594. END
  5595. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5596. END
  5597. END RegisterDynamicOperator;
  5598. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5599. VAR
  5600. traceModule: SyntaxTree.Module;
  5601. procedure: SyntaxTree.Procedure;
  5602. procedureVariable: SyntaxTree.Variable;
  5603. s,msg: Basic.MessageString;
  5604. res: Operand;
  5605. i: LONGINT;
  5606. sv: SyntaxTree.StringValue;
  5607. type: SyntaxTree.Type;
  5608. recordType: SyntaxTree.RecordType;
  5609. printout: Printout.Printer;
  5610. stringWriter: Streams.StringWriter;
  5611. expression: SyntaxTree.Expression;
  5612. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5613. BEGIN
  5614. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5615. IF procedure = NIL THEN
  5616. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5617. END;
  5618. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5619. s := "procedure ";
  5620. Strings.Append(s,backend.traceModuleName);
  5621. Strings.Append(s,".");
  5622. Strings.Append(s,procedureName);
  5623. Strings.Append(s," not present");
  5624. Error(position,s);
  5625. RETURN FALSE
  5626. ELSE
  5627. RETURN TRUE
  5628. END;
  5629. END GetProcedure;
  5630. PROCEDURE CallProcedure;
  5631. VAR size: LONGINT;
  5632. BEGIN
  5633. IF procedure # NIL THEN
  5634. StaticCallOperand(result,procedure);
  5635. size := ProcedureParametersSize(system,procedure);
  5636. ELSE
  5637. Symbol(procedureVariable, result);
  5638. LoadValue(result, procedureVariable.type.resolved);
  5639. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5640. END;
  5641. Emit(Call(position,result.op,size));
  5642. END CallProcedure;
  5643. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5644. VAR res: Operand; string: SyntaxTree.String;
  5645. BEGIN
  5646. IF GetProcedure("String") THEN
  5647. NEW(string, LEN(s)); COPY(s, string^);
  5648. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5649. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5650. sv.SetType(type);
  5651. Designate(sv,res);
  5652. Emit(Push(position,res.tag));
  5653. Emit(Push(position,res.op));
  5654. ReleaseOperand(res);
  5655. CallProcedure;
  5656. END;
  5657. END String;
  5658. PROCEDURE Integer(op: IntermediateCode.Operand);
  5659. BEGIN
  5660. IF GetProcedure("Int") THEN
  5661. Emit(Push(position,op));
  5662. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5663. CallProcedure;
  5664. END;
  5665. END Integer;
  5666. PROCEDURE Float(op: IntermediateCode.Operand);
  5667. BEGIN
  5668. IF GetProcedure("HIntHex") THEN
  5669. Emit(Push(position,op));
  5670. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5671. CallProcedure;
  5672. END;
  5673. END Float;
  5674. PROCEDURE Set(op: IntermediateCode.Operand);
  5675. BEGIN
  5676. IF GetProcedure("Set") THEN
  5677. Emit(Push(position,op));
  5678. (*
  5679. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5680. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5681. *)
  5682. CallProcedure;
  5683. END;
  5684. END Set;
  5685. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5686. BEGIN
  5687. IF GetProcedure("Boolean") THEN
  5688. Emit(Push(position,op));
  5689. CallProcedure;
  5690. END;
  5691. END Boolean;
  5692. PROCEDURE Char(op: IntermediateCode.Operand);
  5693. BEGIN
  5694. IF GetProcedure("Char") THEN
  5695. Emit(Push(position,op));
  5696. CallProcedure;
  5697. END;
  5698. END Char;
  5699. PROCEDURE Address(op: IntermediateCode.Operand);
  5700. BEGIN
  5701. IF GetProcedure("Address") THEN
  5702. Emit(Push(position,op));
  5703. CallProcedure;
  5704. END;
  5705. END Address;
  5706. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5707. VAR len: IntermediateCode.Operand;
  5708. BEGIN
  5709. IF GetProcedure("String") THEN
  5710. len := GetArrayLength(type, op.tag);
  5711. Emit(Push(position,len));
  5712. ReleaseIntermediateOperand(len);
  5713. Emit(Push(position,op.op));
  5714. CallProcedure;
  5715. END;
  5716. END StringOperand;
  5717. PROCEDURE Ln;
  5718. BEGIN
  5719. IF GetProcedure("Ln") THEN
  5720. CallProcedure;
  5721. END;
  5722. END Ln;
  5723. BEGIN
  5724. IF backend.traceModuleName = "" THEN RETURN END;
  5725. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5726. IF GetProcedure("Enter") THEN
  5727. CallProcedure
  5728. END;
  5729. NEW(stringWriter,LEN(s));
  5730. FOR i := 0 TO x.Length()-1 DO
  5731. msg := "";
  5732. expression := x.GetExpression(i);
  5733. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5734. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5735. ELSE
  5736. Global.GetModuleName(module.module, s);
  5737. END;
  5738. IF i = 0 THEN
  5739. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5740. stringWriter.String(":");
  5741. END;
  5742. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5743. IF ~(expression IS SyntaxTree.StringValue) THEN
  5744. printout.Expression(expression);
  5745. stringWriter.Get(s);
  5746. Strings.Append(msg,s);
  5747. Strings.Append(msg,"= ");
  5748. ELSE stringWriter.Get(s); (* remove from string writer *)
  5749. Strings.Append(msg, s);
  5750. END;
  5751. String(msg);
  5752. IF SemanticChecker.IsStringType(expression.type) THEN
  5753. Designate(expression,res);
  5754. StringOperand(res, expression.type);
  5755. ELSE
  5756. Evaluate(expression,res);
  5757. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5758. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5759. Convert(res.op,int64);
  5760. END;
  5761. Integer(res.op);
  5762. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5763. Boolean(res.op);
  5764. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5765. Set(res.op);
  5766. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5767. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5768. Convert(res.op,float64);
  5769. END;
  5770. Float(res.op);
  5771. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5772. Char(res.op);
  5773. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5774. Address(res.op);String("H");
  5775. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5776. Address(res.op);String("H");
  5777. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5778. Address(res.op);String("H");
  5779. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5780. Address(res.op);String("H");
  5781. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5782. String("NIL");
  5783. ELSE HALT(200);
  5784. END;
  5785. END;
  5786. ReleaseOperand(res);
  5787. String("; ");
  5788. END;
  5789. IF GetProcedure("Exit") THEN
  5790. CallProcedure
  5791. ELSE
  5792. Ln;
  5793. END;
  5794. END;
  5795. END SystemTrace;
  5796. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5797. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5798. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5799. BEGIN
  5800. type := type.resolved;
  5801. IF type IS SyntaxTree.RecordType THEN
  5802. WITH type: SyntaxTree.RecordType DO
  5803. baseType := type.baseType;
  5804. IF baseType # NIL THEN
  5805. baseType := baseType.resolved;
  5806. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5807. InitFields(baseType,adr,offset);
  5808. END;
  5809. variable := type.recordScope.firstVariable;
  5810. WHILE variable # NIL DO
  5811. IF variable.initializer # NIL THEN
  5812. Evaluate(variable.initializer,initializerOp);
  5813. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5814. Emit(Mov(position,mem,initializerOp.op));
  5815. ReleaseOperand(initializerOp);
  5816. ReleaseIntermediateOperand(mem);
  5817. END;
  5818. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5819. variable := variable.nextVariable
  5820. END;
  5821. END;
  5822. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5823. WITH type: SyntaxTree.ArrayType DO
  5824. IF type.form = SyntaxTree.Static THEN
  5825. baseType := type.arrayBase;
  5826. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5827. FOR i := 0 TO type.staticLength-1 DO
  5828. InitFields(baseType,adr,offset+i*size);
  5829. END;
  5830. END;
  5831. END;
  5832. ELSIF type IS SyntaxTree.MathArrayType THEN
  5833. WITH type: SyntaxTree.MathArrayType DO
  5834. IF type.form = SyntaxTree.Open THEN
  5835. dim := DynamicDim(type);
  5836. imm := IntermediateCode.Immediate(addressType,dim);
  5837. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5838. baseType := SemanticChecker.ArrayBase(type,dim);
  5839. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5840. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5841. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5842. ReleaseIntermediateOperand(imm);
  5843. (* flags remain empty (=0) for open array *)
  5844. ELSIF type.form = SyntaxTree.Static THEN
  5845. baseType := type.arrayBase;
  5846. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5847. ASSERT(type.staticLength < 1024*1024*1024);
  5848. FOR i := 0 TO type.staticLength-1 DO
  5849. InitFields(baseType,adr,offset+i*size);
  5850. END;
  5851. END;
  5852. END;
  5853. END;
  5854. END InitFields;
  5855. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5856. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5857. BEGIN
  5858. type := variable.type.resolved;
  5859. IF (type IS SyntaxTree.MathArrayType) THEN
  5860. WITH type: SyntaxTree.MathArrayType DO
  5861. IF type.form = SyntaxTree.Open THEN
  5862. Symbol(variable,operand);
  5863. InitFields(type, operand.tag,0);
  5864. ELSIF type.form = SyntaxTree.Tensor THEN
  5865. Symbol(variable, operand);
  5866. MakeMemory(tmp,operand.op,addressType,0);
  5867. ReleaseOperand(operand);
  5868. Emit(Mov(position,tmp, nil ) );
  5869. ReleaseIntermediateOperand(tmp);
  5870. END;
  5871. END;
  5872. ELSE
  5873. Symbol(variable,operand);
  5874. IF variable.initializer # NIL THEN
  5875. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5876. reference.SetType(variable.type.resolved);
  5877. reference.SetAssignable(TRUE);
  5878. Assign(reference,variable.initializer);
  5879. END;
  5880. InitFields(type, operand.op,0);
  5881. ReleaseOperand(operand);
  5882. END;
  5883. END InitVariable;
  5884. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5885. VAR end: Label;
  5886. BEGIN
  5887. IF type.form = SyntaxTree.Tensor THEN
  5888. InitOperand(result,ModeValue);
  5889. ReuseCopy(result.op,base);
  5890. end := NewLabel();
  5891. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5892. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5893. SetLabel(end);
  5894. Convert(result.op,int32);
  5895. ELSE
  5896. InitOperand(result,ModeValue);
  5897. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5898. END
  5899. END MathArrayDim;
  5900. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5901. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5902. BEGIN
  5903. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5904. MakeMemory(mem,base,addressType,offset);
  5905. Emit(Mov(position,mem,value));
  5906. ReleaseIntermediateOperand(mem);
  5907. END PutMathArrayField;
  5908. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5909. VAR mem: IntermediateCode.Operand;
  5910. BEGIN
  5911. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5912. MakeMemory(mem,base,addressType,offset);
  5913. Emit(Mov(position,mem,value));
  5914. ReleaseIntermediateOperand(mem);
  5915. END PutMathArrayFieldOffset;
  5916. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5917. BEGIN
  5918. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5919. MakeMemory(value,base,addressType,offset);
  5920. END GetMathArrayField;
  5921. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5922. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5923. BEGIN
  5924. IF incr THEN
  5925. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5926. ELSE
  5927. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5928. END;
  5929. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5930. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5931. ELSE
  5932. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5933. Emit(Mov(position,reg,dim));
  5934. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5935. Emit(Add(position,reg,reg,base));
  5936. MakeMemory(mem, reg, addressType, offset);
  5937. ReleaseIntermediateOperand(reg);
  5938. Emit(Mov(position,mem,value));
  5939. ReleaseIntermediateOperand(mem);
  5940. END;
  5941. END PutMathArrayLenOrIncr;
  5942. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5943. BEGIN
  5944. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5945. END PutMathArrayLength;
  5946. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5947. BEGIN
  5948. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5949. END PutMathArrayIncrement;
  5950. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5951. BEGIN
  5952. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5953. END GetMathArrayIncrement;
  5954. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5955. BEGIN
  5956. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5957. END GetMathArrayLength;
  5958. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5959. VAR dimOp: IntermediateCode.Operand;
  5960. BEGIN
  5961. dimOp := IntermediateCode.Immediate(int32, dim);
  5962. GetMathArrayLength(type, operand, dimOp, check, result);
  5963. END GetMathArrayLengthAt;
  5964. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5965. VAR dimOp: IntermediateCode.Operand;
  5966. BEGIN
  5967. dimOp := IntermediateCode.Immediate(int32, dim);
  5968. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5969. END GetMathArrayIncrementAt;
  5970. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5971. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5972. offset: LONGINT;
  5973. BEGIN
  5974. IF increment THEN
  5975. offset := MathIncrOffset;
  5976. ELSE
  5977. offset := MathLenOffset;
  5978. END;
  5979. INC(offset,operand.dimOffset*2);
  5980. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5981. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5982. END;
  5983. (* static dimension *)
  5984. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5985. IF check & (type.form = SyntaxTree.Tensor) THEN
  5986. DimensionCheck(operand.tag,dim,BrltL);
  5987. END;
  5988. val := SHORT(dim.intValue);
  5989. IF type.form # SyntaxTree.Tensor THEN
  5990. t := SemanticChecker.ArrayBase(type,val);
  5991. type := t.resolved(SyntaxTree.MathArrayType);
  5992. IF type.form = SyntaxTree.Static THEN
  5993. IF increment THEN
  5994. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5995. ELSE
  5996. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5997. END;
  5998. InitOperand(result,ModeValue);
  5999. result.op := res;
  6000. RETURN;
  6001. END;
  6002. END;
  6003. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6004. MakeMemory(res,operand.tag,addressType,offset);
  6005. (*
  6006. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6007. *)
  6008. InitOperand(result,ModeValue);
  6009. result.op := res;
  6010. ELSE
  6011. Convert(dim,addressType);
  6012. IF check THEN
  6013. IF type.form = SyntaxTree.Tensor THEN
  6014. DimensionCheck(operand.tag,dim,BrltL);
  6015. ELSIF isUnchecked THEN (* do nothing *)
  6016. ELSE
  6017. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6018. END;
  6019. END;
  6020. end := NewLabel(); next := NIL;
  6021. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6022. Emit(Mov(position,res,dim));
  6023. Convert(res,int32);
  6024. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6025. WHILE t IS SyntaxTree.MathArrayType DO
  6026. type := t(SyntaxTree.MathArrayType);
  6027. IF type.form = SyntaxTree.Static THEN
  6028. imm := IntermediateCode.Immediate(int32,val);
  6029. next := NewLabel();
  6030. BrneL(next,imm,res);
  6031. IF increment THEN
  6032. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6033. ELSE
  6034. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6035. END;
  6036. Emit(MovReplace(position,res,imm));
  6037. BrL(end);
  6038. ELSE hasDynamicPart := TRUE;
  6039. END;
  6040. t := type.arrayBase.resolved;
  6041. val := val + 1;
  6042. IF next # NIL THEN SetLabel(next) END;
  6043. END;
  6044. IF hasDynamicPart THEN
  6045. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6046. Emit(Mov(position,res2,dim));
  6047. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6048. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6049. Emit(Add(position,res2,res2,imm));
  6050. Emit(Add(position,res2,res2,operand.tag));
  6051. IntermediateCode.MakeMemory(res2,int32);
  6052. Emit(MovReplace(position,res,res2));
  6053. ReleaseIntermediateOperand(res2);
  6054. END;
  6055. SetLabel(end);
  6056. Convert(res,int32);
  6057. InitOperand(result,ModeValue);
  6058. result.op := res;
  6059. END;
  6060. END MathArrayLenOrIncr;
  6061. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6062. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6063. offset: LONGINT;
  6064. BEGIN
  6065. offset := operand.dimOffset+DynamicDim(type)-1;
  6066. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6067. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6068. val := SHORT(dim.intValue);
  6069. t := SemanticChecker.ArrayBase(type,val);
  6070. type := t.resolved(SyntaxTree.ArrayType);
  6071. IF type.form = SyntaxTree.Static THEN
  6072. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6073. ELSE
  6074. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6075. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6076. END;
  6077. UseIntermediateOperand(res);
  6078. InitOperand(result,ModeValue);
  6079. result.op := res;
  6080. ELSE
  6081. Convert(dim,addressType);
  6082. IF ~isUnchecked THEN
  6083. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6084. END;
  6085. end := NewLabel(); next := NIL;
  6086. (* ReuseCopy(dim,res); *)
  6087. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6088. Emit(Mov(position,res,dim));
  6089. Convert(res,int32);
  6090. Convert(res,int32);
  6091. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6092. WHILE t IS SyntaxTree.ArrayType DO
  6093. type := t(SyntaxTree.ArrayType);
  6094. IF type.form = SyntaxTree.Static THEN
  6095. imm := IntermediateCode.Immediate(int32,val);
  6096. next := NewLabel();
  6097. BrneL(next,imm,res);
  6098. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6099. Emit(MovReplace(position,res,imm));
  6100. BrL(end);
  6101. ELSE hasDynamicPart := TRUE;
  6102. END;
  6103. t := type.arrayBase.resolved;
  6104. val := val + 1;
  6105. IF next # NIL THEN SetLabel(next) END;
  6106. END;
  6107. IF hasDynamicPart THEN
  6108. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6109. Convert(res2,addressType);
  6110. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6111. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6112. Emit(Sub(position,res2,imm,res2));
  6113. Emit(Add(position,res2,res2,operand.tag));
  6114. IntermediateCode.MakeMemory(res2,int32);
  6115. Emit(MovReplace(position,res,res2));
  6116. ReleaseIntermediateOperand(res2);
  6117. END;
  6118. SetLabel(end);
  6119. Convert(res,int32);
  6120. InitOperand(result,ModeValue);
  6121. result.op := res;
  6122. END;
  6123. END ArrayLen;
  6124. (**
  6125. create a temporary variable in current scope
  6126. **)
  6127. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6128. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6129. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6130. BEGIN
  6131. IF ~register THEN
  6132. variable := temporaries.GetFreeVariable(type, index);
  6133. ELSE
  6134. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6135. END;
  6136. scope := currentScope;
  6137. IF variable = NIL THEN
  6138. COPY("@hiddenIRVar",string);
  6139. Basic.AppendNumber(string,index);
  6140. name := SyntaxTree.NewIdentifier(string);
  6141. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6142. variable.SetType(type);
  6143. variable.SetAccess(SyntaxTree.Hidden);
  6144. IF ~register THEN
  6145. temporaries.AddVariable(variable);
  6146. IF scope.lastVariable # NIL THEN
  6147. offset := scope.lastVariable.offsetInBits;
  6148. ELSE
  6149. offset := 0;
  6150. END;
  6151. DEC(offset,system.SizeOf(variable.type));
  6152. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6153. variable(SyntaxTree.Variable).SetOffset(offset);
  6154. scope.AddVariable(variable(SyntaxTree.Variable));
  6155. scope.EnterSymbol(variable, duplicate);
  6156. ASSERT(~duplicate);
  6157. InitVariable(variable(SyntaxTree.Variable));
  6158. ELSE
  6159. variable.SetUseRegister(TRUE);
  6160. variable(SyntaxTree.Variable).SetOffset(0);
  6161. END;
  6162. ELSE
  6163. InitVariable(variable(SyntaxTree.Variable));
  6164. (*
  6165. ASSERT(variable.type.resolved = type.resolved)
  6166. *)
  6167. END;
  6168. RETURN variable(SyntaxTree.Variable)
  6169. END GetTemporaryVariable;
  6170. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6171. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6172. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6173. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6174. i: LONGINT; duplicate: BOOLEAN;
  6175. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6176. VAR variable: SyntaxTree.Variable;
  6177. BEGIN
  6178. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6179. variable.SetType(type);
  6180. recordScope.AddVariable(variable);
  6181. END AddVariable;
  6182. BEGIN
  6183. name := "@ArrayDescriptor";
  6184. Basic.AppendNumber(name,dimensions);
  6185. identifier := SyntaxTree.NewIdentifier(name);
  6186. parentScope := module.module.moduleScope;
  6187. symbol := parentScope.FindSymbol(identifier);
  6188. IF symbol # NIL THEN
  6189. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6190. type := typeDeclaration.declaredType;
  6191. ELSE
  6192. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6193. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6194. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6195. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6196. recordType.SetTypeDeclaration(typeDeclaration);
  6197. recordType.SetState(SyntaxTree.Resolved);
  6198. typeDeclaration.SetDeclaredType(recordType);
  6199. AddVariable("@ptr",system.anyType);
  6200. AddVariable("@adr",system.addressType);
  6201. AddVariable("@flags",system.addressType);
  6202. AddVariable("@dim",system.addressType);
  6203. AddVariable("@elementSize",system.addressType);
  6204. FOR i := 0 TO dimensions-1 DO
  6205. name := "@len";
  6206. Basic.AppendNumber(name,i);
  6207. AddVariable(name,system.addressType);
  6208. name := "@incr";
  6209. Basic.AppendNumber(name,i);
  6210. AddVariable(name,system.addressType);
  6211. END;
  6212. parentScope.AddTypeDeclaration(typeDeclaration);
  6213. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6214. ASSERT(~duplicate);
  6215. type := recordType;
  6216. END;
  6217. RETURN type
  6218. END GetMathArrayDescriptorType;
  6219. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6220. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6221. BEGIN
  6222. type := GetMathArrayDescriptorType(dimensions);
  6223. Emit(Push(position,op.op));
  6224. (* push type descriptor *)
  6225. reg := TypeDescriptorAdr(type);
  6226. IF ~newObjectFile THEN
  6227. IntermediateCode.MakeMemory(reg,addressType);
  6228. END;
  6229. Emit(Push(position,reg));
  6230. ReleaseIntermediateOperand(reg);
  6231. (* push realtime flag: false by default *)
  6232. Emit(Push(position,false));
  6233. CallThis(position,"Heaps","NewRec",3);
  6234. END NewMathArrayDescriptor;
  6235. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6236. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6237. BEGIN
  6238. NEW(string, LEN(s)); COPY(s, string^);
  6239. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6240. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6241. sv.SetType(type);
  6242. Designate(sv,res);
  6243. Emit(Push(position,res.tag));
  6244. Emit(Push(position,res.op));
  6245. ReleaseOperand(res);
  6246. END PushConstString;
  6247. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6248. BEGIN
  6249. IF b THEN
  6250. Emit(Push(Basic.invalidPosition, true));
  6251. ELSE
  6252. Emit(Push(Basic.invalidPosition, false));
  6253. END;
  6254. END PushConstBoolean;
  6255. PROCEDURE PushConstSet(v: SET);
  6256. VAR value: SyntaxTree.Value; op: Operand;
  6257. BEGIN
  6258. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6259. value.SetType(system.setType);
  6260. Evaluate(value, op);
  6261. Emit(Push(Basic.invalidPosition, op.op));
  6262. ReleaseOperand(op);
  6263. END PushConstSet;
  6264. PROCEDURE PushConstInteger(v: LONGINT);
  6265. VAR value: SyntaxTree.Value; op: Operand;
  6266. BEGIN
  6267. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6268. value.SetType(system.longintType);
  6269. Evaluate(value, op);
  6270. Emit(Push(Basic.invalidPosition, op.op));
  6271. ReleaseOperand(op);
  6272. END PushConstInteger;
  6273. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6274. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6275. section: IntermediateCode.Section;
  6276. BEGIN
  6277. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6278. Global.GetSymbolSegmentedName(symbol, name);
  6279. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6280. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6281. procedure.SetScope(moduleScope);
  6282. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6283. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6284. procedure.SetAccess(SyntaxTree.Hidden);
  6285. currentScope := procedureScope;
  6286. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6287. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6288. RETURN section;
  6289. END OpenInitializer;
  6290. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6291. BEGIN
  6292. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6293. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6294. section := prev;
  6295. END CloseInitializer;
  6296. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6297. VAR name: SyntaxTree.IdentifierString;
  6298. variable: SyntaxTree.Variable;
  6299. parameter: SyntaxTree.Parameter;
  6300. type: SyntaxTree.Type;
  6301. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6302. BEGIN
  6303. InitOperand(op,ModeReference);
  6304. op.op := fp;
  6305. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6306. Dereference(op, x, FALSE);
  6307. result := op;
  6308. Symbol(symbol, op);
  6309. END Field;
  6310. PROCEDURE Direction(direction: LONGINT): SET;
  6311. BEGIN
  6312. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6313. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6314. ELSE HALT(100);
  6315. END;
  6316. END Direction;
  6317. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6318. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6319. BEGIN
  6320. Field(port, op);
  6321. ToMemory(op.op,addressType,0);
  6322. Emit(Push(Basic.invalidPosition, op.op));
  6323. ReleaseOperand(op);
  6324. Basic.GetString(modifier.identifier, name);
  6325. PushConstString(name);
  6326. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6327. ASSERT(SemanticChecker.IsStringType(value.type));
  6328. Designate(value, op);
  6329. Emit(Push(modifier.position, op.tag));
  6330. Emit(Push(modifier.position, op.op));
  6331. ReleaseOperand(op);
  6332. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6333. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6334. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6335. Evaluate(value, op);
  6336. Emit(Push(modifier.position, op.op));
  6337. ReleaseOperand(op);
  6338. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6339. ELSE
  6340. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6341. END;
  6342. END AddPortProperty;
  6343. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6344. VAR modifier: SyntaxTree.Modifier;
  6345. BEGIN
  6346. modifier := parameter.modifiers;
  6347. WHILE modifier # NIL DO
  6348. AddPortProperty(parameter,modifier, modifier.expression);
  6349. modifier := modifier.nextModifier;
  6350. END;
  6351. END AddPortProperties;
  6352. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6353. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6354. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6355. PROCEDURE PushLens(type: SyntaxTree.Type);
  6356. BEGIN
  6357. IF IsSemiDynamicArray(type) THEN
  6358. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6359. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6360. Emit(Push(Basic.invalidPosition, op.op));
  6361. ReleaseOperand(op);
  6362. INC(dim);
  6363. ELSIF IsStaticArray(type) THEN
  6364. len := len * type(SyntaxTree.ArrayType).staticLength;
  6365. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6366. INC(dim);
  6367. ELSE
  6368. baseType := type;
  6369. END;
  6370. END PushLens;
  6371. BEGIN
  6372. (* cell *)
  6373. IF parameter.type IS SyntaxTree.ArrayType THEN
  6374. type := parameter.type;
  6375. dim := 0;
  6376. len := 1;
  6377. PushLens(type);
  6378. portType := baseType.resolved(SyntaxTree.PortType);
  6379. ELSE
  6380. portType := parameter.type(SyntaxTree.PortType);
  6381. END;
  6382. PushSelfPointer();
  6383. (* port / array of ports *)
  6384. IF IsStaticArray(type) THEN
  6385. PushConstInteger(len);
  6386. END;
  6387. Field(parameter, op);
  6388. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6389. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6390. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6391. Designate(d, op);*)
  6392. Emit(Push(Basic.invalidPosition, op.op));
  6393. ReleaseOperand(op);
  6394. (* name *)
  6395. PushConstString(name);
  6396. (* inout *)
  6397. PushConstSet(Direction(portType.direction));
  6398. (* width *)
  6399. PushConstInteger(portType.sizeInBits);
  6400. IF parameter.type IS SyntaxTree.PortType THEN
  6401. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6402. AddPortProperties(parameter);
  6403. ELSIF IsStaticArray(type)THEN
  6404. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6405. ELSIF IsSemiDynamicArray(type) THEN
  6406. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6407. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6408. Emit(Add(position,reg, sp, size));
  6409. (* dim *)
  6410. PushConstInteger(dim);
  6411. (* len array *)
  6412. Emit(Push(position, reg));
  6413. ReleaseIntermediateOperand(reg);
  6414. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6415. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6416. Emit(Add(position, sp,sp, size));
  6417. ELSE
  6418. HALT(100);
  6419. END;
  6420. END Parameter;
  6421. BEGIN
  6422. IF backend.cellsAreObjects THEN
  6423. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6424. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6425. END;
  6426. parameter := x.firstParameter;
  6427. WHILE (parameter # NIL) DO
  6428. type := parameter.type.resolved;
  6429. WHILE (type IS SyntaxTree.ArrayType) DO
  6430. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6431. END;
  6432. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6433. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6434. Parameter(name,parameter);
  6435. END;
  6436. parameter := parameter.nextParameter;
  6437. END;
  6438. ELSE HALT(200)
  6439. END;
  6440. END AddPorts;
  6441. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6442. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6443. BEGIN
  6444. Symbol(cell,op);
  6445. ToMemory(op.op,addressType,0);
  6446. Emit(Push(position,op.op));
  6447. ReleaseOperand(op);
  6448. property.GetName(name);
  6449. (* does not work when inheritance is used:
  6450. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6451. *)
  6452. PushConstString(name);
  6453. IF (value # NIL) THEN
  6454. ASSERT(
  6455. SemanticChecker.IsStringType(property.type)
  6456. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6457. OR (property.type.resolved IS SyntaxTree.FloatType)
  6458. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6459. OR (property.type.resolved IS SyntaxTree.SetType)
  6460. );
  6461. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6462. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6463. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6464. Designate(d, op);
  6465. IF SemanticChecker.IsStringType(property.type) THEN
  6466. Emit(Push(Basic.invalidPosition, op.tag))
  6467. END;
  6468. Emit(Push(Basic.invalidPosition, op.op));
  6469. ReleaseOperand(op);
  6470. END;
  6471. IF value = NIL THEN
  6472. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6473. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6474. ASSERT(SemanticChecker.IsStringType(value.type));
  6475. Designate(value, op);
  6476. PushString(op, value.type);
  6477. ReleaseOperand(op);
  6478. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6479. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6480. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6481. Evaluate(value, op);
  6482. Emit(Push(property.position, op.op));
  6483. ReleaseOperand(op);
  6484. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6485. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6486. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6487. Evaluate(value, op);
  6488. Emit(Push(property.position, op.op));
  6489. ReleaseOperand(op);
  6490. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6491. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6492. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6493. Evaluate(value, op);
  6494. Emit(Push(property.position, op.op));
  6495. ReleaseOperand(op);
  6496. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6497. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6498. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6499. Evaluate(value, op);
  6500. Emit(Push(property.position, op.op));
  6501. ReleaseOperand(op);
  6502. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6503. ELSE
  6504. HALT(200);
  6505. END;
  6506. END AddProperty;
  6507. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6508. VAR symbol: SyntaxTree.Symbol;
  6509. BEGIN
  6510. WHILE modifier # NIL DO
  6511. symbol := cellType.FindProperty(modifier.identifier);
  6512. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6513. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6514. modifier := modifier.nextModifier;
  6515. END;
  6516. END AddModifiers;
  6517. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6518. VAR last: SyntaxTree.Modifier;
  6519. BEGIN
  6520. IF to = NIL THEN
  6521. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6522. ELSE
  6523. last := to;
  6524. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6525. last := last.nextModifier;
  6526. END;
  6527. IF last.identifier # this.identifier THEN
  6528. ASSERT(last.nextModifier = NIL);
  6529. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6530. END;
  6531. END;
  6532. END AppendModifier;
  6533. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6534. BEGIN
  6535. WHILE this # NIL DO
  6536. AppendModifier(to, this);
  6537. this := this.nextModifier;
  6538. END;
  6539. END AppendModifiers;
  6540. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6541. VAR base: SyntaxTree.Type;
  6542. BEGIN
  6543. AppendModifiers(to, c.modifiers);
  6544. base := c.GetBaseValueType();
  6545. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6546. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6547. END;
  6548. END AppendCellTypeModifiers;
  6549. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6550. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6551. BEGIN
  6552. Basic.GetString(modifier.identifier, name);
  6553. PushConstString(name);
  6554. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6555. ASSERT(SemanticChecker.IsStringType(value.type));
  6556. Designate(value, op);
  6557. PushString(op, value.type);
  6558. ReleaseOperand(op);
  6559. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6560. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6561. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6562. Evaluate(value, op);
  6563. Emit(Push(modifier.position, op.op));
  6564. ReleaseOperand(op);
  6565. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6566. ELSE
  6567. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6568. END;
  6569. END AddPortProperty;
  6570. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6571. BEGIN
  6572. WHILE modifier # NIL DO
  6573. AddPortProperty(modifier, modifier.expression);
  6574. modifier := modifier.nextModifier;
  6575. END;
  6576. END AddPortProperties;
  6577. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6578. VAR op: Operand;
  6579. BEGIN
  6580. Evaluate(p, op);
  6581. Emit(Push(p.position, op.op));
  6582. ReleaseOperand(op);
  6583. IF p IS SyntaxTree.Designator THEN
  6584. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6585. END;
  6586. END PushPort;
  6587. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6588. VAR tmp: IntermediateCode.Operand;
  6589. BEGIN
  6590. actualType := actualType.resolved;
  6591. IF actualType IS SyntaxTree.StringType THEN
  6592. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6593. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6594. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6595. ELSE
  6596. tmp := op.tag;
  6597. IntermediateCode.MakeMemory(tmp,addressType);
  6598. Emit(Push(position,tmp));
  6599. END;
  6600. Emit(Push(position,op.op))
  6601. END PushString;
  6602. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6603. VAR
  6604. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6605. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6606. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6607. tmp:IntermediateCode.Operand;
  6608. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6609. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6610. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6611. dest: IntermediateCode.Operand;
  6612. staticLength: LONGINT; itype: IntermediateCode.Type;
  6613. convert,isTensor: BOOLEAN;
  6614. recordType: SyntaxTree.RecordType;
  6615. baseType: SyntaxTree.Type;
  6616. flags: SET;
  6617. left: SyntaxTree.Expression;
  6618. call: SyntaxTree.Designator;
  6619. procedure: SyntaxTree.Procedure;
  6620. temporaryVariable: SyntaxTree.Variable;
  6621. dummy: IntermediateCode.Operand;
  6622. customBuiltin: SyntaxTree.CustomBuiltin;
  6623. isVarPar: ARRAY 3 OF BOOLEAN;
  6624. callsection: Sections.Section;
  6625. segmentedName: Basic.SegmentedName;
  6626. needsTrace: BOOLEAN;
  6627. n: ARRAY 256 OF CHAR;
  6628. modifier: SyntaxTree.Modifier;
  6629. previous, init: IntermediateCode.Section;
  6630. prevScope: SyntaxTree.Scope;
  6631. firstPar: LONGINT;
  6632. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6633. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6634. priority: IntermediateCode.Operand;
  6635. op,callop: Operand;
  6636. BEGIN
  6637. IF type = NIL THEN RETURN END;
  6638. type := type.resolved;
  6639. IF type IS SyntaxTree.PointerType THEN
  6640. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6641. END;
  6642. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6643. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6644. recordScope := type(SyntaxTree.RecordType).recordScope;
  6645. IF recordScope.bodyProcedure # NIL THEN
  6646. procedure := recordScope.bodyProcedure;
  6647. body := procedure.procedureScope.body;
  6648. Emit(Push(position,self));
  6649. IF body.isActive THEN
  6650. StaticCallOperand(callop,procedure);
  6651. Emit(Push(position,callop.op));
  6652. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6653. Convert(priority,sizeType);
  6654. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6655. END;
  6656. Emit(Push(position,priority));
  6657. ReleaseIntermediateOperand(priority);
  6658. IF backend.cooperative THEN
  6659. Emit(Push(position,self));
  6660. CallThis(position,"Activities","Create",3)
  6661. ELSE
  6662. flags := 0;
  6663. IF body.isSafe THEN
  6664. flags := 1;
  6665. END;
  6666. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6667. Emit(Push(position,self));
  6668. CallThis(position,"Objects","CreateProcess",4)
  6669. END;
  6670. ELSE
  6671. Emit(Push(position,self));
  6672. StaticCallOperand(callop,procedure);
  6673. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6674. END;
  6675. Emit(Pop(position,self));
  6676. END;
  6677. END CallBodies;
  6678. PROCEDURE PushTD(type: SyntaxTree.Type);
  6679. VAR op: IntermediateCode.Operand;
  6680. BEGIN
  6681. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6682. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6683. ELSE
  6684. IF type.resolved IS SyntaxTree.PointerType THEN
  6685. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6686. END;
  6687. op := TypeDescriptorAdr(type.resolved);
  6688. IF ~newObjectFile THEN
  6689. IntermediateCode.MakeMemory(op,addressType);
  6690. END;
  6691. Emit(Push(position,op));
  6692. END
  6693. END PushTD;
  6694. BEGIN
  6695. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6696. dest := destination; destination := emptyOperand;
  6697. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6698. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6699. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6700. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6701. CASE x.id OF
  6702. (* ---- COPY ----- *)
  6703. |Global.Copy:
  6704. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6705. (* ---- EXCL, INCL----- *)
  6706. |Global.Excl,Global.Incl:
  6707. Evaluate(p0,s0);
  6708. Evaluate(p1,s1);
  6709. Convert(s1.op,setType);
  6710. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6711. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6712. END;
  6713. ReuseCopy(res,s0.op);
  6714. ReleaseOperand(s0);
  6715. Reuse1(tmp,s1.op);
  6716. ReleaseOperand(s1);
  6717. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6718. IF x.id = Global.Excl THEN
  6719. Emit(Not(position,tmp,tmp));
  6720. Emit(And(position,res,res,tmp));
  6721. ELSE
  6722. Emit(Or(position,res,res,tmp));
  6723. END;
  6724. ReleaseIntermediateOperand(tmp);
  6725. Designate(p0,s0);
  6726. ToMemory(s0.op,setType,0);
  6727. Emit(Mov(position,s0.op,res));
  6728. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6729. (* ---- DISPOSE ----- *)
  6730. |Global.Dispose:
  6731. Designate(p0,s0);
  6732. Emit(Push(position,s0.op));
  6733. ReleaseOperand(s0);
  6734. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6735. (* ---- GETPROCEDURE ----- *)
  6736. |Global.GetProcedure:
  6737. Designate(p0,s0);
  6738. PushString(s0,p0.type);
  6739. Designate(p1,s1);
  6740. PushString(s1,p1.type);
  6741. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6742. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6743. ELSE PushTD(procedureType.firstParameter.type)
  6744. END;
  6745. PushTD(procedureType.returnType);
  6746. Designate(p2,s2);
  6747. Emit(Push(position,s2.op));
  6748. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6749. CallThis(position,"Modules","GetProcedure", 7);
  6750. (* ---- ASH, LSH, ROT ----- *)
  6751. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6752. Evaluate(p0,s0);
  6753. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6754. (* make unsigned arguments in order to produced a logical shift *)
  6755. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6756. convert:= TRUE;
  6757. itype := s0.op.type;
  6758. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6759. Convert(s0.op,itype);
  6760. ELSE
  6761. convert := FALSE;
  6762. END;
  6763. END;
  6764. Evaluate(p1,s1);
  6765. IF IsIntegerConstant(p1,hint) THEN
  6766. ReuseCopy(reg,s0.op);
  6767. IF hint > 0 THEN
  6768. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6769. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6770. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6771. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6772. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6773. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6774. END;
  6775. ELSIF hint < 0 THEN
  6776. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6777. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6778. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6779. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6780. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6781. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6782. END;
  6783. END;
  6784. ReleaseOperand(s0); ReleaseOperand(s1);
  6785. ELSE
  6786. exit := NewLabel();
  6787. end := NewLabel();
  6788. ReuseCopy(reg,s0.op);
  6789. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6790. Reuse1(tmp,s1.op);
  6791. Emit(Neg(position,tmp,s1.op));
  6792. Convert(tmp,s1.op.type);
  6793. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6794. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6795. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6796. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6797. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6798. END;
  6799. ReleaseIntermediateOperand(tmp);
  6800. BrL(end);
  6801. SetLabel(exit);
  6802. ReuseCopy(tmp,s1.op);
  6803. Convert(tmp,s1.op.type);
  6804. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6805. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6806. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6807. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6808. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6809. END;
  6810. ReleaseIntermediateOperand(tmp);
  6811. SetLabel(end);
  6812. ReleaseOperand(s0); ReleaseOperand(s1);
  6813. END;
  6814. InitOperand(result,ModeValue);
  6815. IF convert THEN
  6816. itype := reg.type;
  6817. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6818. Convert(reg,itype);
  6819. END;
  6820. result.op := reg;
  6821. (* ---- CAP ----- *)
  6822. |Global.Cap:
  6823. Evaluate(p0,result);
  6824. ReuseCopy(reg,result.op);
  6825. ReleaseIntermediateOperand(result.op);
  6826. ignore := NewLabel();
  6827. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6828. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6829. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6830. SetLabel(ignore);
  6831. result.op := reg;
  6832. (* ---- CHR ----- *)
  6833. |Global.Chr, Global.Chr32:
  6834. Evaluate(p0,result);
  6835. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6836. |Global.Entier, Global.EntierH:
  6837. Evaluate(p0,result);
  6838. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6839. (* ---- MIN and MAX ----- *)
  6840. |Global.Max,Global.Min:
  6841. Evaluate(p0,s0);
  6842. Evaluate(p1,s1);
  6843. Reuse2(res,s0.op,s1.op);
  6844. else := NewLabel();
  6845. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6846. ELSE BrltL(else,s1.op,s0.op) END;
  6847. Emit(Mov(position,res,s0.op));
  6848. ReleaseOperand(s0);
  6849. end := NewLabel();
  6850. BrL(end);
  6851. SetLabel(else);
  6852. Emit(MovReplace(position,res,s1.op));
  6853. SetLabel(end);
  6854. ReleaseOperand(s1);
  6855. InitOperand(result,ModeValue);
  6856. result.op := res;
  6857. (* ---- ODD ----- *)
  6858. |Global.Odd:
  6859. IF ~conditional THEN
  6860. ConditionToValue(x)
  6861. ELSE
  6862. Evaluate(p0,result);
  6863. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6864. Reuse1(res,result.op);
  6865. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6866. ReleaseIntermediateOperand(result.op);
  6867. result.op := res;
  6868. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6869. ReleaseOperand(result);
  6870. BrL(falseLabel);
  6871. END;
  6872. (* ---- ORD ----- *)
  6873. |Global.Ord, Global.Ord32:
  6874. Evaluate(p0,result);
  6875. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6876. (* ---- SHORT, LONG ----- *)
  6877. |Global.Short, Global.Long:
  6878. Evaluate(p0,result);
  6879. IF x.type IS SyntaxTree.ComplexType THEN
  6880. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6881. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6882. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6883. ELSE
  6884. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6885. END
  6886. (* ---- HALT, SYSTEM.HALT----- *)
  6887. |Global.Halt, Global.systemHalt:
  6888. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6889. EmitTrap (position, val);
  6890. (* ---- ASSERT ----- *)
  6891. |Global.Assert:
  6892. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6893. trueL := NewLabel();
  6894. falseL := NewLabel();
  6895. Condition(p0,trueL,falseL);
  6896. IF p1 = NIL THEN val := AssertTrap
  6897. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6898. END;
  6899. SetLabel(falseL);
  6900. EmitTrap(position,val);
  6901. SetLabel(trueL);
  6902. END;
  6903. (*
  6904. Emit(TrapC(result.op,val);
  6905. *)
  6906. (* ---- INC, DEC----- *)
  6907. |Global.Inc,Global.Dec:
  6908. Expression(p0); adr := result.op;
  6909. LoadValue(result,p0.type); l := result;
  6910. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6911. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6912. END;
  6913. IF x.id = Global.Inc THEN
  6914. Emit(Add(position,l.op,l.op,r.op));
  6915. ELSE
  6916. Emit(Sub(position,l.op,l.op,r.op));
  6917. END;
  6918. ReleaseOperand(l); ReleaseOperand(r);
  6919. (* ---- LEN ----- *)
  6920. |Global.Len: (* dynamic length, static length done by checker *)
  6921. Designate(p0,operand);
  6922. IF p1 = NIL THEN
  6923. InitOperand(l,ModeValue);
  6924. l.op := IntermediateCode.Immediate(int32,0);
  6925. ELSE
  6926. Evaluate(p1,l);
  6927. END;
  6928. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6929. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6930. Dereference(operand, p0.type.resolved, FALSE);
  6931. END;
  6932. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6933. ReleaseOperand(operand); ReleaseOperand(l);
  6934. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6935. ASSERT(p1 # NIL);
  6936. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6937. Dereference(operand,p0.type.resolved,FALSE);
  6938. END;
  6939. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6940. ReleaseOperand(operand); ReleaseOperand(l);
  6941. ELSE HALT(100);
  6942. END;
  6943. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6944. (* ---- FIRST ---- *)
  6945. |Global.First:
  6946. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6947. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6948. ELSE
  6949. Designate(p0, result)
  6950. END
  6951. (* ---- LAST ---- *)
  6952. |Global.Last:
  6953. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6954. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6955. ELSE
  6956. Designate(p0, result);
  6957. (* make sure result.op is a register *)
  6958. tmp := result.op;
  6959. ReuseCopy(result.op, result.op);
  6960. ReleaseIntermediateOperand(tmp);
  6961. (* add offset to result.op *)
  6962. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6963. END
  6964. (* ---- STEP ---- *)
  6965. |Global.Step:
  6966. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6967. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6968. ELSE
  6969. Designate(p0, result);
  6970. (* make sure result.op is a register *)
  6971. tmp := result.op;
  6972. ReuseCopy(result.op, result.op);
  6973. ReleaseIntermediateOperand(tmp);
  6974. (* add offset to result.op *)
  6975. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6976. END
  6977. (* ---- RE ---- *)
  6978. |Global.Re:
  6979. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6980. Designate(p0, result)
  6981. ELSE
  6982. Evaluate(p0, result)
  6983. END
  6984. (* ---- IM ---- *)
  6985. |Global.Im:
  6986. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6987. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6988. Designate(p0, result);
  6989. (* make sure result.op is a register *)
  6990. tmp := result.op;
  6991. ReuseCopy(result.op, result.op);
  6992. ReleaseIntermediateOperand(tmp);
  6993. (* add offset to result.op *)
  6994. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6995. (* ---- ABS ----- *)
  6996. |Global.Abs:
  6997. Evaluate(p0,operand);
  6998. type := p0.type.resolved;
  6999. InitOperand(result,ModeValue);
  7000. Reuse1a(result.op,operand.op,dest);
  7001. Emit(Abs(position,result.op,operand.op));
  7002. ReleaseOperand(operand);
  7003. (* ---- WAIT ----- *)
  7004. |Global.Wait:
  7005. Evaluate(p0,operand);
  7006. Emit(Push(position,operand.op));
  7007. ReleaseOperand(operand);
  7008. CallThis(position,"Activities","Wait", 1);
  7009. (* ---- NEW ----- *)
  7010. |Global.New:
  7011. (*! the following code is only correct for "standard" Oberon calling convention *)
  7012. IF x.type # NIL THEN
  7013. type := x.type.resolved;
  7014. firstPar := 0;
  7015. ELSE
  7016. type := p0.type.resolved;
  7017. firstPar := 1;
  7018. END;
  7019. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7020. THEN
  7021. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7022. IF backend.cooperative THEN
  7023. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7024. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7025. IF recordType.isObject THEN
  7026. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7027. IF recordType.IsActive() THEN
  7028. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7029. END;
  7030. IF recordType.IsProtected() THEN
  7031. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7032. END;
  7033. ELSE
  7034. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7035. END;
  7036. END;
  7037. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7038. CallThis(position,"Runtime","New", 1);
  7039. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7040. Emit(Result(position, pointer));
  7041. exit := NewLabel();
  7042. BreqL(exit,pointer,nil);
  7043. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7044. GetRecordTypeName (recordType,name);
  7045. IF ~recordType.isObject THEN
  7046. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7047. END;
  7048. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7049. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7050. IF recordType.isObject THEN
  7051. IF recordType.IsProtected() THEN
  7052. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7053. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7054. END;
  7055. IF recordType.IsActive() THEN
  7056. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7057. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7058. END;
  7059. END;
  7060. END;
  7061. (* initialize fields *)
  7062. IF type(SyntaxTree.PointerType).isPlain THEN
  7063. size := 0;
  7064. ELSIF recordType.isObject THEN
  7065. size := BaseObjectTypeSize;
  7066. ELSE
  7067. size := BaseRecordTypeSize;
  7068. END;
  7069. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7070. (* call initializer *)
  7071. constructor := GetConstructor(recordType);
  7072. IF constructor # NIL THEN
  7073. (*! should be unified with ProcedureCallDesignator *)
  7074. Emit(Push(position,pointer));
  7075. ReleaseIntermediateOperand(pointer);
  7076. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7077. FOR i := firstPar TO x.parameters.Length()-1 DO
  7078. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7079. formalParameter := formalParameter.nextParameter;
  7080. END;
  7081. (* static call of the constructor *)
  7082. GetCodeSectionNameForSymbol(constructor,name);
  7083. ASSERT(~constructor.isInline);
  7084. IF constructor.scope.ownerModule # module.module THEN
  7085. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7086. ELSE
  7087. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7088. END;
  7089. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7090. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7091. Emit(Pop(position,pointer));
  7092. END;
  7093. (* call bodies *)
  7094. CallBodies(pointer,type);
  7095. SetLabel(exit);
  7096. needsTrace := p0.NeedsTrace();
  7097. IF needsTrace THEN ModifyAssignments(true) END;
  7098. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7099. Emit(Push(position, pointer));
  7100. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7101. Emit(Pop(position, pointer));
  7102. END;
  7103. Designate(p0,l);
  7104. IF needsTrace THEN
  7105. CallAssignPointer(l.op, pointer);
  7106. ELSE
  7107. ToMemory(l.op,addressType,0);
  7108. Emit(Mov(position,l.op,pointer));
  7109. END;
  7110. ReleaseIntermediateOperand(pointer);
  7111. ReleaseOperand(l);
  7112. IF needsTrace THEN ModifyAssignments(false) END;
  7113. ELSE (* not cooperative backend *)
  7114. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7115. IF temporaryVariable # NIL THEN
  7116. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7117. ELSE
  7118. Designate(p0,l);
  7119. END;
  7120. (* l.op contains address of pointer to record *)
  7121. Emit(Push(position,l.op)); (* address for use after syscall *)
  7122. Emit(Push(position,l.op));
  7123. ReleaseOperand(l);
  7124. (* push type descriptor *)
  7125. reg := TypeDescriptorAdr(recordType);
  7126. IF ~newObjectFile THEN
  7127. IntermediateCode.MakeMemory(reg,addressType);
  7128. END;
  7129. Emit(Push(position,reg));
  7130. ReleaseIntermediateOperand(reg);
  7131. (* push realtime flag *)
  7132. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7133. ELSE Emit(Push(position,false));
  7134. END;
  7135. CallThis(position,"Heaps","NewRec", 3);
  7136. (* check allocation success, if not successful then do not call initializers and bodies *)
  7137. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7138. Emit(Pop(position,pointer));
  7139. MakeMemory(reg,pointer,addressType,0);
  7140. ReleaseIntermediateOperand(pointer);
  7141. pointer := reg;
  7142. exit := NewLabel();
  7143. BreqL(exit,pointer,nil);
  7144. Emit(Push(position,pointer));
  7145. (* initialize fields *)
  7146. InitFields(recordType, pointer,0);
  7147. (* call initializer *)
  7148. constructor := GetConstructor(recordType);
  7149. IF constructor # NIL THEN
  7150. (*! should be unified with ProcedureCallDesignator *)
  7151. Emit(Push(position,pointer));
  7152. ReleaseIntermediateOperand(pointer);
  7153. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7154. FOR i := firstPar TO x.parameters.Length()-1 DO
  7155. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7156. formalParameter := formalParameter.nextParameter;
  7157. END;
  7158. (* static call of the constructor *)
  7159. GetCodeSectionNameForSymbol(constructor,name);
  7160. ASSERT(~constructor.isInline);
  7161. IF constructor.scope.ownerModule # module.module THEN
  7162. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7163. ELSE
  7164. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7165. END;
  7166. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7167. ELSE
  7168. ReleaseIntermediateOperand(pointer);
  7169. END;
  7170. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7171. Emit(Pop(position,pointer));
  7172. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7173. Designate(p0,l);
  7174. ToMemory(l.op,addressType,0);
  7175. Emit(Mov(position,l.op,pointer));
  7176. ReleaseOperand(l);
  7177. result.tag := emptyOperand;
  7178. ELSIF (x.type # NIL) THEN
  7179. result := l; (* temporary variable is the result of NEW Type() *)
  7180. END;
  7181. (* call bodies *)
  7182. CallBodies(pointer,type);
  7183. ReleaseIntermediateOperand(pointer);
  7184. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7185. end := NewLabel();
  7186. BrL(end);
  7187. SetLabel(exit);
  7188. Designate(p0,l);
  7189. ToMemory(l.op,addressType,0);
  7190. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7191. ReleaseOperand(l);
  7192. SetLabel(end);
  7193. ELSE
  7194. SetLabel(exit);
  7195. END;
  7196. END;
  7197. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7198. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7199. dim := 0;
  7200. IntermediateCode.InitOperand(reg);
  7201. IF p1 # NIL THEN
  7202. FOR i := firstPar TO x.parameters.Length()-1 DO
  7203. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7204. parameter := x.parameters.GetExpression(i);
  7205. Evaluate(parameter,r);
  7206. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7207. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7208. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7209. END;
  7210. Emit(Push(position,r.op));
  7211. IF i=1 THEN
  7212. CopyInt(reg,r.op);
  7213. ELSE
  7214. MulInt(reg, reg, r.op);
  7215. END;
  7216. ReleaseOperand(r);
  7217. INC(dim);
  7218. END;
  7219. Convert(reg,addressType);
  7220. ELSE
  7221. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7222. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7223. END;
  7224. openDim := dim;
  7225. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7226. IF backend.cooperative THEN
  7227. size := ToMemoryUnits(system,system.SizeOf(type));
  7228. WHILE type IS SyntaxTree.ArrayType DO
  7229. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7230. END;
  7231. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7232. IF (size # 1) THEN
  7233. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7234. END;
  7235. Emit(Push(position,reg));
  7236. size := ToMemoryUnits(system,system.SizeOf(type));
  7237. IF (size # 1) THEN
  7238. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7239. END;
  7240. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  7241. Emit(Push(position,reg));
  7242. ReleaseIntermediateOperand(reg);
  7243. CallThis(position,"Runtime","New", 1);
  7244. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7245. Emit(Result(position, pointer));
  7246. exit := NewLabel();
  7247. else := NewLabel();
  7248. BreqL(else,pointer,nil);
  7249. IF ~type.hasPointers THEN
  7250. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7251. ELSIF type IS SyntaxTree.RecordType THEN
  7252. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7253. ELSIF type IS SyntaxTree.ProcedureType THEN
  7254. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7255. ELSE
  7256. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7257. END;
  7258. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7259. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7260. 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))));
  7261. IF type IS SyntaxTree.RecordType THEN
  7262. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7263. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7264. ELSE
  7265. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7266. END;
  7267. i := openDim;
  7268. WHILE i > 0 DO
  7269. DEC (i);
  7270. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7271. END;
  7272. needsTrace := p0.NeedsTrace();
  7273. IF needsTrace THEN ModifyAssignments(true) END;
  7274. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7275. Emit(Push(position, pointer));
  7276. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7277. Emit(Pop(position, pointer));
  7278. END;
  7279. Designate(p0,l);
  7280. IF needsTrace THEN
  7281. CallAssignPointer(l.op, pointer);
  7282. ModifyAssignments(false);
  7283. ELSE
  7284. ToMemory(l.op,addressType,0);
  7285. Emit(Mov(position,l.op,pointer));
  7286. END;
  7287. ReleaseIntermediateOperand(pointer);
  7288. ReleaseOperand(l);
  7289. BrL(exit);
  7290. SetLabel(else);
  7291. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7292. Designate(p0,l);
  7293. IF needsTrace THEN
  7294. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7295. ELSE
  7296. ToMemory(l.op,addressType,0);
  7297. Emit(Mov(position,l.op,pointer));
  7298. END;
  7299. ReleaseOperand(l);
  7300. SetLabel(exit);
  7301. ELSE
  7302. (*! the following code is only correct for "standard" Oberon calling convention *)
  7303. IF SemanticChecker.ContainsPointer(type) THEN
  7304. IF type IS SyntaxTree.ArrayType THEN
  7305. staticLength := 1;
  7306. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7307. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7308. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7309. END;
  7310. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7311. MulInt(reg,reg,tmp);
  7312. END;
  7313. Designate(p0,l);
  7314. IF openDim > 0 THEN
  7315. Emit(Push(position,l.op)); (* address for use after syscall *)
  7316. END;
  7317. Emit(Push(position,l.op)); (* address *)
  7318. ReleaseOperand(l);
  7319. tmp := TypeDescriptorAdr(type);
  7320. IF ~newObjectFile THEN
  7321. IntermediateCode.MakeMemory(tmp,addressType);
  7322. END;
  7323. Emit(Push(position,tmp)); (* type descriptor *)
  7324. ReleaseIntermediateOperand(tmp);
  7325. Emit(Push(position,reg)); (* number Elements *)
  7326. ReleaseIntermediateOperand(reg);
  7327. tmp := IntermediateCode.Immediate(addressType,dim);
  7328. Emit(Push(position,tmp)); (* dimensions *)
  7329. (* push realtime flag *)
  7330. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7331. ELSE Emit(Push(position,false));
  7332. END;
  7333. CallThis(position,"Heaps","NewArr",5)
  7334. ELSE
  7335. size := ToMemoryUnits(system,system.SizeOf(type));
  7336. IF (size # 1) THEN
  7337. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7338. (*
  7339. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7340. *)
  7341. END;
  7342. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7343. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7344. AddInt(reg, reg, tmp);
  7345. (*
  7346. Emit(Add(position,reg,reg,tmp));
  7347. *)
  7348. Designate(p0,l);
  7349. IF openDim >0 THEN
  7350. Emit(Push(position,l.op)); (* address for use after syscall *)
  7351. END;
  7352. Emit(Push(position,l.op)); (* address for syscall *)
  7353. ReleaseOperand(l); (* pointer address *)
  7354. Emit(Push(position,reg)); (* size *)
  7355. ReleaseIntermediateOperand(reg);
  7356. (* push realtime flag *)
  7357. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7358. ELSE Emit(Push(position,false));
  7359. END;
  7360. CallThis(position,"Heaps","NewSys", 3)
  7361. END;
  7362. IF openDim > 0 THEN
  7363. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7364. Emit(Pop(position,adr));
  7365. ToMemory(adr,addressType,0);
  7366. ReuseCopy(tmp,adr);
  7367. ReleaseIntermediateOperand(adr);
  7368. adr := tmp;
  7369. else := NewLabel();
  7370. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7371. i := openDim-1;
  7372. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7373. WHILE (i >= 0) DO
  7374. Emit(Pop(position,reg));
  7375. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7376. Emit(Mov(position,res,reg));
  7377. DEC(i);
  7378. END;
  7379. ReleaseIntermediateOperand(adr);
  7380. ReleaseIntermediateOperand(reg);
  7381. exit := NewLabel();
  7382. BrL(exit);
  7383. SetLabel(else);
  7384. (* else part: array could not be allocated *)
  7385. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7386. Emit(Add(position,sp,sp,tmp));
  7387. SetLabel(exit);
  7388. END;
  7389. END;
  7390. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7391. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7392. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7393. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7394. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7395. left.SetType(procedure.type);
  7396. formalParameter := procedureType.firstParameter;
  7397. (* push array to allocate *)
  7398. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7399. formalParameter :=formalParameter.nextParameter;
  7400. (* push length array *)
  7401. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7402. (* push size *)
  7403. type := t0;
  7404. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7405. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7406. END;
  7407. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7408. Emit(Push(position,tmp));
  7409. (* *)
  7410. IF SemanticChecker.ContainsPointer(type) THEN
  7411. tmp := TypeDescriptorAdr(type);
  7412. IF ~newObjectFile THEN
  7413. IntermediateCode.MakeMemory(tmp,addressType);
  7414. END;
  7415. ELSE
  7416. tmp := IntermediateCode.Immediate(addressType, 0);
  7417. END;
  7418. Emit(Push(position,tmp)); (* type descriptor *)
  7419. StaticCallOperand(result,procedure);
  7420. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7421. ReleaseOperand(result);
  7422. END;
  7423. (*
  7424. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7425. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7426. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7427. *)
  7428. ELSE
  7429. dim := 0;
  7430. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7431. (* generate geometry descriptor *)
  7432. Designate(p0,l);
  7433. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7434. ReleaseOperand(l);
  7435. isTensor := TRUE;
  7436. ELSE
  7437. isTensor := FALSE;
  7438. END;
  7439. FOR i := firstPar TO x.parameters.Length()-1 DO
  7440. IF ~isTensor THEN
  7441. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7442. END;
  7443. parameter := x.parameters.GetExpression(i);
  7444. Evaluate(parameter,r);
  7445. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7446. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7447. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7448. END;
  7449. Emit(Push(position,r.op));
  7450. IF i=1 THEN
  7451. CopyInt(reg, r.op);
  7452. ELSE
  7453. MulInt(reg, reg, r.op);
  7454. END;
  7455. ReleaseOperand(r);
  7456. INC(dim);
  7457. END;
  7458. Convert(reg,addressType);
  7459. openDim := dim;
  7460. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7461. (*! the following code is only correct for "standard" Oberon calling convention *)
  7462. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7463. t := type;
  7464. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7465. staticLength := 1;
  7466. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7467. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7468. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7469. END;
  7470. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7471. MulInt(reg,reg,tmp);
  7472. END;
  7473. Designate(p0,l);
  7474. IF isTensor THEN
  7475. Dereference(l,type,FALSE);
  7476. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7477. END;
  7478. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7479. Emit(Push(position,l.tag)); (* address *)
  7480. ReleaseOperand(l);
  7481. tmp := TypeDescriptorAdr(t);
  7482. IF ~newObjectFile THEN
  7483. IntermediateCode.MakeMemory(tmp,addressType);
  7484. END;
  7485. Emit(Push(position,tmp)); (* type descriptor *)
  7486. ReleaseIntermediateOperand(tmp);
  7487. Emit(Push(position,reg)); (* number Elements *)
  7488. ReleaseIntermediateOperand(reg);
  7489. tmp := IntermediateCode.Immediate(addressType,0);
  7490. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7491. (* push realtime flag: false by default *)
  7492. Emit(Push(position,false));
  7493. CallThis(position,"Heaps","NewArr",5);
  7494. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7495. Emit(Pop(position,adr));
  7496. GetMathArrayField(tmp,adr,MathPtrOffset);
  7497. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7498. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7499. PutMathArrayField(adr,reg,MathAdrOffset);
  7500. ReleaseIntermediateOperand(tmp);
  7501. ReleaseIntermediateOperand(reg);
  7502. ELSE
  7503. IF isTensor THEN
  7504. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7505. ELSE
  7506. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7507. END;
  7508. IF (size # 1) THEN
  7509. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7510. END;
  7511. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7512. AddInt(reg,reg,tmp);
  7513. Designate(p0,l);
  7514. IF isTensor THEN
  7515. Dereference(l,type,FALSE);
  7516. END;
  7517. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7518. Emit(Push(position,l.tag)); (* address for syscall *)
  7519. ReleaseOperand(l); (* pointer address *)
  7520. Emit(Push(position,reg)); (* size *)
  7521. ReleaseIntermediateOperand(reg);
  7522. (* push realtime flag: false by default *)
  7523. Emit(Push(position,false));
  7524. CallThis(position,"Heaps","NewSys",3);
  7525. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7526. Emit(Pop(position,adr));
  7527. GetMathArrayField(tmp,adr,MathPtrOffset);
  7528. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7529. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7530. PutMathArrayField(adr,reg,MathAdrOffset);
  7531. ReleaseIntermediateOperand(tmp);
  7532. ReleaseIntermediateOperand(reg);
  7533. END;
  7534. flags := {};
  7535. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7536. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7537. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7538. PutMathArrayField(adr,tmp,MathDimOffset);
  7539. else := NewLabel();
  7540. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7541. i := openDim-1;
  7542. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7543. IF isTensor THEN
  7544. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7545. ELSE
  7546. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7547. END;
  7548. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7549. WHILE (i >= 0) DO
  7550. Emit(Pop(position,reg));
  7551. PutMathArrayLength(adr,reg,i);
  7552. PutMathArrayIncrement(adr,tmp,i);
  7553. IF i > 0 THEN
  7554. IF i=openDim-1 THEN
  7555. CopyInt(tmp,tmp);
  7556. END;
  7557. MulInt(tmp,tmp,reg);
  7558. END;
  7559. DEC(i);
  7560. END;
  7561. ReleaseIntermediateOperand(adr);
  7562. ReleaseIntermediateOperand(reg);
  7563. ReleaseIntermediateOperand(tmp);
  7564. exit := NewLabel();
  7565. BrL(exit);
  7566. SetLabel(else);
  7567. (* else part: array could not be allocated *)
  7568. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7569. Emit(Add(position,sp,sp,tmp));
  7570. SetLabel(exit);
  7571. END;
  7572. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7573. THEN
  7574. IF ~backend.cellsAreObjects THEN RETURN END;
  7575. IF InCellScope(currentScope) THEN
  7576. PushSelfPointer()
  7577. ELSE
  7578. Emit(Push(position, nil));
  7579. END;
  7580. (* push temp address *)
  7581. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7582. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7583. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7584. (* l.op contains address of pointer to record *)
  7585. Emit(Push(position,l.op)); (* address for use after syscall *)
  7586. ReleaseOperand(l);
  7587. (* push type descriptor *)
  7588. reg := TypeDescriptorAdr(baseType);
  7589. IF ~newObjectFile THEN
  7590. IntermediateCode.MakeMemory(reg,addressType);
  7591. END;
  7592. Emit(Push(position,reg));
  7593. ReleaseIntermediateOperand(reg);
  7594. (* push name *)
  7595. (*Global.GetSymbolName(p0, n);*)
  7596. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7597. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7598. ELSE
  7599. Global.GetModuleName(module.module, n);
  7600. END;
  7601. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7602. (*type.typeDeclaration.GetName(n);*)
  7603. PushConstString(n);
  7604. (* push cellnet boolean *)
  7605. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7606. (* push engine boolean *)
  7607. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7608. (* allocate *)
  7609. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7610. (* add capabilities *)
  7611. modifier := p0(SyntaxTree.Designator).modifiers;
  7612. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7613. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7614. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7615. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7616. END;
  7617. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7618. (*
  7619. modifier := baseType(SyntaxTree.CellType).modifiers;
  7620. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7621. modifier := p0(SyntaxTree.Designator).modifiers;
  7622. *)
  7623. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7624. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7625. (* l.op contains address of pointer to record *)
  7626. ToMemory(l.op,addressType,0);
  7627. (* l.op contains value of pointer to record *)
  7628. Emit(Push(position,l.op)); (* address for use after syscall *)
  7629. ReleaseOperand(l);
  7630. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7631. prevScope := currentScope;
  7632. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7633. previous := section;
  7634. section := init;
  7635. (* add ports *)
  7636. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7637. CloseInitializer(previous);
  7638. currentScope := prevScope;
  7639. Symbol(temporaryVariable,l);
  7640. ToMemory(l.op,addressType,0);
  7641. Emit(Push(position,l.op));
  7642. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7643. (*
  7644. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7645. IF constructor # NIL THEN
  7646. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7647. FOR i := 1 TO x.parameters.Length()-1 DO
  7648. p := x.parameters.GetExpression(i);
  7649. Global.GetSymbolName(parameter,name);
  7650. Evaluate(p, value);
  7651. ASSERT(value.type # NIL);
  7652. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7653. par := instance.AddParameter(name);
  7654. par.SetInteger(value.integer);
  7655. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7656. par := instance.AddParameter(name);
  7657. par.SetBoolean(value.boolean);
  7658. ELSE Error(x.position,NotYetImplemented)
  7659. END;
  7660. parameter := parameter.nextParameter
  7661. END;
  7662. END;
  7663. *)
  7664. (* call initializer *)
  7665. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7666. IF constructor # NIL THEN
  7667. (*! should be unified with ProcedureCallDesignator *)
  7668. IF backend.cellsAreObjects THEN
  7669. Symbol(temporaryVariable,l);
  7670. ToMemory(l.op,addressType,0);
  7671. Emit(Push(position,l.op));
  7672. ReleaseOperand(l);
  7673. END;
  7674. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7675. FOR i := firstPar TO x.parameters.Length()-1 DO
  7676. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7677. formalParameter := formalParameter.nextParameter;
  7678. END;
  7679. (* static call of the constructor *)
  7680. Global.GetSymbolSegmentedName(constructor,name);
  7681. ASSERT(~constructor.isInline);
  7682. IF constructor.scope.ownerModule # module.module THEN
  7683. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7684. ELSE
  7685. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7686. END;
  7687. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7688. (*ELSE
  7689. ReleaseIntermediateOperand(pointer);*)
  7690. END;
  7691. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7692. ToMemory(l.op, addressType, 0);
  7693. Designate(p0,s0);
  7694. ToMemory(s0.op,addressType,0);
  7695. Emit(Mov(position,s0.op,l.op));
  7696. ReleaseOperand(l);
  7697. ReleaseOperand(s0);
  7698. result.tag := emptyOperand;
  7699. (* start *)
  7700. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7701. (* push cell *)
  7702. Symbol(temporaryVariable, l);
  7703. ToMemory(l.op,addressType,0);
  7704. Emit(Push(Basic.invalidPosition,l.op));
  7705. (* push delegate *)
  7706. Emit(Push(Basic.invalidPosition,l.op));
  7707. ReleaseOperand(l);
  7708. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7709. Emit(Push(position, s1.op));
  7710. ReleaseOperand(s1);
  7711. CallThis(position,"ActiveCellsRuntime","Start",3);
  7712. END;
  7713. (*IF temporaryVariable # NIL THEN
  7714. end := NewLabel();
  7715. BrL(end);
  7716. SetLabel(exit);
  7717. Designate(p0,l);
  7718. ToMemory(l.op,addressType,0);
  7719. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7720. ReleaseOperand(l);
  7721. SetLabel(end);
  7722. ELSE
  7723. SetLabel(exit);
  7724. END;
  7725. *)
  7726. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7727. ELSE (* no pointer to record, no pointer to array *)
  7728. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7729. (* ignore new statement *)
  7730. Warning(p0.position, "cannot run on final hardware");
  7731. ELSE
  7732. HALT(200);
  7733. END;
  7734. END;
  7735. (* ---- ADDRESSOF----- *)
  7736. |Global.systemAdr:
  7737. Designate(p0,s0);
  7738. s0.mode := ModeValue;
  7739. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7740. ReleaseIntermediateOperand(s0.op);
  7741. s0.op := s0.tag;
  7742. IntermediateCode.InitOperand(s0.tag);
  7743. END;
  7744. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7745. result := s0;
  7746. (* ---- BIT ----- *)
  7747. |Global.systemBit:
  7748. Evaluate(p0,s0);
  7749. ToMemory(s0.op,addressType,0);
  7750. ReuseCopy(res,s0.op);
  7751. ReleaseOperand(s0);
  7752. Evaluate(p1,s1);
  7753. Emit(Ror(position,res,res,s1.op));
  7754. ReleaseOperand(s1);
  7755. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7756. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7757. IF ~conditional THEN
  7758. InitOperand(result,ModeValue); result.op := res;
  7759. ELSE
  7760. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7761. BrL(falseLabel);
  7762. ReleaseIntermediateOperand(res);
  7763. END;
  7764. (* --- MSK ----*)
  7765. |Global.systemMsk:
  7766. Evaluate(p0,s0);
  7767. Evaluate(p1,s1);
  7768. ReuseCopy(res,s0.op);
  7769. ReleaseOperand(s0);
  7770. Emit(And(position,res,res,s1.op));
  7771. ReleaseOperand(s1);
  7772. InitOperand(result,ModeValue);
  7773. result.op := res;
  7774. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7775. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7776. Evaluate(p0,s0);
  7777. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7778. ReleaseOperand(s0);
  7779. InitOperand(result,ModeValue);
  7780. result.op := res;
  7781. (* ---- SYSTEM.GetStackPointer ----- *)
  7782. |Global.systemGetStackPointer:
  7783. InitOperand(result,ModeValue);
  7784. result.op := sp;
  7785. (* ---- SYSTEM.GetFramePointer ----- *)
  7786. |Global.systemGetFramePointer:
  7787. InitOperand(result,ModeValue);
  7788. result.op := fp;
  7789. (* ---- SYSTEM.GetActivity ----- *)
  7790. |Global.systemGetActivity:
  7791. ASSERT(backend.cooperative);
  7792. InitOperand(result,ModeValue);
  7793. result.op := ap;
  7794. (* ---- SYSTEM.SetStackPointer ----- *)
  7795. |Global.systemSetStackPointer:
  7796. Evaluate(p0,s0); (* *)
  7797. Emit(Mov(position,sp,s0.op));
  7798. ReleaseOperand(s0);
  7799. (* ---- SYSTEM.SetFramePointer ----- *)
  7800. |Global.systemSetFramePointer:
  7801. Evaluate(p0,s0); (* *)
  7802. Emit(Mov(position,fp,s0.op));
  7803. ReleaseOperand(s0);
  7804. (* ---- SYSTEM.Activity ----- *)
  7805. |Global.systemSetActivity:
  7806. ASSERT(backend.cooperative);
  7807. Evaluate(p0,s0); (* *)
  7808. Emit(Mov(position,ap,s0.op));
  7809. ReleaseOperand(s0);
  7810. (* ---- SYSTEM.VAL ----- *)
  7811. |Global.systemVal:
  7812. Expression(p1);
  7813. s1 := result;
  7814. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7815. IF s1.mode = ModeReference THEN
  7816. (* nothing to be done if not record type, just take over new type *)
  7817. IF (type IS SyntaxTree.RecordType) THEN
  7818. ReleaseIntermediateOperand(s1.tag);
  7819. s1.tag := TypeDescriptorAdr(type);
  7820. IF ~newObjectFile THEN
  7821. IntermediateCode.MakeMemory(s1.tag,addressType);
  7822. END;
  7823. UseIntermediateOperand(s1.tag);
  7824. END;
  7825. result := s1;
  7826. ELSE (* copy over result to different type, may not use convert *)
  7827. itype := IntermediateCode.GetType(system,type);
  7828. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7829. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7830. Emit(Mov(position,s0.op,s1.op));
  7831. ReleaseOperand(s1);
  7832. InitOperand(result,ModeValue);
  7833. result.op := s0.op;
  7834. ELSE (* different size, must convert *)
  7835. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7836. Convert(s1.op, IntermediateCode.GetType(system,type));
  7837. result := s1;
  7838. END;
  7839. END;
  7840. (* ---- SYSTEM.GET ----- *)
  7841. |Global.systemGet:
  7842. Evaluate(p0,s0); (* adr *)
  7843. Designate(p1,s1); (* variable *)
  7844. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7845. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7846. Emit(Mov(position,s1.op,s0.op));
  7847. ReleaseOperand(s1);
  7848. ReleaseOperand(s0);
  7849. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7850. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7851. Evaluate(p0,s0); (* *)
  7852. Evaluate(p1,s1); (* variable *)
  7853. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7854. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7855. (* real part *)
  7856. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7857. Emit(Mov(position,res, s1.op));
  7858. ReleaseIntermediateOperand(res);
  7859. (* imaginary part *)
  7860. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7861. Emit(Mov(position,res, s1.tag));
  7862. ReleaseIntermediateOperand(res);
  7863. ReleaseOperand(s1);
  7864. ReleaseOperand(s0);
  7865. ELSE
  7866. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7867. ReleaseOperand(s0);
  7868. Emit(Mov(position,res,s1.op));
  7869. ReleaseIntermediateOperand(res);
  7870. ReleaseOperand(s1);
  7871. END;
  7872. (* ---- SYSTEM.MOVE ----- *)
  7873. |Global.systemMove:
  7874. Evaluate(p0,s0);
  7875. Evaluate(p1,s1);
  7876. Evaluate(p2,s2);
  7877. Emit(Copy(position,s1.op,s0.op,s2.op));
  7878. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7879. (* ---- SYSTEM.NEW ----- *)
  7880. |Global.systemNew:
  7881. Designate(p0,s0);
  7882. Emit(Push(position,s0.op));
  7883. ReleaseOperand(s0);
  7884. Evaluate(p1,s1);
  7885. Emit(Push(position,s1.op));
  7886. ReleaseOperand(s1);
  7887. (* push realtime flag: false by default *)
  7888. Emit(Push(position,false));
  7889. CallThis(position,"Heaps","NewSys",3);
  7890. (* ---- SYSTEM.CALL ----- *)
  7891. |Global.systemRef:
  7892. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7893. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7894. s0.mode := ModeValue;
  7895. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7896. result := s0
  7897. (* ---- INCR ----- *)
  7898. |Global.Incr:
  7899. Designate(p0,operand);
  7900. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7901. Dereference(operand,p0.type.resolved,FALSE);
  7902. END;
  7903. ASSERT(p1 # NIL);
  7904. Evaluate(p1,l);
  7905. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7906. ReleaseOperand(operand); ReleaseOperand(l);
  7907. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7908. (* ---- SUM ----- *)
  7909. |Global.Sum: HALT(200);
  7910. (* ---- ALL ----- *)
  7911. |Global.All: HALT(200);
  7912. (* ---- CAS ----- *)
  7913. |Global.Cas:
  7914. needsTrace := p0.NeedsTrace();
  7915. IF needsTrace THEN ModifyAssignments(true) END;
  7916. Designate(p0,s0);
  7917. Evaluate(p1,s1);
  7918. Evaluate(p2,s2);
  7919. IF needsTrace THEN
  7920. Emit(Push(position, s0.op));
  7921. Emit(Push(position, s1.op));
  7922. Emit(Push(position, s2.op));
  7923. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7924. ELSE
  7925. Emit(Cas(position,s0.op,s1.op,s2.op));
  7926. END;
  7927. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7928. IF needsTrace THEN ModifyAssignments(false) END;
  7929. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7930. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7931. IF conditional THEN
  7932. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7933. BrL(falseLabel);
  7934. ReleaseIntermediateOperand(res);
  7935. END;
  7936. (* ---- DIM ----- *)
  7937. |Global.Dim:
  7938. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7939. Designate(p0,s0);
  7940. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7941. Dereference(s0,p0.type.resolved,FALSE);
  7942. END;
  7943. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7944. ReleaseOperand(s0);
  7945. (* ---- RESHAPE ----- *)
  7946. |Global.Reshape:
  7947. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7948. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7949. left.SetType(procedure.type);
  7950. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7951. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7952. END;
  7953. (* ---- SYSTEM.TYPECODE ----- *)
  7954. |Global.systemTypeCode:
  7955. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7956. IF type.resolved IS SyntaxTree.PointerType THEN
  7957. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7958. END;
  7959. result.op := TypeDescriptorAdr(type);
  7960. IF ~newObjectFile THEN
  7961. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7962. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7963. END;
  7964. result.mode := ModeValue;
  7965. (* ---- SYSTEM.TRACE ----- *)
  7966. |Global.systemTrace:
  7967. SystemTrace(x.parameters, x.position);
  7968. (* ----- CONNECT ------*)
  7969. |Global.Connect:
  7970. IF backend.cellsAreObjects THEN
  7971. PushPort(p0);
  7972. PushPort(p1);
  7973. IF p2 # NIL THEN
  7974. Evaluate(p2, s2);
  7975. Emit(Push(p2.position, s2.op));
  7976. ReleaseOperand(s2);
  7977. ELSE
  7978. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7979. END;
  7980. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7981. ELSE
  7982. Warning(x.position, "cannot run on final hardware");
  7983. END;
  7984. (* ----- DELEGATE ------*)
  7985. |Global.Delegate:
  7986. IF backend.cellsAreObjects THEN
  7987. PushPort(p0);
  7988. PushPort(p1);
  7989. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7990. ELSE
  7991. Warning(x.position, "cannot run on final hardware");
  7992. END;
  7993. (* ----- SEND ------*)
  7994. |Global.Send:
  7995. Evaluate(p0,s0);
  7996. Evaluate(p1,s1);
  7997. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7998. Emit(Push(position,s0.op));
  7999. Emit(Push(position,s1.op));
  8000. (*
  8001. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8002. *)
  8003. IF ~backend.cellsAreObjects THEN
  8004. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8005. END;
  8006. ReleaseOperand(s0);
  8007. ReleaseOperand(s1);
  8008. IF backend.cellsAreObjects THEN
  8009. CallThis(position,"ActiveCellsRuntime","Send",2);
  8010. ELSE
  8011. CallThis(position,ChannelModuleName,"Send",2);
  8012. END;
  8013. (* ----- RECEIVE ------*)
  8014. |Global.Receive:
  8015. Evaluate(p0,s0);
  8016. Emit(Push(position,s0.op));
  8017. Designate(p1,s1);
  8018. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8019. Emit(Push(position,s1.op));
  8020. IF p2 # NIL THEN
  8021. Designate(p2,s2);
  8022. Emit(Push(position,s2.op));
  8023. END;
  8024. (*
  8025. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8026. *)
  8027. IF ~backend.cellsAreObjects THEN
  8028. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8029. END;
  8030. ReleaseOperand(s0);
  8031. ReleaseOperand(s1);
  8032. ReleaseOperand(s2);
  8033. IF backend.cellsAreObjects THEN
  8034. IF p2 = NIL THEN
  8035. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8036. ELSE
  8037. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8038. END;
  8039. ELSE
  8040. IF p2 = NIL THEN
  8041. CallThis(position,ChannelModuleName,"Receive",2)
  8042. ELSE
  8043. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8044. END;
  8045. END;
  8046. | Global.systemSpecial:
  8047. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8048. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8049. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8050. (* determine if parameters are of the VAR kind *)
  8051. ASSERT(x.parameters.Length() <= 3);
  8052. formalParameter := procedureType.firstParameter;
  8053. FOR i := 0 TO x.parameters.Length() - 1 DO
  8054. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8055. formalParameter := formalParameter.nextParameter
  8056. END;
  8057. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8058. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8059. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8060. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8061. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8062. IF procedureType.returnType # NIL THEN
  8063. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8064. Emit(Result(position, res));
  8065. (*InitOperand(result,ModeValue);
  8066. result.op := res;
  8067. *)
  8068. IF ~conditional THEN
  8069. InitOperand(result,ModeValue); result.op := res;
  8070. ELSE
  8071. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8072. BrL(falseLabel);
  8073. ReleaseIntermediateOperand(res);
  8074. END;
  8075. END
  8076. ELSE (* function not yet implemented *)
  8077. Error(position,"not yet implemented");
  8078. END;
  8079. destination := dest;
  8080. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8081. END VisitBuiltinCallDesignator;
  8082. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8083. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8084. BEGIN
  8085. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8086. dest := destination; destination := emptyOperand;
  8087. Expression(x.left);
  8088. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8089. ELSIF isUnchecked THEN (* no check *)
  8090. ELSE
  8091. trueL := NewLabel();
  8092. falseL := NewLabel();
  8093. IF IsPointerToRecord(x.left.type,recordType) THEN
  8094. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8095. Emit(Mov(position,tag, result.op));
  8096. IF result.mode # ModeValue THEN
  8097. ptr := tag;
  8098. IntermediateCode.MakeMemory(ptr,addressType);
  8099. Emit(Mov(position,tag, ptr));
  8100. END;
  8101. IF ~backend.cooperative THEN
  8102. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8103. END;
  8104. IntermediateCode.MakeMemory(tag,addressType);
  8105. ELSE
  8106. tag := result.tag;
  8107. UseIntermediateOperand(tag);
  8108. END;
  8109. TypeTest(tag,x.type,trueL,falseL);
  8110. ReleaseIntermediateOperand(tag);
  8111. SetLabel(falseL);
  8112. EmitTrap(position,TypeCheckTrap);
  8113. SetLabel(trueL);
  8114. END;
  8115. destination := dest;
  8116. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8117. END VisitTypeGuardDesignator;
  8118. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8119. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8120. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8121. VAR label: Label; pc: LONGINT;
  8122. BEGIN
  8123. IF backend.cooperative & ~isUnchecked THEN
  8124. pc := section.pc;
  8125. label := NewLabel();
  8126. BrneL(label, operand.op, nil);
  8127. EmitTrap(position, NilPointerTrap);
  8128. SetLabel(label);
  8129. INC(statCoopNilCheck, section.pc - pc);
  8130. END;
  8131. END NilCheck;
  8132. BEGIN
  8133. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8134. ReuseCopy(dereferenced,operand.op);
  8135. ReleaseOperand(operand);
  8136. operand.mode := ModeReference;
  8137. operand.op := dereferenced;
  8138. operand.tag := dereferenced;
  8139. UseIntermediateOperand(operand.tag);
  8140. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8141. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8142. ReleaseIntermediateOperand(operand.tag);
  8143. operand.tag := TypeDescriptorAdr(type);
  8144. IF ~newObjectFile THEN
  8145. IntermediateCode.MakeMemory(operand.tag,addressType);
  8146. END;
  8147. ELSE
  8148. IF ~backend.cooperative THEN
  8149. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8150. END;
  8151. IntermediateCode.MakeMemory(operand.tag,addressType);
  8152. END;
  8153. NilCheck(operand.op);
  8154. ELSIF type IS SyntaxTree.ArrayType THEN
  8155. IF isUnsafe THEN
  8156. NilCheck(operand.op);
  8157. ReleaseIntermediateOperand(operand.tag);
  8158. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8159. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8160. ELSE
  8161. operand.tag := emptyOperand;
  8162. END;
  8163. ELSE
  8164. NilCheck(operand.op);
  8165. IF backend.cooperative THEN
  8166. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8167. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8168. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8169. ELSE
  8170. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8171. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8172. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8173. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8174. END;
  8175. END;
  8176. ELSIF type IS SyntaxTree.MathArrayType THEN
  8177. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8178. IntermediateCode.MakeMemory(operand.op,addressType);
  8179. ELSE HALT(100);
  8180. END;
  8181. END Dereference;
  8182. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8183. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8184. BEGIN
  8185. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8186. dest := destination; destination := emptyOperand;
  8187. Evaluate(x.left,d);
  8188. type := x.type.resolved;
  8189. prevIsUnchecked := isUnchecked;
  8190. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8191. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8192. END;
  8193. Dereference(d,type,IsUnsafePointer(x.left.type));
  8194. isUnchecked := prevIsUnchecked;
  8195. result := d;
  8196. 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
  8197. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8198. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8199. END;
  8200. END;
  8201. destination := dest;
  8202. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8203. END VisitDereferenceDesignator;
  8204. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8205. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8206. BEGIN
  8207. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8208. dest := destination; destination := emptyOperand;
  8209. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8210. tag := result.op;
  8211. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8212. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8213. StaticCallOperand(result,procedure.super);
  8214. ReleaseIntermediateOperand(result.tag);
  8215. UseIntermediateOperand(tag); (* necessary ? *)
  8216. result.tag := tag;
  8217. destination := dest;
  8218. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8219. END VisitSupercallDesignator;
  8220. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8221. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8222. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8223. name: Basic.SegmentedName;
  8224. procedure: SyntaxTree.Procedure;
  8225. procedureType: SyntaxTree.ProcedureType;
  8226. BEGIN
  8227. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8228. dest := destination; destination := emptyOperand;
  8229. scope := currentScope;
  8230. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8231. scope := scope.outerScope;
  8232. END;
  8233. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8234. IF newObjectFile THEN
  8235. moduleSection := meta.ModuleSection();
  8236. IF backend.cooperative THEN
  8237. moduleOffset := 0;
  8238. ELSE
  8239. moduleOffset := moduleSection.pc;
  8240. END;
  8241. result.mode := ModeValue;
  8242. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8243. ELSE
  8244. Symbol(moduleSelf,result);
  8245. IntermediateCode.MakeMemory(result.op,addressType);
  8246. END
  8247. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8248. result.mode := ModeValue;
  8249. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8250. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8251. ELSE
  8252. GetBaseRegister(basereg,currentScope,scope);
  8253. InitOperand(result,ModeReference);
  8254. result.op := basereg;
  8255. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8256. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8257. parametersSize := ProcedureParametersSize(system,procedure);
  8258. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8259. IF backend.cooperative THEN
  8260. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8261. END;
  8262. (* tag must be loaded when dereferencing SELF pointer *)
  8263. END;
  8264. destination := dest;
  8265. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8266. END VisitSelfDesignator;
  8267. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8268. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8269. BEGIN
  8270. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8271. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8272. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8273. parameter := procedureType.returnParameter;
  8274. VisitParameter(parameter);
  8275. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8276. END VisitResultDesignator;
  8277. (** values *)
  8278. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8279. BEGIN
  8280. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8281. IF conditional THEN
  8282. IF x.value THEN BrL(trueLabel)
  8283. ELSE BrL(falseLabel)
  8284. END;
  8285. ELSE
  8286. InitOperand(result,ModeValue);
  8287. IF x.value THEN result.op := true ELSE result.op := false END;
  8288. END;
  8289. END VisitBooleanValue;
  8290. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8291. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8292. BEGIN
  8293. Global.GetModuleSegmentedName(module.module, name);
  8294. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8295. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8296. RETURN section
  8297. END GetDataSection;
  8298. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8299. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8300. BEGIN
  8301. type := vop.type;
  8302. data := GetDataSection();
  8303. pc := EnterImmediate(data,vop);
  8304. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8305. IntermediateCode.MakeMemory(vop, type);
  8306. END GetImmediateMem;
  8307. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8308. BEGIN
  8309. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8310. InitOperand(result,ModeValue);
  8311. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8312. IF ~supportedImmediate(result.op) &~inData THEN
  8313. GetImmediateMem(result.op)
  8314. END;
  8315. END VisitIntegerValue;
  8316. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8317. BEGIN
  8318. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8319. InitOperand(result,ModeValue);
  8320. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8321. END VisitCharacterValue;
  8322. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8323. BEGIN
  8324. IF Trace THEN TraceEnter("VisitSetValue") END;
  8325. InitOperand(result,ModeValue);
  8326. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8327. END VisitSetValue;
  8328. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8329. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8330. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8331. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8332. BEGIN
  8333. numberElements := x.elements.Length();
  8334. FOR i := 0 TO numberElements-1 DO
  8335. expression := x.elements.GetExpression(i);
  8336. IF expression IS SyntaxTree.MathArrayExpression THEN
  8337. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8338. ELSE
  8339. inData := TRUE;
  8340. Evaluate(expression,op);
  8341. irv.Emit(Data(position,op.op));
  8342. inData := FALSE;
  8343. ReleaseOperand(op);
  8344. END;
  8345. END;
  8346. END RecursiveData;
  8347. BEGIN
  8348. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8349. IF ~TryConstantDeclaration() THEN
  8350. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8351. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8352. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8353. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8354. ELSE
  8355. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8356. END;
  8357. RecursiveData(x.array);
  8358. InitOperand(result,ModeReference);
  8359. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8360. END
  8361. END VisitMathArrayValue;
  8362. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8363. VAR constant: Sections.Section;
  8364. BEGIN
  8365. IF constantDeclaration = NIL THEN
  8366. RETURN FALSE
  8367. ELSE
  8368. (* Is a constant in this module: did we generate it already? *)
  8369. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8370. IF constant # NIL THEN
  8371. InitOperand(result,ModeReference);
  8372. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8373. RETURN TRUE;
  8374. END;
  8375. END;
  8376. RETURN FALSE
  8377. END TryConstantDeclaration;
  8378. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8379. BEGIN
  8380. constantDeclaration := x;
  8381. x.value.resolved.Accept(SELF);
  8382. END VisitConstant;
  8383. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8384. BEGIN
  8385. IF Trace THEN TraceEnter("VisitRealValue") END;
  8386. InitOperand(result,ModeValue);
  8387. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8388. END VisitRealValue;
  8389. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8390. VAR
  8391. componentType: SyntaxTree.Type;
  8392. BEGIN
  8393. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8394. ASSERT(x.type IS SyntaxTree.ComplexType);
  8395. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8396. InitOperand(result,ModeValue);
  8397. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8398. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8399. END VisitComplexValue;
  8400. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8401. VAR i: LONGINT; name: Basic.SegmentedName;
  8402. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8403. BEGIN
  8404. IF Trace THEN TraceEnter("VisitStringValue") END;
  8405. IF ~TryConstantDeclaration() THEN
  8406. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8407. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8408. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8409. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8410. ELSE
  8411. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8412. END;
  8413. FOR i := 0 TO x.length-1 DO
  8414. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8415. irv.Emit(Data(position,op));
  8416. END;
  8417. InitOperand(result,ModeReference);
  8418. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8419. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8420. END
  8421. END VisitStringValue;
  8422. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8423. BEGIN
  8424. IF Trace THEN TraceEnter("VisitNilValue") END;
  8425. InitOperand(result,ModeValue);
  8426. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8427. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8428. END VisitNilValue;
  8429. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8430. BEGIN
  8431. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8432. InitOperand(result,ModeValue);
  8433. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8434. END VisitEnumerationValue;
  8435. (** symbols *)
  8436. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8437. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8438. END VisitImport;
  8439. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8440. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8441. BEGIN
  8442. IF scope # baseScope THEN
  8443. (* left := [fp+8] *)
  8444. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8445. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8446. ReuseCopy(left,right);
  8447. ReleaseIntermediateOperand(right);
  8448. scope := scope.outerScope; DEC(level);
  8449. (* { left := [left+8] } *)
  8450. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8451. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8452. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8453. Emit(Mov(position,left,right));
  8454. scope := scope.outerScope; DEC(level);
  8455. END;
  8456. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8457. result := left;
  8458. ELSE
  8459. result := fp;
  8460. END;
  8461. END GetBaseRegister;
  8462. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8463. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8464. BEGIN
  8465. IF Trace THEN TraceEnter("VisitVariable"); END;
  8466. type := x.type.resolved;
  8467. IF (x.useRegister) THEN
  8468. InitOperand(result, ModeValue);
  8469. IF x.registerNumber < 0 THEN
  8470. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8471. reg := x.registerNumber;
  8472. ELSE
  8473. reg := registerUsageCount.Map(x.registerNumber);
  8474. UseRegister(reg);
  8475. END;
  8476. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8477. ELSIF x.externalName # NIL THEN
  8478. InitOperand(result,ModeReference);
  8479. Basic.ToSegmentedName(x.externalName^, name);
  8480. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8481. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8482. InitOperand(result,ModeReference);
  8483. GetBaseRegister(result.op,currentScope,x.scope);
  8484. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8485. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8486. InitOperand(result,ModeReference);
  8487. GetCodeSectionNameForSymbol(x,name);
  8488. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8489. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8490. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8491. InitOperand(result,ModeReference);
  8492. GetCodeSectionNameForSymbol(x,name);
  8493. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8494. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8495. ELSE (* field, left designator must have been emitted *)
  8496. ASSERT(result.mode = ModeReference);
  8497. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8498. ReuseCopy(temp,result.op);
  8499. ReleaseIntermediateOperand(result.op);
  8500. result.op := temp;
  8501. END;
  8502. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8503. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8504. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8505. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8506. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8507. END;
  8508. END;
  8509. END;
  8510. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8511. ValueToCondition(result);
  8512. ELSIF type IS SyntaxTree.ProcedureType THEN
  8513. ReleaseIntermediateOperand(result.tag);
  8514. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8515. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8516. UseIntermediateOperand(result.tag);
  8517. ELSE
  8518. result.tag := nil; (* nil *)
  8519. END;
  8520. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8521. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8522. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8523. ReleaseIntermediateOperand(result.tag);
  8524. Global.GetSymbolSegmentedName(x,name);
  8525. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8526. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8527. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8528. ELSE
  8529. END;
  8530. ELSE
  8531. ReleaseIntermediateOperand(result.tag);
  8532. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8533. END;
  8534. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8535. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8536. ReleaseIntermediateOperand(result.tag);
  8537. result.tag := result.op;
  8538. UseIntermediateOperand(result.tag);
  8539. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8540. IntermediateCode.MakeMemory(result.op,addressType);
  8541. END;
  8542. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8543. ReleaseIntermediateOperand(result.tag);
  8544. result.tag := TypeDescriptorAdr(type);
  8545. IF ~newObjectFile THEN
  8546. IntermediateCode.MakeMemory(result.tag,addressType);
  8547. END;
  8548. UseIntermediateOperand(result.tag);
  8549. (* tag for pointer type computed not here but during dereferencing *)
  8550. END;
  8551. IF Trace THEN TraceExit("VisitVariable") END;
  8552. END VisitVariable;
  8553. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8554. BEGIN
  8555. VisitVariable(property);
  8556. END VisitProperty;
  8557. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8558. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8559. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8560. BEGIN
  8561. type := x.type.resolved;
  8562. IF Trace THEN TraceEnter("VisitParameter") END;
  8563. IF x.ownerType IS SyntaxTree.CellType THEN
  8564. ptype := x.type.resolved;
  8565. IF backend.cellsAreObjects THEN
  8566. ASSERT(result.mode = ModeReference);
  8567. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8568. ReuseCopy(temp,result.op);
  8569. ReleaseIntermediateOperand(result.op);
  8570. result.op := temp;
  8571. END;
  8572. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8573. RETURN;
  8574. ELSE
  8575. InitOperand(result,ModeReference);
  8576. GetCodeSectionNameForSymbol(x,name);
  8577. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8578. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8579. RETURN;
  8580. END;
  8581. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8582. InitOperand(result,ModeReference);
  8583. GetCodeSectionNameForSymbol(x,name);
  8584. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8585. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8586. RETURN
  8587. ELSE
  8588. GetBaseRegister(basereg,currentScope,x.scope);
  8589. InitOperand(result,ModeReference);
  8590. result.op := basereg;
  8591. END;
  8592. IF IsOpenArray(type) THEN
  8593. result.tag := basereg;
  8594. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8595. IntermediateCode.MakeMemory(result.op,addressType);
  8596. IF Global.IsOberonProcedure(x.ownerType) THEN
  8597. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8598. UseIntermediateOperand(result.tag);
  8599. ELSE
  8600. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8601. END;
  8602. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8603. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8604. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8605. ELSIF IsStaticArray(type) THEN
  8606. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8607. IntermediateCode.MakeMemory(result.op,addressType);
  8608. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8609. ELSIF type IS SyntaxTree.MathArrayType THEN
  8610. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8611. WITH type: SyntaxTree.MathArrayType DO
  8612. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8613. IF type.form = SyntaxTree.Tensor THEN
  8614. ELSIF type.form = SyntaxTree.Open THEN
  8615. result.tag := result.op;
  8616. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8617. IntermediateCode.MakeMemory(result.op,addressType);
  8618. UseIntermediateOperand(result.tag);
  8619. ELSIF type.form = SyntaxTree.Static THEN
  8620. IF x.kind = SyntaxTree.ConstParameter THEN
  8621. IntermediateCode.MakeMemory(result.op,addressType);
  8622. END;
  8623. ELSE HALT(100)
  8624. END;
  8625. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8626. IF type.form = SyntaxTree.Tensor THEN
  8627. ToMemory(result.op,addressType,0);
  8628. ELSIF type.form = SyntaxTree.Open THEN
  8629. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8630. ReuseCopy(result.tag, mem);
  8631. ReleaseIntermediateOperand(mem);
  8632. ReleaseIntermediateOperand(result.op);
  8633. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8634. ELSIF type.form = SyntaxTree.Static THEN
  8635. IntermediateCode.MakeMemory(result.op,addressType);
  8636. ELSE HALT(100)
  8637. END;
  8638. ELSE HALT(100)
  8639. END;
  8640. END;
  8641. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8642. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8643. IntermediateCode.MakeMemory(result.op,addressType);
  8644. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8645. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8646. END;
  8647. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8648. ValueToCondition(result);
  8649. ELSIF type IS SyntaxTree.ProcedureType THEN
  8650. ReleaseIntermediateOperand(result.tag);
  8651. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8652. IF x.kind = SyntaxTree.VarParameter THEN
  8653. ReuseCopy(result.tag,result.op);
  8654. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8655. IntermediateCode.MakeMemory(result.tag,addressType);
  8656. ELSE
  8657. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8658. UseIntermediateOperand(result.tag);
  8659. END;
  8660. ELSE
  8661. result.tag := nil;
  8662. END;
  8663. (* tag for pointer type computed not here but during dereferencing *)
  8664. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8665. ReleaseIntermediateOperand(result.tag);
  8666. result.tag := basereg;
  8667. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8668. IntermediateCode.MakeMemory(result.tag,addressType);
  8669. UseIntermediateOperand(result.tag);
  8670. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8671. ReleaseIntermediateOperand(result.tag);
  8672. result.tag := TypeDescriptorAdr(type);
  8673. IF ~newObjectFile THEN
  8674. IntermediateCode.MakeMemory(result.tag,addressType);
  8675. END;
  8676. UseIntermediateOperand(result.tag);
  8677. END;
  8678. IF Trace THEN TraceExit("VisitParameter") END;
  8679. END VisitParameter;
  8680. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8681. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8682. BEGIN
  8683. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8684. (* left.p: left already emitted *)
  8685. tag := result.op; (* value of pointer to left *)
  8686. (* get type desc *)
  8687. tmp := result.tag;
  8688. IntermediateCode.MakeMemory(tmp,addressType);
  8689. (* get method adr *)
  8690. Reuse1(reg,tmp);
  8691. ReleaseIntermediateOperand(tmp);
  8692. IF backend.cooperative THEN
  8693. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8694. WHILE recordType.baseType # NIL DO
  8695. recordType := recordType.GetBaseRecord ();
  8696. END;
  8697. GetRecordTypeName (recordType,name);
  8698. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8699. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8700. offset := 0;
  8701. ELSE
  8702. offset := 2;
  8703. END;
  8704. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8705. ELSE
  8706. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8707. END;
  8708. InitOperand(operand,ModeReference);
  8709. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8710. operand.op := reg;
  8711. operand.tag := tag;
  8712. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8713. END DynamicCallOperand;
  8714. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8715. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8716. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8717. BEGIN
  8718. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8719. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8720. tag := operand.op;
  8721. ReleaseIntermediateOperand(operand.tag);
  8722. ELSE tag := nil
  8723. END;
  8724. IF x.isInline THEN
  8725. sectionType := Sections.InlineCodeSection;
  8726. ELSE
  8727. sectionType := Sections.CodeSection;
  8728. END;
  8729. IF x.externalName # NIL THEN
  8730. Basic.ToSegmentedName(x.externalName^, name);
  8731. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8732. ELSE
  8733. GetCodeSectionNameForSymbol(x, name);
  8734. IF (x.scope.ownerModule = module.module) THEN
  8735. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8736. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8737. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8738. IF source.pc = 0 THEN (* no code yet *)
  8739. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8740. END;
  8741. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8742. bits := x.procedureScope.body.code.inlineCode;
  8743. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8744. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8745. binary.CopyBits(bits, 0, bits.GetSize());
  8746. source.SetResolved(binary);
  8747. ELSE
  8748. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8749. END;
  8750. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8751. END;
  8752. InitOperand(operand,ModeValue);
  8753. operand.op := reg;
  8754. operand.tag := tag;
  8755. IF Trace THEN TraceExit("StaticCallOperand") END;
  8756. END StaticCallOperand;
  8757. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8758. (* handle expressions of the form designator.procedure or procedure *)
  8759. BEGIN
  8760. IF Trace THEN TraceEnter("VisitProcedure") END;
  8761. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8762. DynamicCallOperand(result,x);
  8763. ELSIF x.isInline THEN
  8764. StaticCallOperand(result,x);
  8765. ELSE
  8766. StaticCallOperand(result,x);
  8767. END;
  8768. IF Trace THEN TraceExit("VisitProcedure") END;
  8769. END VisitProcedure;
  8770. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8771. BEGIN
  8772. VisitProcedure(x);
  8773. END VisitOperator;
  8774. (** statements *)
  8775. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8776. BEGIN
  8777. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8778. Expression(x.call);
  8779. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8780. ReleaseOperand(result)
  8781. END;
  8782. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8783. END VisitProcedureCallStatement;
  8784. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8785. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8786. leftBase, rightBase: SyntaxTree.Type;
  8787. procedureName,s: SyntaxTree.IdentifierString;
  8788. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8789. size: LONGINT; rightKind: LONGINT;
  8790. dummy: IntermediateCode.Operand;
  8791. CONST moduleName = "FoxArrayBase";
  8792. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8793. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8794. BEGIN
  8795. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8796. IF base IS SyntaxTree.MathArrayType THEN
  8797. base := OpenArray(base(SyntaxTree.MathArrayType));
  8798. END;
  8799. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8800. result.SetArrayBase(base);
  8801. RETURN result
  8802. END OpenArray;
  8803. BEGIN
  8804. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8805. SaveRegisters();ReleaseUsedRegisters(saved);
  8806. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8807. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8808. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8809. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8810. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8811. IF leftType.form = SyntaxTree.Tensor THEN
  8812. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8813. ELSIF leftType.form = SyntaxTree.Open THEN
  8814. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8815. ELSIF leftType.form = SyntaxTree.Static THEN
  8816. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8817. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8818. END;
  8819. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8820. IF procedure = NIL THEN
  8821. s := "Instruction not supported on target, emulation procedure ";
  8822. Strings.Append(s,moduleName);
  8823. Strings.Append(s,".");
  8824. Strings.Append(s,procedureName);
  8825. Strings.Append(s," not present");
  8826. Error(position,s);
  8827. ELSE
  8828. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8829. parameter.SetType(leftType);
  8830. parameter.SetAccess(SyntaxTree.Internal);
  8831. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8832. parameter.SetKind(rightKind);
  8833. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8834. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8835. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8836. StaticCallOperand(result,procedure);
  8837. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8838. ReleaseOperand(result);
  8839. END;
  8840. RestoreRegisters(saved);
  8841. END;
  8842. END AssignMathArray;
  8843. VAR modifyAssignmentCounter := 0: LONGINT;
  8844. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8845. VAR processor,mem,dst: IntermediateCode.Operand;
  8846. BEGIN
  8847. IF value.intValue = true.intValue THEN
  8848. INC(modifyAssignmentCounter);
  8849. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8850. modifyAssignmentsPC := section.pc;
  8851. ELSE
  8852. DEC(modifyAssignmentCounter);
  8853. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8854. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8855. END;
  8856. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8857. dst := NewRegisterOperand (addressType);
  8858. Emit(Mov(position,dst, processor));
  8859. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8860. Emit(Mov(position,mem, value));
  8861. ReleaseIntermediateOperand(dst);
  8862. END ModifyAssignments;
  8863. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8864. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8865. BEGIN
  8866. type := left.type.resolved;
  8867. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8868. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8869. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8870. IF procedureType.returnParameter = parameter THEN
  8871. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8872. END;
  8873. END;
  8874. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8875. END CopySize;
  8876. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8877. VAR
  8878. leftO, rightO: Operand;
  8879. mem, sizeOp: IntermediateCode.Operand;
  8880. leftType, rightType, componentType: SyntaxTree.Type;
  8881. size: LONGINT;
  8882. parameters: SyntaxTree.ExpressionList;
  8883. procedure: SyntaxTree.Procedure;
  8884. call: SyntaxTree.ProcedureCallDesignator;
  8885. designator: SyntaxTree.Designator;
  8886. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8887. VAR procedureType: SyntaxTree.ProcedureType;
  8888. BEGIN
  8889. IF ReturnedAsParameter(right.type) THEN
  8890. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8891. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8892. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8893. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8894. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8895. IF right.id = Global.Reshape THEN RETURN TRUE
  8896. END;
  8897. END;
  8898. END;
  8899. END;
  8900. RETURN FALSE
  8901. END CanPassAsResultParameter;
  8902. BEGIN
  8903. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8904. leftType := left.type.resolved; rightType:= right.type.resolved;
  8905. IF backend.cooperative & left.NeedsTrace() THEN
  8906. ModifyAssignments(true);
  8907. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8908. Designate(right, rightO);
  8909. Designate(left, leftO);
  8910. ASSERT(leftO.mode = ModeReference);
  8911. TransferToRegister(leftO.op, leftO.op);
  8912. TransferToRegister(rightO.op, rightO.op);
  8913. Emit(Push(position, leftO.op));
  8914. Emit(Push(position, rightO.op));
  8915. CallAssignMethod(leftO.op, rightO.op, left.type);
  8916. Emit(Pop(position, rightO.op));
  8917. Emit(Pop(position, leftO.op));
  8918. sizeOp := CopySize(left);
  8919. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8920. ReleaseOperand(leftO);
  8921. ReleaseOperand(rightO);
  8922. ELSE
  8923. Evaluate(right,rightO);
  8924. Designate(left,leftO);
  8925. ASSERT(leftO.mode = ModeReference);
  8926. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8927. (* copy procedure address first *)
  8928. MakeMemory(mem,leftO.op,addressType,0);
  8929. Emit(Mov(position,mem,rightO.op));
  8930. ReleaseIntermediateOperand(mem);
  8931. (* copy pointer address *)
  8932. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8933. CallAssignPointer(leftO.tag, rightO.tag);
  8934. ELSE
  8935. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8936. CallAssignPointer(leftO.op, rightO.op);
  8937. END;
  8938. ReleaseOperand(leftO);
  8939. ReleaseOperand(rightO);
  8940. END;
  8941. ModifyAssignments(false);
  8942. RETURN;
  8943. END;
  8944. IF CanPassAsResultParameter(right) THEN
  8945. procedureResultDesignator := left(SyntaxTree.Designator);
  8946. Expression(right);
  8947. procedureResultDesignator := NIL;
  8948. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8949. (* left <-- ALIAS OF right: zerocopy *)
  8950. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8951. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8952. designator.SetType(procedure.type);
  8953. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8954. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8955. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8956. END;
  8957. ELSIF leftType IS SyntaxTree.RangeType THEN
  8958. (* LHS is of array range type *)
  8959. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8960. Evaluate(right, rightO);
  8961. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8962. (* first *)
  8963. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8964. Emit(Mov(position,mem, rightO.op));
  8965. ReleaseIntermediateOperand(mem);
  8966. (* last *)
  8967. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8968. Emit(Mov(position,mem, rightO.tag));
  8969. ReleaseIntermediateOperand(mem);
  8970. (* step *)
  8971. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8972. Emit(Mov(position,mem, rightO.extra));
  8973. ReleaseIntermediateOperand(mem);
  8974. ReleaseOperand(rightO);
  8975. ReleaseOperand(leftO)
  8976. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8977. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8978. Evaluate(right, rightO);
  8979. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8980. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8981. (* real part *)
  8982. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8983. Emit(Mov(position,mem, rightO.op));
  8984. ReleaseIntermediateOperand(mem);
  8985. (* imaginary part *)
  8986. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8987. Emit(Mov(position,mem, rightO.tag));
  8988. ReleaseIntermediateOperand(mem);
  8989. ReleaseOperand(rightO);
  8990. ReleaseOperand(leftO)
  8991. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8992. OR (leftType IS SyntaxTree.PortType) THEN
  8993. (* rightO := leftO;*)
  8994. Evaluate(right,rightO);
  8995. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8996. Designate(left,leftO);
  8997. IF leftO.mode = ModeReference THEN
  8998. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8999. destination := mem;
  9000. ELSE
  9001. destination := leftO.op;
  9002. END;
  9003. ReleaseOperand(leftO);
  9004. IF destination.mode # IntermediateCode.Undefined THEN
  9005. Emit(Mov(position,destination,rightO.op));
  9006. END;
  9007. ReleaseOperand(rightO);
  9008. ReleaseIntermediateOperand(mem);
  9009. IntermediateCode.InitOperand(destination);
  9010. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9011. Evaluate(right,rightO);
  9012. Designate(left,leftO);
  9013. MakeMemory(mem,leftO.op,addressType,0);
  9014. Emit(Mov(position,mem,rightO.op));
  9015. ReleaseIntermediateOperand(mem);
  9016. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9017. (* delegate *)
  9018. (*
  9019. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9020. *)
  9021. Emit(Mov(position,leftO.tag,rightO.tag));
  9022. END;
  9023. ReleaseOperand(leftO);
  9024. ReleaseOperand(rightO);
  9025. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9026. Designate(right,rightO);
  9027. Designate(left,leftO);
  9028. sizeOp := CopySize(left);
  9029. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9030. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9031. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9032. IF (rightType IS SyntaxTree.StringType) THEN
  9033. CopyString(left,right);
  9034. 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
  9035. Designate(right,rightO);
  9036. Designate(left,leftO);
  9037. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9038. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9039. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9040. ELSE
  9041. HALT(201)
  9042. END;
  9043. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9044. AssignMathArray(left,right);
  9045. ELSE
  9046. HALT(200);
  9047. END;
  9048. END Assign;
  9049. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9050. BEGIN
  9051. IF Trace THEN TraceEnter("VisitAssignment") END;
  9052. Assign(x.left,x.right);
  9053. IF Trace THEN TraceExit("VisitAssignment") END;
  9054. END VisitAssignment;
  9055. PROCEDURE EmitCooperativeSwitch;
  9056. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9057. BEGIN
  9058. ASSERT (cooperativeSwitches);
  9059. pc := section.pc;
  9060. IF lastSwitchPC = section.pc THEN RETURN END;
  9061. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9062. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9063. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9064. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9065. INC(statCoopSwitch, section.pc - pc);
  9066. END EmitCooperativeSwitch;
  9067. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9068. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9069. BEGIN
  9070. p0 := communication.left; p1 := communication.right;
  9071. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9072. Evaluate(p0,s0);
  9073. Evaluate(p1,s1);
  9074. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9075. Emit(Push(position,s0.op));
  9076. Emit(Push(position,s1.op));
  9077. (*
  9078. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9079. *)
  9080. IF ~backend.cellsAreObjects THEN
  9081. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9082. END;
  9083. ReleaseOperand(s0);
  9084. ReleaseOperand(s1);
  9085. IF backend.cellsAreObjects THEN
  9086. CallThis(position,"ActiveCellsRuntime","Send",2);
  9087. ELSE
  9088. CallThis(position,ChannelModuleName,"Send",2);
  9089. END;
  9090. (* ----- RECEIVE ------*)
  9091. ELSE
  9092. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9093. tmp := p0; p0 := p1; p1 := tmp;
  9094. END;
  9095. Evaluate(p0,s0);
  9096. Emit(Push(position,s0.op));
  9097. Designate(p1,s1);
  9098. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9099. Emit(Push(position,s1.op));
  9100. (*
  9101. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9102. *)
  9103. IF ~backend.cellsAreObjects THEN
  9104. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9105. END;
  9106. ReleaseOperand(s0);
  9107. ReleaseOperand(s1);
  9108. IF backend.cellsAreObjects THEN
  9109. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9110. ELSE
  9111. CallThis(position,ChannelModuleName,"Receive",2)
  9112. END;
  9113. END;
  9114. END VisitCommunicationStatement;
  9115. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9116. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9117. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9118. VAR true, false: Label; condition, value: BOOLEAN;
  9119. BEGIN
  9120. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9121. IF condition THEN
  9122. true := NewLabel();
  9123. false := NewLabel();
  9124. Condition(if.condition,true,false);
  9125. SetLabel(true);
  9126. StatementSequence(if.statements);
  9127. BrL(end);
  9128. SetLabel(false);
  9129. ELSE
  9130. IF value THEN (* always true *)
  9131. escape := TRUE;
  9132. StatementSequence(if.statements);
  9133. (* no branch necessary -- rest skipped *)
  9134. END;
  9135. END;
  9136. END IfPart;
  9137. BEGIN
  9138. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9139. end := NewLabel();
  9140. elsifs := x.ElsifParts();
  9141. IfPart(x.ifPart);
  9142. FOR i := 0 TO elsifs-1 DO
  9143. IF ~escape THEN
  9144. elsif := x.GetElsifPart(i);
  9145. IfPart(elsif);
  9146. END;
  9147. END;
  9148. IF (x.elsePart # NIL) & ~escape THEN
  9149. StatementSequence(x.elsePart);
  9150. END;
  9151. SetLabel(end);
  9152. IF Trace THEN TraceExit("VisitIfStatement") END;
  9153. END VisitIfStatement;
  9154. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9155. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9156. BEGIN
  9157. (*IF x.variable.type.resolved = x.type.resolved THEN
  9158. (* always true, do nothing *)
  9159. ELSE*)
  9160. Designate(x.variable,res);
  9161. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9162. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9163. END;
  9164. trueL := NewLabel();
  9165. TypeTest(res.tag,x.type,trueL,falseL);
  9166. ReleaseOperand(res);
  9167. SetLabel(trueL);
  9168. StatementSequence(x.statements);
  9169. BrL(endL);
  9170. END WithPart;
  9171. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9172. VAR endL,falseL: Label;i: LONGINT;
  9173. BEGIN
  9174. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9175. endL := NewLabel();
  9176. FOR i := 0 TO x.WithParts()-1 DO
  9177. falseL := NewLabel();
  9178. WithPart(x.GetWithPart(i),falseL,endL);
  9179. SetLabel(falseL);
  9180. END;
  9181. IF x.elsePart = NIL THEN
  9182. IF ~isUnchecked THEN
  9183. EmitTrap(position,WithTrap);
  9184. END;
  9185. ELSE
  9186. StatementSequence(x.elsePart)
  9187. END;
  9188. SetLabel(endL);
  9189. IF Trace THEN TraceExit("VisitWithStatement") END;
  9190. END VisitWithStatement;
  9191. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9192. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9193. out,else: Label; label: Label;
  9194. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9195. symbol: SyntaxTree.Symbol;
  9196. BEGIN
  9197. (*! split case statement into if-elsif statements for large case label lists *)
  9198. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9199. size := x.max-x.min+1;
  9200. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9201. END;
  9202. Evaluate(x.variable,var);
  9203. ReuseCopy(tmp,var.op);
  9204. ReleaseIntermediateOperand(var.op);
  9205. var.op := tmp;
  9206. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9207. Convert(var.op,addressType);
  9208. size := x.max-x.min+1;
  9209. else := NewLabel();
  9210. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9211. (*
  9212. UniqueId(name,module.module,"case",caseId);
  9213. *)
  9214. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9215. Global.GetModuleSegmentedName(module.module, name);
  9216. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9217. symbol := SyntaxTree.NewSymbol(name[1]);
  9218. symbol.SetScope(moduleScope);
  9219. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9220. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9221. section.isCaseTable := TRUE;
  9222. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9223. ReuseCopy(res,var.op);
  9224. ReleaseOperand(var);
  9225. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9226. Emit(Add(position,res,res,jmp));
  9227. IntermediateCode.MakeMemory(res,addressType);
  9228. Emit(Br(position,res));
  9229. ReleaseIntermediateOperand(res);
  9230. out := NewLabel();
  9231. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9232. part := x.GetCasePart(i);
  9233. constant := part.firstConstant;
  9234. label := NewLabel();
  9235. SetLabel(label);
  9236. WHILE(constant # NIL) DO (* case labels for this case part *)
  9237. FOR j := constant.min TO constant.max DO
  9238. fixups[j-x.min] := label;
  9239. END;
  9240. constant := constant.next;
  9241. END;
  9242. StatementSequence(part.statements);
  9243. BrL(out);
  9244. END;
  9245. SetLabel(else);
  9246. FOR i := 0 TO size-1 DO
  9247. IF fixups[i] = NIL THEN
  9248. fixups[i] := else;
  9249. END;
  9250. END;
  9251. IF x.elsePart # NIL THEN
  9252. StatementSequence(x.elsePart);
  9253. ELSIF ~isUnchecked THEN
  9254. EmitTrap(position,CaseTrap);
  9255. END;
  9256. SetLabel(out);
  9257. FOR i := 0 TO size-1 DO
  9258. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9259. section.Emit(Data(position,op));
  9260. END;
  9261. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9262. END VisitCaseStatement;
  9263. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9264. VAR start: Label; true,false: Label;
  9265. BEGIN
  9266. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9267. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9268. start := NewLabel();
  9269. true := NewLabel();
  9270. false := NewLabel();
  9271. SetLabel(start);
  9272. Condition(x.condition,true,false);
  9273. SetLabel(true);
  9274. StatementSequence(x.statements);
  9275. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9276. BrL(start);
  9277. SetLabel(false);
  9278. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9279. END VisitWhileStatement;
  9280. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9281. VAR false,true: Label;
  9282. BEGIN
  9283. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9284. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9285. true := NewLabel();
  9286. false := NewLabel();
  9287. SetLabel(false);
  9288. StatementSequence(x.statements);
  9289. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9290. Condition(x.condition,true,false);
  9291. SetLabel(true);
  9292. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9293. END VisitRepeatStatement;
  9294. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9295. VAR
  9296. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9297. temporaryVariable: SyntaxTree.Variable;
  9298. temporaryVariableDesignator : SyntaxTree.Designator;
  9299. BEGIN
  9300. IF Trace THEN TraceEnter("VisitForStatement") END;
  9301. true := NewLabel();
  9302. false := NewLabel();
  9303. start := NewLabel();
  9304. Assign(x.variable,x.from);
  9305. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9306. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9307. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9308. Assign(temporaryVariableDesignator,x.to);
  9309. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9310. IF by > 0 THEN
  9311. cmp := Scanner.LessEqual
  9312. ELSE
  9313. cmp := Scanner.GreaterEqual
  9314. END;
  9315. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9316. binary.SetType(system.booleanType);
  9317. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9318. SetLabel(start);
  9319. Condition(binary,true,false);
  9320. SetLabel(true);
  9321. StatementSequence(x.statements);
  9322. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9323. binary.SetType(x.variable.type);
  9324. Assign(x.variable,binary);
  9325. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9326. BrL(start);
  9327. SetLabel(false);
  9328. IF Trace THEN TraceExit("VisitForStatement") END;
  9329. END VisitForStatement;
  9330. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9331. VAR prevLoop: Label;
  9332. BEGIN
  9333. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9334. prevLoop := currentLoop;
  9335. currentLoop := NewLabel();
  9336. StatementSequence(x.statements);
  9337. SetLabel(currentLoop);
  9338. currentLoop := prevLoop;
  9339. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9340. END VisitExitableBlock;
  9341. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9342. VAR prevLoop,start: Label;
  9343. BEGIN
  9344. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9345. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9346. start := NewLabel();
  9347. prevLoop := currentLoop;
  9348. SetLabel(start);
  9349. currentLoop := NewLabel();
  9350. StatementSequence(x.statements);
  9351. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9352. BrL(start);
  9353. SetLabel(currentLoop);
  9354. currentLoop := prevLoop;
  9355. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9356. END VisitLoopStatement;
  9357. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9358. VAR outer: SyntaxTree.Statement;
  9359. BEGIN
  9360. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9361. IF locked THEN (* r if we jump out of an exclusive block *)
  9362. outer := x.outer;
  9363. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9364. outer := outer.outer;
  9365. END;
  9366. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9367. Lock(FALSE);
  9368. END;
  9369. END;
  9370. BrL(currentLoop);
  9371. IF Trace THEN TraceExit("VisitExitStatement") END;
  9372. END VisitExitStatement;
  9373. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9374. VAR
  9375. expression, parameterDesignator: SyntaxTree.Expression;
  9376. type, componentType: SyntaxTree.Type;
  9377. res, right: Operand;
  9378. left, mem, reg: IntermediateCode.Operand;
  9379. parameter: SyntaxTree.Parameter;
  9380. procedure: SyntaxTree.Procedure;
  9381. procedureType: SyntaxTree.ProcedureType;
  9382. returnTypeOffset: LONGINT;
  9383. delegate: BOOLEAN;
  9384. map: SymbolMap;
  9385. cc, parametersSize: LONGINT;
  9386. BEGIN
  9387. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9388. expression := x.returnValue;
  9389. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9390. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9391. IF currentIsInline THEN
  9392. IF expression # NIL THEN
  9393. map := currentMapper.Get(NIL);
  9394. IF map # NIL THEN
  9395. Assign(map.to, expression);
  9396. END;
  9397. END;
  9398. BrL(currentInlineExit);
  9399. RETURN;
  9400. END;
  9401. IF expression # NIL THEN
  9402. type := expression.type.resolved;
  9403. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9404. IF locked THEN Lock(FALSE) END;
  9405. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9406. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9407. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9408. (* return without structured return parameter *)
  9409. Evaluate(expression,res);
  9410. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9411. IF locked OR profile THEN
  9412. Emit(Push(position,res.op));
  9413. IF delegate THEN HALT(200) END;
  9414. ReleaseOperand(res);
  9415. IF locked THEN Lock(FALSE) END;
  9416. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9417. reg := NewRegisterOperand(res.op.type);
  9418. Emit(Pop(position,reg));
  9419. Emit(Return(position,reg));
  9420. ReleaseIntermediateOperand(reg);
  9421. ELSE
  9422. Emit(Return(position,res.op));
  9423. ReleaseOperand(res);
  9424. END;
  9425. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9426. THEN
  9427. (* return using structured return parameter *)
  9428. 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)
  9429. OR SemanticChecker.IsPointerType(type));
  9430. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9431. parameter :=procedureType.returnParameter;
  9432. ASSERT(parameter # NIL);
  9433. returnTypeOffset := parameter.offsetInBits;
  9434. (*
  9435. IF parameter# NIL THEN
  9436. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9437. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9438. ELSE
  9439. returnTypeOffset := system.offsetFirstParameter
  9440. END;
  9441. *)
  9442. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9443. IF type IS SyntaxTree.RangeType THEN
  9444. (* array range type *)
  9445. Evaluate(expression, right);
  9446. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9447. Emit(Mov(position,mem, right.op)); (* first *)
  9448. ReleaseIntermediateOperand(mem);
  9449. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9450. Emit(Mov(position,mem, right.tag)); (* last *)
  9451. ReleaseIntermediateOperand(mem);
  9452. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9453. Emit(Mov(position,mem, right.extra)); (* step *)
  9454. ReleaseIntermediateOperand(mem);
  9455. ReleaseOperand(right);
  9456. ELSIF type IS SyntaxTree.ComplexType THEN
  9457. Evaluate(expression, right);
  9458. componentType := type(SyntaxTree.ComplexType).componentType;
  9459. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9460. Emit(Mov(position,mem, right.op)); (* real part *)
  9461. ReleaseIntermediateOperand(mem);
  9462. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9463. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9464. ReleaseIntermediateOperand(mem);
  9465. ReleaseOperand(right);
  9466. ELSE (* covers cases: pointer / record / array *)
  9467. parameter := procedureType.returnParameter;
  9468. checker.SetCurrentScope(currentScope);
  9469. ASSERT(parameter # NIL);
  9470. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9471. Assign(parameterDesignator,expression);
  9472. END;
  9473. ReleaseIntermediateOperand(left);
  9474. IF locked THEN Lock(FALSE) END;
  9475. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9476. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9477. parameter := procedureType.returnParameter;
  9478. checker.SetCurrentScope(currentScope);
  9479. IF parameter = NIL THEN
  9480. Error(procedure.position, "structured return of parameter of procedure not found");
  9481. ELSE
  9482. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9483. Assign(parameterDesignator,expression);
  9484. END;
  9485. IF locked THEN Lock(FALSE) END;
  9486. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9487. ELSE
  9488. HALT(200);
  9489. END;
  9490. ELSE
  9491. IF locked THEN Lock(FALSE) END;
  9492. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9493. END;
  9494. IF backend.cooperative THEN
  9495. BrL(exitLabel);
  9496. ELSE
  9497. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9498. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9499. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9500. ELSE
  9501. parametersSize := 0;
  9502. END;
  9503. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9504. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9505. END;
  9506. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9507. END VisitReturnStatement;
  9508. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9509. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9510. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9511. statements: SyntaxTree.StatementSequence;
  9512. name, suffix: SyntaxTree.IdentifierString;
  9513. BEGIN
  9514. Strings.IntToStr(awaitProcCounter,suffix);
  9515. Strings.Concat("@AwaitProcedure",suffix,name);
  9516. identifier := SyntaxTree.NewIdentifier(name);
  9517. INC(awaitProcCounter);
  9518. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9519. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9520. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9521. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9522. procedure.SetAccess(SyntaxTree.Hidden);
  9523. procedure.SetScope(currentScope);
  9524. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9525. procedureType.SetReturnType(system.booleanType);
  9526. procedure.SetType(procedureType);
  9527. body := SyntaxTree.NewBody(x.position,procedureScope);
  9528. procedureScope.SetBody(body);
  9529. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9530. returnStatement.SetReturnValue(x.condition);
  9531. statements := SyntaxTree.NewStatementSequence();
  9532. statements.AddStatement(returnStatement);
  9533. body.SetStatementSequence(statements);
  9534. currentScope.AddProcedure(procedure);
  9535. RETURN procedure
  9536. END MakeAwaitProcedure;
  9537. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9538. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9539. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9540. BEGIN
  9541. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9542. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9543. IF backend.cooperative THEN
  9544. start := NewLabel();
  9545. true := NewLabel();
  9546. false := NewLabel();
  9547. SetLabel(start);
  9548. Condition(x.condition,true,false);
  9549. SetLabel(false);
  9550. PushSelfPointer();
  9551. CallThis(position,"ExclusiveBlocks","Await",1);
  9552. BrL(start);
  9553. SetLabel(true);
  9554. PushSelfPointer();
  9555. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9556. ELSE
  9557. proc := MakeAwaitProcedure(x);
  9558. Emit(Push(position,fp));
  9559. GetCodeSectionNameForSymbol(proc,name);
  9560. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9561. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9562. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9563. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9564. Emit(Result(position,res));
  9565. (*
  9566. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9567. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9568. *)
  9569. InitOperand(result,ModeValue);
  9570. result.op := res;
  9571. label := NewLabel();
  9572. BreqL(label, result.op, SELF.true);
  9573. ReleaseOperand(result);
  9574. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9575. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9576. Emit(Push(position,res));
  9577. Emit(Push(position,fp));
  9578. PushSelfPointer();
  9579. Emit(Push(position,nil));
  9580. CallThis(position,"Objects","Await",4);
  9581. SetLabel(label);
  9582. END;
  9583. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9584. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9585. END VisitAwaitStatement;
  9586. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9587. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9588. BEGIN
  9589. FOR i := 0 TO x.Length() - 1 DO
  9590. statement := x.GetStatement( i );
  9591. Statement(statement);
  9592. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9593. END;
  9594. END StatementSequence;
  9595. PROCEDURE PushSelfPointer;
  9596. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9597. procedureType: SyntaxTree.ProcedureType;
  9598. BEGIN
  9599. scope := currentScope;
  9600. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9601. scope := scope.outerScope;
  9602. END;
  9603. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9604. IF ~newObjectFile THEN
  9605. Symbol(moduleSelf,op);
  9606. IntermediateCode.MakeMemory(op.op,addressType);
  9607. ELSE
  9608. moduleSection := meta.ModuleSection();
  9609. IF backend.cooperative THEN
  9610. moduleOffset := 0;
  9611. ELSE
  9612. moduleOffset := moduleSection.pc;
  9613. END;
  9614. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9615. END;
  9616. ELSE
  9617. GetBaseRegister(op.op,currentScope,scope);
  9618. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9619. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9620. parametersSize := ProcedureParametersSize(system,procedure);
  9621. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9622. IF backend.cooperative THEN
  9623. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9624. END;
  9625. IntermediateCode.MakeMemory(op.op,addressType);
  9626. END;
  9627. Emit(Push(position,op.op));
  9628. ReleaseOperand(op);
  9629. END PushSelfPointer;
  9630. PROCEDURE Lock(lock: BOOLEAN);
  9631. BEGIN
  9632. IF Trace THEN TraceEnter("Lock") END;
  9633. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9634. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9635. ASSERT(modifyAssignmentCounter = 0);
  9636. IF dump # NIL THEN
  9637. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9638. dump.Ln;dump.Update;
  9639. END;
  9640. PushSelfPointer;
  9641. IF backend.cooperative THEN
  9642. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9643. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9644. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9645. END;
  9646. ELSE
  9647. Emit(Push(position,true));
  9648. IF lock THEN CallThis(position,"Objects","Lock",2)
  9649. ELSE CallThis(position,"Objects","Unlock",2);
  9650. END;
  9651. END;
  9652. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9653. IF Trace THEN TraceExit("Lock") END;
  9654. END Lock;
  9655. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9656. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9657. BEGIN
  9658. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9659. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9660. previouslyUnchecked := isUnchecked;
  9661. isUnchecked := isUnchecked OR x.isUnchecked;
  9662. previouslyCooperativeSwitches := cooperativeSwitches;
  9663. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9664. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9665. IF x.statements # NIL THEN
  9666. StatementSequence(x.statements);
  9667. END;
  9668. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9669. isUnchecked := previouslyUnchecked;
  9670. cooperativeSwitches := previouslyCooperativeSwitches;
  9671. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9672. END VisitStatementBlock;
  9673. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9674. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9675. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9676. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9677. procedure: SyntaxTree.Procedure;
  9678. map: SymbolMap;
  9679. cc, parametersSize: LONGINT;
  9680. BEGIN
  9681. scope := currentScope;
  9682. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9683. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9684. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9685. return := emptyOperand;
  9686. IF Trace THEN TraceEnter("VisitCode") END;
  9687. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9688. NEW(in, x.inRules.Length());
  9689. FOR i := 0 TO LEN(in)-1 DO
  9690. statement := x.inRules.GetStatement(i);
  9691. WITH statement: SyntaxTree.Assignment DO
  9692. Evaluate(statement.right, operand);
  9693. result := operand.op;
  9694. NEW(str, 64);
  9695. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9696. in[i] := result; IntermediateCode.SetString(in[i], str);
  9697. ReleaseIntermediateOperand(operand.tag);
  9698. END;
  9699. END;
  9700. ELSE in := NIL
  9701. END;
  9702. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9703. NEW(out, x.outRules.Length());
  9704. FOR i := 0 TO LEN(out)-1 DO
  9705. statement := x.outRules.GetStatement(i);
  9706. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9707. WITH statement: SyntaxTree.Assignment DO
  9708. Designate(statement.left, operand);
  9709. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9710. NEW(str, 64);
  9711. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9712. out[i] := result; IntermediateCode.SetString(out[i], str);
  9713. ReleaseIntermediateOperand(operand.tag);
  9714. |statement: SyntaxTree.ReturnStatement DO
  9715. NEW(str, 64);
  9716. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9717. IF currentIsInline THEN
  9718. map := currentMapper.Get(NIL);
  9719. Designate(map.to, operand);
  9720. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9721. (*Evaluate(map.to, operand);*)
  9722. out[i] := result;
  9723. ELSE
  9724. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9725. END;
  9726. IntermediateCode.SetString(out[i], str);
  9727. ReleaseIntermediateOperand(operand.tag);
  9728. return := out[i];
  9729. ELSE
  9730. END;
  9731. END;
  9732. ELSE out := NIL
  9733. END;
  9734. Emit(Asm(x.position,x.sourceCode, in, out));
  9735. IF in # NIL THEN
  9736. FOR i := 0 TO LEN(in)-1 DO
  9737. ReleaseIntermediateOperand(in[i]);
  9738. END;
  9739. END;
  9740. IF out # NIL THEN
  9741. FOR i := 0 TO LEN(out)-1 DO
  9742. WITH statement: SyntaxTree.Assignment DO
  9743. ReleaseIntermediateOperand(out[i]);
  9744. |statement: SyntaxTree.ReturnStatement DO
  9745. IF currentIsInline THEN
  9746. ReleaseIntermediateOperand(out[i]);
  9747. END;
  9748. ELSE
  9749. END;
  9750. statement := x.outRules.GetStatement(i);
  9751. END;
  9752. END;
  9753. IF return.mode # IntermediateCode.Undefined THEN
  9754. IF currentIsInline THEN
  9755. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9756. Symbol(procedureType.returnParameter, par);
  9757. MakeMemory(mem, par.op, return.type, 0);
  9758. ReleaseOperand(par);
  9759. Emit(Mov(position, mem, return));
  9760. ReleaseIntermediateOperand(mem);
  9761. ELSE
  9762. Emit(Return(position,return));
  9763. END;
  9764. IF currentIsInline THEN RETURN END;
  9765. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9766. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9767. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9768. ELSE
  9769. parametersSize := 0;
  9770. END;
  9771. EmitLeave(section, position,NIL, cc);
  9772. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9773. ReleaseIntermediateOperand(return);
  9774. END;
  9775. IF Trace THEN TraceExit("VisitCode") END;
  9776. END VisitCode;
  9777. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9778. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9779. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9780. const, call: IntermediateCode.Operand;
  9781. parameterDesignator: SyntaxTree.Expression;
  9782. saved: RegisterEntry;
  9783. name: Basic.SegmentedName;
  9784. BEGIN
  9785. IF Trace THEN TraceEnter("ParameterCopies") END;
  9786. parameter := x.firstParameter;
  9787. WHILE parameter # NIL DO
  9788. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9789. type := parameter.type.resolved;
  9790. IF IsOpenArray(type) THEN
  9791. VisitParameter(parameter);
  9792. op := result;
  9793. IF backend.cooperative & parameter.NeedsTrace() THEN
  9794. length := GetArrayLength(type, op.tag);
  9795. size := NewRegisterOperand(addressType);
  9796. base := ArrayBaseType(type);
  9797. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9798. Emit(Mul(position, size, length, const));
  9799. dst := NewRegisterOperand (addressType);
  9800. Emit(Mov(position, dst, size));
  9801. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9802. Emit(Sub(position,dst,sp,dst));
  9803. Emit(And(position,dst,dst,const));
  9804. Emit(Mov(position,sp,dst));
  9805. par := fp;
  9806. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9807. IntermediateCode.InitImmediate(null, byteType, 0);
  9808. Emit(Fill(position, dst, size, null));
  9809. ReleaseIntermediateOperand(dst);
  9810. ReleaseIntermediateOperand(length);
  9811. SaveRegisters();ReleaseUsedRegisters(saved);
  9812. (* register dst has been freed before SaveRegisters already *)
  9813. base := ArrayBaseType(type);
  9814. (* assign method of open array *)
  9815. IF base.IsRecordType() THEN
  9816. Emit (Push(position, length));
  9817. Emit (Push(position, dst));
  9818. Emit (Push(position, op.op));
  9819. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9820. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9821. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9822. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9823. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9824. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9825. Emit (Push(position,length));
  9826. Emit (Push(position, dst));
  9827. Emit (Push(position, length));
  9828. Emit (Push(position, op.op));
  9829. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9830. ELSE
  9831. Emit (Push(position, length));
  9832. Emit (Push(position, dst));
  9833. Emit (Push(position, length));
  9834. Emit (Push(position, op.op));
  9835. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9836. ASSERT(ArrayBaseType(type).IsPointer());
  9837. END;
  9838. RestoreRegisters(saved);
  9839. ELSE
  9840. temp := GetDynamicSize(type,op.tag);
  9841. ReuseCopy(size,temp);
  9842. ReleaseIntermediateOperand(temp);
  9843. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9844. Emit(Sub(position,size,sp,size));
  9845. Emit(And(position,size,size,const));
  9846. Emit(Mov(position,sp,size));
  9847. par := fp;
  9848. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9849. ReleaseIntermediateOperand(size);
  9850. size := GetDynamicSize(type,op.tag);
  9851. END;
  9852. Emit(Copy(position,sp,op.op,size));
  9853. ReleaseIntermediateOperand(size);
  9854. ReleaseOperand(op);
  9855. IntermediateCode.MakeMemory(par,addressType);
  9856. Emit(Mov(position,par,sp));
  9857. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9858. checker.SetCurrentScope(currentScope);
  9859. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9860. Assign(parameterDesignator,parameterDesignator);
  9861. END;
  9862. END;
  9863. parameter := parameter.nextParameter;
  9864. END;
  9865. IF Trace THEN TraceExit("ParameterCopies") END;
  9866. END ParameterCopies;
  9867. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9868. VAR x: SyntaxTree.Variable;
  9869. BEGIN
  9870. x := scope.firstVariable;
  9871. WHILE x # NIL DO
  9872. InitVariable(x);
  9873. x := x.nextVariable;
  9874. END;
  9875. END InitVariables;
  9876. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9877. BEGIN
  9878. IF (symbol # NIL) THEN
  9879. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9880. ELSE
  9881. RETURN 0
  9882. END;
  9883. END GetFingerprint;
  9884. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9885. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9886. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9887. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9888. name: Basic.SegmentedName;
  9889. offset: LONGINT;
  9890. BEGIN
  9891. IF Trace THEN TraceEnter("Body") END;
  9892. ReleaseUsedRegisters(saved); (* just in case ... *)
  9893. section := ir;
  9894. exitLabel := NewLabel ();
  9895. IF moduleBody THEN moduleBodySection := section END;
  9896. IF ir.comments # NIL THEN
  9897. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9898. commentPrintout.SingleStatement(TRUE);
  9899. dump := ir.comments;
  9900. ELSE
  9901. commentPrintout := NIL;
  9902. dump := NIL;
  9903. END;
  9904. prevScope := currentScope;
  9905. currentScope := scope;
  9906. lastSwitchPC := 0;
  9907. cooperativeSwitches := backend.cooperative;
  9908. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9909. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9910. IF x # NIL THEN
  9911. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9912. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9913. IF moduleBody THEN
  9914. ProfilerInit();
  9915. ELSE
  9916. Basic.SegmentedNameToString(ir.name, string);
  9917. ProfilerAddProcedure(numberProcedures,string);
  9918. ProfilerEnterExit(numberProcedures,TRUE);
  9919. END;
  9920. END;
  9921. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9922. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9923. END;
  9924. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9925. IF moduleBody & ~newObjectFile THEN
  9926. InitVariables(moduleScope)
  9927. END;
  9928. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9929. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9930. IF procedure = cellScope.bodyProcedure THEN
  9931. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9932. StaticCallOperand(op, cellScope.constructor);
  9933. Emit(Call(position,op.op,0));
  9934. END;
  9935. END;
  9936. END;
  9937. ParameterCopies(procedureType);
  9938. InitVariables(scope);
  9939. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9940. GetCodeSectionNameForSymbol(procedure, name);
  9941. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9942. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9943. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9944. IntermediateCode.SetOffset(right,offset); (* tag *)
  9945. IntermediateCode.InitMemory(left,addressType,fp,0);
  9946. Emit(Mov(position, left, right));
  9947. END;
  9948. IF x.code = NIL THEN
  9949. VisitStatementBlock(x);
  9950. ELSE
  9951. VisitCode(x.code)
  9952. END;
  9953. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9954. ir.SetFinally(ir.pc);
  9955. StatementSequence(x.finally)
  9956. END;
  9957. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9958. IF ~backend.cooperative THEN
  9959. ProfilerEnterExit(numberProcedures,FALSE);
  9960. END;
  9961. INC(numberProcedures);
  9962. END;
  9963. END;
  9964. IF backend.cooperative THEN
  9965. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9966. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9967. END;
  9968. IF x # NIL THEN
  9969. SELF.position := x.position;
  9970. END;
  9971. CheckRegistersFree();
  9972. ASSERT(modifyAssignmentCounter = 0);
  9973. currentScope := prevScope;
  9974. IF Trace THEN TraceExit("Body") END;
  9975. END Body;
  9976. END ImplementationVisitor;
  9977. MetaDataGenerator=OBJECT
  9978. VAR
  9979. implementationVisitor: ImplementationVisitor;
  9980. declarationVisitor: DeclarationVisitor;
  9981. module: Sections.Module;
  9982. moduleName: ARRAY 128 OF CHAR;
  9983. moduleNamePool: Basic.HashTableInt;
  9984. moduleNamePoolSection: IntermediateCode.Section;
  9985. modulePointerSection: IntermediateCode.Section;
  9986. modulePointerSizePC: LONGINT;
  9987. modulePointerSectionOffset: LONGINT;
  9988. modulePointers: LONGINT;
  9989. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9990. RecordBaseOffset: LONGINT;
  9991. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9992. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9993. TypeTags: LONGINT; (* type extension level support *)
  9994. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9995. patchInfoPC: LONGINT;
  9996. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9997. BEGIN
  9998. IF implementationVisitor.backend.cooperative THEN
  9999. TypeTags := MAX(LONGINT);
  10000. BaseTypesTableOffset := 0;
  10001. MethodTableOffset := 2;
  10002. TypeRecordBaseOffset := 0;
  10003. RecordBaseOffset := 0;
  10004. ELSIF simple THEN
  10005. TypeTags := 3; (* only 3 extensions allowed *)
  10006. BaseTypesTableOffset := 1;
  10007. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10008. TypeRecordBaseOffset := 0;
  10009. RecordBaseOffset := 1;
  10010. ELSE
  10011. TypeTags := 16;
  10012. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10013. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10014. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10015. RecordBaseOffset := 8;
  10016. END;
  10017. SELF.simple := simple;
  10018. SELF.implementationVisitor := implementationVisitor;
  10019. SELF.declarationVisitor := declarationVisitor;
  10020. implementationVisitor.meta := SELF;
  10021. declarationVisitor.meta := SELF;
  10022. END InitMetaDataGenerator;
  10023. PROCEDURE SetModule(module: Sections.Module);
  10024. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10025. BEGIN
  10026. SELF.module := module;
  10027. Global.GetModuleName(module.module,moduleName);
  10028. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10029. NEW(moduleNamePool, 32);
  10030. (*! require GC protection *)
  10031. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10032. name := "Heaps.AnyPtr";
  10033. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10034. (* set base pointer *)
  10035. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10036. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10037. modulePointers := 0;
  10038. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10039. AddPointer(moduleNamePoolSection, namePoolOffset);
  10040. END;
  10041. END SetModule;
  10042. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10043. BEGIN
  10044. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10045. INC(modulePointers);
  10046. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10047. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10048. END AddPointer;
  10049. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10050. BEGIN
  10051. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10052. END GetTypeRecordBaseOffset;
  10053. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10054. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10055. BEGIN
  10056. INC(dataAdrOffset,6);
  10057. Info(section,"headerAdr");
  10058. Address(section,0);
  10059. Info(section,"typeDesc");
  10060. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10061. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10062. NamedSymbol(section, name, symbol, 0, offset);
  10063. Info(section,"mark: LONGINT;");
  10064. Longint(section,-1);
  10065. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10066. Info(section,"dataAdr-: ADDRESS");
  10067. Symbol(section,section, dataAdrOffset,0);
  10068. Info(section,"size-: SIZE");
  10069. Address(section,0);
  10070. Info(section,"nextRealtime: HeapBlock;");
  10071. Address(section,0);
  10072. END HeapBlock;
  10073. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10074. VAR i: LONGINT;
  10075. BEGIN
  10076. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10077. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10078. Info(section,"count*: LONGINT");
  10079. Longint(section,0);
  10080. Info(section,"locked*: BOOLEAN");
  10081. Longint(section,0);
  10082. Info(section,"awaitingLock*: ProcessQueue");
  10083. Address(section,0);
  10084. Address(section,0);
  10085. Info(section,"awaitingCond*: ProcessQueue");
  10086. Address(section,0);
  10087. Address(section,0);
  10088. Info(section,"lockedBy*: ANY");
  10089. Address(section,0);
  10090. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10091. Longint(section,1);
  10092. FOR i := 2 TO 6 DO
  10093. Longint(section,0);
  10094. END;
  10095. Info(section,"lock*: ANY");
  10096. Address(section,0);
  10097. END ProtectedHeapBlock;
  10098. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10099. BEGIN
  10100. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10101. END Info;
  10102. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10103. VAR op: IntermediateCode.Operand;
  10104. BEGIN
  10105. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10106. section.Emit(Data(Basic.invalidPosition,op));
  10107. END Address;
  10108. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10109. VAR op: IntermediateCode.Operand;
  10110. BEGIN
  10111. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10112. section.Emit(Data(Basic.invalidPosition,op));
  10113. END Size;
  10114. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10115. VAR op: IntermediateCode.Operand;
  10116. BEGIN
  10117. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10118. section.Emit(Data(Basic.invalidPosition,op));
  10119. END Set;
  10120. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10121. VAR op: IntermediateCode.Operand;
  10122. BEGIN
  10123. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10124. section.Emit(Data(Basic.invalidPosition,op));
  10125. END Longint;
  10126. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10127. VAR op,noOperand: IntermediateCode.Operand;
  10128. BEGIN
  10129. IntermediateCode.InitOperand(noOperand);
  10130. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10131. section.PatchOperands(pc,op,noOperand,noOperand);
  10132. END PatchAddress;
  10133. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10134. VAR op,noOperand: IntermediateCode.Operand;
  10135. BEGIN
  10136. IntermediateCode.InitOperand(noOperand);
  10137. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10138. section.PatchOperands(pc,op,noOperand,noOperand);
  10139. END PatchSize;
  10140. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10141. VAR op,noOperand: IntermediateCode.Operand;
  10142. BEGIN
  10143. IntermediateCode.InitOperand(noOperand);
  10144. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10145. section.PatchOperands(pc,op,noOperand,noOperand);
  10146. END PatchLongint;
  10147. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10148. VAR op, noOperand: IntermediateCode.Operand;
  10149. BEGIN
  10150. IntermediateCode.InitOperand(noOperand);
  10151. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10152. section.PatchOperands(pc,op,noOperand,noOperand);
  10153. END PatchSymbol;
  10154. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10155. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10156. BEGIN
  10157. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10158. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10159. section.Emit(Data(Basic.invalidPosition,op));
  10160. END Boolean;
  10161. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10162. VAR op: IntermediateCode.Operand;
  10163. BEGIN
  10164. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10165. section.Emit(Data(Basic.invalidPosition,op));
  10166. END Char;
  10167. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10168. VAR op: IntermediateCode.Operand;
  10169. BEGIN
  10170. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10171. section.Emit(Data(Basic.invalidPosition,op));
  10172. END Integer;
  10173. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10174. VAR i: LONGINT;
  10175. BEGIN
  10176. Info(section,str);
  10177. i := 0;
  10178. WHILE(str[i] # 0X) DO
  10179. Char(section,str[i]);
  10180. INC(i);
  10181. END;
  10182. Char(section,0X);
  10183. END String;
  10184. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10185. VAR s: Basic.SectionName;
  10186. BEGIN
  10187. StringPool.GetString(str, s);
  10188. String(section, s);
  10189. END String0;
  10190. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10191. VAR op: IntermediateCode.Operand;
  10192. BEGIN
  10193. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10194. IntermediateCode.SetOffset(op,realOffset);
  10195. section.Emit(Data(Basic.invalidPosition,op));
  10196. END NamedSymbol;
  10197. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10198. VAR op: IntermediateCode.Operand;
  10199. BEGIN
  10200. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10201. IntermediateCode.SetOffset(op,realOffset);
  10202. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10203. END NamedSymbolAt;
  10204. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10205. BEGIN
  10206. IF symbol= NIL THEN
  10207. Address( section, realOffset);
  10208. ASSERT(virtualOffset = 0);
  10209. ELSE
  10210. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10211. END;
  10212. END Symbol;
  10213. (* OutPointers delivers
  10214. {pointerOffset}
  10215. *)
  10216. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10217. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10218. BEGIN
  10219. type := type.resolved;
  10220. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10221. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10222. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10223. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10224. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10225. ELSIF type IS SyntaxTree.PointerType THEN
  10226. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10227. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10228. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10229. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10230. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10231. ELSIF (type IS SyntaxTree.RecordType) THEN
  10232. (* never treat a record like a pointer, even if the pointer field is set! *)
  10233. WITH type: SyntaxTree.RecordType DO
  10234. base := type.GetBaseRecord();
  10235. IF base # NIL THEN
  10236. Pointers(offset,symbol,section, base,numberPointers);
  10237. END;
  10238. variable := type.recordScope.firstVariable;
  10239. WHILE(variable # NIL) DO
  10240. IF ~(variable.untraced) THEN
  10241. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10242. END;
  10243. variable := variable.nextVariable;
  10244. END;
  10245. END;
  10246. ELSIF (type IS SyntaxTree.CellType) THEN
  10247. WITH type: SyntaxTree.CellType DO
  10248. base := type.GetBaseRecord();
  10249. IF base # NIL THEN
  10250. Pointers(offset,symbol,section, base,numberPointers);
  10251. END;
  10252. variable := type.cellScope.firstVariable;
  10253. WHILE(variable # NIL) DO
  10254. IF ~(variable.untraced) THEN
  10255. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10256. END;
  10257. variable := variable.nextVariable;
  10258. END;
  10259. property := type.firstProperty;
  10260. WHILE(property # NIL) DO
  10261. IF ~(property.untraced) THEN
  10262. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10263. END;
  10264. property := property.nextProperty;
  10265. END;
  10266. parameter := type.firstParameter;
  10267. WHILE(parameter # NIL) DO
  10268. IF ~(parameter.untraced) THEN
  10269. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10270. END;
  10271. parameter := parameter.nextParameter;
  10272. END;
  10273. END;
  10274. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10275. WITH type: SyntaxTree.ArrayType DO
  10276. IF type.form= SyntaxTree.Static THEN
  10277. n := type.staticLength;
  10278. base := type.arrayBase.resolved;
  10279. WHILE(base IS SyntaxTree.ArrayType) DO
  10280. type := base(SyntaxTree.ArrayType);
  10281. n := n* type.staticLength;
  10282. base := type.arrayBase.resolved;
  10283. END;
  10284. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10285. IF SemanticChecker.ContainsPointer(base) THEN
  10286. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10287. FOR i := 0 TO n-1 DO
  10288. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10289. END;
  10290. END;
  10291. ELSE
  10292. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10293. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10294. END;
  10295. END;
  10296. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10297. WITH type: SyntaxTree.MathArrayType DO
  10298. IF type.form = SyntaxTree.Static THEN
  10299. n := type.staticLength;
  10300. base := type.arrayBase.resolved;
  10301. WHILE(base IS SyntaxTree.MathArrayType) DO
  10302. type := base(SyntaxTree.MathArrayType);
  10303. n := n* type.staticLength;
  10304. base := type.arrayBase.resolved;
  10305. END;
  10306. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10307. IF SemanticChecker.ContainsPointer(base) THEN
  10308. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10309. FOR i := 0 TO n-1 DO
  10310. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10311. END;
  10312. END;
  10313. ELSE
  10314. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10315. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10316. END
  10317. END;
  10318. (* ELSE no pointers in type *)
  10319. END;
  10320. END Pointers;
  10321. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10322. VAR position,i: LONGINT; ch: CHAR;
  10323. BEGIN
  10324. IF pool.Has(index) THEN
  10325. RETURN pool.GetInt(index)
  10326. ELSE
  10327. position := source.pc;
  10328. pool.PutInt(index, position);
  10329. Info(source, name);
  10330. i := 0;
  10331. REPEAT
  10332. ch := name[i]; INC(i);
  10333. Char( source, ch);
  10334. UNTIL ch = 0X;
  10335. END;
  10336. RETURN position;
  10337. END EnterDynamicName;
  10338. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10339. VAR name: Basic.SectionName; position: LONGINT;
  10340. BEGIN
  10341. IF pool.Has(index) THEN
  10342. RETURN pool.GetInt(index)
  10343. ELSE
  10344. StringPool.GetString(index, name);
  10345. position := EnterDynamicName(source,name,index, pool);
  10346. END;
  10347. RETURN position;
  10348. END DynamicName;
  10349. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10350. VAR section: IntermediateCode.Section;
  10351. BEGIN
  10352. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10353. IF implementationVisitor.backend.cooperative THEN
  10354. Info(section, "TypeDescriptor");
  10355. Basic.ToSegmentedName("BaseTypes.Array", name);
  10356. NamedSymbol(section, name,NIL, 0, 0);
  10357. BasePointer(section);
  10358. offset := 0;
  10359. ELSE
  10360. HeapBlock(mName,typeName,section,2);
  10361. Info(section, "HeapBlock");
  10362. (*
  10363. Symbol(section,section,2,0);
  10364. *)
  10365. Address(section,0); (* empty such that GC does not go on traversing *)
  10366. Info(section, "TypeDescriptor");
  10367. Address(section,0);
  10368. offset := section.pc;
  10369. END;
  10370. RETURN section
  10371. END NamedBlock;
  10372. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10373. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10374. BEGIN
  10375. COPY(moduleName,name);
  10376. Strings.Append(name,suffix);
  10377. Basic.ToSegmentedName(name, pooledName);
  10378. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10379. END Block;
  10380. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10381. VAR name: Basic.SegmentedName;
  10382. BEGIN
  10383. Info(source,"ArrayHeader");
  10384. IF implementationVisitor.backend.cooperative THEN
  10385. sizePC := source.pc;
  10386. Address(source,0);
  10387. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10388. IF baseType # "" THEN
  10389. Basic.ToSegmentedName(baseType, name);
  10390. NamedSymbol(source, name,NIL, 0, 0);
  10391. ELSE
  10392. Address(source,0);
  10393. END;
  10394. Address(source,0);
  10395. ELSE
  10396. Address(source,0);
  10397. Address(source,0);
  10398. (* first pointer for GC *)
  10399. IF hasPointer THEN
  10400. (* points to first element in the array, this is NOT the base type descriptor *)
  10401. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10402. ELSE
  10403. Address(source,0);
  10404. END;
  10405. sizePC := source.pc;
  10406. Address(source,0);
  10407. Info(source,"array data");
  10408. END;
  10409. END ArrayBlock;
  10410. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10411. BEGIN
  10412. IF implementationVisitor.backend.cooperative THEN
  10413. PatchSize(section, pc, size);
  10414. PatchSize(section, pc + 3, size);
  10415. ELSE
  10416. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10417. PatchSize(section, pc, size);
  10418. END;
  10419. END PatchArray;
  10420. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10421. VAR
  10422. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10423. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10424. poolMap: Basic.HashTableInt;
  10425. namePool: IntermediateCode.Section;
  10426. namePoolOffset: LONGINT;
  10427. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10428. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10429. BEGIN
  10430. Basic.SegmentedNameToString(s1.name,n1);
  10431. Basic.SegmentedNameToString(s2.name,n2);
  10432. index := 0;
  10433. ch1 := n1[index];
  10434. ch2 := n2[index];
  10435. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10436. INC(index);
  10437. ch1 := n1[index];
  10438. ch2 := n2[index];
  10439. END;
  10440. RETURN ch1 < ch2;
  10441. END Compare;
  10442. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10443. VAR
  10444. i, j: LONGINT;
  10445. x, t: Sections.Section;
  10446. BEGIN
  10447. IF lo < hi THEN
  10448. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10449. WHILE i <= j DO
  10450. WHILE Compare(list[i], x) DO INC(i) END;
  10451. WHILE Compare(x, list[j]) DO DEC(j) END;
  10452. IF i <= j THEN
  10453. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10454. INC(i); DEC(j)
  10455. END
  10456. END;
  10457. IF lo < j THEN QuickSort(list, lo, j) END;
  10458. IF i < hi THEN QuickSort(list, i, hi) END
  10459. END;
  10460. END QuickSort;
  10461. (*
  10462. ExportDesc* = RECORD
  10463. fp*: ADDRESS;
  10464. name* {UNTRACED}: DynamicName;
  10465. adr*: ADDRESS;
  10466. exports*: LONGINT;
  10467. dsc* {UNTRACED}: ExportArray
  10468. END;
  10469. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10470. *)
  10471. PROCEDURE ExportDesc2(
  10472. source: IntermediateCode.Section;
  10473. namePool: IntermediateCode.Section;
  10474. fingerPrinter: FingerPrinter.FingerPrinter;
  10475. symbol: Sections.Section;
  10476. name: StringPool.Index;
  10477. VAR patchAdr: LONGINT
  10478. ): BOOLEAN;
  10479. VAR fingerPrint: SyntaxTree.FingerPrint;
  10480. BEGIN
  10481. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10482. & (symbol.type # Sections.InlineCodeSection)
  10483. THEN
  10484. *)
  10485. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10486. & (symbol.type # Sections.InlineCodeSection))
  10487. THEN
  10488. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10489. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10490. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10491. Longint(source,fingerPrint.shallow);
  10492. ELSE
  10493. Longint(source, 0);
  10494. END;
  10495. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10496. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10497. Symbol(source, symbol,0,0);
  10498. patchAdr := source.pc;
  10499. Longint(source, 0);
  10500. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10501. Address(source,0);
  10502. END;
  10503. RETURN TRUE
  10504. ELSE
  10505. RETURN FALSE
  10506. END;
  10507. END ExportDesc2;
  10508. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10509. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10510. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10511. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10512. nextPatch: LONGINT;
  10513. TYPE
  10514. Scope = RECORD
  10515. elements: LONGINT;
  10516. gelements: LONGINT;
  10517. section: IntermediateCode.Section;
  10518. patchAdr: LONGINT;
  10519. arraySizePC: LONGINT;
  10520. beginPC: LONGINT; (* current scope start pc *)
  10521. END;
  10522. BEGIN
  10523. Basic.InitSegmentedName(prev);
  10524. olevel := -1;
  10525. scopes[0].section := source;
  10526. scopes[0].arraySizePC := MIN(LONGINT);
  10527. FOR s := 0 TO LEN(sections)-1 DO
  10528. symbol := sections[s];
  10529. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10530. this := sections[s].name;
  10531. level := 0;
  10532. WHILE (this[level] > 0) DO
  10533. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10534. INC(level);
  10535. END;
  10536. WHILE level < olevel DO
  10537. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10538. IF olevel > 0 THEN
  10539. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10540. nextPatch := scopes[olevel-1].patchAdr+1;
  10541. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10542. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10543. END;
  10544. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10545. DEC(olevel);
  10546. END;
  10547. IF (this[level] > 0) THEN
  10548. IF level > olevel THEN
  10549. (*TRACE("opening",level); *)
  10550. IF scopes[level].section = NIL THEN
  10551. arrayName := ".@ExportArray";
  10552. Strings.AppendInt(arrayName, level);
  10553. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10554. AddPointer(scopes[level].section,offset);
  10555. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10556. END;
  10557. scopes[level].beginPC := scopes[level].section.pc;
  10558. olevel := level;
  10559. scopes[olevel].elements := 0;
  10560. END;
  10561. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10562. sym := sections[s];
  10563. ELSE
  10564. sym := NIL;
  10565. END;
  10566. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10567. THEN
  10568. INC(scopes[olevel].elements);
  10569. END;
  10570. (* enter string in scope *)
  10571. INC(level);
  10572. END;
  10573. END;
  10574. Basic.SegmentedNameToString(this, name);
  10575. prev := this;
  10576. END;
  10577. END;
  10578. WHILE 0 <= olevel DO
  10579. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10580. IF olevel > 0 THEN
  10581. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10582. nextPatch := scopes[olevel-1].patchAdr+1;
  10583. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10584. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10585. END;
  10586. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10587. DEC(olevel);
  10588. END;
  10589. level := 0;
  10590. WHILE (level < LEN(scopes)) DO
  10591. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10592. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10593. END;
  10594. INC(level);
  10595. END;
  10596. END Export;
  10597. BEGIN
  10598. NEW(fingerPrinter, module.system);
  10599. NEW(poolMap, 64);
  10600. (* this is the name pool private to the export table -- it is sorted and should not be mixed / used for other names in a module *)
  10601. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10602. NEW(sectionArray, module.allSections.Length());
  10603. FOR i := 0 TO module.allSections.Length() - 1 DO
  10604. section := module.allSections.GetSection(i);
  10605. sectionArray[i] := section;
  10606. END;
  10607. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10608. Export(sectionArray^);
  10609. END ExportDesc;
  10610. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10611. VAR
  10612. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10613. BEGIN
  10614. Info(source, "exception table offsets array descriptor");
  10615. size := 0;
  10616. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10617. Info(source, "exception table content");
  10618. FOR i := 0 TO module.allSections.Length() - 1 DO
  10619. p := module.allSections.GetSection(i);
  10620. IF p.type = Sections.CodeSection THEN
  10621. finallyPC := p(IntermediateCode.Section).finally;
  10622. IF finallyPC>=0 THEN
  10623. Symbol( source, p, 0,0);
  10624. Symbol( source, p, finallyPC, 0);
  10625. Symbol( source, p, finallyPC,0);
  10626. INC(size);
  10627. END;
  10628. END
  10629. END;
  10630. PatchArray(source,sizePC,size);
  10631. END ExceptionArray;
  10632. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10633. VAR i: LONGINT; ch: CHAR;
  10634. BEGIN
  10635. i := 0;
  10636. REPEAT
  10637. ch := name[i]; INC(i);
  10638. Char( section, ch);
  10639. UNTIL ch = 0X;
  10640. WHILE i < 32 DO
  10641. Char( section, 0X); INC(i);
  10642. END;
  10643. END Name;
  10644. PROCEDURE References(section: IntermediateCode.Section);
  10645. CONST
  10646. sfTypeNone = 0X;
  10647. sfTypeCHAR = 01X;
  10648. sfTypeCHAR8 = 02X;
  10649. sfTypeCHAR16 = 03X;
  10650. sfTypeCHAR32 = 04X;
  10651. sfTypeRANGE = 05X;
  10652. sfTypeSHORTINT = 06X;
  10653. sfTypeINTEGER = 07X;
  10654. sfTypeLONGINT = 08X;
  10655. sfTypeHUGEINT = 09X;
  10656. sfTypeWORD = 0AX;
  10657. sfTypeLONGWORD = 0BX;
  10658. sfTypeSIGNED8 = 0CX;
  10659. sfTypeSIGNED16 = 0DX;
  10660. sfTypeSIGNED32 = 0EX;
  10661. sfTypeSIGNED64 = 0FX;
  10662. sfTypeUNSIGNED8 = 10X;
  10663. sfTypeUNSIGNED16 = 11X;
  10664. sfTypeUNSIGNED32 = 12X;
  10665. sfTypeUNSIGNED64 = 13X;
  10666. sfTypeREAL = 14X;
  10667. sfTypeLONGREAL = 15X;
  10668. sfTypeCOMPLEX = 16X;
  10669. sfTypeLONGCOMPLEX = 17X;
  10670. sfTypeBOOLEAN = 18X;
  10671. sfTypeSET = 19X;
  10672. sfTypeANY = 1AX;
  10673. sfTypeOBJECT = 1BX;
  10674. sfTypeBYTE = 1CX;
  10675. sfTypeADDRESS = 1DX;
  10676. sfTypeSIZE = 1EX;
  10677. sfTypeIndirect = 1FX;
  10678. sfTypeRecord = 20X;
  10679. sfTypePointerToRecord = 21X;
  10680. sfTypePointerToArray = 22X;
  10681. sfTypeOpenArray = 23X;
  10682. sfTypeStaticArray = 24X;
  10683. sfTypeDynamicArray = 25X;
  10684. sfTypeMathStaticArray = 26X;
  10685. sfTypeMathOpenArray = 27X;
  10686. sfTypeMathTensor = 28X;
  10687. sfTypeDelegate = 29X;
  10688. sfTypeENUM = 2AX;
  10689. sfTypeCELL = 2BX;
  10690. sfTypePORT = 2CX;
  10691. sfIN = 0X;
  10692. sfOUT = 1X;
  10693. flagDelegate = 0;
  10694. flagConstructor = 1;
  10695. (* variable / parameter addressing modes *)
  10696. sfAbsolute = 0X; (* global vars *)
  10697. sfRelative = 1X; (* variables, value parameters *)
  10698. sfIndirect = 2X; (* var parameters *)
  10699. sfScopeBegin = 0F0X;
  10700. sfScopeEnd = 0F1X;
  10701. sfProcedure = 0F2X;
  10702. sfVariable = 0F3X;
  10703. sfTypeDeclaration = 0F4X;
  10704. sfModule = 0FFX;
  10705. RefInfo = TRUE;
  10706. VAR
  10707. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10708. indirectTypes: Basic.HashTable;
  10709. PROCEDURE CurrentIndex(): SIZE;
  10710. VAR i: LONGINT;
  10711. BEGIN
  10712. FOR i := startPC TO section.pc -1 DO
  10713. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10714. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10715. END;
  10716. startPC := section.pc;
  10717. RETURN lastOffset;
  10718. END CurrentIndex;
  10719. (*
  10720. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10721. Module = sfModule prevSymbol:SIZE name:String Scope.
  10722. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10723. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10724. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10725. Type =
  10726. sfTypePointerToRecord
  10727. | sfTypePointerToArray Type
  10728. | sfTypeOpenArray Type
  10729. | sfTypeDynamicArray Type
  10730. | sfTypeStaticArray length:SIZE Type
  10731. | sfTypeMathOpenArray Type
  10732. | sfTypeMathStaticArray length:SIZE Type
  10733. | sfTypeMathTensor Type
  10734. | sfTypeRecord tdAdr:ADDRESS
  10735. | sfTypeDelegate {Parameter} return:Type
  10736. | sfTypePort (sfIN | sfOUT)
  10737. | sfTypeBOOLEAN
  10738. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10739. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10740. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10741. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10742. | sfTypeWORD | sfTypeLONGWORD
  10743. | sfTypeREAL | sfTypeLONGREAL
  10744. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10745. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10746. | sfTypeIndirect offset:SIZE.
  10747. *)
  10748. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10749. VAR offset: SIZE;
  10750. BEGIN
  10751. IF indirectTypes.Has(type) THEN
  10752. offset := indirectTypes.GetInt(type);
  10753. Char(section, sfTypeIndirect);
  10754. Size(section, offset);
  10755. RETURN TRUE;
  10756. ELSE
  10757. indirectTypes.PutInt(type, CurrentIndex());
  10758. RETURN FALSE;
  10759. END;
  10760. END Indirect;
  10761. PROCEDURE NType(type: SyntaxTree.Type);
  10762. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10763. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10764. BEGIN
  10765. IF type = NIL THEN
  10766. Char(section, sfTypeNone)
  10767. ELSE
  10768. type := type.resolved;
  10769. size := type.sizeInBits;
  10770. WITH type:SyntaxTree.PointerType DO
  10771. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10772. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10773. Char(section, sfTypePointerToRecord);
  10774. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10775. ELSE
  10776. IF RefInfo THEN Info(section,"PointerToArray") END;
  10777. Char(section, sfTypePointerToArray);
  10778. NType(type.pointerBase);
  10779. END;
  10780. | type: SyntaxTree.ArrayType DO
  10781. IF ~Indirect(type) THEN
  10782. IF type.form = SyntaxTree.Open THEN
  10783. IF RefInfo THEN Info(section,"OpenArray") END;
  10784. Char(section, sfTypeOpenArray);
  10785. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10786. IF RefInfo THEN Info(section,"DynamicArray") END;
  10787. Char(section, sfTypeDynamicArray);
  10788. ELSIF type.form = SyntaxTree.Static THEN
  10789. IF RefInfo THEN Info(section,"StaticArray") END;
  10790. Char(section, sfTypeStaticArray);
  10791. Size(section, type.staticLength);
  10792. ELSE
  10793. HALT(100);
  10794. END;
  10795. NType(type.arrayBase);
  10796. END;
  10797. | type: SyntaxTree.MathArrayType DO
  10798. IF ~Indirect(type) THEN
  10799. IF type.form = SyntaxTree.Open THEN
  10800. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10801. Char(section, sfTypeMathOpenArray);
  10802. ELSIF type.form = SyntaxTree.Static THEN
  10803. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10804. Char(section, sfTypeMathStaticArray);
  10805. Size(section, type.staticLength);
  10806. ELSIF type.form = SyntaxTree.Tensor THEN
  10807. IF RefInfo THEN Info(section,"MathTensor") END;
  10808. Char(section, sfTypeMathTensor);
  10809. ELSE
  10810. HALT(100);
  10811. END;
  10812. NType(type.arrayBase);
  10813. END;
  10814. | type: SyntaxTree.RecordType DO
  10815. IF ~Indirect(type) THEN
  10816. IF type.pointerType # NIL (* OBJECT *) THEN
  10817. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10818. Char(section, sfTypePointerToRecord)
  10819. ELSE
  10820. IF RefInfo THEN Info(section,"Record") END;
  10821. Char(section, sfTypeRecord);
  10822. td := type.typeDeclaration;
  10823. IF RefInfo THEN Info(section,"TD") END;
  10824. IF (td # NIL) THEN
  10825. Global.GetSymbolSegmentedName(td,segmentedName);
  10826. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10827. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10828. ELSE
  10829. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10830. END;
  10831. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10832. Symbol(section, tir, 0, offset);
  10833. ELSE
  10834. Address(section, 0);
  10835. END;
  10836. END;
  10837. END;
  10838. | type: SyntaxTree.CellType DO
  10839. IF ~Indirect(type) THEN
  10840. IF RefInfo THEN Info(section,"Record") END;
  10841. Char(section, sfTypeRecord);
  10842. td := type.typeDeclaration;
  10843. IF RefInfo THEN Info(section,"TD") END;
  10844. IF (td # NIL) THEN
  10845. Global.GetSymbolSegmentedName(td,segmentedName);
  10846. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10847. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10848. ELSE
  10849. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10850. END;
  10851. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10852. Symbol(section, tir, 0, offset);
  10853. ELSE
  10854. Address(section, 0);
  10855. END;
  10856. END;
  10857. | type: SyntaxTree.PortType DO
  10858. Char(section, sfTypePORT);
  10859. IF type.direction = SyntaxTree.OutPort THEN
  10860. Char(section, sfOUT)
  10861. ELSE
  10862. Char(section, sfIN)
  10863. END;
  10864. | type: SyntaxTree.ProcedureType DO
  10865. IF ~Indirect(type) THEN
  10866. Char(section, sfTypeDelegate);
  10867. parameter := type.firstParameter;
  10868. WHILE(parameter # NIL) DO
  10869. NParameter(parameter, -1);
  10870. parameter := parameter.nextParameter;
  10871. END;
  10872. NType(type.returnType);
  10873. END;
  10874. | type:SyntaxTree.EnumerationType DO
  10875. Char(section, sfTypeENUM);
  10876. | type: SyntaxTree.BasicType DO
  10877. WITH type: SyntaxTree.BooleanType DO
  10878. IF RefInfo THEN Info(section,"Boolean") END;
  10879. Char(section, sfTypeBOOLEAN);
  10880. | type: SyntaxTree.CharacterType DO
  10881. IF type = module.system.characterType THEN
  10882. IF RefInfo THEN Info(section,"CHAR") END;
  10883. Char(section, sfTypeCHAR);
  10884. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10885. IF RefInfo THEN Info(section,"CHAR8") END;
  10886. Char(section, sfTypeCHAR8)
  10887. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10888. IF RefInfo THEN Info(section,"CHAR16") END;
  10889. Char(section, sfTypeCHAR16);
  10890. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10891. IF RefInfo THEN Info(section,"CHAR32") END;
  10892. Char(section, sfTypeCHAR32);
  10893. ELSE
  10894. HALT(100);
  10895. END;
  10896. |type: SyntaxTree.IntegerType DO
  10897. IF type(SyntaxTree.IntegerType).signed THEN
  10898. IF (type = module.system.shortintType) THEN
  10899. IF RefInfo THEN Info(section,"SHORTINT") END;
  10900. Char(section, sfTypeSHORTINT)
  10901. ELSIF (type = module.system.integerType) THEN
  10902. IF RefInfo THEN Info(section,"INTEGER") END;
  10903. Char(section, sfTypeINTEGER)
  10904. ELSIF (type = module.system.longintType) THEN
  10905. IF RefInfo THEN Info(section,"LONGINT") END;
  10906. Char(section, sfTypeLONGINT)
  10907. ELSIF (type = module.system.hugeintType) THEN
  10908. IF RefInfo THEN Info(section,"HUGEINT") END;
  10909. Char(section, sfTypeHUGEINT)
  10910. ELSIF (type = module.system.wordType) THEN
  10911. IF RefInfo THEN Info(section,"WORD") END;
  10912. Char(section, sfTypeWORD)
  10913. ELSIF (type = module.system.longWordType) THEN
  10914. IF RefInfo THEN Info(section,"LONGWORD") END;
  10915. Char(section, sfTypeLONGWORD);
  10916. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10917. IF RefInfo THEN Info(section,"SIGNED8") END;
  10918. Char(section, sfTypeSIGNED8)
  10919. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10920. IF RefInfo THEN Info(section,"SIGNED16") END;
  10921. Char(section, sfTypeSIGNED16)
  10922. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10923. IF RefInfo THEN Info(section,"SIGNED32") END;
  10924. Char(section, sfTypeSIGNED32)
  10925. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10926. IF RefInfo THEN Info(section,"SIGNED64") END;
  10927. Char(section, sfTypeSIGNED64)
  10928. ELSE
  10929. HALT(100);
  10930. END
  10931. ELSE (* unsigned *)
  10932. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10933. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10934. Char(section, sfTypeUNSIGNED8)
  10935. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10936. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10937. Char(section, sfTypeUNSIGNED16)
  10938. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10939. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10940. Char(section, sfTypeUNSIGNED32)
  10941. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10942. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10943. Char(section, sfTypeUNSIGNED64)
  10944. ELSE
  10945. HALT(100)
  10946. END
  10947. END;
  10948. | type: SyntaxTree.FloatType DO
  10949. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10950. IF RefInfo THEN Info(section,"REAL") END;
  10951. Char(section, sfTypeREAL);
  10952. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  10953. IF RefInfo THEN Info(section,"LONGREAL") END;
  10954. Char(section, sfTypeLONGREAL);
  10955. ELSE
  10956. HALT(100);
  10957. END;
  10958. |type: SyntaxTree.ComplexType DO
  10959. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  10960. IF RefInfo THEN Info(section,"COMPLEX") END;
  10961. Char(section, sfTypeCOMPLEX);
  10962. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  10963. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  10964. Char(section, sfTypeLONGCOMPLEX);
  10965. ELSE
  10966. HALT(100);
  10967. END;
  10968. |type:SyntaxTree.SetType DO
  10969. IF RefInfo THEN Info(section,"SET") END;
  10970. Char(section, sfTypeSET);
  10971. |type:SyntaxTree.AnyType DO
  10972. IF RefInfo THEN Info(section,"ANY") END;
  10973. Char(section, sfTypeANY);
  10974. |type:SyntaxTree.ObjectType DO
  10975. IF RefInfo THEN Info(section,"OBJECT") END;
  10976. Char(section, sfTypeOBJECT);
  10977. |type:SyntaxTree.ByteType DO
  10978. IF RefInfo THEN Info(section,"BYTE") END;
  10979. Char(section, sfTypeBYTE);
  10980. |type:SyntaxTree.RangeType DO
  10981. IF RefInfo THEN Info(section,"RANGE") END;
  10982. Char(section, sfTypeRANGE)
  10983. |type:SyntaxTree.AddressType DO
  10984. IF RefInfo THEN Info(section,"ADDRESS") END;
  10985. Char(section, sfTypeADDRESS)
  10986. |type:SyntaxTree.SizeType DO
  10987. IF RefInfo THEN Info(section,"SIZE") END;
  10988. Char(section, sfTypeSIZE)
  10989. ELSE
  10990. HALT(100)
  10991. END;
  10992. ELSE HALT(101);
  10993. END;
  10994. END;
  10995. END NType;
  10996. (*
  10997. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  10998. *)
  10999. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11000. VAR pos: LONGINT; type: SyntaxTree.Type;
  11001. BEGIN
  11002. IF RefInfo THEN Info(section, "Parameter") END;
  11003. Char(section, sfVariable);
  11004. Size(section, procOffset);
  11005. String0(section, parameter.name);
  11006. type := parameter.type.resolved;
  11007. IF parameter.kind = SyntaxTree.VarParameter THEN
  11008. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11009. ELSE Char(section, sfIndirect)
  11010. END;
  11011. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11012. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11013. Char(section, sfIndirect);
  11014. ELSE
  11015. Char(section, sfRelative);
  11016. END;
  11017. ELSE
  11018. Char(section, sfRelative);
  11019. END;
  11020. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11021. NType(parameter.type);
  11022. END NParameter;
  11023. (*
  11024. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11025. *)
  11026. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11027. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11028. name: Basic.SegmentedName; flags: SET;
  11029. BEGIN
  11030. IF RefInfo THEN Info(section, "Procedure") END;
  11031. pos := CurrentIndex();
  11032. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11033. Char(section, sfProcedure);
  11034. Size(section, scopeOffset);
  11035. String0(section,procedure.name);
  11036. s := module.allSections.FindBySymbol(procedure);
  11037. Symbol(section,s,0,0); (* start *)
  11038. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11039. flags := {};
  11040. IF procedureType.isDelegate THEN
  11041. INCL(flags, flagDelegate);
  11042. END;
  11043. IF procedure.isConstructor THEN
  11044. INCL(flags, flagConstructor);
  11045. END;
  11046. Set(section, flags);
  11047. Global.GetSymbolSegmentedName(procedure,name);
  11048. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11049. IF RefInfo THEN Info(section, "Parameters") END;
  11050. parameter := procedureType.firstParameter;
  11051. WHILE(parameter # NIL) DO
  11052. NParameter(parameter, pos);
  11053. parameter := parameter.nextParameter;
  11054. END;
  11055. IF RefInfo THEN Info(section, "ReturnType") END;
  11056. NType(procedureType.returnType);
  11057. NScope(procedure.procedureScope, pos);
  11058. END NProcedure;
  11059. (*
  11060. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11061. *)
  11062. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11063. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11064. BEGIN
  11065. IF RefInfo THEN Info(section, "Variable") END;
  11066. pos := CurrentIndex();
  11067. Char(section, sfVariable);
  11068. Size(section, scopeOffset);
  11069. String0(section, variable.name);
  11070. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11071. Char(section, sfAbsolute);
  11072. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11073. NamedSymbol(section, sn,variable, 0,0);
  11074. ELSE
  11075. Char(section, sfRelative);
  11076. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11077. END;
  11078. NType(variable.type);
  11079. s := module.allSections.FindBySymbol(variable);
  11080. END NVariable;
  11081. (*
  11082. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11083. *)
  11084. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11085. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11086. BEGIN
  11087. IF typeDeclaration = NIL THEN RETURN END;
  11088. pos := CurrentIndex();
  11089. s := module.allSections.FindBySymbol(typeDeclaration);
  11090. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11091. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11092. Char(section, sfTypeDeclaration);
  11093. Size(section, scopeOffset);
  11094. String0(section, typeDeclaration.name);
  11095. declared := typeDeclaration.declaredType.resolved;
  11096. IF (declared IS SyntaxTree.PointerType) THEN
  11097. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11098. END;
  11099. WITH declared: SyntaxTree.RecordType DO
  11100. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11101. Symbol(section, s, 0, offset);
  11102. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11103. Basic.AppendToSegmentedName(name,".@Info");
  11104. s := module.allSections.FindByName(name);
  11105. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11106. NScope(declared.recordScope, pos);
  11107. |declared: SyntaxTree.CellType DO
  11108. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11109. Symbol(section, s, 0, offset);
  11110. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11111. Basic.AppendToSegmentedName(name,".@Info");
  11112. s := module.allSections.FindByName(name);
  11113. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11114. NScope(declared.cellScope, pos);
  11115. ELSE
  11116. Address(section, 0);
  11117. END;
  11118. END NTypeDeclaration;
  11119. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11120. VAR pos: LONGINT;
  11121. BEGIN
  11122. pos := CurrentIndex();
  11123. Char(section,sfModule);
  11124. Size(section, prevSymbol);
  11125. String0(section, module.name);
  11126. NScope(module.moduleScope, pos);
  11127. END NModule;
  11128. (*
  11129. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11130. *)
  11131. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11132. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11133. BEGIN
  11134. IF scope = NIL THEN RETURN END;
  11135. IF RefInfo THEN Info(section, "Scope") END;
  11136. Char(section, sfScopeBegin);
  11137. variable := scope.firstVariable;
  11138. WHILE (variable # NIL) DO
  11139. NVariable(variable, prevSymbol);
  11140. variable := variable.nextVariable;
  11141. END;
  11142. WITH scope: SyntaxTree.ModuleScope DO
  11143. bodyProcedure := scope.bodyProcedure;
  11144. |scope: SyntaxTree.RecordScope DO
  11145. bodyProcedure := scope.bodyProcedure;
  11146. ELSE
  11147. bodyProcedure := NIL;
  11148. END;
  11149. IF bodyProcedure # NIL THEN
  11150. NProcedure(bodyProcedure, prevSymbol)
  11151. END;
  11152. procedure := scope.firstProcedure;
  11153. WHILE procedure # NIL DO
  11154. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11155. procedure := procedure.nextProcedure;
  11156. END;
  11157. typeDeclaration := scope.firstTypeDeclaration;
  11158. WHILE typeDeclaration # NIL DO
  11159. NTypeDeclaration(typeDeclaration, prevSymbol);
  11160. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11161. END;
  11162. Char(section, sfScopeEnd); (* scope ends *)
  11163. END NScope;
  11164. BEGIN
  11165. NEW(indirectTypes, 32);
  11166. ArrayBlock(section,sizePC,"", FALSE);
  11167. startPC := section.pc;
  11168. NModule(module.module, -1);
  11169. PatchArray(section,sizePC,CurrentIndex());
  11170. END References;
  11171. (*
  11172. Command* = RECORD
  11173. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11174. name*: Name; (* name of the procedure *)
  11175. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11176. entryAdr* : ADDRESS; (* entry address of procedure *)
  11177. END;
  11178. *)
  11179. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11180. VAR
  11181. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11182. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11183. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11184. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11185. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11186. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11187. BEGIN
  11188. RETURN
  11189. (type = NIL) OR
  11190. (type.resolved IS SyntaxTree.RecordType) OR
  11191. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11192. (type.resolved IS SyntaxTree.AnyType);
  11193. END TypeAllowed;
  11194. BEGIN
  11195. numberParameters := procedureType.numberParameters;
  11196. RETURN
  11197. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11198. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11199. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11200. END GetProcedureAllowed;
  11201. PROCEDURE WriteType(type : SyntaxTree.Type);
  11202. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11203. name: Basic.SegmentedName; offset: LONGINT;
  11204. BEGIN
  11205. IF type = NIL THEN
  11206. Address(source,0);
  11207. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11208. Address(source,1);
  11209. ELSE
  11210. type := type.resolved;
  11211. IF type IS SyntaxTree.PointerType THEN
  11212. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11213. END;
  11214. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11215. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11216. name[0] := typeDeclaration.name; name[1] := -1;
  11217. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11218. ASSERT(section # NIL);
  11219. ELSE
  11220. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11221. (* TODO *)
  11222. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11223. END;
  11224. IF implementationVisitor.backend.cooperative THEN
  11225. offset := 0;
  11226. ELSE
  11227. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11228. END;
  11229. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11230. END;
  11231. END WriteType;
  11232. BEGIN
  11233. Info(source, "command array descriptor");
  11234. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11235. numberCommands := 0;
  11236. Info(source, "command array content");
  11237. FOR i := 0 TO module.allSections.Length() - 1 DO
  11238. p := module.allSections.GetSection(i);
  11239. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11240. procedure := p.symbol(SyntaxTree.Procedure);
  11241. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11242. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11243. procedure.GetName(name);
  11244. Name(source,name);
  11245. numberParameters := procedureType.numberParameters;
  11246. (* offset of type of first parameter *)
  11247. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11248. ELSE WriteType(procedureType.firstParameter.type)
  11249. END;
  11250. (* offset of type of return parameter *)
  11251. WriteType(procedureType.returnType);
  11252. (* command name *)
  11253. (* command code offset *)
  11254. Symbol(source,p,0,0);
  11255. INC(numberCommands);
  11256. IF Trace THEN
  11257. D.Ln;
  11258. END;
  11259. END;
  11260. END
  11261. END;
  11262. PatchArray(source,sizePC,numberCommands);
  11263. END CommandArray;
  11264. (* to prevent from double import of different module aliases *)
  11265. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11266. VAR i: SyntaxTree.Import;
  11267. BEGIN
  11268. i := module.module.moduleScope.firstImport;
  11269. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11270. i := i.nextImport;
  11271. END;
  11272. RETURN i = import
  11273. END IsFirstDirectOccurence;
  11274. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11275. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11276. BEGIN
  11277. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11278. ArrayBlock(source,pc,"", FALSE);
  11279. Info(source, "import module array data");
  11280. IF implementationVisitor.backend.cooperative THEN
  11281. offset := 0;
  11282. ELSE
  11283. IF module.system.addressType.sizeInBits = 64 THEN
  11284. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11285. ELSE
  11286. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11287. END;
  11288. END;
  11289. import := module.module.moduleScope.firstImport;
  11290. numberImports := 0;
  11291. WHILE import # NIL DO
  11292. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11293. Global.GetModuleSegmentedName(import.module,name);
  11294. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11295. NamedSymbol(source, name, NIL, 0, offset);
  11296. INC(numberImports);
  11297. END;
  11298. import := import.nextImport
  11299. END;
  11300. PatchArray(source,pc,numberImports);
  11301. END ImportsArray;
  11302. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11303. VAR
  11304. p: Sections.Section; sizePC, size, i: LONGINT;
  11305. BEGIN
  11306. Info(source, "Type info section");
  11307. size := 0;
  11308. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11309. FOR i := 0 TO module.allSections.Length() - 1 DO
  11310. p := module.allSections.GetSection(i);
  11311. WITH p: IntermediateCode.Section DO
  11312. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11313. Symbol(source,p,0,0);
  11314. INC(size);
  11315. END;
  11316. END
  11317. END;
  11318. PatchArray(source,sizePC,size);
  11319. END TypeInfoSection;
  11320. (*
  11321. ProcTableEntry* = RECORD
  11322. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11323. noPtr*: LONGINT;
  11324. END;
  11325. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11326. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11327. *)
  11328. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11329. VAR
  11330. numberPointers: SIZE;
  11331. procedure: SyntaxTree.Procedure;
  11332. BEGIN
  11333. Info(section,"pcFrom");
  11334. Symbol(section,procedureSection,0,0);
  11335. Info(section,"pcTo");
  11336. Symbol(section, procedureSection, procedureSection.pc, 0);
  11337. Info(section,"pointer to offsets array");
  11338. Symbol(section, section,section.pc+1,0);
  11339. Info(section,"offsets array");
  11340. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11341. PointerArray(section, procedure.procedureScope, numberPointers);
  11342. END ProcedureDescriptor;
  11343. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11344. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11345. BEGIN
  11346. name := procedureSection.name;
  11347. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11348. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11349. ProcedureDescriptor(dest, procedureSection);
  11350. Symbol(section, dest, offset, 0);
  11351. END ProcedureDescriptorPointer;
  11352. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11353. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11354. BEGIN
  11355. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11356. numberProcs := 0;
  11357. FOR i := 0 TO module.allSections.Length() - 1 DO
  11358. destination := module.allSections.GetSection(i);
  11359. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11360. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11361. INC(numberProcs);
  11362. END
  11363. END;
  11364. PatchArray(section, sizePC, numberProcs);
  11365. END ProcedureDescriptorArray;
  11366. (*
  11367. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11368. VAR
  11369. next*: Module; (** once a module is published, all fields are read-only *)
  11370. name*: Name;
  11371. init, published: BOOLEAN;
  11372. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11373. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11374. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11375. command*: POINTER TO ARRAY OF Command;
  11376. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11377. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11378. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11379. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11380. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11381. data*, code*: Bytes;
  11382. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11383. export*: ExportDesc;
  11384. term*: TerminationHandler;
  11385. exTable*: ExceptionTable;
  11386. noProcs*: LONGINT;
  11387. firstProc*: ADDRESS; <- done by loader
  11388. maxPtrs*: LONGINT;
  11389. crc*: LONGINT;
  11390. *)
  11391. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11392. BEGIN
  11393. Info(section, "cycle");
  11394. Size(section,0);
  11395. Info(section, "references");
  11396. Size(section,0);
  11397. Info(section, "nextMarked");
  11398. Address(section,0);
  11399. Info(section, "nextWatched");
  11400. Address(section,0);
  11401. END BasePointer;
  11402. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11403. BEGIN
  11404. BasePointer(section);
  11405. Info(section, "action");
  11406. Address(section,0);
  11407. Info(section, "monitor");
  11408. Address(section,0);
  11409. END BaseObject;
  11410. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11411. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11412. pooledName: Basic.SegmentedName;
  11413. symbol: SyntaxTree.Symbol;
  11414. BEGIN
  11415. Global.GetModuleName(module.module,name);
  11416. Strings.Append(name,".@Module.@Descriptor");
  11417. Basic.ToSegmentedName(name, pooledName);
  11418. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11419. Symbol(section,descriptorSection,0,0);
  11420. Info(descriptorSection, "descriptor");
  11421. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11422. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11423. Address(descriptorSection,0);
  11424. Global.GetModuleName(module.module,name);
  11425. Strings.Append(name,".@Trace");
  11426. Basic.ToSegmentedName(name, pooledName);
  11427. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11428. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11429. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11430. END ModuleDescriptor;
  11431. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11432. VAR name: ARRAY 128 OF CHAR;
  11433. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11434. symbol: SyntaxTree.Symbol;
  11435. BEGIN
  11436. Global.GetModuleName(module.module,name);
  11437. Strings.Append(name,".@Module");
  11438. Basic.ToSegmentedName(name, pooledName);
  11439. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11440. moduleSection.SetExported(TRUE);
  11441. IF moduleSection.pc = 0 THEN
  11442. IF implementationVisitor.backend.cooperative THEN
  11443. Info(moduleSection, "descriptor");
  11444. ModuleDescriptor(moduleSection);
  11445. BaseObject(moduleSection);
  11446. implementationVisitor.CreateTraceModuleMethod(module.module);
  11447. ELSE
  11448. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11449. Info(moduleSection, "HeapBlock");
  11450. Symbol(moduleSection,moduleSection,2,0);
  11451. Info(moduleSection, "TypeDescriptor");
  11452. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11453. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11454. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11455. END;
  11456. END;
  11457. RETURN moduleSection;
  11458. END ModuleSection;
  11459. PROCEDURE NewModuleInfo();
  11460. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11461. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11462. sectionName: Basic.SectionName;
  11463. CONST MPO=-40000000H;
  11464. BEGIN
  11465. (*
  11466. TypeDesc* = POINTER TO RECORD
  11467. descSize: LONGINT;
  11468. sentinel: LONGINT; (* = MPO-4 *)
  11469. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11470. flags*: SET;
  11471. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11472. name*: Name;
  11473. END;
  11474. *)
  11475. Global.GetSymbolSegmentedName(module.module,name);
  11476. Basic.AppendToSegmentedName(name,".@Info");
  11477. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11478. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11479. Address(source,MPO-4);
  11480. Info(source, "type tag pointer");
  11481. Address( source,0);
  11482. Info(source, "type flags");
  11483. flags := {};
  11484. Set( source, flags);
  11485. Info(source, "pointer to module");
  11486. moduleSection := ModuleSection();
  11487. Symbol( source, moduleSection, moduleSection.pc,0);
  11488. Info(source, "type name");
  11489. i := 0;
  11490. sectionName := "@Self";
  11491. (*
  11492. Global.GetSymbolSegmentedName(td,name);
  11493. Basic.SegmentedNameToString(name, sectionName);
  11494. *)
  11495. Name(source,sectionName);
  11496. source.SetReferenced(FALSE);
  11497. patchInfoPC := source.pc;
  11498. Size(source, 0);
  11499. END NewModuleInfo;
  11500. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11501. VAR
  11502. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11503. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11504. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11505. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11506. referenceSectionOffset : LONGINT;
  11507. name: Basic.SegmentedName; offset: LONGINT;
  11508. BEGIN
  11509. NewModuleInfo();
  11510. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11511. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11512. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11513. ImportsArray(importSection);
  11514. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11515. CommandArray(commandsSection);
  11516. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11517. ExceptionArray(exceptionSection);
  11518. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11519. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11520. TypeInfoSection(typeInfoSection);
  11521. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11522. References(referenceSection);
  11523. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11524. ProcedureDescriptorArray(procTableSection, numberProcs);
  11525. name := "Heaps.AnyPtr";
  11526. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11527. (* set base pointer *)
  11528. NamedSymbolAt(procTableSection, procTableSectionOffset -1, name, NIL, 0, offset);
  11529. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11530. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11531. moduleSection := ModuleSection();
  11532. Info(moduleSection, "nextRoot*: RootObject");
  11533. Address(moduleSection,0);
  11534. Info(moduleSection, "next*: Module");
  11535. Address(moduleSection,0);
  11536. Info(moduleSection, "name*: Name");
  11537. Name(moduleSection,moduleName);
  11538. Info(moduleSection, "init, published: BOOLEAN");
  11539. Boolean(moduleSection,FALSE);
  11540. Boolean(moduleSection,FALSE);
  11541. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11542. Boolean(moduleSection,FALSE);
  11543. Boolean(moduleSection,FALSE);
  11544. Info(moduleSection, "refcnt*: LONGINT");
  11545. Longint(moduleSection,0);
  11546. Info(moduleSection, "sb*: ADDRESS");
  11547. Address(moduleSection,0);
  11548. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11549. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11550. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11551. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11552. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11553. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11554. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11555. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11556. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11557. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11558. Info(moduleSection, "procTable*: ProcTable");
  11559. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11560. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11561. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11562. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11563. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11564. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11565. Info(moduleSection, "export*: ExportDesc");
  11566. ExportDesc(moduleSection);
  11567. Info(moduleSection, "term*: TerminationHandler");
  11568. Address(moduleSection,0);
  11569. Info(moduleSection, "exTable*: ExceptionTable");
  11570. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11571. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11572. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11573. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11574. Info(moduleSection, "crc*: LONGINT");
  11575. Longint(moduleSection, 0); (*! must be implemented *)
  11576. Info(moduleSection, "body*: ADDRESS");
  11577. Symbol(moduleSection, bodyProc, 0,0);
  11578. IF implementationVisitor.backend.cooperative THEN
  11579. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11580. Info(moduleSection, "monitor.owner");
  11581. Address(moduleSection,0);
  11582. Info(moduleSection, "monitor.nestingLevel");
  11583. Address(moduleSection,0);
  11584. Info(moduleSection, "monitor.blockedQueue");
  11585. Address(moduleSection,0); Address(moduleSection,0);
  11586. Info(moduleSection, "monitor.waitingQueue");
  11587. Address(moduleSection,0); Address(moduleSection,0);
  11588. Info(moduleSection, "monitor.waitingSentinel");
  11589. Address(moduleSection,0);
  11590. END;
  11591. END Module;
  11592. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11593. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11594. BEGIN
  11595. ArrayBlock(source,pc,"",FALSE);
  11596. Info(source, "pointer offsets array data");
  11597. IF scope IS SyntaxTree.RecordScope THEN
  11598. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11599. ELSIF scope IS SyntaxTree.CellScope THEN
  11600. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11601. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11602. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11603. WHILE variable # NIL DO
  11604. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11605. symbol := module.allSections.FindBySymbol(variable);
  11606. ASSERT(symbol # NIL);
  11607. Pointers(0,symbol, source,variable.type,numberPointers);
  11608. END;
  11609. variable := variable.nextVariable;
  11610. END;
  11611. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11612. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11613. WHILE parameter # NIL DO
  11614. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11615. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11616. END;
  11617. parameter := parameter.nextParameter;
  11618. END;
  11619. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11620. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11621. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11622. IF implementationVisitor.backend.preciseGC THEN
  11623. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11624. END;
  11625. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11626. END;
  11627. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11628. WHILE(variable # NIL) DO
  11629. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11630. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11631. END;
  11632. variable := variable.nextVariable
  11633. END;
  11634. END;
  11635. PatchArray(source,pc,numberPointers);
  11636. END PointerArray;
  11637. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11638. VAR
  11639. name: Basic.SegmentedName;
  11640. section: IntermediateCode.Section;
  11641. BEGIN
  11642. ASSERT(implementationVisitor.newObjectFile);
  11643. Global.GetSymbolSegmentedName(symbol,name);
  11644. Basic.AppendToSegmentedName(name,suffix);
  11645. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11646. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11647. Info(section, "HeapBlock");
  11648. Address(section,0); (* empty such that GC does not go on traversing *)
  11649. Info(section, suffix);
  11650. Address(section,0);
  11651. pc := section.pc;
  11652. RETURN section;
  11653. END SymbolSection;
  11654. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11655. VAR recordType: SyntaxTree.RecordType;
  11656. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11657. section: Sections.Section; cellType: SyntaxTree.CellType;
  11658. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11659. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11660. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11661. sectionName: Basic.SectionName;
  11662. CONST MPO=-40000000H;
  11663. BEGIN
  11664. (*
  11665. TypeDesc* = POINTER TO RECORD
  11666. descSize: LONGINT;
  11667. sentinel: LONGINT; (* = MPO-4 *)
  11668. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11669. flags*: SET;
  11670. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11671. name*: Name;
  11672. END;
  11673. *)
  11674. (* source := module.sections.FindByName(...) *)
  11675. Global.GetSymbolSegmentedName(td,name);
  11676. Basic.AppendToSegmentedName(name,".@Info");
  11677. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11678. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11679. Address(source,MPO-4);
  11680. Info(source, "type tag pointer");
  11681. Symbol( source, tag, offset, 0);
  11682. Info(source, "type flags");
  11683. flags := {};
  11684. IF isProtected THEN INCL(flags,31) END;
  11685. Set( source, flags);
  11686. Info(source, "pointer to module");
  11687. moduleSection := ModuleSection();
  11688. Symbol( source, moduleSection, moduleSection.pc,0);
  11689. Info(source, "type name");
  11690. i := 0;
  11691. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11692. (*
  11693. Global.GetSymbolSegmentedName(td,name);
  11694. Basic.SegmentedNameToString(name, sectionName);
  11695. *)
  11696. Name(source,sectionName);
  11697. source.SetReferenced(FALSE);
  11698. Size(source, 0);
  11699. RETURN source;
  11700. END NewTypeDescriptorInfo;
  11701. PROCEDURE NewTypeDescriptor;
  11702. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11703. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11704. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11705. numberPointers: LONGINT; padding, i: LONGINT;
  11706. CONST MPO=-40000000H;
  11707. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11708. VAR i: LONGINT;
  11709. PROCEDURE Td(record: SyntaxTree.RecordType);
  11710. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11711. BEGIN
  11712. IF record # NIL THEN
  11713. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11714. baseTD := record.typeDeclaration;
  11715. Global.GetSymbolSegmentedName(baseTD,name);
  11716. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11717. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11718. ELSE
  11719. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11720. END;
  11721. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11722. Symbol(source, tir, 0, offset);
  11723. IF reverse THEN Td(record.GetBaseRecord()) END;
  11724. END;
  11725. END Td;
  11726. BEGIN
  11727. Info(source, "tag table");
  11728. baseRecord := recordType;
  11729. i := 0;
  11730. WHILE baseRecord # NIL DO
  11731. INC(i);
  11732. baseRecord := baseRecord.GetBaseRecord();
  11733. END;
  11734. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11735. IF ~reverse THEN Td(recordType) END;
  11736. WHILE i < size DO
  11737. Address(source,0);
  11738. INC(i);
  11739. END;
  11740. IF reverse THEN Td(recordType) END;
  11741. END TdTable;
  11742. PROCEDURE MethodTable(reverse: BOOLEAN);
  11743. VAR i,methods: LONGINT;
  11744. BEGIN
  11745. Info(source, "method table");
  11746. IF recordType # NIL THEN
  11747. methods := recordType.recordScope.numberMethods;
  11748. IF reverse THEN
  11749. FOR i := methods-1 TO 0 BY -1 DO
  11750. procedure := recordType.recordScope.FindMethod(i);
  11751. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11752. NamedSymbol(source, name,procedure, 0,0);
  11753. END;
  11754. ELSE
  11755. FOR i := 0 TO methods-1 DO
  11756. procedure := recordType.recordScope.FindMethod(i);
  11757. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11758. NamedSymbol(source, name,procedure, 0,0);
  11759. END;
  11760. END;
  11761. END;
  11762. END MethodTable;
  11763. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11764. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11765. BEGIN
  11766. Info(source, "method table");
  11767. baseRecord := recordType;
  11768. WHILE baseRecord.baseType # NIL DO
  11769. baseRecord := baseRecord.GetBaseRecord ();
  11770. END;
  11771. GetRecordTypeName (baseRecord, name);
  11772. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11773. IF name = stackFrame THEN
  11774. start := 0;
  11775. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11776. baseRecord := recordType;
  11777. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11778. baseRecord := baseRecord.GetBaseRecord ();
  11779. END;
  11780. IF baseRecord # NIL THEN
  11781. GetRecordTypeName (baseRecord, name);
  11782. IF pointer & ~baseRecord.isObject THEN
  11783. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11784. END;
  11785. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11786. ELSIF recordType.isObject THEN
  11787. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11788. ELSIF pointer THEN
  11789. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11790. ELSE
  11791. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11792. END;
  11793. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11794. Symbol(source, tir, 0, 0);
  11795. start := 0;
  11796. baseRecord := recordType;
  11797. WHILE (baseRecord # NIL) DO
  11798. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11799. baseRecord := baseRecord.GetBaseRecord ();
  11800. END;
  11801. ELSE
  11802. (* explicit trace method: *)
  11803. procedure := recordType.recordScope.FindMethod(0);
  11804. IF ~procedure.isFinalizer THEN
  11805. Global.GetSymbolSegmentedName(procedure, name);
  11806. NamedSymbol(source, name,procedure, 0,0);
  11807. END;
  11808. start := 1;
  11809. END;
  11810. IF (name # stackFrame) & recordType.isObject THEN
  11811. baseRecord := recordType;
  11812. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11813. baseRecord := baseRecord.GetBaseRecord ();
  11814. END;
  11815. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11816. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11817. ELSE
  11818. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11819. END;
  11820. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11821. Symbol(source, tir, 0, 0);
  11822. END;
  11823. methods := recordType.recordScope.numberMethods;
  11824. FOR i := start TO methods-1 DO
  11825. procedure := recordType.recordScope.FindMethod(i);
  11826. IF ~procedure.isFinalizer THEN
  11827. Global.GetSymbolSegmentedName(procedure, name);
  11828. NamedSymbol(source, name,procedure, 0,0);
  11829. END;
  11830. END;
  11831. END CooperativeMethodTable;
  11832. BEGIN
  11833. Global.GetSymbolSegmentedName(td,name);
  11834. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11835. source.SetExported(IsExported(td));
  11836. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11837. IF implementationVisitor.backend.cooperative THEN
  11838. base := NIL;
  11839. baseRecord := recordType.GetBaseRecord();
  11840. IF baseRecord # NIL THEN
  11841. baseTD := baseRecord.typeDeclaration;
  11842. END;
  11843. IF ~recordType.isObject THEN
  11844. Info(source, "parent");
  11845. IF baseRecord # NIL THEN
  11846. Global.GetSymbolSegmentedName(baseTD,name);
  11847. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11848. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11849. ELSE
  11850. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11851. END;
  11852. Symbol(source, tir, 0, 0);
  11853. ELSE
  11854. Address(source,0);
  11855. END;
  11856. Info(source, "record size");
  11857. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11858. source.SetReferenced(FALSE);
  11859. CooperativeMethodTable(FALSE);
  11860. base := source;
  11861. Global.GetSymbolSegmentedName(td,name);
  11862. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11863. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11864. source.SetExported(IsExported(td));
  11865. source.SetReferenced(FALSE);
  11866. END;
  11867. Info(source, "parent");
  11868. IF baseRecord # NIL THEN
  11869. Global.GetSymbolSegmentedName(baseTD,name);
  11870. sym := baseTD;
  11871. IF ~recordType.isObject THEN
  11872. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11873. sym := NIL;
  11874. END;
  11875. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11876. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11877. ELSE
  11878. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11879. END;
  11880. Symbol(source, tir, 0, 0);
  11881. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11882. Address(source,0);
  11883. ELSE
  11884. IF recordType.isObject THEN
  11885. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11886. ELSE
  11887. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11888. END;
  11889. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11890. Symbol(source, tir, 0, 0);
  11891. END;
  11892. Info(source, "base record descriptor");
  11893. Symbol(source, base, 0, 0);
  11894. CooperativeMethodTable(TRUE);
  11895. source.SetReferenced(FALSE);
  11896. IF recordType.hasPointers THEN
  11897. IF ~HasExplicitTraceMethod (recordType) THEN
  11898. implementationVisitor.CreateTraceMethod(recordType);
  11899. END;
  11900. implementationVisitor.CreateResetProcedure(recordType);
  11901. implementationVisitor.CreateAssignProcedure(recordType);
  11902. END;
  11903. ELSIF ~simple THEN
  11904. (*
  11905. MethodEnd = MPO
  11906. ---
  11907. methods (# methods)
  11908. ---
  11909. tags (16)
  11910. ---
  11911. TypeDesc = TypeInfoAdr
  11912. ---
  11913. td adr ---> rec size
  11914. ----
  11915. pointer offsets
  11916. ----
  11917. (padding)
  11918. -----
  11919. empty [2 addresses aligned]
  11920. empty
  11921. empty
  11922. numPtrs
  11923. ---
  11924. pointer offsets
  11925. ---
  11926. *)
  11927. Info(source, "MethodEnd = MPO");
  11928. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11929. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11930. MethodTable(TRUE);
  11931. TdTable(TypeTags, TRUE);
  11932. Info(source, "type descriptor info pointer");
  11933. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11934. IF (cellType # NIL) THEN
  11935. IF cellType.sizeInBits < 0 THEN
  11936. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11937. END;
  11938. Info(source, "cell size");
  11939. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11940. ELSE
  11941. Info(source, "record size");
  11942. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11943. END;
  11944. Info(source, "pointer offsets pointer");
  11945. padding := 1- source.pc MOD 2;
  11946. Symbol(source, source, source.pc+1+padding,0);
  11947. IF padding >0 THEN
  11948. Info(source, "padding");
  11949. FOR i := 1 TO padding DO Address(source,0) END;
  11950. END;
  11951. IF cellType # NIL THEN
  11952. PointerArray(source, cellType.cellScope, numberPointers);
  11953. ELSE
  11954. PointerArray(source, recordType.recordScope, numberPointers);
  11955. END;
  11956. ELSE
  11957. (*
  11958. simple:
  11959. td adr --> size
  11960. tag(1)
  11961. tag(2)
  11962. tag(3)
  11963. methods ->
  11964. *)
  11965. Info(source, "record size");
  11966. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11967. TdTable(TypeTags, FALSE);
  11968. MethodTable(FALSE);
  11969. source.SetReferenced(FALSE);
  11970. END;
  11971. END NewTypeDescriptor;
  11972. BEGIN
  11973. x := x.resolved;
  11974. IF (x IS SyntaxTree.PointerType) THEN
  11975. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11976. END;
  11977. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11978. recordType := x(SyntaxTree.RecordType);
  11979. td := x.typeDeclaration;
  11980. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11981. ASSERT(td # NIL);
  11982. section := module.allSections.FindBySymbol(td); (* TODO *)
  11983. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11984. IF implementationVisitor.newObjectFile THEN
  11985. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11986. NewTypeDescriptor
  11987. END;
  11988. ELSE
  11989. (* data section in intermediate code *)
  11990. Global.GetSymbolSegmentedName(td,name);
  11991. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  11992. Basic.SuffixSegmentedName (name, module.module.name);
  11993. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11994. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11995. tir.Emit(Data(Basic.invalidPosition,op));
  11996. END;
  11997. END;
  11998. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11999. cellType := x(SyntaxTree.CellType);
  12000. td := x.typeDeclaration;
  12001. section := module.allSections.FindBySymbol(td); (* TODO *)
  12002. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12003. IF implementationVisitor.newObjectFile THEN
  12004. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12005. NewTypeDescriptor
  12006. END;
  12007. END;
  12008. END;
  12009. END
  12010. END CheckTypeDeclaration
  12011. END MetaDataGenerator;
  12012. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12013. VAR
  12014. trace-: BOOLEAN;
  12015. traceString-: SyntaxTree.IdentifierString;
  12016. traceModuleName-: SyntaxTree.IdentifierString;
  12017. newObjectFile-: BOOLEAN;
  12018. profile-: BOOLEAN;
  12019. noRuntimeChecks: BOOLEAN;
  12020. simpleMetaData-: BOOLEAN;
  12021. noAsserts: BOOLEAN;
  12022. optimize-: BOOLEAN;
  12023. cooperative-: BOOLEAN;
  12024. preregisterStatic-: BOOLEAN;
  12025. dump-: Basic.Writer;
  12026. cellsAreObjects: BOOLEAN;
  12027. preciseGC: BOOLEAN;
  12028. PROCEDURE &InitIntermediateBackend*;
  12029. BEGIN
  12030. simpleMetaData := FALSE;
  12031. newObjectFile := FALSE;
  12032. InitBackend;
  12033. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12034. SetTraceModuleName(DefaultTraceModuleName);
  12035. END InitIntermediateBackend;
  12036. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12037. VAR
  12038. declarationVisitor: DeclarationVisitor;
  12039. implementationVisitor: ImplementationVisitor;
  12040. module: Sections.Module;
  12041. name, platformName: SyntaxTree.IdentifierString;
  12042. meta: MetaDataGenerator;
  12043. BEGIN
  12044. ResetError;
  12045. Global.GetSymbolName(x,name);
  12046. NEW(module,x,system); (* backend structures *)
  12047. Global.GetModuleName(x, name);
  12048. module.SetModuleName(name);
  12049. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12050. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12051. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12052. declarationVisitor.Module(x,module);
  12053. IF newObjectFile & ~meta.simple THEN
  12054. meta.Module(implementationVisitor.moduleBodySection);
  12055. END;
  12056. GetDescription(platformName);
  12057. module.SetPlatformName(platformName);
  12058. RETURN module
  12059. END GenerateIntermediate;
  12060. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12061. BEGIN RETURN TRUE
  12062. END SupportedImmediate;
  12063. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12064. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12065. END ProcessSyntaxTreeModule;
  12066. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12067. VAR
  12068. result: Sections.Module;
  12069. traceName: Basic.MessageString;
  12070. BEGIN
  12071. ASSERT(intermediateCodeModule IS Sections.Module);
  12072. result := intermediateCodeModule(Sections.Module);
  12073. IF trace THEN
  12074. traceName := "intermediate code trace: ";
  12075. Strings.Append(traceName,traceString);
  12076. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12077. IF (traceString="") OR (traceString="*") THEN
  12078. result.Dump(dump);
  12079. dump.Update
  12080. ELSE
  12081. Sections.DumpFiltered(dump, result, traceString);
  12082. END
  12083. END;
  12084. RETURN result
  12085. END ProcessIntermediateCodeModule;
  12086. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12087. BEGIN instructionSet := "Intermediate";
  12088. END GetDescription;
  12089. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12090. BEGIN
  12091. SELF.newObjectFile := newObjectFile;
  12092. SELF.simpleMetaData := simpleMetaData;
  12093. END SetNewObjectFile;
  12094. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12095. BEGIN COPY(name, traceModuleName)
  12096. END SetTraceModuleName;
  12097. PROCEDURE DefineOptions(options: Options.Options);
  12098. BEGIN
  12099. DefineOptions^(options);
  12100. options.Add(0X,"trace",Options.String);
  12101. options.Add(0X,"runtime",Options.String);
  12102. options.Add(0X,"newObjectFile",Options.Flag);
  12103. options.Add(0X,"traceModule",Options.String);
  12104. options.Add(0X,"profile",Options.Flag);
  12105. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12106. options.Add(0X,"noAsserts",Options.Flag);
  12107. options.Add(0X,"metaData",Options.String);
  12108. options.Add('o',"optimize", Options.Flag);
  12109. options.Add(0X,"preregisterStatic", Options.Flag);
  12110. options.Add(0X,"cellsAreObjects", Options.Flag);
  12111. options.Add(0X,"preciseGC", Options.Flag);
  12112. END DefineOptions;
  12113. PROCEDURE GetOptions(options: Options.Options);
  12114. VAR name,string: SyntaxTree.IdentifierString;
  12115. BEGIN
  12116. GetOptions^(options);
  12117. trace := options.GetString("trace",traceString);
  12118. profile := options.GetFlag("profile");
  12119. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12120. noAsserts := options.GetFlag("noAsserts");
  12121. cooperative := options.GetFlag("cooperative");
  12122. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12123. IF cooperative THEN
  12124. SetRuntimeModuleName("CPU") END
  12125. END;
  12126. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12127. simpleMetaData := TRUE
  12128. END;
  12129. IF options.GetString("metaData",string) THEN
  12130. IF string = "simple" THEN simpleMetaData := TRUE
  12131. ELSIF string ="full" THEN simpleMetaData := FALSE
  12132. END;
  12133. END;
  12134. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12135. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12136. optimize := options.GetFlag("optimize");
  12137. preregisterStatic := options.GetFlag("preregisterStatic");
  12138. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12139. preciseGC := options.GetFlag("preciseGC");
  12140. END GetOptions;
  12141. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12142. BEGIN RETURN SymbolFileFormat.Get()
  12143. END DefaultSymbolFileFormat;
  12144. END IntermediateBackend;
  12145. (* ----------------------------------- register allocation ------------------------------------- *)
  12146. (* register mapping scheme
  12147. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12148. spill offset
  12149. part(n) --> ticket <--> physical register
  12150. spill offset
  12151. *)
  12152. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12153. emptyOperand: IntermediateCode.Operand;
  12154. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12155. statCoopResetVariables: LONGINT;
  12156. statCoopModifyAssignments: LONGINT;
  12157. modifyAssignmentsPC : LONGINT;
  12158. statCoopNilCheck: LONGINT;
  12159. statCoopSwitch: LONGINT;
  12160. statCoopAssignProcedure: LONGINT;
  12161. statCoopTraceMethod: LONGINT;
  12162. statCoopResetProcedure: LONGINT;
  12163. statCoopTraceModule: LONGINT;
  12164. PROCEDURE ResetStatistics*;
  12165. BEGIN
  12166. statCoopResetVariables := 0;
  12167. statCoopModifyAssignments := 0;
  12168. statCoopNilCheck:= 0;
  12169. statCoopSwitch:= 0;
  12170. statCoopAssignProcedure:= 0;
  12171. statCoopTraceMethod:= 0;
  12172. statCoopResetProcedure:= 0;
  12173. statCoopTraceModule:= 0;
  12174. END ResetStatistics;
  12175. PROCEDURE Statistics*;
  12176. BEGIN
  12177. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12178. TRACE(statCoopNilCheck, statCoopSwitch);
  12179. TRACE(statCoopAssignProcedure,
  12180. statCoopTraceMethod,
  12181. statCoopResetProcedure,
  12182. statCoopTraceModule)
  12183. END Statistics;
  12184. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12185. VAR h: LONGINT;
  12186. BEGIN
  12187. WHILE b # 0 DO
  12188. h := a MOD b;
  12189. a := b;
  12190. b := h;
  12191. END;
  12192. RETURN a
  12193. END GCD;
  12194. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12195. BEGIN
  12196. RETURN a*b DIV GCD(a,b)
  12197. END SCM;
  12198. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12199. BEGIN
  12200. (*TRACE(a,b);*)
  12201. IF a = 0 THEN RETURN b
  12202. ELSIF b = 0 THEN RETURN a
  12203. ELSE RETURN SCM(a,b)
  12204. END;
  12205. END CommonAlignment;
  12206. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12207. BEGIN
  12208. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12209. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12210. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12211. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12212. RETURN ~(parameter.type.resolved IS SyntaxTree.ArrayType) & ~(parameter.type.resolved IS SyntaxTree.MathArrayType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12213. END
  12214. END PassBySingleReference;
  12215. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12216. BEGIN
  12217. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12218. END PassInRegister;
  12219. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12220. VAR new: RegisterEntry;
  12221. BEGIN
  12222. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12223. IF queue = NIL THEN
  12224. queue := new
  12225. ELSE
  12226. new.next := queue;
  12227. IF queue#NIL THEN queue.prev := new END;
  12228. queue := new
  12229. END;
  12230. END AddRegisterEntry;
  12231. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12232. VAR this: RegisterEntry;
  12233. BEGIN
  12234. this := queue;
  12235. WHILE (this # NIL) & (this.register # register) DO
  12236. this := this.next;
  12237. END;
  12238. IF this = NIL THEN
  12239. RETURN FALSE
  12240. END;
  12241. ASSERT(this # NIL);
  12242. IF this = queue THEN queue := queue.next END;
  12243. IF this.prev # NIL THEN this.prev.next := this.next END;
  12244. IF this.next # NIL THEN this.next.prev := this.prev END;
  12245. RETURN TRUE
  12246. END RemoveRegisterEntry;
  12247. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12248. BEGIN ASSERT(cond);
  12249. END Assert;
  12250. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12251. BEGIN
  12252. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12253. END ReusableRegister;
  12254. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12255. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12256. BEGIN
  12257. procedure := moduleScope.bodyProcedure;
  12258. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12259. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12260. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12261. procedure.SetScope(moduleScope);
  12262. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12263. procedure.SetAccess(SyntaxTree.Hidden);
  12264. moduleScope.SetBodyProcedure(procedure);
  12265. moduleScope.AddProcedure(procedure);
  12266. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12267. END;
  12268. END EnsureBodyProcedure;
  12269. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12270. VAR import: SyntaxTree.Import;
  12271. selfName: SyntaxTree.IdentifierString;
  12272. module: SyntaxTree.Module;
  12273. BEGIN
  12274. scope.ownerModule.GetName(selfName);
  12275. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12276. module := scope.ownerModule
  12277. ELSE
  12278. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12279. IF import = NIL THEN
  12280. RETURN NIL
  12281. ELSIF import.module = NIL THEN
  12282. RETURN NIL
  12283. ELSE module := import.module
  12284. END;
  12285. END;
  12286. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12287. END GetSymbol;
  12288. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12289. BEGIN
  12290. op.mode := mode;
  12291. IntermediateCode.InitOperand(op.op);
  12292. IntermediateCode.InitOperand(op.tag);
  12293. IntermediateCode.InitOperand(op.extra);
  12294. op.dimOffset := 0;
  12295. END InitOperand;
  12296. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12297. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12298. BEGIN RETURN IntermediateCode.GetType(system, type)
  12299. END GetType;
  12300. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12301. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12302. BEGIN
  12303. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12304. (*
  12305. UniqueId(name,module,name,adr);
  12306. *)
  12307. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12308. constant.SetValue(value);
  12309. constant.SetAccess(SyntaxTree.Hidden);
  12310. module.moduleScope.AddConstant(constant);
  12311. constant.SetScope(module.moduleScope);
  12312. RETURN constant
  12313. END BuildConstant;
  12314. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12315. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12316. BEGIN
  12317. variable := scope.firstVariable;
  12318. WHILE variable # NIL DO
  12319. IF variable.NeedsTrace() THEN
  12320. RETURN TRUE;
  12321. END;
  12322. variable := variable.nextVariable;
  12323. END;
  12324. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12325. WHILE parameter # NIL DO
  12326. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12327. RETURN TRUE;
  12328. END;
  12329. parameter := parameter.nextParameter;
  12330. END;
  12331. RETURN FALSE;
  12332. END HasPointers;
  12333. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12334. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12335. END IsVariableParameter;
  12336. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12337. VAR parameter: SyntaxTree.Parameter;
  12338. BEGIN
  12339. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12340. WHILE parameter # NIL DO
  12341. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12342. IF parameter.movable THEN RETURN TRUE END;
  12343. parameter := parameter.nextParameter;
  12344. END;
  12345. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12346. END HasVariableParameters;
  12347. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12348. BEGIN
  12349. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12350. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12351. END HasExplicitTraceMethod;
  12352. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12353. BEGIN
  12354. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12355. IF (expression IS SyntaxTree.IntegerValue) THEN
  12356. val := expression(SyntaxTree.IntegerValue).value;
  12357. RETURN TRUE
  12358. ELSE
  12359. RETURN FALSE
  12360. END;
  12361. END IsIntegerConstant;
  12362. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12363. BEGIN
  12364. IF val <= 0 THEN RETURN FALSE END;
  12365. exp := 0;
  12366. WHILE ~ODD(val) DO
  12367. val := val DIV 2;
  12368. INC(exp)
  12369. END;
  12370. RETURN val = 1
  12371. END PowerOf2;
  12372. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12373. VAR procedure: SyntaxTree.Procedure;
  12374. BEGIN
  12375. procedure := record.recordScope.constructor;
  12376. IF procedure = NIL THEN
  12377. record := record.GetBaseRecord();
  12378. IF record # NIL THEN
  12379. procedure := GetConstructor(record)
  12380. END;
  12381. END;
  12382. RETURN procedure;
  12383. END GetConstructor;
  12384. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12385. BEGIN
  12386. value := SHORT(op.intValue);
  12387. RETURN op.mode = IntermediateCode.ModeImmediate;
  12388. END IsIntegerImmediate;
  12389. (** whether a type strictily is a pointer to record or object type
  12390. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12391. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12392. BEGIN
  12393. IF type = NIL THEN
  12394. RETURN FALSE
  12395. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12396. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12397. ELSE
  12398. RETURN FALSE
  12399. END
  12400. END IsStrictlyPointerToRecord;
  12401. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12402. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12403. END IsUnsafePointer;
  12404. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12405. BEGIN type := type.resolved;
  12406. IF type IS SyntaxTree.PointerType THEN
  12407. type := type(SyntaxTree.PointerType).pointerBase;
  12408. type := type.resolved;
  12409. IF type IS SyntaxTree.RecordType THEN
  12410. recordType := type(SyntaxTree.RecordType);
  12411. RETURN TRUE
  12412. ELSE
  12413. RETURN FALSE
  12414. END
  12415. ELSIF type IS SyntaxTree.RecordType THEN
  12416. recordType := type(SyntaxTree.RecordType);
  12417. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12418. ELSIF type IS SyntaxTree.ObjectType THEN
  12419. RETURN TRUE
  12420. ELSIF type IS SyntaxTree.AnyType THEN
  12421. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12422. ELSE
  12423. RETURN FALSE
  12424. END;
  12425. END IsPointerToRecord;
  12426. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12427. BEGIN
  12428. type := type.resolved;
  12429. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12430. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12431. END IsArrayOfSystemByte;
  12432. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12433. BEGIN
  12434. IF type = NIL THEN RETURN FALSE END;
  12435. type := type.resolved;
  12436. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12437. END IsOpenArray;
  12438. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12439. BEGIN
  12440. IF type = NIL THEN RETURN FALSE END;
  12441. type := type.resolved;
  12442. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12443. END IsSemiDynamicArray;
  12444. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12445. BEGIN
  12446. IF type = NIL THEN RETURN FALSE END;
  12447. type := type.resolved;
  12448. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12449. END IsStaticArray;
  12450. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12451. VAR size: LONGINT;
  12452. BEGIN
  12453. size := 1;
  12454. WHILE (IsStaticArray(type)) DO
  12455. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12456. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12457. END;
  12458. RETURN size;
  12459. END StaticArrayNumElements;
  12460. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12461. BEGIN
  12462. WHILE (IsStaticArray(type)) DO
  12463. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12464. END;
  12465. RETURN type;
  12466. END StaticArrayBaseType;
  12467. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12468. BEGIN
  12469. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12470. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12471. END;
  12472. RETURN type;
  12473. END ArrayBaseType;
  12474. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12475. BEGIN
  12476. IF type = NIL THEN RETURN FALSE END;
  12477. type := type.resolved;
  12478. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12479. END IsDelegate;
  12480. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12481. VAR i: LONGINT;
  12482. BEGIN
  12483. i := 0; type := type.resolved;
  12484. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12485. INC(i);
  12486. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12487. END;
  12488. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12489. INC(i);
  12490. type := type(SyntaxTree.MathArrayType).arrayBase;
  12491. IF type # NIL THEN type := type.resolved END;
  12492. END;
  12493. RETURN i
  12494. END DynamicDim;
  12495. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12496. BEGIN
  12497. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12498. type := type(SyntaxTree.ArrayType).arrayBase;
  12499. END;
  12500. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12501. type := type(SyntaxTree.MathArrayType).arrayBase;
  12502. END;
  12503. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12504. END StaticSize;
  12505. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12506. BEGIN
  12507. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12508. END IsImmediate;
  12509. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12510. BEGIN
  12511. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12512. END IsAddress;
  12513. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12514. BEGIN
  12515. RETURN (x.mode = IntermediateCode.ModeRegister);
  12516. END IsRegister;
  12517. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12518. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12519. BEGIN
  12520. typeDeclaration := recordType.typeDeclaration;
  12521. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12522. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12523. END GetRecordTypeName;
  12524. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12525. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12526. BEGIN
  12527. parSize := 0;
  12528. IF StructuredReturnType(procedureType) THEN
  12529. parameter := procedureType.returnParameter;
  12530. INC(parSize,system.SizeOfParameter(parameter));
  12531. parSize := parSize + (-parSize) MOD system.addressSize;
  12532. END;
  12533. parameter :=procedureType.lastParameter;
  12534. WHILE (parameter # NIL) DO
  12535. INC(parSize,system.SizeOfParameter(parameter));
  12536. parSize := parSize + (-parSize) MOD system.addressSize;
  12537. parameter := parameter.prevParameter;
  12538. END;
  12539. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  12540. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12541. RETURN ToMemoryUnits(system,parSize)
  12542. END ParametersSize;
  12543. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  12544. BEGIN
  12545. IF type = NIL THEN RETURN FALSE
  12546. ELSE
  12547. type := type.resolved;
  12548. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  12549. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  12550. END
  12551. END ReturnedAsParameter;
  12552. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  12553. BEGIN
  12554. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  12555. END StructuredReturnType;
  12556. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12557. BEGIN
  12558. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12559. END IsNested;
  12560. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12561. BEGIN
  12562. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12563. scope := scope.outerScope;
  12564. END;
  12565. RETURN scope # NIL;
  12566. END InCellScope;
  12567. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12568. BEGIN
  12569. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12570. RETURN 0
  12571. ELSE
  12572. *)
  12573. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12574. (*END;*)
  12575. END ProcedureParametersSize;
  12576. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12577. VAR dataUnit: LONGINT;
  12578. BEGIN dataUnit := system.dataUnit;
  12579. ASSERT(size MOD system.dataUnit = 0);
  12580. RETURN size DIV system.dataUnit
  12581. END ToMemoryUnits;
  12582. PROCEDURE Get*(): Backend.Backend;
  12583. VAR backend: IntermediateBackend;
  12584. BEGIN NEW(backend); RETURN backend
  12585. END Get;
  12586. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12587. VAR instruction: IntermediateCode.Instruction;
  12588. BEGIN
  12589. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12590. RETURN instruction
  12591. END Nop;
  12592. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12593. VAR instruction: IntermediateCode.Instruction;
  12594. BEGIN
  12595. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12596. RETURN instruction
  12597. END Mov;
  12598. (* like Mov but ensures that no new register will be allocated for dest *)
  12599. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12600. VAR instruction: IntermediateCode.Instruction;
  12601. BEGIN
  12602. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12603. RETURN instruction
  12604. END MovReplace;
  12605. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12606. VAR instruction: IntermediateCode.Instruction;
  12607. BEGIN
  12608. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12609. RETURN instruction
  12610. END Conv;
  12611. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12612. BEGIN
  12613. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12614. END SysvABI;
  12615. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12616. BEGIN
  12617. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12618. END SysvABIorWINAPI;
  12619. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12620. VAR instruction: IntermediateCode.Instruction;
  12621. BEGIN
  12622. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12623. RETURN instruction
  12624. END Call;
  12625. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12626. VAR op1, op2, op3: IntermediateCode.Operand;
  12627. VAR instruction: IntermediateCode.Instruction;
  12628. BEGIN
  12629. IntermediateCode.InitNumber(op1,pcOffset);
  12630. IntermediateCode.InitNumber(op2,callingConvention);
  12631. IntermediateCode.InitNumber(op3,unwind);
  12632. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12633. RETURN instruction
  12634. END Exit;
  12635. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12636. VAR instruction: IntermediateCode.Instruction;
  12637. BEGIN
  12638. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12639. RETURN instruction
  12640. END Return;
  12641. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12642. VAR instruction: IntermediateCode.Instruction;
  12643. BEGIN
  12644. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12645. RETURN instruction
  12646. END Result;
  12647. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12648. VAR op1: IntermediateCode.Operand;
  12649. VAR instruction: IntermediateCode.Instruction;
  12650. BEGIN
  12651. IntermediateCode.InitNumber(op1,nr);
  12652. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12653. RETURN instruction
  12654. END Trap;
  12655. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12656. VAR instruction: IntermediateCode.Instruction;
  12657. BEGIN
  12658. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12659. RETURN instruction
  12660. END Br;
  12661. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12662. VAR instruction: IntermediateCode.Instruction;
  12663. BEGIN
  12664. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12665. RETURN instruction
  12666. END Breq;
  12667. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12668. VAR instruction: IntermediateCode.Instruction;
  12669. BEGIN
  12670. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12671. RETURN instruction
  12672. END Brne;
  12673. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12674. VAR instruction: IntermediateCode.Instruction;
  12675. BEGIN
  12676. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12677. RETURN instruction
  12678. END Brge;
  12679. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12680. VAR instruction: IntermediateCode.Instruction;
  12681. BEGIN
  12682. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12683. RETURN instruction
  12684. END Brlt;
  12685. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12686. VAR instruction: IntermediateCode.Instruction;
  12687. BEGIN
  12688. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12689. RETURN instruction
  12690. END Pop;
  12691. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12692. VAR instruction: IntermediateCode.Instruction;
  12693. BEGIN
  12694. ASSERT(op.mode # IntermediateCode.Undefined);
  12695. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12696. RETURN instruction
  12697. END Push;
  12698. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12699. VAR instruction: IntermediateCode.Instruction;
  12700. BEGIN
  12701. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12702. RETURN instruction
  12703. END Neg;
  12704. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12705. VAR instruction: IntermediateCode.Instruction;
  12706. BEGIN
  12707. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12708. RETURN instruction
  12709. END Not;
  12710. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12711. VAR instruction: IntermediateCode.Instruction;
  12712. BEGIN
  12713. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12714. RETURN instruction
  12715. END Abs;
  12716. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12717. VAR instruction: IntermediateCode.Instruction;
  12718. BEGIN
  12719. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12720. ASSERT(~IsImmediate(instruction.op1));
  12721. RETURN instruction
  12722. END Mul;
  12723. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12724. VAR instruction: IntermediateCode.Instruction;
  12725. BEGIN
  12726. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12727. RETURN instruction
  12728. END Div;
  12729. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12730. VAR instruction: IntermediateCode.Instruction;
  12731. BEGIN
  12732. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12733. RETURN instruction
  12734. END Mod;
  12735. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12736. VAR instruction: IntermediateCode.Instruction;
  12737. BEGIN
  12738. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12739. RETURN instruction
  12740. END Sub;
  12741. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12742. VAR instruction: IntermediateCode.Instruction;
  12743. BEGIN
  12744. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12745. RETURN instruction
  12746. END Add;
  12747. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12748. VAR instruction: IntermediateCode.Instruction;
  12749. BEGIN
  12750. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12751. RETURN instruction
  12752. END And;
  12753. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12754. VAR instruction: IntermediateCode.Instruction;
  12755. BEGIN
  12756. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12757. RETURN instruction
  12758. END Or;
  12759. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12760. VAR instruction: IntermediateCode.Instruction;
  12761. BEGIN
  12762. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12763. RETURN instruction
  12764. END Xor;
  12765. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12766. VAR instruction: IntermediateCode.Instruction;
  12767. BEGIN
  12768. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12769. RETURN instruction
  12770. END Shl;
  12771. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12772. VAR instruction: IntermediateCode.Instruction;
  12773. BEGIN
  12774. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12775. RETURN instruction
  12776. END Shr;
  12777. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12778. VAR instruction: IntermediateCode.Instruction;
  12779. BEGIN
  12780. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12781. RETURN instruction
  12782. END Rol;
  12783. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12784. VAR instruction: IntermediateCode.Instruction;
  12785. BEGIN
  12786. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12787. RETURN instruction
  12788. END Ror;
  12789. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12790. VAR instruction: IntermediateCode.Instruction;
  12791. BEGIN
  12792. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12793. RETURN instruction
  12794. END Cas;
  12795. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12796. VAR instruction: IntermediateCode.Instruction;
  12797. BEGIN
  12798. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12799. RETURN instruction
  12800. END Copy;
  12801. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12802. VAR instruction: IntermediateCode.Instruction;
  12803. BEGIN
  12804. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12805. RETURN instruction
  12806. END Fill;
  12807. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12808. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12809. BEGIN
  12810. string := IntermediateCode.String(s);
  12811. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12812. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12813. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12814. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12815. RETURN instruction
  12816. END Asm;
  12817. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12818. VAR instruction: IntermediateCode.Instruction;
  12819. BEGIN
  12820. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12821. RETURN instruction
  12822. END Data;
  12823. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12824. VAR instruction: IntermediateCode.Instruction;
  12825. BEGIN
  12826. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12827. IntermediateCode.SetSubType(instruction, subtype);
  12828. RETURN instruction
  12829. END SpecialInstruction;
  12830. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12831. VAR op1: IntermediateCode.Operand;
  12832. VAR instruction: IntermediateCode.Instruction;
  12833. BEGIN
  12834. (*! generate a warning if size exceeds a certain limit *)
  12835. (*
  12836. ASSERT(bytes < 1000000); (* sanity check *)
  12837. *)
  12838. ASSERT(0 <= units); (* sanity check *)
  12839. IntermediateCode.InitNumber(op1,units);
  12840. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12841. RETURN instruction
  12842. END Reserve;
  12843. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12844. VAR op1: IntermediateCode.Operand;
  12845. VAR instruction: IntermediateCode.Instruction;
  12846. BEGIN
  12847. IntermediateCode.InitNumber(op1,position.start);
  12848. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12849. RETURN instruction
  12850. END LabelInstruction;
  12851. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12852. VAR pc: LONGINT;
  12853. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12854. BEGIN
  12855. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12856. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12857. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12858. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12859. IF instr.op1.type.form = IntermediateCode.Float THEN
  12860. RETURN instr.op1.floatValue = op.floatValue
  12861. ELSE
  12862. RETURN instr.op1.intValue = op.intValue
  12863. END;
  12864. END ProvidesValue;
  12865. BEGIN
  12866. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12867. pc := 0;
  12868. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12869. INC(pc);
  12870. END;
  12871. IF pc = data.pc THEN
  12872. data.Emit(Data(Basic.invalidPosition,vop));
  12873. END;
  12874. RETURN pc
  12875. END EnterImmediate;
  12876. PROCEDURE Init;
  12877. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12878. BEGIN
  12879. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12880. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12881. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12882. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12883. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12884. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12885. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12886. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12887. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12888. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12889. IntermediateCode.InitOperand(emptyOperand);
  12890. FOR i := 0 TO NumberSystemCalls-1 DO
  12891. name := "@SystemCall";
  12892. Basic.AppendNumber(name,i);
  12893. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12894. END;
  12895. END Init;
  12896. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12897. BEGIN
  12898. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12899. END IsExported;
  12900. BEGIN
  12901. Init;
  12902. END FoxIntermediateBackend.
  12903. Compiler.Compile FoxIntermediateBackend.Mod ~
  12904. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12905. # StaticLinker.Link --fileFormat=PE32 --fileName=A2Z.exe --extension=GofW --displacement=401000H --path="/temp/obg/" Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands Files WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~
  12906. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12907. # StaticLinker.Link --fileFormat=PE32 --fileName=A2H.exe --extension=GofW --displacement=401000H Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands Files WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~
  12908. FSTools.CloseFiles A2Z.exe ~
  12909. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12910. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12911. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12912. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12913. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12914. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12915. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12916. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12917. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12918. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12919. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod