FoxIntermediateBackend.Mod 558 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211
  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 ~SemanticChecker.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 ~SemanticChecker.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
  1051. this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap;
  1052. isAddress: BOOLEAN;
  1053. END;
  1054. SymbolMapper = OBJECT
  1055. VAR
  1056. first: SymbolMap;
  1057. PROCEDURE & Init;
  1058. BEGIN
  1059. first := NIL;
  1060. END Init;
  1061. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; isAddress: BOOLEAN);
  1062. VAR new: SymbolMap;
  1063. BEGIN
  1064. NEW(new); new.this := this; new.to := to; new.tag := tag; new.isAddress := isAddress;
  1065. new.next := first; first := new;
  1066. END Add;
  1067. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1068. VAR s: SymbolMap;
  1069. BEGIN
  1070. s := first;
  1071. WHILE (s # NIL) & (s.this # this) DO
  1072. s := s.next
  1073. END;
  1074. RETURN s
  1075. END Get;
  1076. END SymbolMapper;
  1077. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1078. VAR
  1079. system: Global.System;
  1080. section: IntermediateCode.Section;
  1081. module: Sections.Module;
  1082. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1083. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1084. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1085. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1086. checker: SemanticChecker.Checker;
  1087. backend: IntermediateBackend;
  1088. meta: MetaDataGenerator;
  1089. position: Position;
  1090. moduleSelf: SyntaxTree.Variable;
  1091. (* variables for hand over of variables / temporary state *)
  1092. currentScope: SyntaxTree.Scope;
  1093. constantDeclaration : SyntaxTree.Symbol;
  1094. result: Operand; (* result of the most recent expression / statement *)
  1095. destination: IntermediateCode.Operand;
  1096. arrayDestinationTag: IntermediateCode.Operand;
  1097. arrayDestinationDimension:LONGINT;
  1098. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1099. conditional: BOOLEAN;
  1100. trueLabel, falseLabel, exitLabel: Label;
  1101. locked: BOOLEAN;
  1102. (*
  1103. usedRegisters: Registers;
  1104. *)
  1105. registerUsageCount: RegisterUsageCount;
  1106. usedRegisters: RegisterEntry;
  1107. (* useful operands and types *)
  1108. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1109. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1110. commentPrintout: Printout.Printer;
  1111. dump: Streams.Writer;
  1112. tagsAvailable : BOOLEAN;
  1113. supportedInstruction: SupportedInstructionProcedure;
  1114. supportedImmediate: SupportedImmediateProcedure;
  1115. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1116. emitLabels: BOOLEAN;
  1117. runtimeModuleName : SyntaxTree.IdentifierString;
  1118. newObjectFile: BOOLEAN;
  1119. indexCounter: LONGINT;
  1120. profile: BOOLEAN;
  1121. profileId, profileInit: IntermediateCode.Section;
  1122. profileInitPatchPosition: LONGINT;
  1123. numberProcedures: LONGINT;
  1124. procedureResultDesignator : SyntaxTree.Designator;
  1125. operatorInitializationCodeSection: IntermediateCode.Section;
  1126. fingerPrinter: FingerPrinter.FingerPrinter;
  1127. temporaries: Variables;
  1128. canBeLoaded : BOOLEAN;
  1129. currentIsInline: BOOLEAN;
  1130. currentMapper: SymbolMapper;
  1131. currentInlineExit: Label;
  1132. moduleBodySection: IntermediateCode.Section;
  1133. NeedDescriptor : BOOLEAN;
  1134. cooperativeSwitches: BOOLEAN;
  1135. lastSwitchPC: LONGINT;
  1136. isUnchecked: BOOLEAN;
  1137. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1138. newObjectFile: BOOLEAN);
  1139. BEGIN
  1140. SELF.system := system;
  1141. SELF.runtimeModuleName := runtime;
  1142. currentScope := NIL;
  1143. hiddenPointerType := NIL;
  1144. delegatePointerType := NIL;
  1145. awaitProcCounter := 0;
  1146. labelId := 0; constId := 0; labelId := 0;
  1147. SELF.checker := checker;
  1148. SELF.backend := backend;
  1149. position := Basic.invalidPosition;
  1150. conditional := FALSE;
  1151. locked := FALSE;
  1152. InitOperand(result,ModeUndefined);
  1153. addressType := IntermediateCode.GetType(system,system.addressType);
  1154. setType := IntermediateCode.GetType(system,system.setType);
  1155. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1156. byteType := IntermediateCode.GetType(system, system.byteType);
  1157. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1158. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1159. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1160. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1161. nil := IntermediateCode.Immediate(addressType,0);
  1162. one := IntermediateCode.Immediate(addressType,1);
  1163. IntermediateCode.InitOperand(destination);
  1164. tagsAvailable := TRUE;
  1165. supportedInstruction := supportedInstructionProcedure;
  1166. supportedImmediate := supportedImmediateProcedure;
  1167. inData := FALSE;
  1168. SELF.emitLabels := emitLabels;
  1169. IntermediateCode.InitOperand(arrayDestinationTag);
  1170. bool := IntermediateCode.GetType(system,system.booleanType);
  1171. IntermediateCode.InitImmediate(false,bool,0);
  1172. IntermediateCode.InitImmediate(true,bool,1);
  1173. SELF.newObjectFile := newObjectFile;
  1174. indexCounter := 0;
  1175. NEW(registerUsageCount);
  1176. usedRegisters := NIL;
  1177. procedureResultDesignator := NIL;
  1178. NEW(fingerPrinter);
  1179. NEW(temporaries);
  1180. currentIsInline := FALSE;
  1181. NeedDescriptor := FALSE;
  1182. isUnchecked := backend.noRuntimeChecks;
  1183. END Init;
  1184. TYPE Context = RECORD
  1185. section: IntermediateCode.Section;
  1186. registerUsageCount: RegisterUsageCount;
  1187. usedRegisters: RegisterEntry;
  1188. END;
  1189. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1190. VAR context: Context;
  1191. BEGIN
  1192. context.section := section;
  1193. context.registerUsageCount := registerUsageCount;
  1194. context.usedRegisters := usedRegisters;
  1195. section := new;
  1196. NEW(registerUsageCount);
  1197. usedRegisters := NIL;
  1198. RETURN context;
  1199. END SwitchContext;
  1200. PROCEDURE ReturnToContext(context: Context);
  1201. BEGIN
  1202. section := context.section;
  1203. registerUsageCount := context.registerUsageCount;
  1204. usedRegisters := context.usedRegisters;
  1205. END ReturnToContext;
  1206. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1207. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1208. BEGIN
  1209. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1210. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1211. END;
  1212. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1213. section.SetExported(IsExported(syntaxTreeSymbol));
  1214. RETURN section
  1215. END NewSection;
  1216. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1217. VAR new: LONGINT;
  1218. BEGIN
  1219. new := registerUsageCount.Next(type,class);
  1220. UseRegister(new);
  1221. RETURN new
  1222. END AcquireRegister;
  1223. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1224. VAR
  1225. fingerPrint: SyntaxTree.FingerPrint;
  1226. fingerPrintString: ARRAY 32 OF CHAR;
  1227. BEGIN
  1228. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1229. string := "[";
  1230. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1231. Strings.Append(string, fingerPrintString);
  1232. Strings.Append(string, "]");
  1233. END GetFingerprintString;
  1234. (** get the name for the code section that represens a certain symbol
  1235. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1236. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1237. VAR string: ARRAY 32 OF CHAR;
  1238. BEGIN
  1239. Global.GetSymbolSegmentedName(symbol, name);
  1240. (* if the symbol is an operator, then append the fingerprint to the name *)
  1241. IF symbol IS SyntaxTree.Operator THEN
  1242. GetFingerprintString(symbol, string);
  1243. Basic.AppendToSegmentedName(name,string);
  1244. END
  1245. END GetCodeSectionNameForSymbol;
  1246. (** get the name for the code section that represens a certain symbol
  1247. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1248. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1249. VAR string: ARRAY 32 OF CHAR;
  1250. BEGIN
  1251. Global.GetSymbolNameInScope(symbol, scope, name);
  1252. (* if the symbol is an operator, then append the fingerprint to the name *)
  1253. IF symbol IS SyntaxTree.Operator THEN
  1254. GetFingerprintString(symbol, string);
  1255. Strings.Append(name, string);
  1256. END
  1257. END GetCodeSectionNameForSymbolInScope;
  1258. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1259. BEGIN
  1260. IF dump # NIL THEN
  1261. dump.String("enter "); dump.String(s); dump.Ln;
  1262. END;
  1263. END TraceEnter;
  1264. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1265. BEGIN
  1266. IF dump # NIL THEN
  1267. dump.String("exit "); dump.String(s); dump.Ln;
  1268. END;
  1269. END TraceExit;
  1270. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1271. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1272. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1273. VAR i: LONGINT;
  1274. BEGIN
  1275. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1276. IF op.rule # NIL THEN
  1277. FOR i := 0 TO LEN(op.rule)-1 DO
  1278. CheckRegister(op.rule[i])
  1279. END;
  1280. END;
  1281. END CheckRegister;
  1282. BEGIN
  1283. CheckRegister(instruction.op1);
  1284. CheckRegister(instruction.op2);
  1285. CheckRegister(instruction.op3);
  1286. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1287. ELSE section.Emit(instruction);
  1288. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1289. END;
  1290. END Emit;
  1291. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1292. BEGIN
  1293. IF backend.cooperative THEN
  1294. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1295. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1296. ELSE
  1297. Emit(Trap(position,trapNo));
  1298. END;
  1299. END EmitTrap;
  1300. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1301. VAR name: Basic.SegmentedName;
  1302. VAR op1, op2, reg: IntermediateCode.Operand;
  1303. VAR call, nocall: Label;
  1304. VAR parametersSize: LONGINT;
  1305. VAR prevSection: IntermediateCode.Section;
  1306. VAR prevDump: Streams.Writer;
  1307. VAR body: SyntaxTree.Body;
  1308. VAR procedureType: SyntaxTree.ProcedureType;
  1309. BEGIN
  1310. IF procedure # NIL THEN
  1311. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1312. ELSE procedureType := NIL;
  1313. END;
  1314. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1315. prevSection := SELF.section;
  1316. SELF.section := section;
  1317. prevDump := dump;
  1318. dump := section.comments;
  1319. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1320. IF backend.hasLinkRegister THEN
  1321. Emit(Push(Basic.invalidPosition, lr));
  1322. END;
  1323. Emit(Push(Basic.invalidPosition,fp));
  1324. END;
  1325. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1326. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1327. GetCodeSectionNameForSymbol(procedure, name);
  1328. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1329. ELSE
  1330. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1331. END;
  1332. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1333. Emit(Push(Basic.invalidPosition,op1));
  1334. Emit(Mov(Basic.invalidPosition,fp, sp));
  1335. body := procedure.procedureScope.body;
  1336. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1337. NEW(call, section);
  1338. NEW(nocall, section);
  1339. reg := NewRegisterOperand(addressType);
  1340. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1341. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1342. BrltL(call, sp, reg);
  1343. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1344. BrgeL(nocall, sp, op2);
  1345. call.Resolve(section.pc);
  1346. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1347. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1348. Emit(Push(Basic.invalidPosition, op2));
  1349. Emit(Push(Basic.invalidPosition, reg));
  1350. ReleaseIntermediateOperand(reg);
  1351. CallThis(position, "Activities","ExpandStack",2);
  1352. Emit(Result(Basic.invalidPosition, sp));
  1353. nocall.Resolve(section.pc);
  1354. END;
  1355. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1356. IF procedure # NIL THEN
  1357. body := procedure.procedureScope.body;
  1358. ELSE
  1359. body := NIL;
  1360. END;
  1361. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1362. Emit(Push(Basic.invalidPosition, one)) ;
  1363. procedureType.SetParametersOffset(1);
  1364. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1365. END;
  1366. Emit(Mov(Basic.invalidPosition, fp, sp));
  1367. END;
  1368. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1369. SELF.section := prevSection;
  1370. dump := prevDump;
  1371. END EmitEnter;
  1372. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1373. VAR op1,op2: IntermediateCode.Operand;
  1374. VAR instruction: IntermediateCode.Instruction;
  1375. BEGIN
  1376. IntermediateCode.InitNumber(op1,callconv);
  1377. IntermediateCode.InitNumber(op2,varSize);
  1378. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1379. RETURN instruction
  1380. END Enter;
  1381. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1382. VAR op1: IntermediateCode.Operand;
  1383. VAR instruction: IntermediateCode.Instruction;
  1384. BEGIN
  1385. IntermediateCode.InitNumber(op1,callconv);
  1386. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1387. RETURN instruction
  1388. END Leave;
  1389. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1390. VAR prevSection: IntermediateCode.Section;
  1391. VAR op2, size: IntermediateCode.Operand;
  1392. VAR body: SyntaxTree.Body;
  1393. BEGIN
  1394. prevSection := SELF.section;
  1395. SELF.section := section;
  1396. Emit(Leave(position, callconv));
  1397. IF procedure # NIL THEN
  1398. body := procedure.procedureScope.body;
  1399. ELSE
  1400. body := NIL;
  1401. END;
  1402. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1403. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1404. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1405. Emit(Add(position, sp, fp, op2));
  1406. ELSE
  1407. Emit(Mov(position, sp, fp));
  1408. END;
  1409. Emit(Pop(position, fp));
  1410. END;
  1411. SELF.section := prevSection;
  1412. END EmitLeave;
  1413. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1414. VAR m: SymbolMap;
  1415. BEGIN
  1416. position := x.position;
  1417. IF currentIsInline THEN
  1418. m := currentMapper.Get(x);
  1419. IF m # NIL THEN
  1420. (*
  1421. Printout.Info("mapping from", x);
  1422. Printout.Info("mapping to ", m.to);
  1423. *)
  1424. m.to.Accept(SELF);
  1425. op := result;
  1426. IF m.tag # NIL THEN
  1427. ReleaseIntermediateOperand(result.tag);
  1428. m.tag.Accept(SELF);
  1429. op.tag := result.op;
  1430. ReleaseIntermediateOperand(result.tag);
  1431. END;
  1432. RETURN
  1433. END;
  1434. END;
  1435. x.Accept(SELF);
  1436. op := result;
  1437. END Symbol;
  1438. PROCEDURE Expression(x: SyntaxTree.Expression);
  1439. BEGIN
  1440. position := x.position;
  1441. constantDeclaration := NIL;
  1442. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1443. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1444. END;
  1445. IF x.resolved # NIL THEN
  1446. x.resolved.Accept(SELF)
  1447. ELSE
  1448. x.Accept(SELF)
  1449. END;
  1450. (* check this, was commented out in ActiveCells3 *)
  1451. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1452. Error(x.position, "unsupported modifier");
  1453. END;
  1454. END Expression;
  1455. (*
  1456. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1457. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1458. BEGIN
  1459. temporaries.GetUsage(current);
  1460. FOR i := 0 TO LEN(current)-1 DO
  1461. set := current[i] - previous[i];
  1462. IF set # {} THEN
  1463. FOR j := 0 TO MAX(SET)-1 DO
  1464. IF j IN set THEN
  1465. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1466. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1467. Symbol(variable, op);
  1468. MakeMemory(tmp,op.op,addressType,0);
  1469. ReleaseOperand(op);
  1470. Emit(Mov(position,tmp, nil ) );
  1471. ReleaseIntermediateOperand(tmp);
  1472. END;
  1473. END;
  1474. END;
  1475. END;
  1476. END;
  1477. END ResetUsedTemporaries;
  1478. *)
  1479. PROCEDURE Statement(x: SyntaxTree.Statement);
  1480. VAR use: VariableUse;
  1481. BEGIN
  1482. temporaries.GetUsage(use);
  1483. position := x.position;
  1484. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1485. IF commentPrintout # NIL THEN
  1486. commentPrintout.Statement(x);
  1487. dump.Ln;
  1488. (*dump.Update;*)
  1489. END;
  1490. x.Accept(SELF);
  1491. (*
  1492. CheckRegistersFree();
  1493. *)
  1494. (*ResetUsedTemporaries(use);*)
  1495. temporaries.SetUsage(use);
  1496. END Statement;
  1497. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1498. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1499. *)
  1500. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1501. BEGIN
  1502. ASSERT(op.mode # IntermediateCode.Undefined);
  1503. IF op.mode = IntermediateCode.ModeMemory THEN
  1504. ReuseCopy(res,op);
  1505. ELSE
  1506. res := op;
  1507. UseIntermediateOperand(res);
  1508. END;
  1509. IntermediateCode.AddOffset(res,offset);
  1510. IntermediateCode.MakeMemory(res,type);
  1511. END MakeMemory;
  1512. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1513. VAR mem: IntermediateCode.Operand;
  1514. BEGIN
  1515. MakeMemory(mem,res,type,offset);
  1516. ReleaseIntermediateOperand(res);
  1517. res := mem;
  1518. END ToMemory;
  1519. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1520. VAR mem: IntermediateCode.Operand;
  1521. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1522. componentType: SyntaxTree.Type;
  1523. BEGIN
  1524. type := type.resolved;
  1525. IF operand.mode = ModeReference THEN
  1526. IF type IS SyntaxTree.RangeType THEN
  1527. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1528. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1529. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1530. ReleaseOperand(operand);
  1531. operand.op := firstOp;
  1532. operand.tag := lastOp;
  1533. operand.extra := stepOp;
  1534. ELSIF type IS SyntaxTree.ComplexType THEN
  1535. componentType := type(SyntaxTree.ComplexType).componentType;
  1536. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1537. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1538. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1539. ReleaseOperand(operand);
  1540. operand.op := firstOp;
  1541. operand.tag := lastOp
  1542. ELSE
  1543. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1544. ReleaseIntermediateOperand(operand.op);
  1545. operand.op := mem;
  1546. END;
  1547. operand.mode := ModeValue;
  1548. END;
  1549. ASSERT(operand.mode = ModeValue);
  1550. END LoadValue;
  1551. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1552. VAR prevConditional: BOOLEAN;
  1553. BEGIN
  1554. prevConditional := conditional;
  1555. conditional := FALSE;
  1556. InitOperand(result, ModeUndefined);
  1557. Expression(x);
  1558. op := result;
  1559. LoadValue(op,x.type.resolved);
  1560. conditional := prevConditional;
  1561. END Evaluate;
  1562. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1563. VAR prevConditional: BOOLEAN;
  1564. BEGIN
  1565. prevConditional := conditional;
  1566. conditional := FALSE;
  1567. InitOperand(result,ModeUndefined);
  1568. Expression(x);
  1569. op := result;
  1570. (*
  1571. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1572. *)
  1573. conditional := prevConditional;
  1574. END Designate;
  1575. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1576. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1577. BEGIN
  1578. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1579. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1580. conditional := TRUE;
  1581. trueLabel := trueL; falseLabel := falseL;
  1582. Expression(x);
  1583. trueL := trueLabel; falseL := falseLabel;
  1584. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1585. END Condition;
  1586. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1587. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1588. BEGIN
  1589. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1590. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1591. RETURN op
  1592. END NewRegisterOperand;
  1593. PROCEDURE UnuseRegister(register: LONGINT);
  1594. BEGIN
  1595. IF (register > 0) THEN
  1596. register := registerUsageCount.Map(register);
  1597. registerUsageCount.DecUse(register);
  1598. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1599. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1600. END;
  1601. IF registerUsageCount.Use(register)=0 THEN
  1602. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1603. Warning(position, "register cannot be removed");
  1604. END;
  1605. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1606. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1607. END;
  1608. ELSIF registerUsageCount.Use(register)<0 THEN
  1609. Warning(position, "register removed too often");
  1610. IF dump # NIL THEN
  1611. dump.String("register removed too often"); dump.Ln; dump.Update;
  1612. END;
  1613. END;
  1614. END;
  1615. END UnuseRegister;
  1616. PROCEDURE UseRegister(register: LONGINT);
  1617. BEGIN
  1618. IF (register > 0) THEN
  1619. register := registerUsageCount.Map(register);
  1620. registerUsageCount.IncUse(register);
  1621. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1622. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1623. END;
  1624. IF registerUsageCount.Use(register)=1 THEN
  1625. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1626. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1627. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1628. END;
  1629. END;
  1630. END;
  1631. END UseRegister;
  1632. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1633. BEGIN
  1634. UnuseRegister(op.register)
  1635. END ReleaseIntermediateOperand;
  1636. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1637. BEGIN
  1638. UseRegister(op.register)
  1639. END UseIntermediateOperand;
  1640. PROCEDURE ReleaseOperand(CONST op: Operand);
  1641. BEGIN
  1642. UnuseRegister(op.op.register);
  1643. UnuseRegister(op.tag.register);
  1644. UnuseRegister(op.extra.register);
  1645. END ReleaseOperand;
  1646. (* save registers marked in array "markedRegisters" to the stack
  1647. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1648. *)
  1649. PROCEDURE SaveRegisters();
  1650. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1651. BEGIN
  1652. entry := usedRegisters;
  1653. WHILE entry # NIL DO
  1654. type := registerUsageCount.used[entry.register].type;
  1655. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1656. Emit(Push(position,op));
  1657. entry := entry.next;
  1658. END;
  1659. END SaveRegisters;
  1660. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1661. BEGIN
  1662. saved := usedRegisters;
  1663. usedRegisters := NIL;
  1664. END ReleaseUsedRegisters;
  1665. (** remove parameter registers from used queue *)
  1666. PROCEDURE ReleaseParameterRegisters;
  1667. VAR entry,prev,next: RegisterEntry;
  1668. BEGIN
  1669. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1670. WHILE entry # NIL DO
  1671. next := entry.next;
  1672. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1673. registerUsageCount.DecUse(entry.register);
  1674. ASSERT(registerUsageCount.Use(entry.register)=0);
  1675. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1676. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1677. END;
  1678. ELSIF prev = NIL THEN
  1679. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1680. ELSE
  1681. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1682. END;
  1683. entry := next;
  1684. END;
  1685. END ReleaseParameterRegisters;
  1686. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1687. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1688. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1689. BEGIN
  1690. entry := saved;
  1691. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1692. entry := prev;
  1693. WHILE entry # NIL DO
  1694. prev := entry.prev;
  1695. type := entry.type;
  1696. class := entry.registerClass;
  1697. IntermediateCode.InitRegister(op,type,class,entry.register);
  1698. (*
  1699. new := registerUsageCount.Next(type,class);
  1700. registerUsageCount.Remap(entry.register,new);
  1701. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1702. dump.String("remap register "); dump.Int(entry.register,1);
  1703. dump.String("to "); dump.Int(new,1);
  1704. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1705. END;
  1706. entry.register := new;
  1707. *)
  1708. Emit(Pop(position,op));
  1709. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1710. entry := prev;
  1711. END;
  1712. (*
  1713. usedRegisters := saved;
  1714. *)
  1715. END RestoreRegisters;
  1716. PROCEDURE CheckRegistersFree;
  1717. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1718. BEGIN
  1719. IF usedRegisters # NIL THEN
  1720. r := usedRegisters;
  1721. WHILE r # NIL DO
  1722. warning := "register ";
  1723. Strings.AppendInt(warning, r.register);
  1724. Strings.Append(warning, " not released.");
  1725. Warning(position,warning);
  1726. r := r .next;
  1727. END;
  1728. END;
  1729. FOR i := 0 TO registerUsageCount.count-1 DO
  1730. IF registerUsageCount.used[i].count < 0 THEN
  1731. warning := "register ";
  1732. Strings.AppendInt(warning, i);
  1733. Strings.Append(warning, " unused too often.");
  1734. Warning(position,warning);
  1735. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1736. warning := "register ";
  1737. Strings.AppendInt(warning, i);
  1738. Strings.Append(warning, " not unused often enough.");
  1739. Warning(position,warning);
  1740. END;
  1741. END;
  1742. END CheckRegistersFree;
  1743. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1744. Otherwise allocate a new register.
  1745. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1746. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1747. BEGIN
  1748. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1749. UseIntermediateOperand(result);
  1750. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1751. UseIntermediateOperand(result);
  1752. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1753. END;
  1754. END Reuse2;
  1755. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1756. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1757. If not then allocate a new register.
  1758. Does NOT necessarily keep the content of src1 or src2 in result!
  1759. *)
  1760. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1761. BEGIN
  1762. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1765. UseIntermediateOperand(result);
  1766. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1767. result := alternative; alternative := emptyOperand;
  1768. UseIntermediateOperand(result);
  1769. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1770. END;
  1771. END Reuse2a;
  1772. (* like reuse2 but only one source *)
  1773. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1774. BEGIN
  1775. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1776. UseIntermediateOperand(result);
  1777. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1778. END;
  1779. END Reuse1;
  1780. (* like reuse2a but only one source *)
  1781. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1782. BEGIN
  1783. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1784. UseIntermediateOperand(result);
  1785. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1786. UseIntermediateOperand(result);
  1787. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1788. END;
  1789. END Reuse1a;
  1790. (* like reuse1 but guarantees that content of src1 is in result *)
  1791. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1792. BEGIN
  1793. IF ReusableRegister(src1) THEN
  1794. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1795. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1796. UseIntermediateOperand(result);
  1797. ELSE
  1798. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1799. Emit(Mov(position,result,src1));
  1800. END
  1801. END ReuseCopy;
  1802. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1803. BEGIN
  1804. IF ReusableRegister(src) THEN
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1806. ELSE
  1807. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1808. Emit(Mov(position,result,src));
  1809. ReleaseIntermediateOperand(src);
  1810. END
  1811. END TransferToRegister;
  1812. (** labels and branches **)
  1813. PROCEDURE NewLabel(): Label;
  1814. VAR label: Label;
  1815. BEGIN
  1816. NEW(label,section); RETURN label;
  1817. END NewLabel;
  1818. PROCEDURE SetLabel(label: Label);
  1819. BEGIN label.Resolve(section.pc);
  1820. END SetLabel;
  1821. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1822. BEGIN
  1823. ASSERT(label # NIL);
  1824. IF label.pc < 0 THEN (* label not yet set *)
  1825. label.AddFixup(section.pc);
  1826. END;
  1827. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1828. END LabelOperand;
  1829. PROCEDURE BrL(label: Label);
  1830. BEGIN
  1831. Emit(Br(position,LabelOperand(label)));
  1832. END BrL;
  1833. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1834. BEGIN
  1835. Emit(Brge(position,LabelOperand(label),left,right));
  1836. END BrgeL;
  1837. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1838. BEGIN
  1839. Emit(Brlt(position,LabelOperand(label),left,right));
  1840. END BrltL;
  1841. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1842. BEGIN
  1843. Emit(Breq(position,LabelOperand(label),left,right));
  1844. END BreqL;
  1845. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1846. BEGIN
  1847. Emit(Brne(position,LabelOperand(label),left,right));
  1848. END BrneL;
  1849. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1850. VAR new: IntermediateCode.Operand;
  1851. BEGIN
  1852. IF Trace THEN TraceEnter("Convert") END;
  1853. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1854. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1855. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1856. & (operand.offset = 0)
  1857. THEN
  1858. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1859. Emit(Conv(position,new,operand));
  1860. ELSE
  1861. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1862. Emit(Conv(position,new,operand));
  1863. ReleaseIntermediateOperand(operand);
  1864. END;
  1865. operand := new;
  1866. 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
  1867. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1868. ELSE
  1869. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1870. Emit(Conv(position,new,operand));
  1871. ReleaseIntermediateOperand(operand);
  1872. operand := new;
  1873. END;
  1874. IF Trace THEN TraceExit("Convert") END;
  1875. END Convert;
  1876. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1877. VAR exit: Label;
  1878. BEGIN
  1879. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1880. exit := NewLabel();
  1881. br(exit,left,right);
  1882. EmitTrap(position,trapNo);
  1883. SetLabel(exit);
  1884. END TrapC;
  1885. (** expressions *)
  1886. (** emit necessary runtime check for set elements **)
  1887. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1888. VAR max: IntermediateCode.Operand;
  1889. BEGIN
  1890. IF isUnchecked THEN RETURN END;
  1891. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1892. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1893. TrapC(BrgeL, max, o, SetElementTrap);
  1894. END;
  1895. END CheckSetElement;
  1896. (** the set that a range represents **)
  1897. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1898. VAR
  1899. operand: Operand;
  1900. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1901. BEGIN
  1902. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1903. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1904. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1905. one := IntermediateCode.Immediate(setType, 1);
  1906. Evaluate(x, operand);
  1907. Convert(operand.op, setType);
  1908. Convert(operand.tag, setType);
  1909. CheckSetElement(operand.op);
  1910. CheckSetElement(operand.tag);
  1911. (* create mask for lower bound
  1912. i.e. shift 11111111 to the left by the value of the lower bound
  1913. *)
  1914. Reuse1(temp, operand.op);
  1915. Emit(Shl(position,temp, allBits, operand.op));
  1916. ReleaseIntermediateOperand(operand.op);
  1917. operand.op := temp;
  1918. (* create mask for upper bound
  1919. i.e. shift 11111111 to the right by the difference between the
  1920. upper bound and the maximum number of set elements
  1921. *)
  1922. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1923. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1924. Reuse1(temp, operand.tag);
  1925. ELSE
  1926. Reuse1(temp, operand.tag);
  1927. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1928. Emit(Sub(position,temp, size, operand.tag));
  1929. END;
  1930. Emit(Shr(position,temp, allBits, operand.tag));
  1931. ReleaseIntermediateOperand(operand.tag);
  1932. operand.tag := temp;
  1933. Reuse2(resultingSet, operand.op, operand.tag);
  1934. (* intersect the two masks *)
  1935. Emit(And(position,resultingSet, operand.op, operand.tag));
  1936. ReleaseOperand(operand);
  1937. RETURN resultingSet
  1938. END SetFromRange;
  1939. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1940. VAR
  1941. res, operand: Operand;
  1942. temp, one, noBits, dest: IntermediateCode.Operand;
  1943. expression: SyntaxTree.Expression;
  1944. i: LONGINT;
  1945. BEGIN
  1946. IF Trace THEN TraceEnter("VisitSet") END;
  1947. dest := destination;
  1948. destination := emptyOperand;
  1949. noBits := IntermediateCode.Immediate(setType, 0);
  1950. one := IntermediateCode.Immediate(setType, 1);
  1951. (* start off with the empty set *)
  1952. InitOperand(res, ModeValue);
  1953. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1954. Emit(Mov(position,res.op, noBits));
  1955. FOR i := 0 TO x.elements.Length() - 1 DO
  1956. expression := x.elements.GetExpression(i);
  1957. IF expression IS SyntaxTree.RangeExpression THEN
  1958. (* range of set elements *)
  1959. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1960. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1961. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1962. ReleaseIntermediateOperand(temp)
  1963. ELSE
  1964. (* singelton element *)
  1965. Evaluate(expression, operand);
  1966. Convert(operand.op, setType);
  1967. CheckSetElement(operand.op);
  1968. (* create subset containing single element *)
  1969. Reuse1(temp, operand.op);
  1970. Emit(Shl(position,temp, one, operand.op));
  1971. ReleaseOperand(operand);
  1972. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1973. ReleaseIntermediateOperand(temp);
  1974. END
  1975. END;
  1976. result := res;
  1977. destination := dest;
  1978. IF Trace THEN TraceExit("VisitSet") END;
  1979. END VisitSet;
  1980. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1981. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1982. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1983. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1984. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1985. element: SyntaxTree.IntegerValue;
  1986. BEGIN
  1987. numberElements := x.elements.Length();
  1988. expression := index.parameters.GetExpression(dim);
  1989. element := expression(SyntaxTree.IntegerValue);
  1990. FOR i := 0 TO numberElements-1 DO
  1991. expression := x.elements.GetExpression(i);
  1992. element.SetValue(i);
  1993. IF expression IS SyntaxTree.MathArrayExpression THEN
  1994. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1995. ELSE
  1996. Assign(index,expression);
  1997. END;
  1998. END;
  1999. END RecursiveAssignment;
  2000. BEGIN
  2001. variable := GetTemporaryVariable(x.type, FALSE);
  2002. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2003. designator.SetType(variable.type);
  2004. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2005. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2006. FOR i := 0 TO dim-1 DO
  2007. element := SyntaxTree.NewIntegerValue(x.position,0);
  2008. element.SetType(system.longintType);
  2009. index.parameters.AddExpression(element);
  2010. END;
  2011. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2012. RecursiveAssignment(x,0);
  2013. Expression(designator);
  2014. END VisitMathArrayExpression;
  2015. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2016. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2017. BEGIN
  2018. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2019. dest := destination; destination := emptyOperand;
  2020. IF x.operator = Scanner.Not THEN
  2021. IF conditional THEN
  2022. Condition(x.left,falseLabel,trueLabel)
  2023. ELSE
  2024. Evaluate(x.left,operand);
  2025. InitOperand(result,ModeValue);
  2026. Reuse1a(result.op,operand.op,dest);
  2027. Emit(Xor(position,result.op,operand.op,true));
  2028. ReleaseOperand(operand);
  2029. END;
  2030. ELSIF x.operator = Scanner.Minus THEN
  2031. Evaluate(x.left,operand);
  2032. InitOperand(result,ModeValue);
  2033. Reuse1a(result.op,operand.op,dest);
  2034. type := x.left.type.resolved;
  2035. IF type IS SyntaxTree.SetType THEN
  2036. Emit(Not(position,result.op,operand.op));
  2037. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2038. Reuse1(result.tag,operand.tag);
  2039. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2040. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2041. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2042. Emit(Neg(position,result.op,operand.op));
  2043. ELSE HALT(200)
  2044. END;
  2045. ReleaseOperand(operand);
  2046. ELSIF x.operator = Scanner.Address THEN
  2047. Designate(x.left,operand);
  2048. operand.mode := ModeValue;
  2049. t0 := x.left.type.resolved;
  2050. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2051. ReleaseIntermediateOperand(operand.op);
  2052. operand.op := operand.tag;
  2053. IntermediateCode.InitOperand(operand.tag);
  2054. END;
  2055. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2056. result := operand;
  2057. ELSE HALT(100)
  2058. END;
  2059. destination := dest;
  2060. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2061. END VisitUnaryExpression;
  2062. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2063. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2064. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2065. BEGIN
  2066. type := type.resolved;
  2067. originalType := type;
  2068. IF type IS SyntaxTree.PointerType THEN
  2069. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2070. END;
  2071. IF type IS SyntaxTree.ObjectType THEN
  2072. BrL(trueL);
  2073. ELSE
  2074. ASSERT(type IS SyntaxTree.RecordType);
  2075. (*
  2076. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2077. *)
  2078. ReuseCopy(left,tag);
  2079. right := TypeDescriptorAdr(type);
  2080. IF ~newObjectFile THEN
  2081. IntermediateCode.MakeMemory(right,addressType);
  2082. END;
  2083. IF backend.cooperative THEN
  2084. repeatL := NewLabel();
  2085. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2086. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2087. END;
  2088. SetLabel(repeatL);
  2089. BreqL(trueL,left,right);
  2090. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2091. BrneL(repeatL,left,nil);
  2092. ELSIF meta.simple THEN
  2093. level := type(SyntaxTree.RecordType).Level();
  2094. (* get type desc tag of level relative to base tag *)
  2095. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2096. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2097. IntermediateCode.MakeMemory(left,addressType);
  2098. BreqL(trueL,left,right);
  2099. ELSE
  2100. level := type(SyntaxTree.RecordType).Level();
  2101. (* get type desc tag of level relative to base tag *)
  2102. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2103. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2104. IntermediateCode.MakeMemory(left,addressType);
  2105. BreqL(trueL,left,right);
  2106. END;
  2107. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2108. BrL(falseL);
  2109. END;
  2110. END TypeTest;
  2111. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2112. BEGIN
  2113. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2114. IF dump # NIL THEN
  2115. dump.String(s); dump.Ln;
  2116. END;
  2117. END Error;
  2118. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2119. BEGIN
  2120. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2121. IF dump # NIL THEN
  2122. dump.String(s); dump.Ln; dump.Update;
  2123. END;
  2124. END Warning;
  2125. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2126. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2127. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2128. operand:Operand;
  2129. BEGIN
  2130. previousSection := section;
  2131. Global.GetModuleName(mod,name);
  2132. Strings.Append(name,".@Trace");
  2133. Basic.ToSegmentedName(name, pooledName);
  2134. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2135. IF dump # NIL THEN dump := section.comments END;
  2136. IF backend.hasLinkRegister THEN
  2137. Emit(Push(Basic.invalidPosition, lr));
  2138. END;
  2139. variable := mod.moduleScope.firstVariable;
  2140. WHILE variable # NIL DO
  2141. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2142. Symbol(variable, operand);
  2143. register := operand.op;
  2144. CallTraceMethod(register, variable.type);
  2145. ReleaseIntermediateOperand(register);
  2146. END;
  2147. variable := variable.nextVariable;
  2148. END;
  2149. IF backend.hasLinkRegister THEN
  2150. Emit(Pop(Basic.invalidPosition, lr));
  2151. END;
  2152. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2153. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2154. Emit(Br(position,op));
  2155. INC(statCoopTraceModule, section.pc);
  2156. section := previousSection;
  2157. IF dump # NIL THEN dump := section.comments END;
  2158. END CreateTraceModuleMethod;
  2159. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2160. BEGIN
  2161. Emit (Push(position, dst));
  2162. Emit (Push(position, src));
  2163. CallThis(position,"GarbageCollector","Assign", 2);
  2164. END CallAssignPointer;
  2165. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2166. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2167. BEGIN
  2168. IF SemanticChecker.IsPointerType (type) THEN
  2169. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2170. ELSIF type.IsRecordType() THEN
  2171. Emit (Push(position,dst));
  2172. Emit (Push(position,src));
  2173. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2174. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2175. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2176. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2177. ELSIF IsStaticArray(type) THEN
  2178. size := StaticArrayNumElements(type);
  2179. base := StaticArrayBaseType(type);
  2180. IF base.IsRecordType() THEN
  2181. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2182. Emit (Push(position, dst));
  2183. Emit (Push(position, src));
  2184. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2185. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2187. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2188. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2189. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, dst));
  2192. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2193. Emit (Push(position, src));
  2194. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2195. ELSE
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, dst));
  2198. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2199. Emit (Push(position, src));
  2200. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2201. ASSERT(StaticArrayBaseType(type).IsPointer());
  2202. END;
  2203. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2204. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2205. Emit (Push(position, dst));
  2206. Emit (Push(position, src));
  2207. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2208. ELSE HALT(100); (* missing ? *)
  2209. END;
  2210. END CallAssignMethod;
  2211. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2212. VAR name: Basic.SegmentedName;
  2213. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2214. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2215. context: Context;
  2216. BEGIN
  2217. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2218. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2219. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2220. GetRecordTypeName (recordType,name);
  2221. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2222. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2223. IF dump # NIL THEN dump := section.comments END;
  2224. IF backend.hasLinkRegister THEN
  2225. Emit(Push(Basic.invalidPosition, lr));
  2226. END;
  2227. variable := recordType.recordScope.firstVariable;
  2228. WHILE variable # NIL DO
  2229. IF variable.NeedsTrace() THEN
  2230. dst := NewRegisterOperand (addressType);
  2231. src := NewRegisterOperand (addressType);
  2232. Emit (Mov(position, dst, parameter1));
  2233. Emit (Mov(position, src, parameter2));
  2234. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2235. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2236. CallAssignMethod(dst, src, variable.type);
  2237. ReleaseIntermediateOperand(src);
  2238. ReleaseIntermediateOperand(dst);
  2239. END;
  2240. variable := variable.nextVariable;
  2241. END;
  2242. recordBase := recordType.GetBaseRecord();
  2243. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2244. IF backend.hasLinkRegister THEN
  2245. Emit(Pop(Basic.invalidPosition, lr));
  2246. END;
  2247. GetRecordTypeName (recordBase,name);
  2248. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2249. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2250. Emit(Br(position,op));
  2251. ELSE
  2252. Emit(Exit(position,0,0, 0));
  2253. END;
  2254. IF ~recordType.isObject THEN
  2255. GetRecordTypeName (recordType,name);
  2256. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2257. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2258. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2259. NEW(registerUsageCount);
  2260. usedRegisters := NIL;
  2261. dst := NewRegisterOperand (addressType);
  2262. src := NewRegisterOperand (addressType);
  2263. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2264. Emit(Mov(position, dst, parameter1));
  2265. Emit(Mov(position, src, parameter2));
  2266. label := NewLabel();
  2267. SetLabel(label);
  2268. Emit(Push(position, dst));
  2269. Emit(Push(position, src));
  2270. GetRecordTypeName (recordType,name);
  2271. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2272. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2273. Emit(Call(position, op, 0));
  2274. Emit(Pop(position, src));
  2275. Emit(Pop(position, dst));
  2276. Emit(Add(position, dst, dst, ofs));
  2277. Emit(Add(position, src, src, ofs));
  2278. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2279. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2280. Emit(Exit(position,0,0, 0));
  2281. END;
  2282. INC(statCoopAssignProcedure, section.pc);
  2283. ReturnToContext(context);
  2284. IF dump # NIL THEN dump := section.comments END;
  2285. END CreateAssignProcedure;
  2286. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2287. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2288. BEGIN
  2289. IF IsUnsafePointer (type) THEN
  2290. skip := NewLabel();
  2291. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2292. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2293. BreqL (skip, op, nil);
  2294. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2295. SetLabel (skip);
  2296. ELSIF SemanticChecker.IsPointerType (type) THEN
  2297. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2298. CallThis(position,"GarbageCollector","Mark", 1);
  2299. ELSIF type.IsRecordType() THEN
  2300. Emit (Push(position,register));
  2301. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2302. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2303. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2304. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2305. ELSIF IsStaticArray(type) THEN
  2306. size := StaticArrayNumElements(type);
  2307. base := StaticArrayBaseType(type);
  2308. IF base.IsRecordType() THEN
  2309. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2310. Emit (Push(position, register));
  2311. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2312. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2314. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2315. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2316. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2317. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2318. Emit (Push(position, register));
  2319. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2320. ELSE
  2321. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2322. Emit (Push(position, register));
  2323. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2324. ASSERT(base.IsPointer());
  2325. END;
  2326. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2327. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2328. CallThis(position,"GarbageCollector","Mark", 1);
  2329. ELSE HALT(100); (* missing ? *)
  2330. END;
  2331. END CallTraceMethod;
  2332. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2333. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2334. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2335. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2336. BEGIN
  2337. previousSection := section;
  2338. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2339. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2340. GetRecordTypeName (recordType,name);
  2341. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2342. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2343. IF dump # NIL THEN dump := section.comments END;
  2344. IF backend.hasLinkRegister THEN
  2345. Emit(Push(Basic.invalidPosition, lr));
  2346. END;
  2347. variable := recordType.recordScope.firstVariable;
  2348. WHILE variable # NIL DO
  2349. IF variable.NeedsTrace() THEN
  2350. register := NewRegisterOperand (addressType);
  2351. Emit (Mov(position,register,parameter1));
  2352. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2353. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2354. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2355. END;
  2356. CallTraceMethod(register, variable.type);
  2357. ReleaseIntermediateOperand(register);
  2358. END;
  2359. variable := variable.nextVariable;
  2360. END;
  2361. recordBase := recordType.GetBaseRecord();
  2362. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2363. recordBase := recordBase.GetBaseRecord();
  2364. END;
  2365. IF recordBase # NIL THEN
  2366. IF backend.hasLinkRegister THEN
  2367. Emit(Pop(Basic.invalidPosition, lr));
  2368. END;
  2369. GetRecordTypeName (recordBase,name);
  2370. IF HasExplicitTraceMethod (recordBase) THEN
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2372. ELSE
  2373. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2374. END;
  2375. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2376. Emit(Br(position,op));
  2377. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2378. Emit(Exit(position,0,0,0));
  2379. ELSE
  2380. IF backend.hasLinkRegister THEN
  2381. Emit(Pop(Basic.invalidPosition, lr));
  2382. END;
  2383. IF recordType.isObject THEN
  2384. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2385. ELSE
  2386. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2387. END;
  2388. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2389. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2390. Emit(Br(position,op));
  2391. END;
  2392. IF ~recordType.isObject THEN
  2393. GetRecordTypeName (recordType,name);
  2394. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2395. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2396. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2397. NEW(registerUsageCount);
  2398. usedRegisters := NIL;
  2399. IF dump # NIL THEN dump := section.comments END;
  2400. register := NewRegisterOperand (addressType);
  2401. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2402. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2403. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2404. END;
  2405. Emit (Push(position,register));
  2406. GetRecordTypeName (recordType,name);
  2407. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2408. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2409. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2410. ReleaseIntermediateOperand(register);
  2411. Emit(Exit(position,0,0,0));
  2412. GetRecordTypeName (recordType,name);
  2413. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2414. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2415. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2416. NEW(registerUsageCount);
  2417. usedRegisters := NIL;
  2418. register := NewRegisterOperand (addressType);
  2419. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2420. Emit(Mov(position, register, parameter1));
  2421. label := NewLabel();
  2422. SetLabel(label);
  2423. Emit(Push(position, register));
  2424. GetRecordTypeName (recordType,name);
  2425. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2426. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2427. Emit(Call(position, op, 0));
  2428. Emit(Pop(position, register));
  2429. Emit(Add(position, register, register, ofs));
  2430. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2431. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2432. Emit(Exit(position,0,0,0));
  2433. END;
  2434. INC(statCoopTraceMethod, section.pc);
  2435. ReturnToContext(context);
  2436. IF dump # NIL THEN dump := section.comments END;
  2437. END CreateTraceMethod;
  2438. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2439. VAR name: Basic.SegmentedName;
  2440. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2441. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2442. context: Context;
  2443. BEGIN
  2444. IF recordType.isObject THEN RETURN END;
  2445. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2446. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2447. GetRecordTypeName (recordType,name);
  2448. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2449. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2450. IF dump # NIL THEN dump := section.comments END;
  2451. IF backend.hasLinkRegister THEN
  2452. Emit(Push(Basic.invalidPosition, lr));
  2453. END;
  2454. variable := recordType.recordScope.firstVariable;
  2455. WHILE variable # NIL DO
  2456. IF variable.NeedsTrace() THEN
  2457. dst := NewRegisterOperand (addressType);
  2458. Emit (Mov(position, dst, parameter1));
  2459. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2460. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2461. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2462. END;
  2463. CallResetProcedure(dst, nil, variable.type);
  2464. ReleaseIntermediateOperand(dst);
  2465. END;
  2466. variable := variable.nextVariable;
  2467. END;
  2468. recordBase := recordType.GetBaseRecord();
  2469. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2470. IF backend.hasLinkRegister THEN
  2471. Emit(Pop(Basic.invalidPosition, lr));
  2472. END;
  2473. GetRecordTypeName (recordBase,name);
  2474. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2475. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2476. Emit(Br(position,op));
  2477. ELSE
  2478. Emit(Exit(position,0,0, 0));
  2479. END;
  2480. GetRecordTypeName (recordType,name);
  2481. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2482. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2483. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2484. NEW(registerUsageCount);
  2485. usedRegisters := NIL;
  2486. dst := NewRegisterOperand (addressType);
  2487. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2488. Emit(Mov(position, dst, parameter1));
  2489. label := NewLabel();
  2490. SetLabel(label);
  2491. Emit(Push(position, dst));
  2492. GetRecordTypeName (recordType,name);
  2493. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2494. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2495. Emit(Call(position, op, 0));
  2496. Emit(Pop(position, dst));
  2497. Emit(Add(position, dst, dst, ofs));
  2498. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2499. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2500. Emit(Exit(position,0,0, 0));
  2501. INC(statCoopResetProcedure, section.pc);
  2502. ReturnToContext(context);
  2503. IF dump # NIL THEN dump := section.comments END;
  2504. END CreateResetProcedure;
  2505. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2506. VAR name: Basic.SegmentedName; context: Context;
  2507. BEGIN
  2508. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2509. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2510. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2511. IF dump # NIL THEN dump := section.comments END;
  2512. IF backend.hasLinkRegister THEN
  2513. Emit(Push(Basic.invalidPosition, lr));
  2514. END;
  2515. Emit(Push(position,fp));
  2516. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2517. ResetVariables(scope);
  2518. Emit(Pop(position,fp));
  2519. Emit(Exit(position,0,0, 0));
  2520. ReturnToContext(context);
  2521. IF dump # NIL THEN dump := section.comments END;
  2522. END CreateResetMethod;
  2523. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2524. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2525. BEGIN
  2526. IF SemanticChecker.IsPointerType (type) THEN
  2527. Emit (Push(position, dest));
  2528. CallThis(position,"GarbageCollector","Reset", 1);
  2529. ELSIF type.IsRecordType() THEN
  2530. Emit (Push(position, dest));
  2531. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2532. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2533. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2534. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2535. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2536. size := GetArrayLength(type, tag);
  2537. base := ArrayBaseType(type);
  2538. IF base.IsRecordType() THEN
  2539. Emit (Push(position, size));
  2540. Emit (Push(position, dest));
  2541. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2542. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2543. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2544. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2545. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2546. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2547. Emit (Push(position, size));
  2548. Emit (Push(position, dest));
  2549. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2550. ELSE
  2551. Emit (Push(position, size));
  2552. Emit (Push(position, dest));
  2553. CallThis(position,"GarbageCollector","ResetArray", 2);
  2554. ASSERT(ArrayBaseType(type).IsPointer());
  2555. END;
  2556. ReleaseIntermediateOperand(size);
  2557. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2558. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2559. Emit (Push(position, dest));
  2560. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2561. ELSE HALT(100); (* missing ? *)
  2562. END;
  2563. END CallResetProcedure;
  2564. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2565. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2566. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2567. VAR operand: Operand;
  2568. BEGIN
  2569. Symbol (symbol, operand);
  2570. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2571. ReleaseOperand(operand);
  2572. END Reset;
  2573. BEGIN
  2574. previousScope := currentScope;
  2575. currentScope := scope;
  2576. pc := section.pc;
  2577. variable := scope.firstVariable;
  2578. WHILE variable # NIL DO
  2579. IF variable.NeedsTrace() THEN
  2580. Reset (variable);
  2581. END;
  2582. variable := variable.nextVariable;
  2583. END;
  2584. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2585. WHILE parameter # NIL DO
  2586. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2587. Reset (parameter);
  2588. END;
  2589. parameter := parameter.nextParameter;
  2590. END;
  2591. INC(statCoopResetVariables, section.pc - pc);
  2592. currentScope := previousScope;
  2593. END ResetVariables;
  2594. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2595. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2596. VAR op: IntermediateCode.Operand; context: Context;
  2597. BEGIN
  2598. previousSection := section;
  2599. GetCodeSectionNameForSymbol(procedure, name);
  2600. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2601. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2602. IF dump # NIL THEN dump := section.comments END;
  2603. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2604. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2605. Emit(Data(position,op));
  2606. Emit(Data(position,nil));
  2607. IF HasPointers (procedure.procedureScope) THEN
  2608. GetCodeSectionNameForSymbol(procedure, name);
  2609. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2610. ELSE
  2611. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2612. END;
  2613. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2614. Emit(Data(position,op));
  2615. ReturnToContext(context);
  2616. IF dump # NIL THEN dump := section.comments END;
  2617. END CreateProcedureDescriptor;
  2618. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2619. VAR import: SyntaxTree.Import;
  2620. s: Basic.MessageString;
  2621. selfName: SyntaxTree.IdentifierString;
  2622. BEGIN
  2623. moduleScope.ownerModule.GetName(selfName);
  2624. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2625. module := moduleScope.ownerModule
  2626. ELSE
  2627. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2628. IF import = NIL THEN
  2629. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2630. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2631. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2632. s := "Module ";
  2633. Strings.Append(s,moduleName);
  2634. Strings.Append(s," cannot be imported.");
  2635. IF force THEN
  2636. Error(position,s);
  2637. ELSIF canBeLoaded THEN
  2638. Strings.Append(s, "=> no dynamic linking.");
  2639. Warning(position, s);
  2640. canBeLoaded := FALSE;
  2641. END;
  2642. RETURN FALSE
  2643. ELSE
  2644. SELF.module.imports.AddName(moduleName)
  2645. END;
  2646. ELSIF import.module = NIL THEN (* already tried *)
  2647. RETURN FALSE
  2648. END;
  2649. module := import.module;
  2650. END;
  2651. RETURN TRUE
  2652. END AddImport;
  2653. (* needed for old binary object file format*)
  2654. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2655. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2656. BEGIN
  2657. IF AddImport(moduleName,module,TRUE) THEN
  2658. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2659. IF procedure = NIL THEN
  2660. s := "Instruction not supported on target, emulation procedure ";
  2661. Strings.Append(s,moduleName);
  2662. Strings.Append(s,".");
  2663. Strings.Append(s,procedureName);
  2664. Strings.Append(s," not present");
  2665. Error(position,s);
  2666. ELSE
  2667. StaticCallOperand(r,procedure);
  2668. ReleaseOperand(r);
  2669. fp := GetFingerprint(procedure);
  2670. END;
  2671. END;
  2672. END EnsureSymbol;
  2673. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2674. VAR exit: Label; trueL,falseL: Label;
  2675. BEGIN
  2676. trueL := NewLabel();
  2677. falseL := NewLabel();
  2678. exit := NewLabel();
  2679. Condition(x,trueL,falseL);
  2680. InitOperand(result,ModeValue);
  2681. SetLabel(trueL);
  2682. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2683. Emit(Mov(position,result.op,true));
  2684. BrL(exit);
  2685. SetLabel(falseL);
  2686. Emit(MovReplace(position,result.op,false));
  2687. SetLabel(exit);
  2688. END ConditionToValue;
  2689. PROCEDURE ValueToCondition(VAR op: Operand);
  2690. BEGIN
  2691. LoadValue(op,system.booleanType);
  2692. BrneL(trueLabel,op.op, false);
  2693. ReleaseOperand(op);
  2694. BrL(falseLabel);
  2695. END ValueToCondition;
  2696. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2697. VAR size: LONGINT;
  2698. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2699. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2700. BEGIN
  2701. ASSERT(type.form = SyntaxTree.Open);
  2702. baseType := type.arrayBase.resolved;
  2703. IF IsOpenArray(baseType) THEN
  2704. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2705. ELSE
  2706. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2707. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2708. END;
  2709. len := tag;
  2710. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2711. IntermediateCode.MakeMemory(len,addressType);
  2712. UseIntermediateOperand(len);
  2713. Reuse2(res,sizeOperand,len);
  2714. Emit(Mul(position,res,sizeOperand,len));
  2715. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2716. RETURN res
  2717. END GetArraySize;
  2718. BEGIN
  2719. type := type.resolved;
  2720. IF IsOpenArray(type) THEN
  2721. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2722. RETURN tag
  2723. ELSE
  2724. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2725. END;
  2726. ELSE
  2727. size := ToMemoryUnits(system,system.SizeOf(type));
  2728. RETURN IntermediateCode.Immediate(addressType,size)
  2729. END;
  2730. END GetDynamicSize;
  2731. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2732. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2733. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2734. BEGIN
  2735. ASSERT(type.form = SyntaxTree.Open);
  2736. baseType := type.arrayBase.resolved;
  2737. IF IsOpenArray(baseType) THEN
  2738. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2739. ELSE
  2740. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2741. END;
  2742. len := tag;
  2743. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2744. IntermediateCode.MakeMemory(len,addressType);
  2745. UseIntermediateOperand(len);
  2746. Reuse2(res,sizeOperand,len);
  2747. Emit(Mul(position,res,sizeOperand,len));
  2748. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2749. RETURN res
  2750. END GetLength;
  2751. BEGIN
  2752. type := type.resolved;
  2753. IF IsOpenArray(type) THEN
  2754. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2755. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2756. ELSIF type IS SyntaxTree.StringType THEN
  2757. RETURN tag;
  2758. ELSE
  2759. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2760. END;
  2761. END GetArrayLength;
  2762. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2763. VAR result: IntermediateCode.Operand;
  2764. BEGIN
  2765. IF backend.cooperative THEN
  2766. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2767. ELSE
  2768. MakeMemory(result, tag, addressType, 0);
  2769. END;
  2770. RETURN result
  2771. END GetSizeFromTag;
  2772. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2773. VAR parameter: SyntaxTree.Parameter;
  2774. BEGIN
  2775. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2776. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2777. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2779. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2780. END;
  2781. RETURN GetDynamicSize(e.type, tag);
  2782. END GetArrayOfBytesSize;
  2783. (*
  2784. to find imported symbol. not needed ?
  2785. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2786. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2787. BEGIN
  2788. IF AddImport(moduleName,importedModule,FALSE) THEN
  2789. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2790. RETURN symbol
  2791. ELSE
  2792. RETURN NIL
  2793. END
  2794. END SymbolByName;
  2795. *)
  2796. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2797. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2798. BEGIN
  2799. IF AddImport(moduleName,runtimeModule,force) THEN
  2800. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2801. IF procedure = NIL THEN
  2802. s := "Procedure ";
  2803. Strings.Append(s,moduleName);
  2804. Strings.Append(s,".");
  2805. Strings.Append(s,procedureName);
  2806. Strings.Append(s," not present");
  2807. Error(position,s);
  2808. RETURN FALSE
  2809. ELSE
  2810. RETURN TRUE
  2811. END;
  2812. ELSE RETURN FALSE
  2813. END;
  2814. END GetRuntimeProcedure;
  2815. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2816. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2817. s: Basic.MessageString;
  2818. BEGIN
  2819. Basic.InitSegmentedName(name);
  2820. name[0] := Basic.MakeString(moduleName);
  2821. name[1] := Basic.MakeString(typeName);
  2822. name[2] := -1;
  2823. IF AddImport(moduleName,importedModule, FALSE) THEN
  2824. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2825. IF symbol = NIL THEN
  2826. s := "type ";
  2827. Strings.Append(s,moduleName);
  2828. Strings.Append(s,".");
  2829. Strings.Append(s,typeName);
  2830. Strings.Append(s," not present");
  2831. Error(position,s);
  2832. END;
  2833. ELSE symbol := NIL;
  2834. END;
  2835. IF importedModule = moduleScope.ownerModule THEN
  2836. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. ELSE
  2838. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2839. END;
  2840. RETURN symbol
  2841. END GetTypeDescriptor;
  2842. (* 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. *)
  2843. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2844. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2845. pooledName: Basic.SegmentedName; size: LONGINT;
  2846. BEGIN
  2847. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2848. StaticCallOperand(result,procedure);
  2849. IF numberParameters < 0 THEN
  2850. size := ProcedureParametersSize(system,procedure);
  2851. ELSE
  2852. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2853. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2854. Error(position,"runtime call parameter count mismatch");
  2855. END;
  2856. END;
  2857. Emit(Call(position, result.op, size));
  2858. ReleaseOperand(result);
  2859. ELSE (* only static linking possible *)
  2860. ASSERT(numberParameters >= 0);
  2861. Basic.InitSegmentedName(pooledName);
  2862. pooledName[0] := Basic.MakeString(moduleName);
  2863. pooledName[1] := Basic.MakeString(procedureName);
  2864. pooledName[2] := -1;
  2865. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2866. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2867. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2868. END;
  2869. END CallThisChecked;
  2870. (* 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. *)
  2871. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2872. BEGIN
  2873. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2874. END CallThis;
  2875. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2876. VAR
  2877. left,right: Operand;
  2878. leftSize, rightSize: IntermediateCode.Operand;
  2879. saved: RegisterEntry;
  2880. reg: IntermediateCode.Operand;
  2881. procedureName: SyntaxTree.IdentifierString;
  2882. BEGIN
  2883. procedureName := "CompareString";
  2884. SaveRegisters();ReleaseUsedRegisters(saved);
  2885. Designate(leftExpression,left);
  2886. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2887. Emit(Push(position,leftSize));
  2888. ReleaseIntermediateOperand(leftSize);
  2889. Emit(Push(position,left.op));
  2890. ReleaseOperand(left);
  2891. Designate(rightExpression,right);
  2892. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2893. Emit(Push(position,rightSize));
  2894. ReleaseIntermediateOperand(rightSize);
  2895. Emit(Push(position,right.op));
  2896. ReleaseOperand(right);
  2897. IF backend.cooperative THEN
  2898. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2899. ELSE
  2900. CallThis(position,runtimeModuleName,procedureName, 4);
  2901. END;
  2902. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2903. Emit(Result(position,reg));
  2904. (*
  2905. AcquireThisRegister(int8,IntermediateCode.Result);
  2906. *)
  2907. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2908. (*
  2909. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2910. *)
  2911. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2912. ReleaseIntermediateOperand(reg);
  2913. BrL(falseLabel);
  2914. END CompareString;
  2915. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2916. VAR
  2917. left,right: Operand;
  2918. leftSize, rightSize: IntermediateCode.Operand;
  2919. saved: RegisterEntry;
  2920. procedureName: SyntaxTree.IdentifierString;
  2921. BEGIN
  2922. procedureName := "CopyString";
  2923. SaveRegisters();ReleaseUsedRegisters(saved);
  2924. Designate(leftExpression,left);
  2925. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2926. Emit(Push(position,leftSize));
  2927. ReleaseIntermediateOperand(leftSize);
  2928. Emit(Push(position,left.op));
  2929. ReleaseOperand(left);
  2930. Designate(rightExpression,right);
  2931. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2932. Emit(Push(position,rightSize));
  2933. ReleaseIntermediateOperand(rightSize);
  2934. Emit(Push(position,right.op));
  2935. ReleaseOperand(right);
  2936. IF backend.cooperative THEN
  2937. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2938. ELSE
  2939. CallThis(position,runtimeModuleName,procedureName,4);
  2940. END;
  2941. RestoreRegisters(saved);
  2942. END CopyString;
  2943. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2944. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2945. leftType,rightType: SyntaxTree.Type;
  2946. leftExpression,rightExpression : SyntaxTree.Expression;
  2947. componentType: IntermediateCode.Type;
  2948. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2949. size: LONGINT;
  2950. BEGIN
  2951. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2952. dest := destination; destination := emptyOperand;
  2953. leftType := x.left.type.resolved;
  2954. rightType := x.right.type.resolved;
  2955. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2956. CASE x.operator OF
  2957. Scanner.Or:
  2958. (* shortcut evaluation of left OR right *)
  2959. IF ~conditional THEN ConditionToValue(x);
  2960. ELSE
  2961. next := NewLabel();
  2962. Condition(x.left,trueLabel,next);
  2963. SetLabel(next);
  2964. Condition(x.right,trueLabel,falseLabel);
  2965. END;
  2966. |Scanner.And:
  2967. (* shortcut evaluation of left & right *)
  2968. IF ~conditional THEN ConditionToValue(x);
  2969. ELSE
  2970. next := NewLabel();
  2971. Condition(x.left,next,falseLabel);
  2972. SetLabel(next);
  2973. Condition(x.right,trueLabel,falseLabel);
  2974. END;
  2975. |Scanner.Is:
  2976. IF ~conditional THEN ConditionToValue(x);
  2977. ELSE
  2978. (* get type desc tag *)
  2979. IF IsPointerToRecord(leftType,recordType) THEN
  2980. Evaluate(x.left,left);
  2981. Dereference(left,recordType,IsUnsafePointer(leftType))
  2982. ELSE
  2983. Designate(x.left,left);
  2984. END;
  2985. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2986. ReleaseOperand(left);
  2987. END;
  2988. |Scanner.Plus:
  2989. Evaluate(x.left,left);
  2990. Evaluate(x.right,right);
  2991. IF leftType IS SyntaxTree.SetType THEN
  2992. InitOperand(result,ModeValue);
  2993. Reuse2a(result.op,left.op,right.op,dest);
  2994. Emit(Or(position,result.op,left.op,right.op));
  2995. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2996. InitOperand(result,ModeValue);
  2997. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2998. Reuse2(result.tag,left.tag,right.tag);
  2999. Emit(Add(position,result.op,left.op,right.op));
  3000. Emit(Add(position,result.tag,left.tag,right.tag))
  3001. ELSE
  3002. InitOperand(result,ModeValue);
  3003. Reuse2a(result.op,left.op,right.op,dest);
  3004. Emit(Add(position,result.op,left.op,right.op));
  3005. END;
  3006. ReleaseOperand(left); ReleaseOperand(right);
  3007. |Scanner.Minus:
  3008. Evaluate(x.left,left);
  3009. Evaluate(x.right,right);
  3010. IF leftType IS SyntaxTree.SetType THEN
  3011. InitOperand(result,ModeValue);
  3012. Reuse1(result.op,right.op);
  3013. Emit(Not(position,result.op,right.op));
  3014. ReleaseOperand(right);
  3015. Emit(And(position,result.op,result.op,left.op));
  3016. ReleaseOperand(left);
  3017. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3018. InitOperand(result,ModeValue);
  3019. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3020. Reuse2(result.tag,left.tag,right.tag);
  3021. Emit(Sub(position,result.op,left.op,right.op));
  3022. Emit(Sub(position,result.tag,left.tag,right.tag));
  3023. ReleaseOperand(left); ReleaseOperand(right)
  3024. ELSE
  3025. InitOperand(result,ModeValue);
  3026. Reuse2a(result.op,left.op,right.op,dest);
  3027. Emit(Sub(position,result.op,left.op,right.op));
  3028. ReleaseOperand(left); ReleaseOperand(right);
  3029. END;
  3030. |Scanner.Times:
  3031. Evaluate(x.left,left);
  3032. Evaluate(x.right,right);
  3033. IF leftType IS SyntaxTree.SetType THEN
  3034. InitOperand(result,ModeValue);
  3035. Reuse2a(result.op,left.op,right.op,dest);
  3036. Emit(And(position,result.op,left.op,right.op));
  3037. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3038. InitOperand(result, ModeValue);
  3039. componentType := left.op.type;
  3040. (* TODO: review this *)
  3041. (*
  3042. result.op = left.op * right.op - left.tag * right.tag
  3043. result.tag = left.tag * right.op + left.op * right.tag
  3044. *)
  3045. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3046. Emit(Mul(position,result.op, left.op, right.op));
  3047. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3048. Emit(Mul(position,tempReg, left.tag, right.tag));
  3049. Emit(Sub(position,result.op, result.op, tempReg));
  3050. Reuse2(result.tag, left.tag, right.op);
  3051. Emit(Mul(position,result.tag, left.tag, right.op));
  3052. Emit(Mul(position,tempReg, left.op, right.tag));
  3053. Emit(Add(position,result.tag, result.tag, tempReg));
  3054. ReleaseIntermediateOperand(tempReg)
  3055. ELSE
  3056. InitOperand(result,ModeValue);
  3057. Reuse2a(result.op,left.op,right.op,dest);
  3058. Emit(Mul(position,result.op,left.op,right.op));
  3059. END;
  3060. ReleaseOperand(left); ReleaseOperand(right);
  3061. |Scanner.Div:
  3062. Evaluate(x.left,left);
  3063. Evaluate(x.right,right);
  3064. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3065. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3066. IF ~isUnchecked THEN
  3067. exit := NewLabel();
  3068. BrltL(exit,zero,right.op);
  3069. EmitTrap(position,NegativeDivisorTrap);
  3070. SetLabel(exit);
  3071. END;
  3072. END;
  3073. InitOperand(result,ModeValue);
  3074. Reuse2a(result.op,left.op,right.op,dest);
  3075. Emit(Div(position,result.op,left.op,right.op));
  3076. ReleaseOperand(left); ReleaseOperand(right);
  3077. |Scanner.Mod:
  3078. Evaluate(x.left,left);
  3079. Evaluate(x.right,right);
  3080. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3081. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3082. IF ~isUnchecked THEN
  3083. exit := NewLabel();
  3084. BrltL(exit,zero,right.op);
  3085. EmitTrap(position,NegativeDivisorTrap);
  3086. SetLabel(exit);
  3087. END;
  3088. END;
  3089. InitOperand(result,ModeValue);
  3090. Reuse2a(result.op,left.op,right.op,dest);
  3091. Emit(Mod(position,result.op,left.op,right.op));
  3092. ReleaseOperand(left); ReleaseOperand(right);
  3093. |Scanner.Slash:
  3094. Evaluate(x.left,left);
  3095. Evaluate(x.right,right);
  3096. IF leftType IS SyntaxTree.SetType THEN
  3097. InitOperand(result,ModeValue);
  3098. Reuse2a(result.op,left.op,right.op,dest);
  3099. Emit(Xor(position,result.op,left.op,right.op));
  3100. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3101. InitOperand(result,ModeValue);
  3102. componentType := left.op.type;
  3103. (* review this *)
  3104. (*
  3105. divisor = right.op * right.op + right.tag * right.tag
  3106. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3107. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3108. *)
  3109. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3110. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3111. Emit(Mul(position,tempReg, right.op, right.op));
  3112. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3113. Emit(Add(position,tempReg, tempReg, tempReg2));
  3114. result.op := tempReg2;
  3115. Emit(Mul(position,result.op, left.op, right.op));
  3116. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3117. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3118. Emit(Add(position,result.op, result.op, tempReg2));
  3119. Emit(Div(position,result.op, result.op, tempReg));
  3120. Reuse2(result.tag, left.tag, right.op);
  3121. Emit(Mul(position,result.tag, left.tag, right.op));
  3122. Emit(Mul(position,tempReg2, left.op, right.tag));
  3123. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3124. Emit(Div(position,result.tag, result.tag, tempReg));
  3125. ReleaseIntermediateOperand(tempReg);
  3126. ReleaseIntermediateOperand(tempReg2)
  3127. ELSE
  3128. InitOperand(result,ModeValue);
  3129. Reuse2a(result.op,left.op,right.op,dest);
  3130. Emit(Div(position,result.op,left.op,right.op));
  3131. END;
  3132. ReleaseOperand(left); ReleaseOperand(right);
  3133. |Scanner.Equal:
  3134. IF ~conditional THEN ConditionToValue(x);
  3135. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3136. CompareString(BreqL,x.left,x.right);
  3137. ELSE
  3138. Evaluate(x.left,left);
  3139. Evaluate(x.right,right);
  3140. IF leftType IS SyntaxTree.RangeType THEN
  3141. ASSERT(rightType IS SyntaxTree.RangeType);
  3142. BrneL(falseLabel, left.op, right.op); (* first *)
  3143. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3144. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3145. ReleaseOperand(left); ReleaseOperand(right);
  3146. BrL(trueLabel)
  3147. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3148. BrneL(falseLabel, left.op, right.op); (* first *)
  3149. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3150. ReleaseOperand(left); ReleaseOperand(right);
  3151. BrL(trueLabel)
  3152. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3153. (* TODO: review this *)
  3154. BrneL(falseLabel, left.op, right.op); (* real part *)
  3155. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3156. ReleaseOperand(left); ReleaseOperand(right);
  3157. BrL(trueLabel)
  3158. ELSE
  3159. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3160. ReleaseOperand(left); ReleaseOperand(right);
  3161. BrL(trueLabel);
  3162. END;
  3163. END;
  3164. |Scanner.LessEqual:
  3165. IF ~conditional THEN ConditionToValue(x);
  3166. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3167. CompareString(BrgeL,x.right,x.left);
  3168. ELSE
  3169. Evaluate(x.left,left);
  3170. Evaluate(x.right,right);
  3171. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3172. Reuse1(temp.op,right.op);
  3173. Emit(And(position,temp.op,left.op,right.op));
  3174. ReleaseOperand(right);
  3175. BreqL(trueLabel,temp.op,left.op);
  3176. BrL(falseLabel);
  3177. ReleaseOperand(temp);ReleaseOperand(left);
  3178. ELSE
  3179. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3180. ReleaseOperand(left); ReleaseOperand(right);
  3181. BrL(trueLabel);
  3182. END;
  3183. END;
  3184. |Scanner.Less:
  3185. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3186. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3187. leftExpression.SetType(system.booleanType);
  3188. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3189. rightExpression.SetType(system.booleanType);
  3190. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3191. leftExpression.SetType(system.booleanType);
  3192. Expression(leftExpression);
  3193. ELSIF ~conditional THEN ConditionToValue(x);
  3194. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3195. CompareString(BrltL,x.left,x.right);
  3196. ELSE
  3197. Evaluate(x.left,left);
  3198. Evaluate(x.right,right);
  3199. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3200. ReleaseOperand(left); ReleaseOperand(right);
  3201. BrL(trueLabel);
  3202. END;
  3203. |Scanner.Greater:
  3204. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3205. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3206. leftExpression.SetType(system.booleanType);
  3207. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3208. rightExpression.SetType(system.booleanType);
  3209. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3210. leftExpression.SetType(system.booleanType);
  3211. Expression(leftExpression);
  3212. ELSIF ~conditional THEN ConditionToValue(x);
  3213. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3214. CompareString(BrltL,x.right,x.left);
  3215. ELSE
  3216. Evaluate(x.left,left);
  3217. Evaluate(x.right,right);
  3218. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3219. ReleaseOperand(left); ReleaseOperand(right);
  3220. BrL(trueLabel);
  3221. END;
  3222. |Scanner.GreaterEqual:
  3223. IF ~conditional THEN ConditionToValue(x);
  3224. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3225. CompareString(BrgeL,x.left,x.right);
  3226. ELSE
  3227. Evaluate(x.left,left);
  3228. Evaluate(x.right,right);
  3229. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3230. Reuse1(temp.op,left.op);
  3231. Emit(And(position,temp.op,left.op,right.op));
  3232. ReleaseOperand(left);
  3233. BreqL(trueLabel, temp.op,right.op);
  3234. ReleaseOperand(temp); ReleaseOperand(right);
  3235. BrL(falseLabel);
  3236. ELSE
  3237. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3238. ReleaseOperand(left); ReleaseOperand(right);
  3239. BrL(trueLabel);
  3240. END;
  3241. END;
  3242. |Scanner.Unequal:
  3243. IF ~conditional THEN ConditionToValue(x);
  3244. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3245. CompareString(BrneL,x.left,x.right);
  3246. ELSE
  3247. Evaluate(x.left,left);
  3248. Evaluate(x.right,right);
  3249. IF leftType IS SyntaxTree.RangeType THEN
  3250. ASSERT(rightType IS SyntaxTree.RangeType);
  3251. BrneL(trueLabel, left.op, right.op); (* first *)
  3252. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3253. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3254. ReleaseOperand(left); ReleaseOperand(right);
  3255. BrL(falseLabel)
  3256. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3257. BrneL(trueLabel, left.op, right.op); (* first *)
  3258. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3259. ReleaseOperand(left); ReleaseOperand(right);
  3260. BrL(falseLabel)
  3261. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3262. (* TODO: review this *)
  3263. BrneL(trueLabel, left.op, right.op); (* real part *)
  3264. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3265. ReleaseOperand(left); ReleaseOperand(right);
  3266. BrL(falseLabel)
  3267. ELSE
  3268. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3269. ReleaseOperand(left); ReleaseOperand(right);
  3270. BrL(trueLabel);
  3271. END;
  3272. END;
  3273. |Scanner.In:
  3274. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3275. Evaluate(x.left,left);
  3276. Evaluate(x.right,right);
  3277. Convert(left.op,setType);
  3278. ReuseCopy(temp.op,right.op);
  3279. Emit(Shr(position,temp.op,temp.op,left.op));
  3280. ReleaseOperand(right); ReleaseOperand(left);
  3281. IntermediateCode.InitImmediate(one,setType,1);
  3282. Emit(And(position,temp.op,temp.op,one));
  3283. IF conditional THEN
  3284. IntermediateCode.InitImmediate(zero,setType,0);
  3285. BrneL(trueLabel,temp.op,zero);
  3286. ReleaseOperand(temp);
  3287. BrL(falseLabel);
  3288. ELSE
  3289. Convert(temp.op,bool);
  3290. result.mode := ModeValue;
  3291. result.op := temp.op;
  3292. result.tag := nil; (* may be left over from calls to evaluate *)
  3293. END;
  3294. ELSE
  3295. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3296. IF x.operator = Scanner.Questionmarks THEN
  3297. leftExpression := x.left;
  3298. rightExpression := x.right;
  3299. ELSE
  3300. leftExpression := x.right;
  3301. rightExpression := x.left;
  3302. END;
  3303. Evaluate(leftExpression, left);
  3304. Emit(Push(position,left.op));
  3305. ReleaseOperand(left);
  3306. Designate(rightExpression, right);
  3307. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3308. IF ~backend.cellsAreObjects THEN
  3309. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3310. END;
  3311. Emit(Push(position,right.op));
  3312. ReleaseOperand(right);
  3313. IF backend.cellsAreObjects THEN
  3314. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3315. ELSE
  3316. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3317. END;
  3318. InitOperand(result, ModeValue);
  3319. result.op := NewRegisterOperand(bool);
  3320. Emit(Result(position,result.op));
  3321. IF conditional THEN
  3322. IntermediateCode.InitImmediate(zero,setType,0);
  3323. BrneL(trueLabel,result.op,zero);
  3324. ReleaseOperand(result);
  3325. BrL(falseLabel);
  3326. END;
  3327. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3328. leftExpression := x.left;
  3329. rightExpression := x.right;
  3330. Evaluate(leftExpression, left);
  3331. Emit(Push(position,left.op));
  3332. ReleaseOperand(left);
  3333. Evaluate(rightExpression, right);
  3334. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3335. IF ~backend.cellsAreObjects THEN
  3336. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3337. END;
  3338. Emit(Push(position,right.op));
  3339. ReleaseOperand(right);
  3340. IF backend.cellsAreObjects THEN
  3341. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3342. ELSE
  3343. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3344. END;
  3345. InitOperand(result, ModeValue);
  3346. result.op := NewRegisterOperand(bool);
  3347. Emit(Result(position,result.op));
  3348. IF conditional THEN
  3349. IntermediateCode.InitImmediate(zero,setType,0);
  3350. BrneL(trueLabel,result.op,zero);
  3351. ReleaseOperand(result);
  3352. BrL(falseLabel);
  3353. END;
  3354. ELSE
  3355. HALT(100);
  3356. END;
  3357. END;
  3358. destination := dest;
  3359. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3360. END VisitBinaryExpression;
  3361. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3362. VAR localResult, operand: Operand;
  3363. BEGIN
  3364. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3365. InitOperand(localResult, ModeValue);
  3366. ASSERT(x.first # NIL);
  3367. Evaluate(x.first, operand);
  3368. localResult.op := operand.op;
  3369. ReleaseOperand(operand);
  3370. UseIntermediateOperand(localResult.op);
  3371. ASSERT(x.last # NIL);
  3372. Evaluate(x.last, operand);
  3373. localResult.tag := operand.op;
  3374. ReleaseOperand(operand);
  3375. UseIntermediateOperand(localResult.tag);
  3376. IF x.step # NIL THEN
  3377. Evaluate(x.step, operand);
  3378. localResult.extra := operand.op;
  3379. ReleaseOperand(operand);
  3380. UseIntermediateOperand(localResult.extra);
  3381. END;
  3382. result := localResult;
  3383. IF Trace THEN TraceExit("VisitRangeExpression") END
  3384. END VisitRangeExpression;
  3385. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3386. BEGIN
  3387. HALT(100); (* should never be evaluated *)
  3388. END VisitTensorRangeExpression;
  3389. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3390. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3391. BEGIN
  3392. IF Trace THEN TraceEnter("VisitConversion") END;
  3393. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3394. dest := destination; destination := emptyOperand;
  3395. Evaluate(x.expression,old);
  3396. InitOperand(result,ModeValue);
  3397. result.op := old.op;
  3398. ASSERT(result.op.mode # 0);
  3399. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3400. (* convert TO a complex number *)
  3401. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3402. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3403. ASSERT(result.op.mode # 0);
  3404. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3405. (* convert FROM a complex number TO a complex number*)
  3406. result.tag := old.tag;
  3407. ASSERT(result.tag.mode # 0);
  3408. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3409. ASSERT(result.tag.mode # 0)
  3410. ELSE
  3411. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3412. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3413. END
  3414. ELSE
  3415. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3416. ASSERT(result.op.mode # 0);
  3417. result.tag := old.tag; (*! probably never used *)
  3418. END;
  3419. destination := dest;
  3420. IF Trace THEN TraceExit("VisitConversion") END;
  3421. END VisitConversion;
  3422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3423. BEGIN
  3424. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3425. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3426. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3427. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3428. END VisitTypeDeclaration;
  3429. (** designators (expressions) *)
  3430. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3431. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3432. BEGIN
  3433. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3434. IF x.left # NIL THEN Expression(x.left) END;
  3435. Symbol(x.symbol,result);
  3436. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3437. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3438. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3439. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3440. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3441. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3442. END;
  3443. END;
  3444. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3445. END VisitSymbolDesignator;
  3446. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3447. BEGIN
  3448. IF isUnchecked THEN RETURN END;
  3449. IF tagsAvailable THEN
  3450. TrapC(BrltL,index,length,IndexCheckTrap);
  3451. END;
  3452. END BoundCheck;
  3453. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3454. VAR d: IntermediateCode.Operand;
  3455. BEGIN
  3456. IF isUnchecked THEN RETURN END;
  3457. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3458. TrapC(op,dim,d,ArraySizeTrap);
  3459. ReleaseIntermediateOperand(d);
  3460. END DimensionCheck;
  3461. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3462. VAR
  3463. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3464. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3465. expression: SyntaxTree.Expression;
  3466. resultingType, leftType, baseType: SyntaxTree.Type;
  3467. skipLabel1: Label;
  3468. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3469. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3470. variableOp: Operand;
  3471. variable: SyntaxTree.Variable;
  3472. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3473. VAR expression: SyntaxTree.Expression;
  3474. BEGIN
  3475. (* count the number of indices, ranges and tensorRanges in the index list *)
  3476. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3477. FOR i := 0 TO parameters.Length() - 1 DO
  3478. expression := parameters.GetExpression(i);
  3479. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3480. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3481. ELSE INC(indexCount)
  3482. END
  3483. END;
  3484. END CountIndices;
  3485. BEGIN
  3486. ASSERT(tagsAvailable);
  3487. resultingType := x.type.resolved; (* resulting type *)
  3488. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3489. InitOperand(localResult, ModeReference);
  3490. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3491. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3492. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3493. (* a globally defined array destination tag is available -> use and invalidate it*)
  3494. localResult.tag := arrayDestinationTag;
  3495. IntermediateCode.InitOperand(arrayDestinationTag)
  3496. ELSE
  3497. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3498. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3499. Symbol(variable, variableOp);
  3500. ReuseCopy(localResult.tag, variableOp.op);
  3501. ReleaseOperand(variableOp);
  3502. END
  3503. END;
  3504. indexListSize := x.parameters.Length();
  3505. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3506. ASSERT(tensorRangeCount <= 1);
  3507. (* designate the array to be indexed over, perform tensor range check if known *)
  3508. Designate(x.left, array);
  3509. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3510. Dereference(array, leftType,FALSE);
  3511. IF tensorRangeCount=0 THEN
  3512. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3513. END
  3514. END;
  3515. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3516. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3517. srcDimOffset := -indexListSize;
  3518. destDimOffset := -rangeCount
  3519. ELSE
  3520. srcDimOffset := 0;
  3521. destDimOffset := 0
  3522. END;
  3523. indexDim := 0;
  3524. (* use address of source array as basis *)
  3525. (*
  3526. ReuseCopy(localResult.op, array.op);
  3527. *)
  3528. localResult.op := array.op;
  3529. UseIntermediateOperand(localResult.op);
  3530. (* go through the index list *)
  3531. FOR i := 0 TO indexListSize - 1 DO
  3532. expression := x.parameters.GetExpression(i);
  3533. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3534. (* nothing to do *)
  3535. ELSE
  3536. (* determine which dimension of source array is currently looked at *)
  3537. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3538. (* get the memory operand pointing to array descriptor dimension *)
  3539. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3540. (* make a reusable register from it *)
  3541. ReuseCopy(srcDim, tmp);
  3542. ReleaseIntermediateOperand(tmp);
  3543. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3544. ELSE
  3545. srcDim := IntermediateCode.Immediate(int32, i)
  3546. END;
  3547. (* get length and increment of source array for current dimension *)
  3548. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3549. Convert(sourceLength.op, sizeType);
  3550. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3551. Convert(sourceIncrement.op, sizeType);
  3552. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3553. ReleaseIntermediateOperand(srcDim);
  3554. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3555. (* SINGLE INDEX *)
  3556. Evaluate(expression, index);
  3557. ReleaseIntermediateOperand(index.tag);
  3558. index.tag := emptyOperand;
  3559. Convert(index.op, sizeType);
  3560. (* lower bound check *)
  3561. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3562. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3563. ELSIF isUnchecked THEN (* do nothing *)
  3564. ELSE
  3565. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3566. END;
  3567. (* upper bound check *)
  3568. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3569. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3570. ELSIF isUnchecked THEN (* do nothing *)
  3571. ELSE
  3572. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3573. END;
  3574. ReleaseOperand(sourceLength);
  3575. Convert(index.op, addressType);
  3576. summand := index.op;
  3577. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3578. (* RANGE OF INDICES *)
  3579. Evaluate(expression, range);
  3580. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3581. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3582. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3583. ReleaseOperand(range);
  3584. Convert(firstIndex, sizeType);
  3585. Convert(lastIndex, sizeType);
  3586. Convert(stepSize, sizeType);
  3587. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3588. if it is 'MAX(LONGINT)' *)
  3589. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3590. TransferToRegister(lastIndex, lastIndex);
  3591. skipLabel1 := NewLabel();
  3592. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3593. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3594. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3595. SetLabel(skipLabel1)
  3596. END;
  3597. (* check if step size is valid *)
  3598. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3599. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3600. ELSIF isUnchecked THEN (* do nothing *)
  3601. ELSE
  3602. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3603. END;
  3604. (* check lower bound check *)
  3605. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3606. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3607. ELSIF isUnchecked THEN (* do nothing *)
  3608. ELSE
  3609. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3610. END;
  3611. (* check upper bound check *)
  3612. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3613. (* statically open range: nothing to do *)
  3614. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3615. ASSERT(staticLastIndex < staticSourceLength)
  3616. ELSIF isUnchecked THEN (* do nothing *)
  3617. ELSE
  3618. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3619. END;
  3620. (* determine length of target array for current dimension *)
  3621. (* 1. incorporate last index: *)
  3622. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3623. (* last index is static *)
  3624. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3625. targetLength := sourceLength.op
  3626. ELSE
  3627. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3628. END;
  3629. UseIntermediateOperand(targetLength);
  3630. ELSE
  3631. (* targetLength := lastIndex + 1
  3632. Reuse1(targetLength, lastIndex);
  3633. *)
  3634. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3635. END;
  3636. ReleaseOperand(sourceLength);
  3637. ReleaseIntermediateOperand(lastIndex);
  3638. (* 2. incorporate first index: *)
  3639. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3640. (* first index and current target length are static *)
  3641. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3642. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3643. (* first index = 0: nothing to do *)
  3644. ELSE
  3645. (* targetLength := targetLength - firstIndex *)
  3646. TransferToRegister(targetLength, targetLength);
  3647. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3648. END;
  3649. (* clip negative lengths to 0 *)
  3650. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3651. IF staticTargetLength < 0 THEN
  3652. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3653. END
  3654. ELSE
  3655. skipLabel1 := NewLabel();
  3656. TransferToRegister(targetLength, targetLength);
  3657. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3658. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3659. SetLabel(skipLabel1)
  3660. END;
  3661. (* 3. incorporate index step size: *)
  3662. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3663. (*step size and current target length are static *)
  3664. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3665. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3666. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3667. (* step size = 1: nothing to do *)
  3668. ELSE
  3669. (* emit code for this:
  3670. targetLength := (targetLength-1) DIV stepSize +1;
  3671. *)
  3672. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3673. DivInt(targetLength, targetLength, stepSize);
  3674. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3675. END;
  3676. (* determine increment of target array for current dimension *)
  3677. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3678. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3679. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3680. (* step size = 1 *)
  3681. targetIncrement := sourceIncrement.op;
  3682. UseIntermediateOperand(targetIncrement)
  3683. ELSE
  3684. (* targetIncrement := sourceIncrement * stepSize *)
  3685. Reuse1(targetIncrement, stepSize);
  3686. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3687. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3688. END;
  3689. ReleaseIntermediateOperand(stepSize);
  3690. (* write length and increment of target array to descriptor *)
  3691. IF destDimOffset < 0 THEN
  3692. (* determine which dimension of target array is currently looked at *)
  3693. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3694. TransferToRegister(destDim, tmp);
  3695. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3696. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3697. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3698. ReleaseIntermediateOperand(destDim)
  3699. ELSE
  3700. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3701. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3702. END;
  3703. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3704. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3705. INC(indexDim);
  3706. Convert(firstIndex, addressType);
  3707. TransferToRegister(summand, firstIndex);
  3708. ELSE HALT(100);
  3709. END;
  3710. (*
  3711. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3712. *)
  3713. Convert(sourceIncrement.op, addressType);
  3714. Convert(summand, addressType);
  3715. MulInt(summand, summand, sourceIncrement.op);
  3716. ReleaseIntermediateOperand(sourceIncrement.op);
  3717. AddInt(localResult.op, localResult.op, summand);
  3718. ReleaseIntermediateOperand(summand);
  3719. END
  3720. END;
  3721. result := localResult;
  3722. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3723. ReleaseIntermediateOperand(result.tag);
  3724. result.tag := TypeDescriptorAdr(resultingType);
  3725. IF ~newObjectFile THEN
  3726. IntermediateCode.MakeMemory(result.tag,addressType);
  3727. END;
  3728. ELSIF IsDelegate(resultingType) THEN
  3729. ReleaseIntermediateOperand(result.tag);
  3730. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3731. UseIntermediateOperand(result.tag);
  3732. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3733. ReleaseIntermediateOperand(result.tag);
  3734. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3735. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3736. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3737. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3738. (* finalize target array descriptor *)
  3739. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3740. (* write lengths and increments of target array for remaining dimensions *)
  3741. i := indexListSize;
  3742. WHILE indexDim < targetArrayDimensionality DO
  3743. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3744. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3745. ReleaseOperand(sourceLength);
  3746. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3747. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3748. ReleaseOperand(sourceIncrement);
  3749. INC(i); INC(indexDim);
  3750. END;
  3751. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3752. tmp := nil;
  3753. ELSE
  3754. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3755. END;
  3756. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3757. ReleaseIntermediateOperand(tmp);
  3758. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3759. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3760. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3761. ELSE
  3762. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3763. END;
  3764. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3765. ReleaseIntermediateOperand(tmp);
  3766. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3767. (* write dimensionality *)
  3768. IF targetArrayDimensionality # 0 THEN
  3769. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3770. END;
  3771. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3772. END;
  3773. ReleaseOperand(array);
  3774. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3775. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3776. END;
  3777. END MathIndexDesignator;
  3778. (* get the length of an array , trying to make use of static information *)
  3779. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3780. VAR res: IntermediateCode.Operand; size: LONGINT;
  3781. BEGIN
  3782. type := type.resolved;
  3783. IF type IS SyntaxTree.ArrayType THEN
  3784. WITH type: SyntaxTree.ArrayType DO
  3785. IF type.form = SyntaxTree.Static THEN
  3786. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3787. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3788. Evaluate(type.length, op);
  3789. ReleaseIntermediateOperand(op.tag);
  3790. RETURN op.op;*)
  3791. ELSE
  3792. res := tag;
  3793. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3794. IntermediateCode.MakeMemory(res,addressType);
  3795. UseIntermediateOperand(res);
  3796. RETURN res
  3797. END
  3798. END;
  3799. ELSE
  3800. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3801. RETURN IntermediateCode.Immediate(addressType,size);
  3802. END;
  3803. END ArrayLength;
  3804. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3805. BEGIN
  3806. IF IsImmediate(x) THEN
  3807. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3808. ELSE
  3809. IF ~ReusableRegister(res) THEN
  3810. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3811. ELSE
  3812. UseIntermediateOperand(res);
  3813. END;
  3814. Emit(Mov(position,res,x))
  3815. END;
  3816. END CopyInt;
  3817. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3818. BEGIN
  3819. ReleaseIntermediateOperand(res);
  3820. IF IsImmediate(x) & IsImmediate(y) THEN
  3821. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3822. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3823. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3824. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3825. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3826. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3827. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3828. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3829. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3830. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3831. UseIntermediateOperand(res);
  3832. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3833. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3834. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3835. UseIntermediateOperand(res);
  3836. ELSE
  3837. IF ~ReusableRegister(res) THEN
  3838. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3839. ELSE
  3840. UseIntermediateOperand(res);
  3841. END;
  3842. IF IsImmediate(x) & (x.intValue = 0) THEN
  3843. Emit(Mov(position,res,y))
  3844. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3845. Emit(Mov(position,res,x))
  3846. ELSE
  3847. Emit(Add(position,res, x, y));
  3848. END;
  3849. END;
  3850. END AddInt;
  3851. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3852. BEGIN
  3853. ReleaseIntermediateOperand(res);
  3854. IF IsImmediate(x) & IsImmediate(y) THEN
  3855. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3856. ELSE
  3857. IF ~ReusableRegister(res) THEN
  3858. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3859. ELSE UseIntermediateOperand(res);
  3860. END;
  3861. IF IsImmediate(x) & (x.intValue = 1) THEN
  3862. Emit(Mov(position,res,y))
  3863. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3864. Emit(Mov(position,res,x))
  3865. ELSE
  3866. Emit(Mul(position,res, x, y));
  3867. END;
  3868. END;
  3869. END MulInt;
  3870. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3871. BEGIN
  3872. ReleaseIntermediateOperand(res);
  3873. IF IsImmediate(x) & IsImmediate(y) THEN
  3874. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3875. ELSE
  3876. IF ~ReusableRegister(res) THEN
  3877. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3878. ELSE UseIntermediateOperand(res);
  3879. END;
  3880. IF IsImmediate(x) & (x.intValue = 1) THEN
  3881. Emit(Mov(position,res,y))
  3882. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3883. Emit(Mov(position,res,x))
  3884. ELSE
  3885. Emit(Div(position,res, x, y));
  3886. END;
  3887. END;
  3888. END DivInt;
  3889. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3890. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3891. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3892. BEGIN
  3893. type := x.left.type.resolved;
  3894. IF type IS SyntaxTree.StringType THEN
  3895. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3896. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3897. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3898. type := atype;
  3899. x.left.SetType(type);
  3900. END;
  3901. IntermediateCode.InitImmediate(res,addressType,0);
  3902. maxDim := x.parameters.Length()-1;
  3903. (*
  3904. computation rule:
  3905. a: ARRAY X,Y,Z OF Element with size S
  3906. a[i,j,k] -->
  3907. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3908. *)
  3909. FOR i := 0 TO maxDim DO
  3910. e := x.parameters.GetExpression(i);
  3911. Evaluate(e,index);
  3912. Convert(index.op,addressType);
  3913. AddInt(res, res, index.op);
  3914. IF i = 0 THEN
  3915. (*
  3916. ReuseCopy(res, index.op);
  3917. *)
  3918. Designate(x.left,array);
  3919. type := x.left.type.resolved;
  3920. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3921. Dereference(array, type, FALSE);
  3922. END;
  3923. (*
  3924. ELSE AddInt(res, res, index.op);
  3925. *)
  3926. END;
  3927. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3928. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3929. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3930. BoundCheck(index.op, length);
  3931. END;
  3932. ReleaseIntermediateOperand(length);
  3933. END;
  3934. ReleaseOperand(index);
  3935. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3936. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3937. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3938. MulInt(res,res,length);
  3939. END;
  3940. ReleaseIntermediateOperand(length);
  3941. END;
  3942. (* remaining open dimensions -- compute address *)
  3943. i := maxDim+1;
  3944. IF type IS SyntaxTree.ArrayType THEN
  3945. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3946. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3947. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3948. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3949. MulInt(res,res,length);
  3950. END;
  3951. ReleaseIntermediateOperand(length);
  3952. INC(i);
  3953. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3954. END;
  3955. END;
  3956. IF (type IS SyntaxTree.ArrayType) THEN
  3957. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3958. size := StaticSize(system, type);
  3959. IF size # 1 THEN
  3960. length := IntermediateCode.Immediate(addressType,size);
  3961. MulInt(res,res,length);
  3962. END;
  3963. ELSE
  3964. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3965. IF size # 1 THEN
  3966. length := IntermediateCode.Immediate(addressType,size);
  3967. MulInt(res,res,length);
  3968. END;
  3969. END;
  3970. END;
  3971. AddInt(res,res,array.op);
  3972. InitOperand(result,ModeReference);
  3973. result.op := res;
  3974. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3975. ReleaseIntermediateOperand(result.tag);
  3976. result.tag := TypeDescriptorAdr(type);
  3977. IF ~newObjectFile THEN
  3978. IntermediateCode.MakeMemory(result.tag,addressType);
  3979. END
  3980. ELSIF IsDelegate(type) THEN
  3981. ReleaseIntermediateOperand(result.tag);
  3982. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3983. UseIntermediateOperand(result.tag);
  3984. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3985. ReleaseIntermediateOperand(result.tag);
  3986. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3987. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3988. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3989. END;
  3990. ReleaseOperand(array);
  3991. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3992. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3993. END;
  3994. END IndexDesignator;
  3995. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3996. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3997. BEGIN
  3998. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3999. dest := destination; destination := emptyOperand;
  4000. type := x.left.type.resolved;
  4001. IF type IS SyntaxTree.MathArrayType THEN
  4002. MathIndexDesignator(x);
  4003. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4004. IndexDesignator(x);
  4005. END;
  4006. destination := dest;
  4007. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4008. END VisitIndexDesignator;
  4009. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4010. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4011. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4012. procedure: SyntaxTree.Procedure;
  4013. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4014. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4015. BEGIN
  4016. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4017. parameters := expression.parameters;
  4018. moduleName := "FoxArrayBase";
  4019. procedureName := "CopyDescriptor";
  4020. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4021. SaveRegisters();ReleaseUsedRegisters(saved);
  4022. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4023. IF procedure = NIL THEN
  4024. s := "procedure ";
  4025. Strings.Append(s,moduleName);
  4026. Strings.Append(s,".");
  4027. Strings.Append(s,procedureName);
  4028. Strings.Append(s," not present");
  4029. Error(position,s);
  4030. ELSE
  4031. (* push address of temporary variable *)
  4032. Symbol(variable,destOperand);
  4033. Emit(Push(position,destOperand.op));
  4034. ReleaseOperand(destOperand);
  4035. (* push src *)
  4036. Evaluate(expression.left,srcOperand);
  4037. (*
  4038. Dereference(srcOperand,expression.type.resolved);
  4039. Emit(Push(position,srcOperand.tag));
  4040. *)
  4041. Emit(Push(position,srcOperand.op));
  4042. ReleaseOperand(srcOperand);
  4043. tensorFound := FALSE;
  4044. FOR i := 0 TO parameters.Length()-1 DO
  4045. e := parameters.GetExpression(i);
  4046. IF e IS SyntaxTree.TensorRangeExpression THEN
  4047. tensorFound := TRUE;
  4048. ELSIF e IS SyntaxTree.RangeExpression THEN
  4049. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4050. ELSE
  4051. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4052. END;
  4053. END;
  4054. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4055. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4056. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4057. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4058. StaticCallOperand(procOp,procedure);
  4059. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4060. ReleaseOperand(procOp);
  4061. END;
  4062. RestoreRegisters(saved);
  4063. END;
  4064. RETURN variable
  4065. END PrepareTensorDescriptor;
  4066. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4067. VAR
  4068. type, descriptorType, baseType: SyntaxTree.Type;
  4069. operand, tmpOperand, variableOp, variable2Op: Operand;
  4070. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4071. variable, variable2: SyntaxTree.Variable;
  4072. dim, i, size: LONGINT;
  4073. (* TODO: needed? *)
  4074. oldArrayDestinationTag: IntermediateCode.Operand;
  4075. oldArrayDestinationDimension: LONGINT;
  4076. position: Position;
  4077. saved: RegisterEntry;
  4078. arrayFlags: SET;
  4079. m, n: LONGINT;
  4080. PROCEDURE Pass(op: IntermediateCode.Operand);
  4081. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4082. BEGIN
  4083. IF numberRegister >= 0 THEN
  4084. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4085. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4086. Emit(Mov(position,parameterRegister, op));
  4087. ELSE
  4088. Emit(Push(position,op))
  4089. END
  4090. END Pass;
  4091. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4092. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4093. BEGIN
  4094. formalType := formalType.resolved; actualType := actualType.resolved;
  4095. IF IsOpenArray(formalType)THEN
  4096. IF actualType IS SyntaxTree.StringType THEN
  4097. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4098. RETURN;
  4099. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4100. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4101. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4102. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4103. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4104. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4105. ELSE
  4106. tmp := baseReg;
  4107. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4108. IntermediateCode.MakeMemory(tmp,addressType);
  4109. Pass((tmp));
  4110. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4111. END;
  4112. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4113. END;
  4114. END PushArrayLens;
  4115. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4116. BEGIN
  4117. CASE size OF
  4118. |2: INCL(flags,Size2Flag);
  4119. |3: INCL(flags,Size3Flag);
  4120. |4: INCL(flags,Size4Flag);
  4121. |5: INCL(flags,Size5Flag);
  4122. |6: INCL(flags,Size6Flag);
  4123. |7: INCL(flags,Size7Flag);
  4124. |8: INCL(flags,Size8Flag);
  4125. END;
  4126. END SetSmallArraySizeFlag;
  4127. BEGIN
  4128. IF Trace THEN TraceEnter("PushParameter") END;
  4129. position := expression.position;
  4130. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4131. type := expression.type.resolved;
  4132. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4133. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4134. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4135. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4136. );
  4137. (* TODO: needed? *)
  4138. oldArrayDestinationTag := arrayDestinationTag;
  4139. oldArrayDestinationDimension := arrayDestinationDimension;
  4140. IF IsArrayOfSystemByte(parameter.type) THEN
  4141. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4142. Designate(expression,operand);
  4143. ELSE
  4144. Evaluate(expression, tmpOperand);
  4145. variable := GetTemporaryVariable(expression.type, FALSE);
  4146. Symbol(variable, operand);
  4147. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4148. Emit(Mov(position,tmp, tmpOperand.op));
  4149. ReleaseOperand(tmpOperand);
  4150. END;
  4151. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4152. ReleaseIntermediateOperand(operand.tag);
  4153. operand.tag := tmp;
  4154. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4155. Pass((operand.tag));
  4156. END;
  4157. Pass((operand.op));
  4158. ELSIF IsOpenArray(parameter.type) THEN
  4159. Designate(expression,operand);
  4160. baseReg := operand.tag;
  4161. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4162. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4163. END;
  4164. Pass((operand.op)); (* address of the array *)
  4165. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4166. (* case 1 *)
  4167. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4168. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4169. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4170. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4171. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4172. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4173. arrayDestinationTag := sp;
  4174. (* case 1b *)
  4175. IF expression IS SyntaxTree.IndexDesignator THEN
  4176. (*
  4177. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4178. descriptorType := GetMathArrayDescriptorType(dim);
  4179. variable := GetTemporaryVariable(descriptorType,expression.position);
  4180. Symbol(variable,variableOp);
  4181. arrayDestinationTag := variableOp.op;
  4182. *)
  4183. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4184. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4185. arrayDestinationDimension := dim;
  4186. Designate(expression,operand);
  4187. (* case 1a *)
  4188. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4189. Designate(expression,operand);
  4190. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4191. i := 0;
  4192. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4193. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4194. INC(i);
  4195. END;
  4196. type := expression.type.resolved;
  4197. WHILE (i<dim) DO (* remaining static dimensions *)
  4198. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4199. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4200. ReleaseOperand(tmpOperand);
  4201. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4202. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4203. ReleaseOperand(tmpOperand);
  4204. INC(i);
  4205. END;
  4206. dimOp := IntermediateCode.Immediate(addressType,dim);
  4207. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4208. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4209. (* case 1d *)
  4210. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4211. Designate(expression,operand);
  4212. Dereference(operand,type.resolved,FALSE);
  4213. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4214. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4215. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4216. (* case 1f *)
  4217. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4218. Designate(expression,operand);
  4219. FOR i := 0 TO dim-1 DO
  4220. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4221. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4222. ReleaseOperand(tmpOperand);
  4223. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4224. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4225. ReleaseOperand(tmpOperand);
  4226. END;
  4227. (*******
  4228. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4229. *)
  4230. arrayFlags := {StaticFlag};
  4231. IF dim = 1 THEN
  4232. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4233. ReleaseOperand(tmpOperand);
  4234. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4235. m := LONGINT(tmpOperand.op.intValue);
  4236. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4237. ELSIF dim = 2 THEN
  4238. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4239. ReleaseOperand(tmpOperand);
  4240. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4241. m := LONGINT(tmpOperand.op.intValue);
  4242. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4243. ReleaseOperand(tmpOperand);
  4244. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4245. n := LONGINT(tmpOperand.op.intValue);
  4246. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4247. INCL(arrayFlags,SmallMatrixFlag);
  4248. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4249. END;
  4250. END;
  4251. (*******)
  4252. dimOp := IntermediateCode.Immediate(addressType,dim);
  4253. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4254. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4255. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4256. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4257. baseType := SemanticChecker.ArrayBase(type,dim);
  4258. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4259. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4260. ELSE HALT(100);
  4261. END;
  4262. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4263. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4264. (* case 2b *)
  4265. IF expression IS SyntaxTree.IndexDesignator THEN
  4266. descriptorType := GetMathArrayDescriptorType(dim);
  4267. variable := GetTemporaryVariable(descriptorType, FALSE);
  4268. Symbol(variable,variableOp);
  4269. arrayDestinationTag := variableOp.op;
  4270. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4271. arrayDestinationDimension := dim;
  4272. NeedDescriptor := TRUE;
  4273. Designate(expression,operand);
  4274. Pass((operand.tag));
  4275. NeedDescriptor := FALSE;
  4276. (* case 2a *)
  4277. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4278. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4279. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4280. INC(i);
  4281. END;
  4282. IF i = dim THEN
  4283. Designate(expression,operand);
  4284. Pass((operand.tag));
  4285. ELSE (* open-static *)
  4286. type := expression.type.resolved;
  4287. descriptorType := GetMathArrayDescriptorType(dim);
  4288. variable := GetTemporaryVariable(descriptorType, FALSE);
  4289. Symbol(variable,variableOp);
  4290. arrayDestinationTag := variableOp.op;
  4291. Designate(expression,operand);
  4292. FOR i := 0 TO dim-1 DO
  4293. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4294. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4295. ReleaseOperand(tmpOperand);
  4296. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4297. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4298. ReleaseOperand(tmpOperand);
  4299. END;
  4300. dimOp := IntermediateCode.Immediate(addressType,dim);
  4301. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4302. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4303. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4304. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4305. baseType := SemanticChecker.ArrayBase(type,dim);
  4306. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4307. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4308. Pass((arrayDestinationTag));
  4309. END;
  4310. (* case 2d *)
  4311. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4312. Designate(expression,operand);
  4313. Dereference(operand,type.resolved,FALSE);
  4314. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4315. Pass((operand.tag));
  4316. (* case 2f *)
  4317. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4318. descriptorType := GetMathArrayDescriptorType(dim);
  4319. variable := GetTemporaryVariable(descriptorType, FALSE);
  4320. Symbol(variable,variableOp);
  4321. arrayDestinationTag := variableOp.op;
  4322. Designate(expression,operand);
  4323. FOR i := 0 TO dim-1 DO
  4324. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4325. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4326. ReleaseOperand(tmpOperand);
  4327. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4328. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4329. ReleaseOperand(tmpOperand);
  4330. END;
  4331. (*
  4332. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4333. *)
  4334. arrayFlags := {StaticFlag};
  4335. IF dim = 1 THEN
  4336. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4337. ReleaseOperand(tmpOperand);
  4338. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4339. m := LONGINT(tmpOperand.op.intValue);
  4340. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4341. ELSIF dim = 2 THEN
  4342. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4343. ReleaseOperand(tmpOperand);
  4344. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4345. m := LONGINT(tmpOperand.op.intValue);
  4346. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4347. ReleaseOperand(tmpOperand);
  4348. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4349. n := LONGINT(tmpOperand.op.intValue);
  4350. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4351. INCL(arrayFlags,SmallMatrixFlag);
  4352. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4353. END;
  4354. END;
  4355. (*******)
  4356. dimOp := IntermediateCode.Immediate(addressType,dim);
  4357. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4358. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4359. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4360. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4361. baseType := SemanticChecker.ArrayBase(type,dim);
  4362. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4363. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4364. Pass((arrayDestinationTag));
  4365. ELSE HALT(100);
  4366. END;
  4367. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4368. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4369. (* case 3b *)
  4370. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4371. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4372. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4373. Symbol(variable,variableOp);
  4374. LoadValue(variableOp,system.addressType);
  4375. ELSE
  4376. descriptorType := GetMathArrayDescriptorType(dim);
  4377. variable := GetTemporaryVariable(descriptorType, FALSE);
  4378. Symbol(variable,variableOp);
  4379. END;
  4380. arrayDestinationTag := variableOp.op;
  4381. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4382. arrayDestinationDimension := 0;
  4383. Designate(expression,operand);
  4384. Pass((operand.tag));
  4385. (* case 3a *)
  4386. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4387. i := 0;
  4388. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4389. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4390. INC(i);
  4391. END;
  4392. IF i = dim THEN
  4393. Designate(expression,operand);
  4394. Pass((operand.tag));
  4395. ELSE (* open-static *)
  4396. type := expression.type.resolved;
  4397. descriptorType := GetMathArrayDescriptorType(dim);
  4398. variable := GetTemporaryVariable(descriptorType, FALSE);
  4399. Symbol(variable,variableOp);
  4400. arrayDestinationTag := variableOp.op;
  4401. Designate(expression,operand);
  4402. FOR i := 0 TO dim-1 DO
  4403. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4404. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4405. ReleaseOperand(tmpOperand);
  4406. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4407. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4408. ReleaseOperand(tmpOperand);
  4409. END;
  4410. dimOp := IntermediateCode.Immediate(addressType,dim);
  4411. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4412. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4413. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4414. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4415. baseType := SemanticChecker.ArrayBase(type,dim);
  4416. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4417. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4418. Pass((arrayDestinationTag));
  4419. END;
  4420. (* case 3d *)
  4421. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4422. Designate(expression,operand);
  4423. Dereference(operand,type.resolved,FALSE);
  4424. (*
  4425. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4426. *)
  4427. Pass((operand.tag));
  4428. (* case 3f *)
  4429. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4430. descriptorType := GetMathArrayDescriptorType(dim);
  4431. variable := GetTemporaryVariable(descriptorType, FALSE);
  4432. Symbol(variable,variableOp);
  4433. arrayDestinationTag := variableOp.op;
  4434. Designate(expression,operand);
  4435. IF operand.op.type.length >1 THEN (* vector register *)
  4436. variable2 := GetTemporaryVariable(type, FALSE);
  4437. Symbol(variable2, variable2Op);
  4438. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4439. Emit(Mov(position,tmp, operand.op));
  4440. ReleaseOperand(operand);
  4441. Symbol(variable2, operand);
  4442. END;
  4443. FOR i := 0 TO dim-1 DO
  4444. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4445. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4446. ReleaseOperand(tmpOperand);
  4447. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4448. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4449. ReleaseOperand(tmpOperand);
  4450. END;
  4451. dimOp := IntermediateCode.Immediate(addressType,dim);
  4452. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4453. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4454. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4455. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4456. baseType := SemanticChecker.ArrayBase(type,dim);
  4457. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4458. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4459. Pass((arrayDestinationTag));
  4460. ELSE HALT(100);
  4461. END;
  4462. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4463. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4464. (* case 4b *)
  4465. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4466. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4467. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4468. Symbol(variable,variableOp);
  4469. LoadValue(variableOp,system.addressType);
  4470. ELSE
  4471. descriptorType := GetMathArrayDescriptorType(dim);
  4472. variable := GetTemporaryVariable(descriptorType, FALSE);
  4473. Symbol(variable,variableOp);
  4474. END;
  4475. arrayDestinationTag := variableOp.op;
  4476. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4477. arrayDestinationDimension := 0;
  4478. Designate(expression,operand);
  4479. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4480. Symbol(variable,variableOp);
  4481. ELSE
  4482. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4483. Symbol(variable,variableOp);
  4484. MakeMemory(tmp,variableOp.op,addressType,0);
  4485. Emit(Mov(position,tmp,operand.tag));
  4486. ReleaseIntermediateOperand(tmp);
  4487. END;
  4488. Pass((variableOp.op));
  4489. ReleaseOperand(variableOp);
  4490. (* case 4a *)
  4491. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4492. i := 0;
  4493. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4494. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4495. INC(i);
  4496. END;
  4497. IF i = dim THEN
  4498. Designate(expression,operand);
  4499. arrayDestinationTag := operand.tag;
  4500. ELSE (* open-static *)
  4501. type := expression.type.resolved;
  4502. descriptorType := GetMathArrayDescriptorType(dim);
  4503. variable := GetTemporaryVariable(descriptorType, FALSE);
  4504. Symbol(variable,variableOp);
  4505. arrayDestinationTag := variableOp.op;
  4506. Designate(expression,operand);
  4507. FOR i := 0 TO dim-1 DO
  4508. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4509. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4510. ReleaseOperand(tmpOperand);
  4511. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4512. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4513. ReleaseOperand(tmpOperand);
  4514. END;
  4515. dimOp := IntermediateCode.Immediate(addressType,dim);
  4516. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4517. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4518. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4519. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4520. baseType := SemanticChecker.ArrayBase(type,dim);
  4521. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4522. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4523. END;
  4524. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4525. Symbol(variable,variableOp);
  4526. MakeMemory(tmp,variableOp.op,addressType,0);
  4527. Emit(Mov(position,tmp,arrayDestinationTag));
  4528. ReleaseIntermediateOperand(tmp);
  4529. Pass((variableOp.op));
  4530. ReleaseOperand(variableOp);
  4531. (* case 4d *)
  4532. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4533. Designate(expression,operand);
  4534. (*
  4535. Dereference(operand,type.resolved,FALSE);
  4536. *)
  4537. (*
  4538. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4539. *)
  4540. Pass((operand.op));
  4541. (* case 4f *)
  4542. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4543. descriptorType := GetMathArrayDescriptorType(dim);
  4544. variable := GetTemporaryVariable(descriptorType, FALSE);
  4545. Symbol(variable,variableOp);
  4546. arrayDestinationTag := variableOp.op;
  4547. Designate(expression,operand);
  4548. FOR i := 0 TO dim-1 DO
  4549. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4550. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4551. ReleaseOperand(tmpOperand);
  4552. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4553. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4554. ReleaseOperand(tmpOperand);
  4555. END;
  4556. dimOp := IntermediateCode.Immediate(addressType,dim);
  4557. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4558. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4559. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4560. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4561. baseType := SemanticChecker.ArrayBase(type,dim);
  4562. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4563. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4564. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4565. Symbol(variable,variableOp);
  4566. MakeMemory(tmp,variableOp.op,addressType,0);
  4567. Emit(Mov(position,tmp,arrayDestinationTag));
  4568. ReleaseIntermediateOperand(tmp);
  4569. Pass((variableOp.op));
  4570. ReleaseOperand(variableOp);
  4571. ELSE HALT(100);
  4572. END;
  4573. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4574. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4575. Designate(expression,operand);
  4576. IF operand.op.type.length > 1 THEN
  4577. Emit(Push(position, operand.op));
  4578. ELSE
  4579. size := system.SizeOf(type);
  4580. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4581. size := ToMemoryUnits(system,size);
  4582. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4583. arrayDestinationTag := sp;
  4584. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4585. END;
  4586. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4587. Designate(expression,operand);
  4588. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4589. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4590. Symbol(variable,variableOp);
  4591. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4592. Emit(Mov(position,tmp,operand.op));
  4593. Emit(Push(position,variableOp.op));
  4594. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4595. Pass((operand.op));
  4596. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4597. END;
  4598. ELSE HALT(200)
  4599. END;
  4600. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4601. IF parameter.kind = SyntaxTree.VarParameter THEN
  4602. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4603. Designate(expression, operand);
  4604. Pass((operand.op));
  4605. ELSE
  4606. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4607. Evaluate(expression, operand);
  4608. Pass((operand.extra)); (* step *)
  4609. Pass((operand.tag)); (* last *)
  4610. Pass((operand.op)) (* first *)
  4611. END
  4612. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4613. IF parameter.kind = SyntaxTree.VarParameter THEN
  4614. Designate(expression, operand);
  4615. Pass((operand.op));
  4616. ELSE
  4617. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4618. Evaluate(expression, operand);
  4619. Pass((operand.tag)); (* real part *)
  4620. Pass((operand.op)) (* imaginary part *)
  4621. END
  4622. ELSE
  4623. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4624. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4625. Designate(expression,operand);
  4626. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4627. (* stack allocation *)
  4628. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4629. (*! parameter alignment to be discussed ... *)
  4630. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4631. size := type(SyntaxTree.StringType).length;
  4632. END;
  4633. IF backend.cooperative & parameter.NeedsTrace() THEN
  4634. dst := NewRegisterOperand (addressType);
  4635. Emit(Mov(position,dst, sp));
  4636. IntermediateCode.InitImmediate(null, byteType, 0);
  4637. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4638. ReleaseIntermediateOperand(dst);
  4639. SaveRegisters();ReleaseUsedRegisters(saved);
  4640. (* register dst has been freed before SaveRegisters already *)
  4641. CallAssignMethod(dst, operand.op, parameter.type);
  4642. RestoreRegisters(saved);
  4643. END;
  4644. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4645. ELSIF IsOpenArray(parameter.type) THEN
  4646. Designate(expression,operand);
  4647. baseReg := operand.tag;
  4648. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4649. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4650. END;
  4651. Pass((operand.op)); (* address of the array *)
  4652. ELSIF IsDelegate(parameter.type) THEN
  4653. Evaluate(expression,operand);
  4654. IF backend.cooperative & parameter.NeedsTrace() THEN
  4655. Emit(Push(position, nil));
  4656. dst := NewRegisterOperand (addressType);
  4657. Emit(Mov(position,dst, sp));
  4658. ReleaseIntermediateOperand(dst);
  4659. SaveRegisters();ReleaseUsedRegisters(saved);
  4660. Emit(Push(position, dst));
  4661. (* register dst has been freed before SaveRegisters already *)
  4662. Emit(Push(position, operand.tag));
  4663. CallThis(position,"GarbageCollector","Assign",2);
  4664. RestoreRegisters(saved);
  4665. ELSE
  4666. Pass((operand.tag));
  4667. END;
  4668. Pass((operand.op));
  4669. ELSE
  4670. Evaluate(expression,operand);
  4671. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4672. Emit(Push(position, nil));
  4673. dst := NewRegisterOperand (addressType);
  4674. Emit(Mov(position,dst, sp));
  4675. ReleaseIntermediateOperand(dst);
  4676. SaveRegisters();ReleaseUsedRegisters(saved);
  4677. Emit(Push(position, dst));
  4678. (* register dst has been freed before SaveRegisters already *)
  4679. Emit(Push(position, operand.op));
  4680. CallThis(position,"GarbageCollector","Assign",2);
  4681. RestoreRegisters(saved);
  4682. ELSE
  4683. Pass((operand.op));
  4684. END;
  4685. END;
  4686. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4687. Evaluate(expression,operand);
  4688. Pass((operand.op));
  4689. ELSE (* var parameter *)
  4690. Designate(expression,operand);
  4691. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4692. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4693. Pass((operand.tag));
  4694. END;
  4695. END;
  4696. Pass((operand.op));
  4697. END;
  4698. END;
  4699. (* TODO: needed? *)
  4700. arrayDestinationTag := oldArrayDestinationTag;
  4701. arrayDestinationDimension := oldArrayDestinationDimension;
  4702. IF needsParameterBackup THEN
  4703. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4704. ReuseCopy(parameterBackup, operand.op)
  4705. END;
  4706. ReleaseOperand(operand);
  4707. IF Trace THEN TraceExit("PushParameter") END;
  4708. END PushParameter;
  4709. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4710. VAR prevConditional: BOOLEAN;
  4711. BEGIN
  4712. prevConditional := conditional;
  4713. conditional := FALSE;
  4714. 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
  4715. Expression(x.result); (* use register *)
  4716. END;
  4717. Statement(x.statement);
  4718. conditional := prevConditional;
  4719. IF x.result # NIL THEN Expression(x.result) END;
  4720. 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
  4721. ReleaseIntermediateOperand(result.op);
  4722. END;
  4723. END VisitStatementDesignator;
  4724. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4725. VAR
  4726. procedure: SyntaxTree.Procedure;
  4727. procedureType: SyntaxTree.ProcedureType;
  4728. wasInline: BOOLEAN;
  4729. actualParameters: SyntaxTree.ExpressionList;
  4730. formalParameter: SyntaxTree.Parameter;
  4731. actualParameter: SyntaxTree.Expression;
  4732. i: LONGINT;
  4733. localVariable: SyntaxTree.Variable;
  4734. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4735. src, dest: Operand;
  4736. prevInlineExit : Label;
  4737. prevMapper: SymbolMapper;
  4738. tooComplex: BOOLEAN;
  4739. resultDesignator: SyntaxTree.Expression;
  4740. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4741. BEGIN
  4742. IF e = NIL THEN RETURN TRUE
  4743. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4744. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4745. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4746. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4747. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4748. ELSE RETURN FALSE
  4749. END;
  4750. END SimpleExpression;
  4751. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4752. BEGIN
  4753. RETURN checker.CanPassInRegister(type)
  4754. END FitsInRegister;
  4755. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4756. VAR
  4757. variable: SyntaxTree.Variable;
  4758. variableDesignator: SyntaxTree.Designator;
  4759. BEGIN
  4760. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4761. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4762. variableDesignator.SetType(type);
  4763. RETURN variableDesignator
  4764. END GetTemp;
  4765. BEGIN
  4766. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4767. wasInline := currentIsInline;
  4768. prevInlineExit := currentInlineExit;
  4769. prevMapper := currentMapper;
  4770. currentInlineExit := NewLabel();
  4771. tooComplex := FALSE;
  4772. NEW(currentMapper);
  4773. currentIsInline := TRUE;
  4774. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4775. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4776. formalParameter := procedureType.firstParameter;
  4777. actualParameters := x.parameters;
  4778. i := 0;
  4779. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4780. actualParameter := actualParameters.GetExpression(i);
  4781. IF actualParameter.resolved # NIL THEN
  4782. actualParameter := actualParameter.resolved
  4783. END;
  4784. (*
  4785. if expression is simple and can be passed immediately
  4786. or if type fits in register then we can proceed
  4787. otherwise we escape to ordinary procedure call.
  4788. *)
  4789. (* cases where the expression can be mapped identically *)
  4790. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4791. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4792. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4793. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4794. (*
  4795. Assign(variableDesignator, actualParameter);
  4796. *)
  4797. Evaluate(actualParameter, src);
  4798. Designate(variableDesignator, dest);
  4799. Emit(Mov(x.position, dest.op, src.op));
  4800. ReleaseOperand(dest);
  4801. ReleaseOperand(src);
  4802. (* the src operand should now have been completely released ! *)
  4803. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4804. ELSE tooComplex := TRUE
  4805. END;
  4806. (*
  4807. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4808. currentMapper.Add(formalParameter, actualParameter, NIL);
  4809. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4810. variableDesignator := GetTemp(system.addressType, FALSE);
  4811. Designate(actualParameter, src);
  4812. Designate(variableDesignator, dest);
  4813. IntermediateCode.MakeMemory(dest.op,addressType);
  4814. Emit(Mov(x.position, dest.op, src.op));
  4815. ReleaseOperand(dest);
  4816. IF src.tag.mode # IntermediateCode.Undefined THEN
  4817. tagDesignator := GetTemp(system.addressType, FALSE);
  4818. Designate(tagDesignator, dest);
  4819. IntermediateCode.MakeMemory(dest.op,addressType);
  4820. Emit(Mov(x.position, dest.op, src.op));
  4821. END;
  4822. ReleaseOperand(dest); ReleaseOperand(src);
  4823. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4824. END;
  4825. *)
  4826. formalParameter := formalParameter.nextParameter;
  4827. INC(i);
  4828. END;
  4829. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4830. IF resultDesignator # NIL THEN
  4831. returnDesignator := resultDesignator
  4832. ELSE
  4833. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4834. END;
  4835. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4836. END;
  4837. localVariable := procedure.procedureScope.firstVariable;
  4838. WHILE ~tooComplex & (localVariable # NIL) DO
  4839. variableDesignator := GetTemp(localVariable.type, FALSE);
  4840. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4841. localVariable := localVariable.nextVariable;
  4842. END;
  4843. IF ~tooComplex THEN
  4844. VisitStatementBlock(procedure.procedureScope.body);
  4845. SetLabel(currentInlineExit);
  4846. IF procedureType.returnType # NIL THEN
  4847. Designate(returnDesignator, result);
  4848. IF conditional THEN
  4849. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4850. ValueToCondition(result)
  4851. END;
  4852. END;
  4853. END;
  4854. currentMapper := prevMapper;
  4855. currentInlineExit := prevInlineExit;
  4856. currentIsInline := wasInline;
  4857. RETURN ~tooComplex
  4858. END InlineProcedureCall;
  4859. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4860. VAR
  4861. parameters: SyntaxTree.ExpressionList;
  4862. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4863. designator: SyntaxTree.Designator;
  4864. procedureType: SyntaxTree.ProcedureType;
  4865. formalParameter: SyntaxTree.Parameter;
  4866. operand, returnValue: Operand;
  4867. reg, size, mask, dest: IntermediateCode.Operand;
  4868. saved: RegisterEntry;
  4869. symbol: SyntaxTree.Symbol;
  4870. variable: SyntaxTree.Variable;
  4871. i, parametersSize, returnTypeSize : LONGINT;
  4872. structuredReturnType: BOOLEAN;
  4873. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4874. tempVariableDesignator: SyntaxTree.Designator;
  4875. gap, alignment: LONGINT; (*fld*)
  4876. (* TODO: remove unnecessary backup variables *)
  4877. oldResult: Operand;
  4878. oldCurrentScope: SyntaxTree.Scope;
  4879. oldArrayDestinationTag: IntermediateCode.Operand;
  4880. oldArrayDestinationDimension: LONGINT;
  4881. oldConstantDeclaration: SyntaxTree.Symbol;
  4882. oldDestination: IntermediateCode.Operand;
  4883. oldCurrentLoop: Label;
  4884. oldConditional: BOOLEAN;
  4885. oldTrueLabel, oldFalseLabel: Label;
  4886. oldLocked: BOOLEAN;
  4887. usedRegisters,oldUsedRegisters: RegisterEntry;
  4888. return: IntermediateCode.Operand;
  4889. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4890. arg: IntermediateCode.Operand;
  4891. dummy: IntermediateCode.Operand;
  4892. recordType: SyntaxTree.RecordType;
  4893. operatorSelectionProcedureOperand: Operand;
  4894. operatorSelectionProcedure: SyntaxTree.Procedure;
  4895. fingerPrint: SyntaxTree.FingerPrint;
  4896. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4897. identifierNumber: LONGINT;
  4898. parameterRegister: Backend.Registers;
  4899. parameterRegisters: LONGINT;
  4900. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4901. procedure: SyntaxTree.Procedure;
  4902. PROCEDURE BackupGlobalState;
  4903. BEGIN
  4904. oldResult := result;
  4905. oldCurrentScope := currentScope;
  4906. oldArrayDestinationTag := arrayDestinationTag;
  4907. oldArrayDestinationDimension := arrayDestinationDimension;
  4908. oldConstantDeclaration := constantDeclaration;
  4909. oldDestination := destination;
  4910. oldCurrentLoop := currentLoop;
  4911. oldConditional := conditional;
  4912. oldTrueLabel := trueLabel;
  4913. oldFalseLabel := falseLabel;
  4914. oldLocked := locked;
  4915. oldUsedRegisters := usedRegisters
  4916. END BackupGlobalState;
  4917. PROCEDURE RestoreGlobalState;
  4918. BEGIN
  4919. result := oldResult;
  4920. currentScope := oldCurrentScope;
  4921. arrayDestinationTag := oldArrayDestinationTag;
  4922. arrayDestinationDimension := oldArrayDestinationDimension;
  4923. constantDeclaration := oldConstantDeclaration;
  4924. destination := oldDestination;
  4925. currentLoop := oldCurrentLoop;
  4926. conditional := oldConditional;
  4927. trueLabel := oldTrueLabel;
  4928. falseLabel := oldFalseLabel;
  4929. locked := oldLocked;
  4930. usedRegisters := oldUsedRegisters
  4931. END RestoreGlobalState;
  4932. (** do preparations before parameter push for array-structured object types (ASOTs):
  4933. if ASOT is passed as VAR parameter:
  4934. - allocate temporary variable of math array type
  4935. - copy contents of ASOT to be passed to temporary variable
  4936. - use temporary variable as the actual parameter instead
  4937. - 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)
  4938. **)
  4939. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4940. VAR
  4941. expression: SyntaxTree.Expression;
  4942. BEGIN
  4943. IF actualParameter IS SyntaxTree.Designator THEN
  4944. designator := actualParameter(SyntaxTree.Designator);
  4945. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4946. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4947. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4948. ASSERT(checker # NIL);
  4949. checker.SetCurrentScope(currentScope);
  4950. (* allocate temporary variable *)
  4951. ASSERT(actualParameter.type # NIL);
  4952. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4953. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4954. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4955. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4956. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4957. ASSERT(tempVariableDesignator.type # NIL);
  4958. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4959. (* copy math array stored in actual parameter to temporary variable *)
  4960. BackupGlobalState;
  4961. AssignMathArray(tempVariableDesignator, actualParameter);
  4962. RestoreGlobalState;
  4963. (* use temporary variable as actual parameter instead of the original one *)
  4964. actualParameter := tempVariableDesignator;
  4965. (* create write-back call and store it in linked list *)
  4966. (* create new list entry *)
  4967. IF firstWriteBackCall = NIL THEN
  4968. NEW(firstWriteBackCall);
  4969. currentWriteBackCall := firstWriteBackCall
  4970. ELSE
  4971. ASSERT(currentWriteBackCall # NIL);
  4972. NEW(currentWriteBackCall.next);
  4973. currentWriteBackCall := currentWriteBackCall.next
  4974. END;
  4975. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4976. ASSERT(expression.type = NIL);
  4977. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4978. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4979. (* prepare writeback for any other "normal" indexer *)
  4980. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4981. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4982. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4983. Assign(tempVariableDesignator, actualParameter);
  4984. actualParameter := tempVariableDesignator;
  4985. IF firstWriteBackCall = NIL THEN
  4986. NEW(firstWriteBackCall);
  4987. currentWriteBackCall := firstWriteBackCall
  4988. ELSE
  4989. ASSERT(currentWriteBackCall # NIL);
  4990. NEW(currentWriteBackCall.next);
  4991. currentWriteBackCall := currentWriteBackCall.next
  4992. END;
  4993. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  4994. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4995. END
  4996. END
  4997. END PrepareParameter;
  4998. BEGIN
  4999. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5000. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5001. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5002. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5003. IF InlineProcedureCall(x) THEN
  5004. RETURN
  5005. ELSE
  5006. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5007. END
  5008. END;
  5009. END;
  5010. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5011. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5012. dest := destination; destination := emptyOperand;
  5013. SaveRegisters();ReleaseUsedRegisters(saved);
  5014. parameters := x.parameters;
  5015. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5016. (* an operator is called *)
  5017. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5018. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5019. (* check if a dynamic operator call should be performed *)
  5020. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5021. ELSE
  5022. isCallOfDynamicOperator := FALSE
  5023. END;
  5024. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5025. Emit(Push(position, ap));
  5026. END;
  5027. alignment := procedureType.stackAlignment;
  5028. IF alignment > 1 THEN
  5029. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5030. Emit(Mov(position,reg, sp));
  5031. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5032. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5033. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5034. Emit(And(position,sp, sp, mask));
  5035. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5036. Emit(Push(position,reg));
  5037. (*
  5038. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5039. Emit(Mov(position,mem,reg));
  5040. *)
  5041. ReleaseIntermediateOperand(reg);
  5042. END;
  5043. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5044. (* align stack to 16-byte boundary *)
  5045. IntermediateCode.InitImmediate(mask,addressType,-16);
  5046. Emit(And(position,sp, sp, mask));
  5047. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5048. IF gap # 0 THEN
  5049. IntermediateCode.InitImmediate(size,addressType,gap);
  5050. Emit(Sub(position,sp,sp,size))
  5051. END;
  5052. END;
  5053. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5054. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5055. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5056. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5057. ELSE
  5058. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5059. END;
  5060. Evaluate(x.left, operand);
  5061. IF symbol IS SyntaxTree.Procedure THEN
  5062. IF (procedureType.selfParameter # NIL) THEN
  5063. Emit(Push(position,operand.tag));
  5064. ELSIF x.left IS SyntaxTree.SupercallDesignator THEN
  5065. Emit(Push(position,operand.tag));
  5066. ELSIF (procedureType.isDelegate) THEN
  5067. Emit(Push(position,operand.tag));
  5068. END;
  5069. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5070. IF (procedureType.selfParameter # NIL) THEN
  5071. Emit(Push(position,operand.tag));
  5072. ELSIF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5073. Emit(Push(position,operand.tag));
  5074. END;
  5075. ELSE HALT(200);
  5076. END;
  5077. ReleaseIntermediateOperand(operand.tag);
  5078. operand.tag := emptyOperand;
  5079. (* determine if a structured return type is needed *)
  5080. structuredReturnType := SemanticChecker.StructuredReturnType(procedureType);
  5081. IF structuredReturnType THEN
  5082. IF resultDesignator # NIL THEN
  5083. d := resultDesignator;
  5084. ELSE
  5085. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5086. variable.SetUntraced(procedureType.hasUntracedReturn);
  5087. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5088. d.SetType(variable.type);
  5089. END;
  5090. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5091. Designate(d,returnValue);
  5092. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5093. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5094. Emit(Push(position,size));
  5095. Emit(Push(position,returnValue.op));
  5096. ReleaseOperand(returnValue);
  5097. ELSE
  5098. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5099. END;
  5100. END;
  5101. firstWriteBackCall := NIL; (* reset write-back call list *)
  5102. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5103. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5104. IF parameterRegister = NIL THEN parameterRegisters := 0
  5105. ELSE parameterRegisters := LEN(parameterRegister)
  5106. END;
  5107. passByRegister := parameterRegisters > 0;
  5108. registerNumber := 0;
  5109. formalParameter := procedureType.lastParameter;
  5110. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5111. actualParameter := parameters.GetExpression(i);
  5112. PrepareParameter(actualParameter, formalParameter);
  5113. IF passByRegister & (i < parameterRegisters) THEN
  5114. IF ~PassInRegister(formalParameter) THEN
  5115. Error(actualParameter.position,"cannot be passed by register")
  5116. ELSE
  5117. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5118. END;
  5119. INC(registerNumber);
  5120. ELSE
  5121. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5122. END;
  5123. formalParameter := formalParameter.prevParameter;
  5124. END;
  5125. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5126. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5127. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5128. END;
  5129. ELSE
  5130. hasDynamicOperands := FALSE;
  5131. formalParameter := procedureType.firstParameter;
  5132. FOR i := 0 TO parameters.Length() - 1 DO
  5133. actualParameter := parameters.GetExpression(i);
  5134. IF formalParameter # NIL THEN (* TENTATIVE *)
  5135. PrepareParameter(actualParameter, formalParameter);
  5136. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5137. ASSERT(i < 2);
  5138. hasDynamicOperands := TRUE;
  5139. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5140. ELSE
  5141. IF passByRegister & (registerNumber > 0) THEN
  5142. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5143. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5144. END;
  5145. passByRegister := FALSE;
  5146. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5147. END;
  5148. formalParameter := formalParameter.nextParameter;
  5149. END;
  5150. END;
  5151. END;
  5152. IF symbol IS SyntaxTree.Procedure THEN
  5153. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5154. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5155. Emit(Push(position,reg));
  5156. ReleaseIntermediateOperand(reg);
  5157. END;
  5158. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5159. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5160. parametersSize := ParametersSize(system,procedureType,FALSE);
  5161. END;
  5162. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5163. (*
  5164. dynamic operator overloading:
  5165. first push parameters, regularly:
  5166. [self]
  5167. par1
  5168. par2
  5169. then push parameters for GetOperator
  5170. identifier
  5171. ptr1
  5172. tag
  5173. ptr2
  5174. tag
  5175. call GetOperatorRuntimeProc
  5176. call Operator
  5177. *)
  5178. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5179. (* push ID *)
  5180. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5181. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5182. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5183. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5184. formalParameter := procedureType.firstParameter;
  5185. FOR i := 0 TO parameters.Length() - 1 DO
  5186. IF formalParameter.access # SyntaxTree.Hidden THEN
  5187. ASSERT(i < 2);
  5188. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5189. (* push pointer *)
  5190. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5191. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5192. (* add dereference *)
  5193. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5194. (*! better: do refer to stack above than using parameter backups !!*)
  5195. ReleaseIntermediateOperand(parameterBackups[i]);
  5196. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5197. END;
  5198. Emit(Push(position,parameterBackups[i]));
  5199. ReleaseIntermediateOperand(parameterBackups[i]);
  5200. (* push typetag *)
  5201. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5202. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5203. arg := TypeDescriptorAdr(recordType);
  5204. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5205. Emit(Push(position,arg));
  5206. ELSE
  5207. (* push 'NonPointer' *)
  5208. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5209. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5210. (* push fingerprint *)
  5211. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5212. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5213. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5214. END
  5215. END;
  5216. formalParameter := formalParameter.nextParameter
  5217. END;
  5218. (* for unary operators: complete the information for the second parameter *)
  5219. IF procedureType.numberParameters < 2 THEN
  5220. (* push 'NonPointer' *)
  5221. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5222. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5223. (* push 'NoType' *)
  5224. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5225. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5226. END;
  5227. (* call operator selection procedure *)
  5228. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5229. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5230. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5231. ReleaseOperand(operatorSelectionProcedureOperand);
  5232. (* use the address that the operator selection procedure returned as the target address of the call *)
  5233. InitOperand(operand, ModeValue);
  5234. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5235. Emit(Result(position,operand.op))
  5236. END
  5237. END;
  5238. ReleaseParameterRegisters();
  5239. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5240. Emit(Call(position,operand.op,0));
  5241. ELSE
  5242. Emit(Call(position,operand.op,parametersSize));
  5243. END;
  5244. ReleaseOperand(operand);
  5245. IF procedureType.noReturn THEN
  5246. EmitTrap(position,NoReturnTrap);
  5247. END;
  5248. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5249. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5250. Emit(Result(position,return));
  5251. END;
  5252. (* === return parameter space === *)
  5253. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5254. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5255. IF parametersSize < 32 THEN
  5256. (* allocated space for all parameter registers *)
  5257. parametersSize := 32
  5258. END;
  5259. size := IntermediateCode.Immediate(addressType,parametersSize);
  5260. Emit(Add(position,sp,sp,size))
  5261. END;
  5262. IF SysvABI(procedureType.callingConvention) THEN
  5263. IF passByRegister THEN
  5264. IF parameters.Length() > parameterRegisters THEN
  5265. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5266. ELSE
  5267. parametersSize := 0
  5268. END;
  5269. ELSE
  5270. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5271. INC( parametersSize, (-parametersSize) MOD 16 )
  5272. END;
  5273. IF parametersSize > 0 THEN
  5274. size := IntermediateCode.Immediate(addressType,parametersSize);
  5275. Emit(Add(position,sp,sp,size))
  5276. END;
  5277. END;
  5278. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5279. IF structuredReturnType THEN
  5280. (* stack pointer rewinding done by callee
  5281. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5282. Emit(Add(position,sp,sp,size));
  5283. *)
  5284. RestoreRegisters(saved);
  5285. InitOperand(result,ModeReference);
  5286. Symbol(variable,result);
  5287. ELSE
  5288. RestoreRegisters(saved);
  5289. InitOperand(result,ModeValue);
  5290. result.op := return;
  5291. END;
  5292. END;
  5293. IF alignment > 1 THEN
  5294. Emit(Pop(position,sp));
  5295. END;
  5296. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5297. Emit(Pop(position, ap));
  5298. END;
  5299. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5300. ValueToCondition(result);
  5301. END;
  5302. destination := dest;
  5303. (* perform all write-back calls in the list *)
  5304. BackupGlobalState;
  5305. currentWriteBackCall := firstWriteBackCall;
  5306. WHILE currentWriteBackCall # NIL DO
  5307. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5308. currentWriteBackCall := currentWriteBackCall.next
  5309. END;
  5310. RestoreGlobalState;
  5311. (* TENATIVE *)
  5312. (*
  5313. IF isOperatorCall THEN
  5314. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5315. END;
  5316. *)
  5317. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5318. END VisitProcedureCallDesignator;
  5319. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5320. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5321. td: SyntaxTree.Symbol;
  5322. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5323. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5324. BEGIN
  5325. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5326. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5327. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5328. variable.SetType(system.anyType);
  5329. scope.AddVariable(variable);
  5330. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5331. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5332. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5333. typeDeclaration.SetScope(module.module.moduleScope);
  5334. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5335. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5336. END;
  5337. RETURN hiddenPointerType;
  5338. END GetHiddenPointerType;
  5339. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5340. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5341. BEGIN
  5342. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5343. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5344. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5345. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5346. scope.AddVariable(variable);
  5347. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5348. variable.SetType(system.anyType);
  5349. scope.AddVariable(variable);
  5350. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5351. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5352. typeDeclaration.SetDeclaredType(delegatePointerType);
  5353. typeDeclaration.SetScope(module.module.moduleScope);
  5354. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5355. delegatePointerType.SetState(SyntaxTree.Resolved);
  5356. END;
  5357. RETURN delegatePointerType
  5358. END GetDelegateType;
  5359. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5360. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5361. such as in VAR a: RECORD ... END;
  5362. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5363. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5364. *)
  5365. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5366. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5367. BEGIN (* no code emission *)
  5368. source := NIL;
  5369. x := x.resolved;
  5370. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5371. x := GetHiddenPointerType();
  5372. ELSIF IsDelegate(x) THEN
  5373. x := GetDelegateType();
  5374. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5375. ELSE HALT(200);
  5376. END;
  5377. td := x.typeDeclaration;
  5378. IF td = NIL THEN
  5379. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5380. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5381. ASSERT(td # NIL);
  5382. END;
  5383. IF newObjectFile THEN
  5384. GetCodeSectionNameForSymbol(td,name);
  5385. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5386. meta.CheckTypeDeclaration(x);
  5387. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5388. ELSE
  5389. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5390. END;
  5391. IF backend.cooperative OR meta.simple THEN
  5392. offset := 0;
  5393. ELSE
  5394. IF x IS SyntaxTree.CellType THEN
  5395. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5396. IF baseRecord = NIL THEN
  5397. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5398. ELSE
  5399. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5400. END;
  5401. ELSE
  5402. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5403. END;
  5404. END;
  5405. ELSE
  5406. offset := 0;
  5407. source := module.allSections.FindBySymbol(td); (*TODO*)
  5408. IF source = NIL THEN
  5409. null := 0;
  5410. GetCodeSectionNameForSymbol(td,name);
  5411. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5412. Basic.SuffixSegmentedName (name, module.module.name);
  5413. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5414. IntermediateCode.InitImmediate(op,addressType,0);
  5415. source(IntermediateCode.Section).Emit(Data(position,op));
  5416. source.SetReferenced(FALSE)
  5417. ELSE
  5418. name := source.name;
  5419. END;
  5420. END;
  5421. RETURN td
  5422. END GetBackendType;
  5423. BEGIN
  5424. (*td := t.typeDeclaration;*)
  5425. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5426. (*
  5427. IF t IS SyntaxTree.PointerType THEN
  5428. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5429. ELSE
  5430. source := GetBackendType(t);
  5431. END;
  5432. *)
  5433. IF newObjectFile THEN
  5434. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5435. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5436. IntermediateCode.SetOffset(res,offset);
  5437. ELSE
  5438. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5439. END;
  5440. (*
  5441. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5442. make memory should be used when tag is used, not earlier
  5443. *)
  5444. RETURN res
  5445. END TypeDescriptorAdr;
  5446. (*
  5447. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5448. VAR result: IntermediateCode.Operand;
  5449. BEGIN
  5450. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5451. operand.tag := TypeDescriptorAdr(operand.type);
  5452. IntermediateCode.MakeMemory(operand.tag,addressType);
  5453. UseIntermediateOperand(operand.tag);
  5454. END;
  5455. END MakeTypeTag;
  5456. *)
  5457. PROCEDURE ProfilerInit;
  5458. VAR reg: IntermediateCode.Operand;
  5459. BEGIN
  5460. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5461. Emit(Call(position,reg,0));
  5462. END ProfilerInit;
  5463. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5464. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5465. BEGIN
  5466. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5467. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5468. THEN
  5469. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5470. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5471. Emit(Push(position,reg));
  5472. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5473. Emit(Push(position,reg));
  5474. StaticCallOperand(result,procedure);
  5475. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5476. ReleaseOperand(result);
  5477. END;
  5478. END ProfilerEnterExit;
  5479. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5480. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5481. BEGIN
  5482. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5483. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5484. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5485. profileInit.Emit(Push(position,reg));
  5486. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5487. profileInit.Emit(Push(position,reg));
  5488. NEW(string, LEN(name)); COPY(name, string^);
  5489. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5490. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5491. sv.SetType(type);
  5492. Designate(sv,result);
  5493. profileInit.Emit(Push(position,result.tag));
  5494. profileInit.Emit(Push(position,result.op));
  5495. StaticCallOperand(result,procedure);
  5496. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5497. ReleaseOperand(result);
  5498. END;
  5499. END ProfilerAddProcedure;
  5500. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5501. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5502. BEGIN
  5503. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5504. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5505. profileInit.Emit(Push(position,reg));
  5506. profileInitPatchPosition := profileInit.pc;
  5507. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5508. NEW(string, LEN(name)); COPY(name, string^);
  5509. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5510. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5511. sv.SetType(type);
  5512. Designate(sv,result);
  5513. profileInit.Emit(Push(position,result.tag));
  5514. profileInit.Emit(Push(position,result.op));
  5515. StaticCallOperand(result,procedure);
  5516. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5517. ReleaseOperand(result);
  5518. END;
  5519. END ProfilerAddModule;
  5520. PROCEDURE ProfilerPatchInit;
  5521. VAR reg: IntermediateCode.Operand;
  5522. BEGIN
  5523. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5524. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5525. EmitLeave(profileInit,position,NIL,0);
  5526. profileInit.Emit(Exit(position,0,0,0));
  5527. END ProfilerPatchInit;
  5528. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5529. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5530. VAR
  5531. id: LONGINT;
  5532. leftType, rightType: SyntaxTree.Type;
  5533. procedureType: SyntaxTree.ProcedureType;
  5534. runtimeProcedure: SyntaxTree.Procedure;
  5535. runtimeProcedureOperand, operatorOperand: Operand;
  5536. kind: SET;
  5537. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5538. VAR
  5539. arg: IntermediateCode.Operand;
  5540. recordType: SyntaxTree.RecordType;
  5541. fingerPrint: SyntaxTree.FingerPrint;
  5542. BEGIN
  5543. IF type = NIL THEN
  5544. (* no type: push 'NoType' *)
  5545. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5546. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5547. ELSIF IsStrictlyPointerToRecord(type) THEN
  5548. (* pointer to record type: push typetag *)
  5549. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5550. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5551. arg := TypeDescriptorAdr(recordType);
  5552. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5553. ELSE
  5554. (* non-pointer to record type: push fingerprint *)
  5555. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5556. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5557. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5558. END;
  5559. operatorInitializationCodeSection.Emit(Push(position,arg))
  5560. END PushTypeInfo;
  5561. BEGIN
  5562. ASSERT(operatorInitializationCodeSection # NIL);
  5563. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5564. procedureType := operator.type(SyntaxTree.ProcedureType);
  5565. (* determine types *)
  5566. leftType := procedureType.firstParameter.type;
  5567. IF procedureType.numberParameters = 2 THEN
  5568. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5569. rightType := procedureType.firstParameter.nextParameter.type;
  5570. ELSE
  5571. rightType := NIL
  5572. END;
  5573. (* determine operator kind *)
  5574. IF IsStrictlyPointerToRecord(leftType) THEN
  5575. kind := {LhsIsPointer}
  5576. ELSE
  5577. kind := {}
  5578. END;
  5579. IF IsStrictlyPointerToRecord(rightType) THEN
  5580. kind := kind + {RhsIsPointer}
  5581. END;
  5582. IF kind # {} THEN (* TODO: to be removed later on *)
  5583. (* at least one of the types is a pointer to record *)
  5584. (* emit a code that registers this specific operator in the runtime *)
  5585. dump := operatorInitializationCodeSection.comments;
  5586. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5587. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5588. (* push ID *)
  5589. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5590. id := Global.GetSymbol(module.module.case, operator.name);
  5591. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5592. (* push kind *)
  5593. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5594. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5595. (* push type infos *)
  5596. PushTypeInfo(leftType);
  5597. PushTypeInfo(rightType);
  5598. (* push operator address *)
  5599. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5600. StaticCallOperand(operatorOperand, operator);
  5601. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5602. ReleaseOperand(operatorOperand);
  5603. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5604. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5605. ReleaseOperand(runtimeProcedureOperand)
  5606. END
  5607. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5608. END
  5609. END RegisterDynamicOperator;
  5610. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5611. VAR
  5612. traceModule: SyntaxTree.Module;
  5613. procedure: SyntaxTree.Procedure;
  5614. procedureVariable: SyntaxTree.Variable;
  5615. s,msg: Basic.MessageString;
  5616. res: Operand;
  5617. i: LONGINT;
  5618. sv: SyntaxTree.StringValue;
  5619. type: SyntaxTree.Type;
  5620. recordType: SyntaxTree.RecordType;
  5621. printout: Printout.Printer;
  5622. stringWriter: Streams.StringWriter;
  5623. expression: SyntaxTree.Expression;
  5624. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5625. BEGIN
  5626. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5627. IF procedure = NIL THEN
  5628. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5629. END;
  5630. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5631. s := "procedure ";
  5632. Strings.Append(s,backend.traceModuleName);
  5633. Strings.Append(s,".");
  5634. Strings.Append(s,procedureName);
  5635. Strings.Append(s," not present");
  5636. Error(position,s);
  5637. RETURN FALSE
  5638. ELSE
  5639. RETURN TRUE
  5640. END;
  5641. END GetProcedure;
  5642. PROCEDURE CallProcedure;
  5643. VAR size: LONGINT;
  5644. BEGIN
  5645. IF procedure # NIL THEN
  5646. StaticCallOperand(result,procedure);
  5647. size := ProcedureParametersSize(system,procedure);
  5648. ELSE
  5649. Symbol(procedureVariable, result);
  5650. LoadValue(result, procedureVariable.type.resolved);
  5651. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5652. END;
  5653. Emit(Call(position,result.op,size));
  5654. END CallProcedure;
  5655. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5656. VAR res: Operand; string: SyntaxTree.String;
  5657. BEGIN
  5658. IF GetProcedure("String") THEN
  5659. NEW(string, LEN(s)); COPY(s, string^);
  5660. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5661. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5662. sv.SetType(type);
  5663. Designate(sv,res);
  5664. Emit(Push(position,res.tag));
  5665. Emit(Push(position,res.op));
  5666. ReleaseOperand(res);
  5667. CallProcedure;
  5668. END;
  5669. END String;
  5670. PROCEDURE Integer(op: IntermediateCode.Operand);
  5671. BEGIN
  5672. IF GetProcedure("Int") THEN
  5673. Emit(Push(position,op));
  5674. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5675. CallProcedure;
  5676. END;
  5677. END Integer;
  5678. PROCEDURE Float(op: IntermediateCode.Operand);
  5679. BEGIN
  5680. IF GetProcedure("HIntHex") THEN
  5681. Emit(Push(position,op));
  5682. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5683. CallProcedure;
  5684. END;
  5685. END Float;
  5686. PROCEDURE Set(op: IntermediateCode.Operand);
  5687. BEGIN
  5688. IF GetProcedure("Set") THEN
  5689. Emit(Push(position,op));
  5690. (*
  5691. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5692. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5693. *)
  5694. CallProcedure;
  5695. END;
  5696. END Set;
  5697. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5698. BEGIN
  5699. IF GetProcedure("Boolean") THEN
  5700. Emit(Push(position,op));
  5701. CallProcedure;
  5702. END;
  5703. END Boolean;
  5704. PROCEDURE Char(op: IntermediateCode.Operand);
  5705. BEGIN
  5706. IF GetProcedure("Char") THEN
  5707. Emit(Push(position,op));
  5708. CallProcedure;
  5709. END;
  5710. END Char;
  5711. PROCEDURE Address(op: IntermediateCode.Operand);
  5712. BEGIN
  5713. IF GetProcedure("Address") THEN
  5714. Emit(Push(position,op));
  5715. CallProcedure;
  5716. END;
  5717. END Address;
  5718. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5719. VAR len: IntermediateCode.Operand;
  5720. BEGIN
  5721. IF GetProcedure("String") THEN
  5722. len := GetArrayLength(type, op.tag);
  5723. Emit(Push(position,len));
  5724. ReleaseIntermediateOperand(len);
  5725. Emit(Push(position,op.op));
  5726. CallProcedure;
  5727. END;
  5728. END StringOperand;
  5729. PROCEDURE Ln;
  5730. BEGIN
  5731. IF GetProcedure("Ln") THEN
  5732. CallProcedure;
  5733. END;
  5734. END Ln;
  5735. BEGIN
  5736. IF backend.traceModuleName = "" THEN RETURN END;
  5737. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5738. IF GetProcedure("Enter") THEN
  5739. CallProcedure
  5740. END;
  5741. NEW(stringWriter,LEN(s));
  5742. FOR i := 0 TO x.Length()-1 DO
  5743. msg := "";
  5744. expression := x.GetExpression(i);
  5745. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5746. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5747. ELSE
  5748. Global.GetModuleName(module.module, s);
  5749. END;
  5750. IF i = 0 THEN
  5751. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5752. stringWriter.String(":");
  5753. END;
  5754. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5755. IF ~(expression IS SyntaxTree.StringValue) THEN
  5756. printout.Expression(expression);
  5757. stringWriter.Get(s);
  5758. Strings.Append(msg,s);
  5759. Strings.Append(msg,"= ");
  5760. ELSE stringWriter.Get(s); (* remove from string writer *)
  5761. Strings.Append(msg, s);
  5762. END;
  5763. String(msg);
  5764. IF SemanticChecker.IsStringType(expression.type) THEN
  5765. Designate(expression,res);
  5766. StringOperand(res, expression.type);
  5767. ELSE
  5768. Evaluate(expression,res);
  5769. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5770. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5771. Convert(res.op,int64);
  5772. END;
  5773. Integer(res.op);
  5774. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5775. Boolean(res.op);
  5776. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5777. Set(res.op);
  5778. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5779. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5780. Convert(res.op,float64);
  5781. END;
  5782. Float(res.op);
  5783. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5784. Char(res.op);
  5785. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5786. Address(res.op);String("H");
  5787. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5788. Address(res.op);String("H");
  5789. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5790. Address(res.op);String("H");
  5791. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5792. Address(res.op);String("H");
  5793. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5794. String("NIL");
  5795. ELSE HALT(200);
  5796. END;
  5797. END;
  5798. ReleaseOperand(res);
  5799. String("; ");
  5800. END;
  5801. IF GetProcedure("Exit") THEN
  5802. CallProcedure
  5803. ELSE
  5804. Ln;
  5805. END;
  5806. END;
  5807. END SystemTrace;
  5808. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5809. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5810. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5811. BEGIN
  5812. type := type.resolved;
  5813. IF type IS SyntaxTree.RecordType THEN
  5814. WITH type: SyntaxTree.RecordType DO
  5815. baseType := type.baseType;
  5816. IF baseType # NIL THEN
  5817. baseType := baseType.resolved;
  5818. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5819. InitFields(baseType,adr,offset);
  5820. END;
  5821. variable := type.recordScope.firstVariable;
  5822. WHILE variable # NIL DO
  5823. IF variable.initializer # NIL THEN
  5824. Evaluate(variable.initializer,initializerOp);
  5825. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5826. Emit(Mov(position,mem,initializerOp.op));
  5827. ReleaseOperand(initializerOp);
  5828. ReleaseIntermediateOperand(mem);
  5829. END;
  5830. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5831. variable := variable.nextVariable
  5832. END;
  5833. END;
  5834. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5835. WITH type: SyntaxTree.ArrayType DO
  5836. IF type.form = SyntaxTree.Static THEN
  5837. baseType := type.arrayBase;
  5838. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5839. FOR i := 0 TO type.staticLength-1 DO
  5840. InitFields(baseType,adr,offset+i*size);
  5841. END;
  5842. END;
  5843. END;
  5844. ELSIF type IS SyntaxTree.MathArrayType THEN
  5845. WITH type: SyntaxTree.MathArrayType DO
  5846. IF type.form = SyntaxTree.Open THEN
  5847. dim := DynamicDim(type);
  5848. imm := IntermediateCode.Immediate(addressType,dim);
  5849. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5850. baseType := SemanticChecker.ArrayBase(type,dim);
  5851. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5852. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5853. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5854. ReleaseIntermediateOperand(imm);
  5855. (* flags remain empty (=0) for open array *)
  5856. ELSIF type.form = SyntaxTree.Static THEN
  5857. baseType := type.arrayBase;
  5858. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5859. ASSERT(type.staticLength < 1024*1024*1024);
  5860. FOR i := 0 TO type.staticLength-1 DO
  5861. InitFields(baseType,adr,offset+i*size);
  5862. END;
  5863. END;
  5864. END;
  5865. END;
  5866. END InitFields;
  5867. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5868. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5869. BEGIN
  5870. type := variable.type.resolved;
  5871. IF (type IS SyntaxTree.MathArrayType) THEN
  5872. WITH type: SyntaxTree.MathArrayType DO
  5873. IF type.form = SyntaxTree.Open THEN
  5874. Symbol(variable,operand);
  5875. InitFields(type, operand.tag,0);
  5876. ELSIF type.form = SyntaxTree.Tensor THEN
  5877. Symbol(variable, operand);
  5878. MakeMemory(tmp,operand.op,addressType,0);
  5879. ReleaseOperand(operand);
  5880. Emit(Mov(position,tmp, nil ) );
  5881. ReleaseIntermediateOperand(tmp);
  5882. END;
  5883. END;
  5884. ELSE
  5885. Symbol(variable,operand);
  5886. IF variable.initializer # NIL THEN
  5887. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5888. reference.SetType(variable.type.resolved);
  5889. reference.SetAssignable(TRUE);
  5890. Assign(reference,variable.initializer);
  5891. END;
  5892. InitFields(type, operand.op,0);
  5893. ReleaseOperand(operand);
  5894. END;
  5895. END InitVariable;
  5896. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5897. VAR end: Label;
  5898. BEGIN
  5899. IF type.form = SyntaxTree.Tensor THEN
  5900. InitOperand(result,ModeValue);
  5901. ReuseCopy(result.op,base);
  5902. end := NewLabel();
  5903. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5904. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5905. SetLabel(end);
  5906. Convert(result.op,int32);
  5907. ELSE
  5908. InitOperand(result,ModeValue);
  5909. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5910. END
  5911. END MathArrayDim;
  5912. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5913. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5914. BEGIN
  5915. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5916. MakeMemory(mem,base,addressType,offset);
  5917. Emit(Mov(position,mem,value));
  5918. ReleaseIntermediateOperand(mem);
  5919. END PutMathArrayField;
  5920. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5921. VAR mem: IntermediateCode.Operand;
  5922. BEGIN
  5923. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5924. MakeMemory(mem,base,addressType,offset);
  5925. Emit(Mov(position,mem,value));
  5926. ReleaseIntermediateOperand(mem);
  5927. END PutMathArrayFieldOffset;
  5928. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5929. BEGIN
  5930. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5931. MakeMemory(value,base,addressType,offset);
  5932. END GetMathArrayField;
  5933. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5934. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5935. BEGIN
  5936. IF incr THEN
  5937. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5938. ELSE
  5939. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5940. END;
  5941. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5942. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5943. ELSE
  5944. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5945. Emit(Mov(position,reg,dim));
  5946. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5947. Emit(Add(position,reg,reg,base));
  5948. MakeMemory(mem, reg, addressType, offset);
  5949. ReleaseIntermediateOperand(reg);
  5950. Emit(Mov(position,mem,value));
  5951. ReleaseIntermediateOperand(mem);
  5952. END;
  5953. END PutMathArrayLenOrIncr;
  5954. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5955. BEGIN
  5956. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5957. END PutMathArrayLength;
  5958. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5959. BEGIN
  5960. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5961. END PutMathArrayIncrement;
  5962. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5963. BEGIN
  5964. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5965. END GetMathArrayIncrement;
  5966. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5967. BEGIN
  5968. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5969. END GetMathArrayLength;
  5970. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5971. VAR dimOp: IntermediateCode.Operand;
  5972. BEGIN
  5973. dimOp := IntermediateCode.Immediate(int32, dim);
  5974. GetMathArrayLength(type, operand, dimOp, check, result);
  5975. END GetMathArrayLengthAt;
  5976. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5977. VAR dimOp: IntermediateCode.Operand;
  5978. BEGIN
  5979. dimOp := IntermediateCode.Immediate(int32, dim);
  5980. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5981. END GetMathArrayIncrementAt;
  5982. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5983. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5984. offset: LONGINT;
  5985. BEGIN
  5986. IF increment THEN
  5987. offset := MathIncrOffset;
  5988. ELSE
  5989. offset := MathLenOffset;
  5990. END;
  5991. INC(offset,operand.dimOffset*2);
  5992. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5993. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5994. END;
  5995. (* static dimension *)
  5996. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5997. IF check & (type.form = SyntaxTree.Tensor) THEN
  5998. DimensionCheck(operand.tag,dim,BrltL);
  5999. END;
  6000. val := SHORT(dim.intValue);
  6001. IF type.form # SyntaxTree.Tensor THEN
  6002. t := SemanticChecker.ArrayBase(type,val);
  6003. type := t.resolved(SyntaxTree.MathArrayType);
  6004. IF type.form = SyntaxTree.Static THEN
  6005. IF increment THEN
  6006. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6007. ELSE
  6008. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6009. END;
  6010. InitOperand(result,ModeValue);
  6011. result.op := res;
  6012. RETURN;
  6013. END;
  6014. END;
  6015. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6016. MakeMemory(res,operand.tag,addressType,offset);
  6017. (*
  6018. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6019. *)
  6020. InitOperand(result,ModeValue);
  6021. result.op := res;
  6022. ELSE
  6023. Convert(dim,addressType);
  6024. IF check THEN
  6025. IF type.form = SyntaxTree.Tensor THEN
  6026. DimensionCheck(operand.tag,dim,BrltL);
  6027. ELSIF isUnchecked THEN (* do nothing *)
  6028. ELSE
  6029. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6030. END;
  6031. END;
  6032. end := NewLabel(); next := NIL;
  6033. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6034. Emit(Mov(position,res,dim));
  6035. Convert(res,int32);
  6036. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6037. WHILE t IS SyntaxTree.MathArrayType DO
  6038. type := t(SyntaxTree.MathArrayType);
  6039. IF type.form = SyntaxTree.Static THEN
  6040. imm := IntermediateCode.Immediate(int32,val);
  6041. next := NewLabel();
  6042. BrneL(next,imm,res);
  6043. IF increment THEN
  6044. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6045. ELSE
  6046. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6047. END;
  6048. Emit(MovReplace(position,res,imm));
  6049. BrL(end);
  6050. ELSE hasDynamicPart := TRUE;
  6051. END;
  6052. t := type.arrayBase.resolved;
  6053. val := val + 1;
  6054. IF next # NIL THEN SetLabel(next) END;
  6055. END;
  6056. IF hasDynamicPart THEN
  6057. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6058. Emit(Mov(position,res2,dim));
  6059. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6060. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6061. Emit(Add(position,res2,res2,imm));
  6062. Emit(Add(position,res2,res2,operand.tag));
  6063. IntermediateCode.MakeMemory(res2,int32);
  6064. Emit(MovReplace(position,res,res2));
  6065. ReleaseIntermediateOperand(res2);
  6066. END;
  6067. SetLabel(end);
  6068. Convert(res,int32);
  6069. InitOperand(result,ModeValue);
  6070. result.op := res;
  6071. END;
  6072. END MathArrayLenOrIncr;
  6073. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6074. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6075. offset: LONGINT;
  6076. BEGIN
  6077. offset := operand.dimOffset+DynamicDim(type)-1;
  6078. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6079. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6080. val := SHORT(dim.intValue);
  6081. t := SemanticChecker.ArrayBase(type,val);
  6082. type := t.resolved(SyntaxTree.ArrayType);
  6083. IF type.form = SyntaxTree.Static THEN
  6084. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6085. ELSE
  6086. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6087. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6088. END;
  6089. UseIntermediateOperand(res);
  6090. InitOperand(result,ModeValue);
  6091. result.op := res;
  6092. ELSE
  6093. Convert(dim,addressType);
  6094. IF ~isUnchecked THEN
  6095. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6096. END;
  6097. end := NewLabel(); next := NIL;
  6098. (* ReuseCopy(dim,res); *)
  6099. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6100. Emit(Mov(position,res,dim));
  6101. Convert(res,int32);
  6102. Convert(res,int32);
  6103. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6104. WHILE t IS SyntaxTree.ArrayType DO
  6105. type := t(SyntaxTree.ArrayType);
  6106. IF type.form = SyntaxTree.Static THEN
  6107. imm := IntermediateCode.Immediate(int32,val);
  6108. next := NewLabel();
  6109. BrneL(next,imm,res);
  6110. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6111. Emit(MovReplace(position,res,imm));
  6112. BrL(end);
  6113. ELSE hasDynamicPart := TRUE;
  6114. END;
  6115. t := type.arrayBase.resolved;
  6116. val := val + 1;
  6117. IF next # NIL THEN SetLabel(next) END;
  6118. END;
  6119. IF hasDynamicPart THEN
  6120. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6121. Convert(res2,addressType);
  6122. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6123. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6124. Emit(Sub(position,res2,imm,res2));
  6125. Emit(Add(position,res2,res2,operand.tag));
  6126. IntermediateCode.MakeMemory(res2,int32);
  6127. Emit(MovReplace(position,res,res2));
  6128. ReleaseIntermediateOperand(res2);
  6129. END;
  6130. SetLabel(end);
  6131. Convert(res,int32);
  6132. InitOperand(result,ModeValue);
  6133. result.op := res;
  6134. END;
  6135. END ArrayLen;
  6136. (**
  6137. create a temporary variable in current scope
  6138. **)
  6139. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6140. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6141. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6142. BEGIN
  6143. IF ~register THEN
  6144. variable := temporaries.GetFreeVariable(type, index);
  6145. ELSE
  6146. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6147. END;
  6148. scope := currentScope;
  6149. IF variable = NIL THEN
  6150. COPY("@hiddenIRVar",string);
  6151. Basic.AppendNumber(string,index);
  6152. name := SyntaxTree.NewIdentifier(string);
  6153. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6154. variable.SetType(type);
  6155. variable.SetAccess(SyntaxTree.Hidden);
  6156. IF ~register THEN
  6157. temporaries.AddVariable(variable);
  6158. IF scope.lastVariable # NIL THEN
  6159. offset := scope.lastVariable.offsetInBits;
  6160. ELSE
  6161. offset := 0;
  6162. END;
  6163. DEC(offset,system.SizeOf(variable.type));
  6164. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6165. variable(SyntaxTree.Variable).SetOffset(offset);
  6166. scope.AddVariable(variable(SyntaxTree.Variable));
  6167. scope.EnterSymbol(variable, duplicate);
  6168. ASSERT(~duplicate);
  6169. InitVariable(variable(SyntaxTree.Variable));
  6170. ELSE
  6171. variable.SetUseRegister(TRUE);
  6172. variable(SyntaxTree.Variable).SetOffset(0);
  6173. END;
  6174. ELSE
  6175. InitVariable(variable(SyntaxTree.Variable));
  6176. (*
  6177. ASSERT(variable.type.resolved = type.resolved)
  6178. *)
  6179. END;
  6180. RETURN variable(SyntaxTree.Variable)
  6181. END GetTemporaryVariable;
  6182. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6183. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6184. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6185. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6186. i: LONGINT; duplicate: BOOLEAN;
  6187. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6188. VAR variable: SyntaxTree.Variable;
  6189. BEGIN
  6190. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6191. variable.SetType(type);
  6192. recordScope.AddVariable(variable);
  6193. END AddVariable;
  6194. BEGIN
  6195. name := "@ArrayDescriptor";
  6196. Basic.AppendNumber(name,dimensions);
  6197. identifier := SyntaxTree.NewIdentifier(name);
  6198. parentScope := module.module.moduleScope;
  6199. symbol := parentScope.FindSymbol(identifier);
  6200. IF symbol # NIL THEN
  6201. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6202. type := typeDeclaration.declaredType;
  6203. ELSE
  6204. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6205. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6206. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6207. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6208. recordType.SetTypeDeclaration(typeDeclaration);
  6209. recordType.SetState(SyntaxTree.Resolved);
  6210. typeDeclaration.SetDeclaredType(recordType);
  6211. AddVariable("@ptr",system.anyType);
  6212. AddVariable("@adr",system.addressType);
  6213. AddVariable("@flags",system.addressType);
  6214. AddVariable("@dim",system.addressType);
  6215. AddVariable("@elementSize",system.addressType);
  6216. FOR i := 0 TO dimensions-1 DO
  6217. name := "@len";
  6218. Basic.AppendNumber(name,i);
  6219. AddVariable(name,system.addressType);
  6220. name := "@incr";
  6221. Basic.AppendNumber(name,i);
  6222. AddVariable(name,system.addressType);
  6223. END;
  6224. parentScope.AddTypeDeclaration(typeDeclaration);
  6225. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6226. ASSERT(~duplicate);
  6227. type := recordType;
  6228. END;
  6229. RETURN type
  6230. END GetMathArrayDescriptorType;
  6231. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6232. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6233. BEGIN
  6234. type := GetMathArrayDescriptorType(dimensions);
  6235. Emit(Push(position,op.op));
  6236. (* push type descriptor *)
  6237. reg := TypeDescriptorAdr(type);
  6238. IF ~newObjectFile THEN
  6239. IntermediateCode.MakeMemory(reg,addressType);
  6240. END;
  6241. Emit(Push(position,reg));
  6242. ReleaseIntermediateOperand(reg);
  6243. (* push realtime flag: false by default *)
  6244. Emit(Push(position,false));
  6245. CallThis(position,"Heaps","NewRec",3);
  6246. END NewMathArrayDescriptor;
  6247. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6248. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6249. BEGIN
  6250. NEW(string, LEN(s)); COPY(s, string^);
  6251. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6252. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6253. sv.SetType(type);
  6254. Designate(sv,res);
  6255. Emit(Push(position,res.tag));
  6256. Emit(Push(position,res.op));
  6257. ReleaseOperand(res);
  6258. END PushConstString;
  6259. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6260. BEGIN
  6261. IF b THEN
  6262. Emit(Push(Basic.invalidPosition, true));
  6263. ELSE
  6264. Emit(Push(Basic.invalidPosition, false));
  6265. END;
  6266. END PushConstBoolean;
  6267. PROCEDURE PushConstSet(v: SET);
  6268. VAR value: SyntaxTree.Value; op: Operand;
  6269. BEGIN
  6270. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6271. value.SetType(system.setType);
  6272. Evaluate(value, op);
  6273. Emit(Push(Basic.invalidPosition, op.op));
  6274. ReleaseOperand(op);
  6275. END PushConstSet;
  6276. PROCEDURE PushConstInteger(v: LONGINT);
  6277. VAR value: SyntaxTree.Value; op: Operand;
  6278. BEGIN
  6279. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6280. value.SetType(system.longintType);
  6281. Evaluate(value, op);
  6282. Emit(Push(Basic.invalidPosition, op.op));
  6283. ReleaseOperand(op);
  6284. END PushConstInteger;
  6285. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6286. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6287. section: IntermediateCode.Section;
  6288. BEGIN
  6289. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6290. Global.GetSymbolSegmentedName(symbol, name);
  6291. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6292. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6293. procedure.SetScope(moduleScope);
  6294. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6295. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6296. procedure.SetAccess(SyntaxTree.Hidden);
  6297. currentScope := procedureScope;
  6298. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6299. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6300. RETURN section;
  6301. END OpenInitializer;
  6302. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6303. BEGIN
  6304. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6305. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6306. section := prev;
  6307. END CloseInitializer;
  6308. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6309. VAR name: SyntaxTree.IdentifierString;
  6310. variable: SyntaxTree.Variable;
  6311. parameter: SyntaxTree.Parameter;
  6312. type: SyntaxTree.Type;
  6313. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6314. BEGIN
  6315. InitOperand(op,ModeReference);
  6316. op.op := fp;
  6317. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6318. Dereference(op, x, FALSE);
  6319. result := op;
  6320. Symbol(symbol, op);
  6321. END Field;
  6322. PROCEDURE Direction(direction: LONGINT): SET;
  6323. BEGIN
  6324. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6325. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6326. ELSE HALT(100);
  6327. END;
  6328. END Direction;
  6329. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6330. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6331. BEGIN
  6332. Field(port, op);
  6333. ToMemory(op.op,addressType,0);
  6334. Emit(Push(Basic.invalidPosition, op.op));
  6335. ReleaseOperand(op);
  6336. Basic.GetString(modifier.identifier, name);
  6337. PushConstString(name);
  6338. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6339. ASSERT(SemanticChecker.IsStringType(value.type));
  6340. Designate(value, op);
  6341. Emit(Push(modifier.position, op.tag));
  6342. Emit(Push(modifier.position, op.op));
  6343. ReleaseOperand(op);
  6344. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6345. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6346. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6347. Evaluate(value, op);
  6348. Emit(Push(modifier.position, op.op));
  6349. ReleaseOperand(op);
  6350. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6351. ELSE
  6352. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6353. END;
  6354. END AddPortProperty;
  6355. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6356. VAR modifier: SyntaxTree.Modifier;
  6357. BEGIN
  6358. modifier := parameter.modifiers;
  6359. WHILE modifier # NIL DO
  6360. AddPortProperty(parameter,modifier, modifier.expression);
  6361. modifier := modifier.nextModifier;
  6362. END;
  6363. END AddPortProperties;
  6364. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6365. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6366. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6367. PROCEDURE PushLens(type: SyntaxTree.Type);
  6368. BEGIN
  6369. IF IsSemiDynamicArray(type) THEN
  6370. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6371. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6372. Emit(Push(Basic.invalidPosition, op.op));
  6373. ReleaseOperand(op);
  6374. INC(dim);
  6375. ELSIF IsStaticArray(type) THEN
  6376. len := len * type(SyntaxTree.ArrayType).staticLength;
  6377. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6378. INC(dim);
  6379. ELSE
  6380. baseType := type;
  6381. END;
  6382. END PushLens;
  6383. BEGIN
  6384. (* cell *)
  6385. IF parameter.type IS SyntaxTree.ArrayType THEN
  6386. type := parameter.type;
  6387. dim := 0;
  6388. len := 1;
  6389. PushLens(type);
  6390. portType := baseType.resolved(SyntaxTree.PortType);
  6391. ELSE
  6392. portType := parameter.type(SyntaxTree.PortType);
  6393. END;
  6394. PushSelfPointer();
  6395. (* port / array of ports *)
  6396. IF IsStaticArray(type) THEN
  6397. PushConstInteger(len);
  6398. END;
  6399. Field(parameter, op);
  6400. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6401. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6402. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6403. Designate(d, op);*)
  6404. Emit(Push(Basic.invalidPosition, op.op));
  6405. ReleaseOperand(op);
  6406. (* name *)
  6407. PushConstString(name);
  6408. (* inout *)
  6409. PushConstSet(Direction(portType.direction));
  6410. (* width *)
  6411. PushConstInteger(portType.sizeInBits);
  6412. IF parameter.type IS SyntaxTree.PortType THEN
  6413. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6414. AddPortProperties(parameter);
  6415. ELSIF IsStaticArray(type)THEN
  6416. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6417. ELSIF IsSemiDynamicArray(type) THEN
  6418. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6419. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6420. Emit(Add(position,reg, sp, size));
  6421. (* dim *)
  6422. PushConstInteger(dim);
  6423. (* len array *)
  6424. Emit(Push(position, reg));
  6425. ReleaseIntermediateOperand(reg);
  6426. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6427. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6428. Emit(Add(position, sp,sp, size));
  6429. ELSE
  6430. HALT(100);
  6431. END;
  6432. END Parameter;
  6433. BEGIN
  6434. IF backend.cellsAreObjects THEN
  6435. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6436. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6437. END;
  6438. parameter := x.firstParameter;
  6439. WHILE (parameter # NIL) DO
  6440. type := parameter.type.resolved;
  6441. WHILE (type IS SyntaxTree.ArrayType) DO
  6442. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6443. END;
  6444. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6445. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6446. Parameter(name,parameter);
  6447. END;
  6448. parameter := parameter.nextParameter;
  6449. END;
  6450. ELSE HALT(200)
  6451. END;
  6452. END AddPorts;
  6453. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6454. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6455. BEGIN
  6456. Symbol(cell,op);
  6457. ToMemory(op.op,addressType,0);
  6458. Emit(Push(position,op.op));
  6459. ReleaseOperand(op);
  6460. property.GetName(name);
  6461. (* does not work when inheritance is used:
  6462. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6463. *)
  6464. PushConstString(name);
  6465. IF (value # NIL) THEN
  6466. ASSERT(
  6467. SemanticChecker.IsStringType(property.type)
  6468. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6469. OR (property.type.resolved IS SyntaxTree.FloatType)
  6470. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6471. OR (property.type.resolved IS SyntaxTree.SetType)
  6472. );
  6473. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6474. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6475. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6476. Designate(d, op);
  6477. IF SemanticChecker.IsStringType(property.type) THEN
  6478. Emit(Push(Basic.invalidPosition, op.tag))
  6479. END;
  6480. Emit(Push(Basic.invalidPosition, op.op));
  6481. ReleaseOperand(op);
  6482. END;
  6483. IF value = NIL THEN
  6484. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6485. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6486. ASSERT(SemanticChecker.IsStringType(value.type));
  6487. Designate(value, op);
  6488. PushString(op, value.type);
  6489. ReleaseOperand(op);
  6490. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6491. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6492. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6493. Evaluate(value, op);
  6494. Emit(Push(property.position, op.op));
  6495. ReleaseOperand(op);
  6496. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6497. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6498. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6499. Evaluate(value, op);
  6500. Emit(Push(property.position, op.op));
  6501. ReleaseOperand(op);
  6502. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6503. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6504. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6505. Evaluate(value, op);
  6506. Emit(Push(property.position, op.op));
  6507. ReleaseOperand(op);
  6508. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6509. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6510. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6511. Evaluate(value, op);
  6512. Emit(Push(property.position, op.op));
  6513. ReleaseOperand(op);
  6514. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6515. ELSE
  6516. HALT(200);
  6517. END;
  6518. END AddProperty;
  6519. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6520. VAR symbol: SyntaxTree.Symbol;
  6521. BEGIN
  6522. WHILE modifier # NIL DO
  6523. symbol := cellType.FindProperty(modifier.identifier);
  6524. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6525. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6526. modifier := modifier.nextModifier;
  6527. END;
  6528. END AddModifiers;
  6529. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6530. VAR last: SyntaxTree.Modifier;
  6531. BEGIN
  6532. IF to = NIL THEN
  6533. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6534. ELSE
  6535. last := to;
  6536. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6537. last := last.nextModifier;
  6538. END;
  6539. IF last.identifier # this.identifier THEN
  6540. ASSERT(last.nextModifier = NIL);
  6541. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6542. END;
  6543. END;
  6544. END AppendModifier;
  6545. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6546. BEGIN
  6547. WHILE this # NIL DO
  6548. AppendModifier(to, this);
  6549. this := this.nextModifier;
  6550. END;
  6551. END AppendModifiers;
  6552. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6553. VAR base: SyntaxTree.Type;
  6554. BEGIN
  6555. AppendModifiers(to, c.modifiers);
  6556. base := c.GetBaseValueType();
  6557. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6558. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6559. END;
  6560. END AppendCellTypeModifiers;
  6561. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6562. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6563. BEGIN
  6564. Basic.GetString(modifier.identifier, name);
  6565. PushConstString(name);
  6566. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6567. ASSERT(SemanticChecker.IsStringType(value.type));
  6568. Designate(value, op);
  6569. PushString(op, value.type);
  6570. ReleaseOperand(op);
  6571. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6572. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6573. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6574. Evaluate(value, op);
  6575. Emit(Push(modifier.position, op.op));
  6576. ReleaseOperand(op);
  6577. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6578. ELSE
  6579. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6580. END;
  6581. END AddPortProperty;
  6582. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6583. BEGIN
  6584. WHILE modifier # NIL DO
  6585. AddPortProperty(modifier, modifier.expression);
  6586. modifier := modifier.nextModifier;
  6587. END;
  6588. END AddPortProperties;
  6589. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6590. VAR op: Operand;
  6591. BEGIN
  6592. Evaluate(p, op);
  6593. Emit(Push(p.position, op.op));
  6594. ReleaseOperand(op);
  6595. IF p IS SyntaxTree.Designator THEN
  6596. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6597. END;
  6598. END PushPort;
  6599. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6600. VAR tmp: IntermediateCode.Operand;
  6601. BEGIN
  6602. actualType := actualType.resolved;
  6603. IF actualType IS SyntaxTree.StringType THEN
  6604. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6605. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6606. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6607. ELSE
  6608. tmp := op.tag;
  6609. IntermediateCode.MakeMemory(tmp,addressType);
  6610. Emit(Push(position,tmp));
  6611. END;
  6612. Emit(Push(position,op.op))
  6613. END PushString;
  6614. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6615. VAR
  6616. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6617. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6618. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6619. tmp:IntermediateCode.Operand;
  6620. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6621. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6622. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6623. dest: IntermediateCode.Operand;
  6624. staticLength: LONGINT; itype: IntermediateCode.Type;
  6625. convert,isTensor: BOOLEAN;
  6626. recordType: SyntaxTree.RecordType;
  6627. baseType: SyntaxTree.Type;
  6628. flags: SET;
  6629. left: SyntaxTree.Expression;
  6630. call: SyntaxTree.Designator;
  6631. procedure: SyntaxTree.Procedure;
  6632. temporaryVariable: SyntaxTree.Variable;
  6633. dummy: IntermediateCode.Operand;
  6634. customBuiltin: SyntaxTree.CustomBuiltin;
  6635. isVarPar: ARRAY 3 OF BOOLEAN;
  6636. callsection: Sections.Section;
  6637. segmentedName: Basic.SegmentedName;
  6638. needsTrace: BOOLEAN;
  6639. n: ARRAY 256 OF CHAR;
  6640. modifier: SyntaxTree.Modifier;
  6641. previous, init: IntermediateCode.Section;
  6642. prevScope: SyntaxTree.Scope;
  6643. firstPar: LONGINT;
  6644. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6645. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6646. priority: IntermediateCode.Operand;
  6647. op,callop: Operand;
  6648. BEGIN
  6649. IF type = NIL THEN RETURN END;
  6650. type := type.resolved;
  6651. IF type IS SyntaxTree.PointerType THEN
  6652. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6653. END;
  6654. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6655. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6656. recordScope := type(SyntaxTree.RecordType).recordScope;
  6657. IF recordScope.bodyProcedure # NIL THEN
  6658. procedure := recordScope.bodyProcedure;
  6659. body := procedure.procedureScope.body;
  6660. Emit(Push(position,self));
  6661. IF body.isActive THEN
  6662. StaticCallOperand(callop,procedure);
  6663. Emit(Push(position,callop.op));
  6664. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6665. Convert(priority,sizeType);
  6666. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6667. END;
  6668. Emit(Push(position,priority));
  6669. ReleaseIntermediateOperand(priority);
  6670. IF backend.cooperative THEN
  6671. Emit(Push(position,self));
  6672. CallThis(position,"Activities","Create",3)
  6673. ELSE
  6674. flags := 0;
  6675. IF body.isSafe THEN
  6676. flags := 1;
  6677. END;
  6678. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6679. Emit(Push(position,self));
  6680. CallThis(position,"Objects","CreateProcess",4)
  6681. END;
  6682. ELSE
  6683. Emit(Push(position,self));
  6684. StaticCallOperand(callop,procedure);
  6685. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6686. END;
  6687. Emit(Pop(position,self));
  6688. END;
  6689. END CallBodies;
  6690. PROCEDURE PushTD(type: SyntaxTree.Type);
  6691. VAR op: IntermediateCode.Operand;
  6692. BEGIN
  6693. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6694. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6695. ELSE
  6696. IF type.resolved IS SyntaxTree.PointerType THEN
  6697. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6698. END;
  6699. op := TypeDescriptorAdr(type.resolved);
  6700. IF ~newObjectFile THEN
  6701. IntermediateCode.MakeMemory(op,addressType);
  6702. END;
  6703. Emit(Push(position,op));
  6704. END
  6705. END PushTD;
  6706. BEGIN
  6707. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6708. dest := destination; destination := emptyOperand;
  6709. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6710. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6711. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6712. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6713. CASE x.id OF
  6714. (* ---- COPY ----- *)
  6715. |Global.Copy:
  6716. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6717. (* ---- EXCL, INCL----- *)
  6718. |Global.Excl,Global.Incl:
  6719. Evaluate(p0,s0);
  6720. Evaluate(p1,s1);
  6721. Convert(s1.op,setType);
  6722. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6723. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6724. END;
  6725. ReuseCopy(res,s0.op);
  6726. ReleaseOperand(s0);
  6727. Reuse1(tmp,s1.op);
  6728. ReleaseOperand(s1);
  6729. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6730. IF x.id = Global.Excl THEN
  6731. Emit(Not(position,tmp,tmp));
  6732. Emit(And(position,res,res,tmp));
  6733. ELSE
  6734. Emit(Or(position,res,res,tmp));
  6735. END;
  6736. ReleaseIntermediateOperand(tmp);
  6737. Designate(p0,s0);
  6738. ToMemory(s0.op,setType,0);
  6739. Emit(Mov(position,s0.op,res));
  6740. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6741. (* ---- DISPOSE ----- *)
  6742. |Global.Dispose:
  6743. Designate(p0,s0);
  6744. Emit(Push(position,s0.op));
  6745. ReleaseOperand(s0);
  6746. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6747. (* ---- GETPROCEDURE ----- *)
  6748. |Global.GetProcedure:
  6749. Designate(p0,s0);
  6750. PushString(s0,p0.type);
  6751. Designate(p1,s1);
  6752. PushString(s1,p1.type);
  6753. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6754. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6755. ELSE PushTD(procedureType.firstParameter.type)
  6756. END;
  6757. PushTD(procedureType.returnType);
  6758. Designate(p2,s2);
  6759. Emit(Push(position,s2.op));
  6760. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6761. CallThis(position,"Modules","GetProcedure", 7);
  6762. (* ---- ASH, LSH, ROT ----- *)
  6763. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6764. Evaluate(p0,s0);
  6765. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6766. (* make unsigned arguments in order to produced a logical shift *)
  6767. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6768. convert:= TRUE;
  6769. itype := s0.op.type;
  6770. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6771. Convert(s0.op,itype);
  6772. ELSE
  6773. convert := FALSE;
  6774. END;
  6775. END;
  6776. Evaluate(p1,s1);
  6777. IF IsIntegerConstant(p1,hint) THEN
  6778. ReuseCopy(reg,s0.op);
  6779. IF hint > 0 THEN
  6780. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6781. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6782. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6783. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6784. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6785. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6786. END;
  6787. ELSIF hint < 0 THEN
  6788. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6789. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6790. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6791. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6792. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6793. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6794. END;
  6795. END;
  6796. ReleaseOperand(s0); ReleaseOperand(s1);
  6797. ELSE
  6798. exit := NewLabel();
  6799. end := NewLabel();
  6800. ReuseCopy(reg,s0.op);
  6801. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6802. Reuse1(tmp,s1.op);
  6803. Emit(Neg(position,tmp,s1.op));
  6804. Convert(tmp,s1.op.type);
  6805. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6806. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6807. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6808. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6809. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6810. END;
  6811. ReleaseIntermediateOperand(tmp);
  6812. BrL(end);
  6813. SetLabel(exit);
  6814. ReuseCopy(tmp,s1.op);
  6815. Convert(tmp,s1.op.type);
  6816. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6817. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6818. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6819. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6820. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6821. END;
  6822. ReleaseIntermediateOperand(tmp);
  6823. SetLabel(end);
  6824. ReleaseOperand(s0); ReleaseOperand(s1);
  6825. END;
  6826. InitOperand(result,ModeValue);
  6827. IF convert THEN
  6828. itype := reg.type;
  6829. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6830. Convert(reg,itype);
  6831. END;
  6832. result.op := reg;
  6833. (* ---- CAP ----- *)
  6834. |Global.Cap:
  6835. Evaluate(p0,result);
  6836. ReuseCopy(reg,result.op);
  6837. ReleaseIntermediateOperand(result.op);
  6838. ignore := NewLabel();
  6839. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6840. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6841. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6842. SetLabel(ignore);
  6843. result.op := reg;
  6844. (* ---- CHR ----- *)
  6845. |Global.Chr, Global.Chr32:
  6846. Evaluate(p0,result);
  6847. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6848. |Global.Entier, Global.EntierH:
  6849. Evaluate(p0,result);
  6850. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6851. (* ---- MIN and MAX ----- *)
  6852. |Global.Max,Global.Min:
  6853. Evaluate(p0,s0);
  6854. Evaluate(p1,s1);
  6855. Reuse2(res,s0.op,s1.op);
  6856. else := NewLabel();
  6857. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6858. ELSE BrltL(else,s1.op,s0.op) END;
  6859. Emit(Mov(position,res,s0.op));
  6860. ReleaseOperand(s0);
  6861. end := NewLabel();
  6862. BrL(end);
  6863. SetLabel(else);
  6864. Emit(MovReplace(position,res,s1.op));
  6865. SetLabel(end);
  6866. ReleaseOperand(s1);
  6867. InitOperand(result,ModeValue);
  6868. result.op := res;
  6869. (* ---- ODD ----- *)
  6870. |Global.Odd:
  6871. IF ~conditional THEN
  6872. ConditionToValue(x)
  6873. ELSE
  6874. Evaluate(p0,result);
  6875. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6876. Reuse1(res,result.op);
  6877. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6878. ReleaseIntermediateOperand(result.op);
  6879. result.op := res;
  6880. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6881. ReleaseOperand(result);
  6882. BrL(falseLabel);
  6883. END;
  6884. (* ---- ORD ----- *)
  6885. |Global.Ord, Global.Ord32:
  6886. Evaluate(p0,result);
  6887. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6888. (* ---- SHORT, LONG ----- *)
  6889. |Global.Short, Global.Long:
  6890. Evaluate(p0,result);
  6891. IF x.type IS SyntaxTree.ComplexType THEN
  6892. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6893. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6894. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6895. ELSE
  6896. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6897. END
  6898. (* ---- HALT, SYSTEM.HALT----- *)
  6899. |Global.Halt, Global.systemHalt:
  6900. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6901. EmitTrap (position, val);
  6902. (* ---- ASSERT ----- *)
  6903. |Global.Assert:
  6904. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6905. trueL := NewLabel();
  6906. falseL := NewLabel();
  6907. Condition(p0,trueL,falseL);
  6908. IF p1 = NIL THEN val := AssertTrap
  6909. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6910. END;
  6911. SetLabel(falseL);
  6912. EmitTrap(position,val);
  6913. SetLabel(trueL);
  6914. END;
  6915. (*
  6916. Emit(TrapC(result.op,val);
  6917. *)
  6918. (* ---- INC, DEC----- *)
  6919. |Global.Inc,Global.Dec:
  6920. Expression(p0); adr := result.op;
  6921. LoadValue(result,p0.type); l := result;
  6922. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6923. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6924. END;
  6925. IF x.id = Global.Inc THEN
  6926. Emit(Add(position,l.op,l.op,r.op));
  6927. ELSE
  6928. Emit(Sub(position,l.op,l.op,r.op));
  6929. END;
  6930. ReleaseOperand(l); ReleaseOperand(r);
  6931. (* ---- LEN ----- *)
  6932. |Global.Len: (* dynamic length, static length done by checker *)
  6933. Designate(p0,operand);
  6934. IF p1 = NIL THEN
  6935. InitOperand(l,ModeValue);
  6936. l.op := IntermediateCode.Immediate(int32,0);
  6937. ELSE
  6938. Evaluate(p1,l);
  6939. END;
  6940. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6941. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6942. Dereference(operand, p0.type.resolved, FALSE);
  6943. END;
  6944. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6945. ReleaseOperand(operand); ReleaseOperand(l);
  6946. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6947. ASSERT(p1 # NIL);
  6948. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6949. Dereference(operand,p0.type.resolved,FALSE);
  6950. END;
  6951. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6952. ReleaseOperand(operand); ReleaseOperand(l);
  6953. ELSE HALT(100);
  6954. END;
  6955. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6956. (* ---- FIRST ---- *)
  6957. |Global.First:
  6958. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6959. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6960. ELSE
  6961. Designate(p0, result)
  6962. END
  6963. (* ---- LAST ---- *)
  6964. |Global.Last:
  6965. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6966. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6967. ELSE
  6968. Designate(p0, result);
  6969. (* make sure result.op is a register *)
  6970. tmp := result.op;
  6971. ReuseCopy(result.op, result.op);
  6972. ReleaseIntermediateOperand(tmp);
  6973. (* add offset to result.op *)
  6974. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6975. END
  6976. (* ---- STEP ---- *)
  6977. |Global.Step:
  6978. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6979. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6980. ELSE
  6981. Designate(p0, result);
  6982. (* make sure result.op is a register *)
  6983. tmp := result.op;
  6984. ReuseCopy(result.op, result.op);
  6985. ReleaseIntermediateOperand(tmp);
  6986. (* add offset to result.op *)
  6987. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6988. END
  6989. (* ---- RE ---- *)
  6990. |Global.Re:
  6991. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6992. Designate(p0, result)
  6993. ELSE
  6994. Evaluate(p0, result)
  6995. END
  6996. (* ---- IM ---- *)
  6997. |Global.Im:
  6998. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6999. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  7000. Designate(p0, result);
  7001. (* make sure result.op is a register *)
  7002. tmp := result.op;
  7003. ReuseCopy(result.op, result.op);
  7004. ReleaseIntermediateOperand(tmp);
  7005. (* add offset to result.op *)
  7006. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7007. (* ---- ABS ----- *)
  7008. |Global.Abs:
  7009. Evaluate(p0,operand);
  7010. type := p0.type.resolved;
  7011. InitOperand(result,ModeValue);
  7012. Reuse1a(result.op,operand.op,dest);
  7013. Emit(Abs(position,result.op,operand.op));
  7014. ReleaseOperand(operand);
  7015. (* ---- WAIT ----- *)
  7016. |Global.Wait:
  7017. Evaluate(p0,operand);
  7018. Emit(Push(position,operand.op));
  7019. ReleaseOperand(operand);
  7020. CallThis(position,"Activities","Wait", 1);
  7021. (* ---- NEW ----- *)
  7022. |Global.New:
  7023. (*! the following code is only correct for "standard" Oberon calling convention *)
  7024. IF x.type # NIL THEN
  7025. type := x.type.resolved;
  7026. firstPar := 0;
  7027. ELSE
  7028. type := p0.type.resolved;
  7029. firstPar := 1;
  7030. END;
  7031. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7032. THEN
  7033. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7034. IF backend.cooperative THEN
  7035. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7036. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7037. IF recordType.isObject THEN
  7038. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7039. IF recordType.IsActive() THEN
  7040. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7041. END;
  7042. IF recordType.IsProtected() THEN
  7043. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7044. END;
  7045. ELSE
  7046. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7047. END;
  7048. END;
  7049. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7050. CallThis(position,"Runtime","New", 1);
  7051. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7052. Emit(Result(position, pointer));
  7053. exit := NewLabel();
  7054. BreqL(exit,pointer,nil);
  7055. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7056. GetRecordTypeName (recordType,name);
  7057. IF ~recordType.isObject THEN
  7058. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7059. END;
  7060. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7061. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7062. IF recordType.isObject THEN
  7063. IF recordType.IsProtected() THEN
  7064. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7065. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7066. END;
  7067. IF recordType.IsActive() THEN
  7068. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7069. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7070. END;
  7071. END;
  7072. END;
  7073. (* initialize fields *)
  7074. IF type(SyntaxTree.PointerType).isPlain THEN
  7075. size := 0;
  7076. ELSIF recordType.isObject THEN
  7077. size := BaseObjectTypeSize;
  7078. ELSE
  7079. size := BaseRecordTypeSize;
  7080. END;
  7081. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7082. (* call initializer *)
  7083. constructor := GetConstructor(recordType);
  7084. IF constructor # NIL THEN
  7085. (*! should be unified with ProcedureCallDesignator *)
  7086. Emit(Push(position,pointer));
  7087. ReleaseIntermediateOperand(pointer);
  7088. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7089. FOR i := firstPar TO x.parameters.Length()-1 DO
  7090. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7091. formalParameter := formalParameter.nextParameter;
  7092. END;
  7093. (* static call of the constructor *)
  7094. GetCodeSectionNameForSymbol(constructor,name);
  7095. ASSERT(~constructor.isInline);
  7096. IF constructor.scope.ownerModule # module.module THEN
  7097. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7098. ELSE
  7099. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7100. END;
  7101. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7102. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7103. Emit(Pop(position,pointer));
  7104. END;
  7105. (* call bodies *)
  7106. CallBodies(pointer,type);
  7107. SetLabel(exit);
  7108. needsTrace := p0.NeedsTrace();
  7109. IF needsTrace THEN ModifyAssignments(true) END;
  7110. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7111. Emit(Push(position, pointer));
  7112. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7113. Emit(Pop(position, pointer));
  7114. END;
  7115. Designate(p0,l);
  7116. IF needsTrace THEN
  7117. CallAssignPointer(l.op, pointer);
  7118. ELSE
  7119. ToMemory(l.op,addressType,0);
  7120. Emit(Mov(position,l.op,pointer));
  7121. END;
  7122. ReleaseIntermediateOperand(pointer);
  7123. ReleaseOperand(l);
  7124. IF needsTrace THEN ModifyAssignments(false) END;
  7125. ELSE (* not cooperative backend *)
  7126. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7127. IF temporaryVariable # NIL THEN
  7128. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7129. ELSE
  7130. Designate(p0,l);
  7131. END;
  7132. (* l.op contains address of pointer to record *)
  7133. Emit(Push(position,l.op)); (* address for use after syscall *)
  7134. Emit(Push(position,l.op));
  7135. ReleaseOperand(l);
  7136. (* push type descriptor *)
  7137. reg := TypeDescriptorAdr(recordType);
  7138. IF ~newObjectFile THEN
  7139. IntermediateCode.MakeMemory(reg,addressType);
  7140. END;
  7141. Emit(Push(position,reg));
  7142. ReleaseIntermediateOperand(reg);
  7143. (* push realtime flag *)
  7144. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7145. ELSE Emit(Push(position,false));
  7146. END;
  7147. CallThis(position,"Heaps","NewRec", 3);
  7148. (* check allocation success, if not successful then do not call initializers and bodies *)
  7149. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7150. Emit(Pop(position,pointer));
  7151. MakeMemory(reg,pointer,addressType,0);
  7152. ReleaseIntermediateOperand(pointer);
  7153. pointer := reg;
  7154. exit := NewLabel();
  7155. BreqL(exit,pointer,nil);
  7156. Emit(Push(position,pointer));
  7157. (* initialize fields *)
  7158. InitFields(recordType, pointer,0);
  7159. (* call initializer *)
  7160. constructor := GetConstructor(recordType);
  7161. IF constructor # NIL THEN
  7162. (*! should be unified with ProcedureCallDesignator *)
  7163. Emit(Push(position,pointer));
  7164. ReleaseIntermediateOperand(pointer);
  7165. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7166. FOR i := firstPar TO x.parameters.Length()-1 DO
  7167. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7168. formalParameter := formalParameter.nextParameter;
  7169. END;
  7170. (* static call of the constructor *)
  7171. GetCodeSectionNameForSymbol(constructor,name);
  7172. ASSERT(~constructor.isInline);
  7173. IF constructor.scope.ownerModule # module.module THEN
  7174. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7175. ELSE
  7176. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7177. END;
  7178. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7179. ELSE
  7180. ReleaseIntermediateOperand(pointer);
  7181. END;
  7182. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7183. Emit(Pop(position,pointer));
  7184. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7185. Designate(p0,l);
  7186. ToMemory(l.op,addressType,0);
  7187. Emit(Mov(position,l.op,pointer));
  7188. ReleaseOperand(l);
  7189. result.tag := emptyOperand;
  7190. ELSIF (x.type # NIL) THEN
  7191. result := l; (* temporary variable is the result of NEW Type() *)
  7192. END;
  7193. (* call bodies *)
  7194. CallBodies(pointer,type);
  7195. ReleaseIntermediateOperand(pointer);
  7196. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7197. end := NewLabel();
  7198. BrL(end);
  7199. SetLabel(exit);
  7200. Designate(p0,l);
  7201. ToMemory(l.op,addressType,0);
  7202. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7203. ReleaseOperand(l);
  7204. SetLabel(end);
  7205. ELSE
  7206. SetLabel(exit);
  7207. END;
  7208. END;
  7209. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7210. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7211. dim := 0;
  7212. IntermediateCode.InitOperand(reg);
  7213. IF p1 # NIL THEN
  7214. FOR i := firstPar TO x.parameters.Length()-1 DO
  7215. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7216. parameter := x.parameters.GetExpression(i);
  7217. Evaluate(parameter,r);
  7218. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7219. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7220. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7221. END;
  7222. Emit(Push(position,r.op));
  7223. IF i=1 THEN
  7224. CopyInt(reg,r.op);
  7225. ELSE
  7226. MulInt(reg, reg, r.op);
  7227. END;
  7228. ReleaseOperand(r);
  7229. INC(dim);
  7230. END;
  7231. Convert(reg,addressType);
  7232. ELSE
  7233. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7234. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7235. END;
  7236. openDim := dim;
  7237. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7238. IF backend.cooperative THEN
  7239. size := ToMemoryUnits(system,system.SizeOf(type));
  7240. WHILE type IS SyntaxTree.ArrayType DO
  7241. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7242. END;
  7243. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7244. IF (size # 1) THEN
  7245. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7246. END;
  7247. Emit(Push(position,reg));
  7248. size := ToMemoryUnits(system,system.SizeOf(type));
  7249. IF (size # 1) THEN
  7250. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7251. END;
  7252. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  7253. Emit(Push(position,reg));
  7254. ReleaseIntermediateOperand(reg);
  7255. CallThis(position,"Runtime","New", 1);
  7256. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7257. Emit(Result(position, pointer));
  7258. exit := NewLabel();
  7259. else := NewLabel();
  7260. BreqL(else,pointer,nil);
  7261. IF ~type.hasPointers THEN
  7262. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7263. ELSIF type IS SyntaxTree.RecordType THEN
  7264. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7265. ELSIF type IS SyntaxTree.ProcedureType THEN
  7266. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7267. ELSE
  7268. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7269. END;
  7270. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7271. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7272. 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))));
  7273. IF type IS SyntaxTree.RecordType THEN
  7274. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7275. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7276. ELSE
  7277. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7278. END;
  7279. i := openDim;
  7280. WHILE i > 0 DO
  7281. DEC (i);
  7282. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7283. END;
  7284. needsTrace := p0.NeedsTrace();
  7285. IF needsTrace THEN ModifyAssignments(true) END;
  7286. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7287. Emit(Push(position, pointer));
  7288. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7289. Emit(Pop(position, pointer));
  7290. END;
  7291. Designate(p0,l);
  7292. IF needsTrace THEN
  7293. CallAssignPointer(l.op, pointer);
  7294. ModifyAssignments(false);
  7295. ELSE
  7296. ToMemory(l.op,addressType,0);
  7297. Emit(Mov(position,l.op,pointer));
  7298. END;
  7299. ReleaseIntermediateOperand(pointer);
  7300. ReleaseOperand(l);
  7301. BrL(exit);
  7302. SetLabel(else);
  7303. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7304. Designate(p0,l);
  7305. IF needsTrace THEN
  7306. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7307. ELSE
  7308. ToMemory(l.op,addressType,0);
  7309. Emit(Mov(position,l.op,pointer));
  7310. END;
  7311. ReleaseOperand(l);
  7312. SetLabel(exit);
  7313. ELSE
  7314. (*! the following code is only correct for "standard" Oberon calling convention *)
  7315. IF SemanticChecker.ContainsPointer(type) THEN
  7316. IF type IS SyntaxTree.ArrayType THEN
  7317. staticLength := 1;
  7318. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7319. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7320. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7321. END;
  7322. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7323. MulInt(reg,reg,tmp);
  7324. END;
  7325. Designate(p0,l);
  7326. IF openDim > 0 THEN
  7327. Emit(Push(position,l.op)); (* address for use after syscall *)
  7328. END;
  7329. Emit(Push(position,l.op)); (* address *)
  7330. ReleaseOperand(l);
  7331. tmp := TypeDescriptorAdr(type);
  7332. IF ~newObjectFile THEN
  7333. IntermediateCode.MakeMemory(tmp,addressType);
  7334. END;
  7335. Emit(Push(position,tmp)); (* type descriptor *)
  7336. ReleaseIntermediateOperand(tmp);
  7337. Emit(Push(position,reg)); (* number Elements *)
  7338. ReleaseIntermediateOperand(reg);
  7339. tmp := IntermediateCode.Immediate(addressType,dim);
  7340. Emit(Push(position,tmp)); (* dimensions *)
  7341. (* push realtime flag *)
  7342. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7343. ELSE Emit(Push(position,false));
  7344. END;
  7345. CallThis(position,"Heaps","NewArr",5)
  7346. ELSE
  7347. size := ToMemoryUnits(system,system.SizeOf(type));
  7348. IF (size # 1) THEN
  7349. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7350. (*
  7351. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7352. *)
  7353. END;
  7354. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7355. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7356. AddInt(reg, reg, tmp);
  7357. (*
  7358. Emit(Add(position,reg,reg,tmp));
  7359. *)
  7360. Designate(p0,l);
  7361. IF openDim >0 THEN
  7362. Emit(Push(position,l.op)); (* address for use after syscall *)
  7363. END;
  7364. Emit(Push(position,l.op)); (* address for syscall *)
  7365. ReleaseOperand(l); (* pointer address *)
  7366. Emit(Push(position,reg)); (* size *)
  7367. ReleaseIntermediateOperand(reg);
  7368. (* push realtime flag *)
  7369. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7370. ELSE Emit(Push(position,false));
  7371. END;
  7372. CallThis(position,"Heaps","NewSys", 3)
  7373. END;
  7374. IF openDim > 0 THEN
  7375. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7376. Emit(Pop(position,adr));
  7377. ToMemory(adr,addressType,0);
  7378. ReuseCopy(tmp,adr);
  7379. ReleaseIntermediateOperand(adr);
  7380. adr := tmp;
  7381. else := NewLabel();
  7382. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7383. i := openDim-1;
  7384. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7385. WHILE (i >= 0) DO
  7386. Emit(Pop(position,reg));
  7387. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7388. Emit(Mov(position,res,reg));
  7389. DEC(i);
  7390. END;
  7391. ReleaseIntermediateOperand(adr);
  7392. ReleaseIntermediateOperand(reg);
  7393. exit := NewLabel();
  7394. BrL(exit);
  7395. SetLabel(else);
  7396. (* else part: array could not be allocated *)
  7397. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7398. Emit(Add(position,sp,sp,tmp));
  7399. SetLabel(exit);
  7400. END;
  7401. END;
  7402. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7403. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7404. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7405. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7406. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7407. left.SetType(procedure.type);
  7408. formalParameter := procedureType.firstParameter;
  7409. (* push array to allocate *)
  7410. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7411. formalParameter :=formalParameter.nextParameter;
  7412. (* push length array *)
  7413. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7414. (* push size *)
  7415. type := t0;
  7416. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7417. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7418. END;
  7419. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7420. Emit(Push(position,tmp));
  7421. (* *)
  7422. IF SemanticChecker.ContainsPointer(type) THEN
  7423. tmp := TypeDescriptorAdr(type);
  7424. IF ~newObjectFile THEN
  7425. IntermediateCode.MakeMemory(tmp,addressType);
  7426. END;
  7427. ELSE
  7428. tmp := IntermediateCode.Immediate(addressType, 0);
  7429. END;
  7430. Emit(Push(position,tmp)); (* type descriptor *)
  7431. StaticCallOperand(result,procedure);
  7432. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7433. ReleaseOperand(result);
  7434. END;
  7435. (*
  7436. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7437. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7438. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7439. *)
  7440. ELSE
  7441. dim := 0;
  7442. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7443. (* generate geometry descriptor *)
  7444. Designate(p0,l);
  7445. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7446. ReleaseOperand(l);
  7447. isTensor := TRUE;
  7448. ELSE
  7449. isTensor := FALSE;
  7450. END;
  7451. FOR i := firstPar TO x.parameters.Length()-1 DO
  7452. IF ~isTensor THEN
  7453. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7454. END;
  7455. parameter := x.parameters.GetExpression(i);
  7456. Evaluate(parameter,r);
  7457. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7458. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7459. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7460. END;
  7461. Emit(Push(position,r.op));
  7462. IF i=1 THEN
  7463. CopyInt(reg, r.op);
  7464. ELSE
  7465. MulInt(reg, reg, r.op);
  7466. END;
  7467. ReleaseOperand(r);
  7468. INC(dim);
  7469. END;
  7470. Convert(reg,addressType);
  7471. openDim := dim;
  7472. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7473. (*! the following code is only correct for "standard" Oberon calling convention *)
  7474. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7475. t := type;
  7476. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7477. staticLength := 1;
  7478. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7479. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7480. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7481. END;
  7482. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7483. MulInt(reg,reg,tmp);
  7484. END;
  7485. Designate(p0,l);
  7486. IF isTensor THEN
  7487. Dereference(l,type,FALSE);
  7488. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7489. END;
  7490. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7491. Emit(Push(position,l.tag)); (* address *)
  7492. ReleaseOperand(l);
  7493. tmp := TypeDescriptorAdr(t);
  7494. IF ~newObjectFile THEN
  7495. IntermediateCode.MakeMemory(tmp,addressType);
  7496. END;
  7497. Emit(Push(position,tmp)); (* type descriptor *)
  7498. ReleaseIntermediateOperand(tmp);
  7499. Emit(Push(position,reg)); (* number Elements *)
  7500. ReleaseIntermediateOperand(reg);
  7501. tmp := IntermediateCode.Immediate(addressType,0);
  7502. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7503. (* push realtime flag: false by default *)
  7504. Emit(Push(position,false));
  7505. CallThis(position,"Heaps","NewArr",5);
  7506. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7507. Emit(Pop(position,adr));
  7508. GetMathArrayField(tmp,adr,MathPtrOffset);
  7509. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7510. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7511. PutMathArrayField(adr,reg,MathAdrOffset);
  7512. ReleaseIntermediateOperand(tmp);
  7513. ReleaseIntermediateOperand(reg);
  7514. ELSE
  7515. IF isTensor THEN
  7516. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7517. ELSE
  7518. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7519. END;
  7520. IF (size # 1) THEN
  7521. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7522. END;
  7523. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7524. AddInt(reg,reg,tmp);
  7525. Designate(p0,l);
  7526. IF isTensor THEN
  7527. Dereference(l,type,FALSE);
  7528. END;
  7529. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7530. Emit(Push(position,l.tag)); (* address for syscall *)
  7531. ReleaseOperand(l); (* pointer address *)
  7532. Emit(Push(position,reg)); (* size *)
  7533. ReleaseIntermediateOperand(reg);
  7534. (* push realtime flag: false by default *)
  7535. Emit(Push(position,false));
  7536. CallThis(position,"Heaps","NewSys",3);
  7537. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7538. Emit(Pop(position,adr));
  7539. GetMathArrayField(tmp,adr,MathPtrOffset);
  7540. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7541. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7542. PutMathArrayField(adr,reg,MathAdrOffset);
  7543. ReleaseIntermediateOperand(tmp);
  7544. ReleaseIntermediateOperand(reg);
  7545. END;
  7546. flags := {};
  7547. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7548. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7549. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7550. PutMathArrayField(adr,tmp,MathDimOffset);
  7551. else := NewLabel();
  7552. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7553. i := openDim-1;
  7554. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7555. IF isTensor THEN
  7556. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7557. ELSE
  7558. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7559. END;
  7560. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7561. WHILE (i >= 0) DO
  7562. Emit(Pop(position,reg));
  7563. PutMathArrayLength(adr,reg,i);
  7564. PutMathArrayIncrement(adr,tmp,i);
  7565. IF i > 0 THEN
  7566. IF i=openDim-1 THEN
  7567. CopyInt(tmp,tmp);
  7568. END;
  7569. MulInt(tmp,tmp,reg);
  7570. END;
  7571. DEC(i);
  7572. END;
  7573. ReleaseIntermediateOperand(adr);
  7574. ReleaseIntermediateOperand(reg);
  7575. ReleaseIntermediateOperand(tmp);
  7576. exit := NewLabel();
  7577. BrL(exit);
  7578. SetLabel(else);
  7579. (* else part: array could not be allocated *)
  7580. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7581. Emit(Add(position,sp,sp,tmp));
  7582. SetLabel(exit);
  7583. END;
  7584. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7585. THEN
  7586. IF ~backend.cellsAreObjects THEN RETURN END;
  7587. IF InCellScope(currentScope) THEN
  7588. PushSelfPointer()
  7589. ELSE
  7590. Emit(Push(position, nil));
  7591. END;
  7592. (* push temp address *)
  7593. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7594. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7595. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7596. (* l.op contains address of pointer to record *)
  7597. Emit(Push(position,l.op)); (* address for use after syscall *)
  7598. ReleaseOperand(l);
  7599. (* push type descriptor *)
  7600. reg := TypeDescriptorAdr(baseType);
  7601. IF ~newObjectFile THEN
  7602. IntermediateCode.MakeMemory(reg,addressType);
  7603. END;
  7604. Emit(Push(position,reg));
  7605. ReleaseIntermediateOperand(reg);
  7606. (* push name *)
  7607. (*Global.GetSymbolName(p0, n);*)
  7608. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7609. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7610. ELSE
  7611. Global.GetModuleName(module.module, n);
  7612. END;
  7613. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7614. (*type.typeDeclaration.GetName(n);*)
  7615. PushConstString(n);
  7616. (* push cellnet boolean *)
  7617. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7618. (* push engine boolean *)
  7619. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7620. (* allocate *)
  7621. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7622. (* add capabilities *)
  7623. modifier := p0(SyntaxTree.Designator).modifiers;
  7624. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7625. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7626. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7627. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7628. END;
  7629. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7630. (*
  7631. modifier := baseType(SyntaxTree.CellType).modifiers;
  7632. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7633. modifier := p0(SyntaxTree.Designator).modifiers;
  7634. *)
  7635. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7636. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7637. (* l.op contains address of pointer to record *)
  7638. ToMemory(l.op,addressType,0);
  7639. (* l.op contains value of pointer to record *)
  7640. Emit(Push(position,l.op)); (* address for use after syscall *)
  7641. ReleaseOperand(l);
  7642. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7643. prevScope := currentScope;
  7644. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7645. previous := section;
  7646. section := init;
  7647. (* add ports *)
  7648. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7649. CloseInitializer(previous);
  7650. currentScope := prevScope;
  7651. Symbol(temporaryVariable,l);
  7652. ToMemory(l.op,addressType,0);
  7653. Emit(Push(position,l.op));
  7654. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7655. (*
  7656. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7657. IF constructor # NIL THEN
  7658. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7659. FOR i := 1 TO x.parameters.Length()-1 DO
  7660. p := x.parameters.GetExpression(i);
  7661. Global.GetSymbolName(parameter,name);
  7662. Evaluate(p, value);
  7663. ASSERT(value.type # NIL);
  7664. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7665. par := instance.AddParameter(name);
  7666. par.SetInteger(value.integer);
  7667. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7668. par := instance.AddParameter(name);
  7669. par.SetBoolean(value.boolean);
  7670. ELSE Error(x.position,NotYetImplemented)
  7671. END;
  7672. parameter := parameter.nextParameter
  7673. END;
  7674. END;
  7675. *)
  7676. (* call initializer *)
  7677. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7678. IF constructor # NIL THEN
  7679. (*! should be unified with ProcedureCallDesignator *)
  7680. IF backend.cellsAreObjects THEN
  7681. Symbol(temporaryVariable,l);
  7682. ToMemory(l.op,addressType,0);
  7683. Emit(Push(position,l.op));
  7684. ReleaseOperand(l);
  7685. END;
  7686. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7687. FOR i := firstPar TO x.parameters.Length()-1 DO
  7688. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7689. formalParameter := formalParameter.nextParameter;
  7690. END;
  7691. (* static call of the constructor *)
  7692. Global.GetSymbolSegmentedName(constructor,name);
  7693. ASSERT(~constructor.isInline);
  7694. IF constructor.scope.ownerModule # module.module THEN
  7695. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7696. ELSE
  7697. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7698. END;
  7699. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7700. (*ELSE
  7701. ReleaseIntermediateOperand(pointer);*)
  7702. END;
  7703. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7704. ToMemory(l.op, addressType, 0);
  7705. Designate(p0,s0);
  7706. ToMemory(s0.op,addressType,0);
  7707. Emit(Mov(position,s0.op,l.op));
  7708. ReleaseOperand(l);
  7709. ReleaseOperand(s0);
  7710. result.tag := emptyOperand;
  7711. (* start *)
  7712. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7713. (* push cell *)
  7714. Symbol(temporaryVariable, l);
  7715. ToMemory(l.op,addressType,0);
  7716. Emit(Push(Basic.invalidPosition,l.op));
  7717. (* push delegate *)
  7718. Emit(Push(Basic.invalidPosition,l.op));
  7719. ReleaseOperand(l);
  7720. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7721. Emit(Push(position, s1.op));
  7722. ReleaseOperand(s1);
  7723. CallThis(position,"ActiveCellsRuntime","Start",3);
  7724. END;
  7725. (*IF temporaryVariable # NIL THEN
  7726. end := NewLabel();
  7727. BrL(end);
  7728. SetLabel(exit);
  7729. Designate(p0,l);
  7730. ToMemory(l.op,addressType,0);
  7731. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7732. ReleaseOperand(l);
  7733. SetLabel(end);
  7734. ELSE
  7735. SetLabel(exit);
  7736. END;
  7737. *)
  7738. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7739. ELSE (* no pointer to record, no pointer to array *)
  7740. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7741. (* ignore new statement *)
  7742. Warning(p0.position, "cannot run on final hardware");
  7743. ELSE
  7744. HALT(200);
  7745. END;
  7746. END;
  7747. (* ---- ADDRESSOF----- *)
  7748. |Global.systemAdr:
  7749. Designate(p0,s0);
  7750. s0.mode := ModeValue;
  7751. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7752. ReleaseIntermediateOperand(s0.op);
  7753. s0.op := s0.tag;
  7754. IntermediateCode.InitOperand(s0.tag);
  7755. END;
  7756. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7757. result := s0;
  7758. (* ---- BIT ----- *)
  7759. |Global.systemBit:
  7760. Evaluate(p0,s0);
  7761. ToMemory(s0.op,addressType,0);
  7762. ReuseCopy(res,s0.op);
  7763. ReleaseOperand(s0);
  7764. Evaluate(p1,s1);
  7765. Emit(Ror(position,res,res,s1.op));
  7766. ReleaseOperand(s1);
  7767. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7768. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7769. IF ~conditional THEN
  7770. InitOperand(result,ModeValue); result.op := res;
  7771. ELSE
  7772. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7773. BrL(falseLabel);
  7774. ReleaseIntermediateOperand(res);
  7775. END;
  7776. (* --- MSK ----*)
  7777. |Global.systemMsk:
  7778. Evaluate(p0,s0);
  7779. Evaluate(p1,s1);
  7780. ReuseCopy(res,s0.op);
  7781. ReleaseOperand(s0);
  7782. Emit(And(position,res,res,s1.op));
  7783. ReleaseOperand(s1);
  7784. InitOperand(result,ModeValue);
  7785. result.op := res;
  7786. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7787. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7788. Evaluate(p0,s0);
  7789. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7790. ReleaseOperand(s0);
  7791. InitOperand(result,ModeValue);
  7792. result.op := res;
  7793. (* ---- SYSTEM.GetStackPointer ----- *)
  7794. |Global.systemGetStackPointer:
  7795. InitOperand(result,ModeValue);
  7796. result.op := sp;
  7797. (* ---- SYSTEM.GetFramePointer ----- *)
  7798. |Global.systemGetFramePointer:
  7799. InitOperand(result,ModeValue);
  7800. result.op := fp;
  7801. (* ---- SYSTEM.GetActivity ----- *)
  7802. |Global.systemGetActivity:
  7803. ASSERT(backend.cooperative);
  7804. InitOperand(result,ModeValue);
  7805. result.op := ap;
  7806. (* ---- SYSTEM.SetStackPointer ----- *)
  7807. |Global.systemSetStackPointer:
  7808. Evaluate(p0,s0); (* *)
  7809. Emit(Mov(position,sp,s0.op));
  7810. ReleaseOperand(s0);
  7811. (* ---- SYSTEM.SetFramePointer ----- *)
  7812. |Global.systemSetFramePointer:
  7813. Evaluate(p0,s0); (* *)
  7814. Emit(Mov(position,fp,s0.op));
  7815. ReleaseOperand(s0);
  7816. (* ---- SYSTEM.Activity ----- *)
  7817. |Global.systemSetActivity:
  7818. ASSERT(backend.cooperative);
  7819. Evaluate(p0,s0); (* *)
  7820. Emit(Mov(position,ap,s0.op));
  7821. ReleaseOperand(s0);
  7822. (* ---- SYSTEM.VAL ----- *)
  7823. |Global.systemVal:
  7824. Expression(p1);
  7825. s1 := result;
  7826. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7827. IF s1.mode = ModeReference THEN
  7828. (* nothing to be done if not record type, just take over new type *)
  7829. IF (type IS SyntaxTree.RecordType) THEN
  7830. ReleaseIntermediateOperand(s1.tag);
  7831. s1.tag := TypeDescriptorAdr(type);
  7832. IF ~newObjectFile THEN
  7833. IntermediateCode.MakeMemory(s1.tag,addressType);
  7834. END;
  7835. UseIntermediateOperand(s1.tag);
  7836. END;
  7837. result := s1;
  7838. ELSE (* copy over result to different type, may not use convert *)
  7839. itype := IntermediateCode.GetType(system,type);
  7840. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7841. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7842. Emit(Mov(position,s0.op,s1.op));
  7843. ReleaseOperand(s1);
  7844. InitOperand(result,ModeValue);
  7845. result.op := s0.op;
  7846. ELSE (* different size, must convert *)
  7847. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7848. Convert(s1.op, IntermediateCode.GetType(system,type));
  7849. result := s1;
  7850. END;
  7851. END;
  7852. (* ---- SYSTEM.GET ----- *)
  7853. |Global.systemGet:
  7854. Evaluate(p0,s0); (* adr *)
  7855. Designate(p1,s1); (* variable *)
  7856. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7857. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7858. Emit(Mov(position,s1.op,s0.op));
  7859. ReleaseOperand(s1);
  7860. ReleaseOperand(s0);
  7861. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7862. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7863. Evaluate(p0,s0); (* *)
  7864. Evaluate(p1,s1); (* variable *)
  7865. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7866. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7867. (* real part *)
  7868. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7869. Emit(Mov(position,res, s1.op));
  7870. ReleaseIntermediateOperand(res);
  7871. (* imaginary part *)
  7872. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7873. Emit(Mov(position,res, s1.tag));
  7874. ReleaseIntermediateOperand(res);
  7875. ReleaseOperand(s1);
  7876. ReleaseOperand(s0);
  7877. ELSE
  7878. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7879. ReleaseOperand(s0);
  7880. Emit(Mov(position,res,s1.op));
  7881. ReleaseIntermediateOperand(res);
  7882. ReleaseOperand(s1);
  7883. END;
  7884. (* ---- SYSTEM.MOVE ----- *)
  7885. |Global.systemMove:
  7886. Evaluate(p0,s0);
  7887. Evaluate(p1,s1);
  7888. Evaluate(p2,s2);
  7889. Emit(Copy(position,s1.op,s0.op,s2.op));
  7890. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7891. (* ---- SYSTEM.NEW ----- *)
  7892. |Global.systemNew:
  7893. Designate(p0,s0);
  7894. Emit(Push(position,s0.op));
  7895. ReleaseOperand(s0);
  7896. Evaluate(p1,s1);
  7897. Emit(Push(position,s1.op));
  7898. ReleaseOperand(s1);
  7899. (* push realtime flag: false by default *)
  7900. Emit(Push(position,false));
  7901. CallThis(position,"Heaps","NewSys",3);
  7902. (* ---- SYSTEM.CALL ----- *)
  7903. |Global.systemRef:
  7904. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7905. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7906. s0.mode := ModeValue;
  7907. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7908. result := s0
  7909. (* ---- INCR ----- *)
  7910. |Global.Incr:
  7911. Designate(p0,operand);
  7912. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7913. Dereference(operand,p0.type.resolved,FALSE);
  7914. END;
  7915. ASSERT(p1 # NIL);
  7916. Evaluate(p1,l);
  7917. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7918. ReleaseOperand(operand); ReleaseOperand(l);
  7919. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7920. (* ---- SUM ----- *)
  7921. |Global.Sum: HALT(200);
  7922. (* ---- ALL ----- *)
  7923. |Global.All: HALT(200);
  7924. (* ---- CAS ----- *)
  7925. |Global.Cas:
  7926. needsTrace := p0.NeedsTrace();
  7927. IF needsTrace THEN ModifyAssignments(true) END;
  7928. Designate(p0,s0);
  7929. Evaluate(p1,s1);
  7930. Evaluate(p2,s2);
  7931. IF needsTrace THEN
  7932. Emit(Push(position, s0.op));
  7933. Emit(Push(position, s1.op));
  7934. Emit(Push(position, s2.op));
  7935. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7936. ELSE
  7937. Emit(Cas(position,s0.op,s1.op,s2.op));
  7938. END;
  7939. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7940. IF needsTrace THEN ModifyAssignments(false) END;
  7941. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7942. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7943. IF conditional THEN
  7944. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7945. BrL(falseLabel);
  7946. ReleaseIntermediateOperand(res);
  7947. END;
  7948. (* ---- DIM ----- *)
  7949. |Global.Dim:
  7950. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7951. Designate(p0,s0);
  7952. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7953. Dereference(s0,p0.type.resolved,FALSE);
  7954. END;
  7955. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7956. ReleaseOperand(s0);
  7957. (* ---- RESHAPE ----- *)
  7958. |Global.Reshape:
  7959. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7960. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7961. left.SetType(procedure.type);
  7962. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7963. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7964. END;
  7965. (* ---- SYSTEM.TYPECODE ----- *)
  7966. |Global.systemTypeCode:
  7967. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7968. IF type.resolved IS SyntaxTree.PointerType THEN
  7969. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7970. END;
  7971. result.op := TypeDescriptorAdr(type);
  7972. IF ~newObjectFile THEN
  7973. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7974. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7975. END;
  7976. result.mode := ModeValue;
  7977. (* ---- SYSTEM.TRACE ----- *)
  7978. |Global.systemTrace:
  7979. SystemTrace(x.parameters, x.position);
  7980. (* ----- CONNECT ------*)
  7981. |Global.Connect:
  7982. IF backend.cellsAreObjects THEN
  7983. PushPort(p0);
  7984. PushPort(p1);
  7985. IF p2 # NIL THEN
  7986. Evaluate(p2, s2);
  7987. Emit(Push(p2.position, s2.op));
  7988. ReleaseOperand(s2);
  7989. ELSE
  7990. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7991. END;
  7992. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7993. ELSE
  7994. Warning(x.position, "cannot run on final hardware");
  7995. END;
  7996. (* ----- DELEGATE ------*)
  7997. |Global.Delegate:
  7998. IF backend.cellsAreObjects THEN
  7999. PushPort(p0);
  8000. PushPort(p1);
  8001. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  8002. ELSE
  8003. Warning(x.position, "cannot run on final hardware");
  8004. END;
  8005. (* ----- SEND ------*)
  8006. |Global.Send:
  8007. Evaluate(p0,s0);
  8008. Evaluate(p1,s1);
  8009. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8010. Emit(Push(position,s0.op));
  8011. Emit(Push(position,s1.op));
  8012. (*
  8013. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8014. *)
  8015. IF ~backend.cellsAreObjects THEN
  8016. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8017. END;
  8018. ReleaseOperand(s0);
  8019. ReleaseOperand(s1);
  8020. IF backend.cellsAreObjects THEN
  8021. CallThis(position,"ActiveCellsRuntime","Send",2);
  8022. ELSE
  8023. CallThis(position,ChannelModuleName,"Send",2);
  8024. END;
  8025. (* ----- RECEIVE ------*)
  8026. |Global.Receive:
  8027. Evaluate(p0,s0);
  8028. Emit(Push(position,s0.op));
  8029. Designate(p1,s1);
  8030. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8031. Emit(Push(position,s1.op));
  8032. IF p2 # NIL THEN
  8033. Designate(p2,s2);
  8034. Emit(Push(position,s2.op));
  8035. END;
  8036. (*
  8037. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8038. *)
  8039. IF ~backend.cellsAreObjects THEN
  8040. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8041. END;
  8042. ReleaseOperand(s0);
  8043. ReleaseOperand(s1);
  8044. ReleaseOperand(s2);
  8045. IF backend.cellsAreObjects THEN
  8046. IF p2 = NIL THEN
  8047. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8048. ELSE
  8049. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8050. END;
  8051. ELSE
  8052. IF p2 = NIL THEN
  8053. CallThis(position,ChannelModuleName,"Receive",2)
  8054. ELSE
  8055. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8056. END;
  8057. END;
  8058. | Global.systemSpecial:
  8059. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8060. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8061. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8062. (* determine if parameters are of the VAR kind *)
  8063. ASSERT(x.parameters.Length() <= 3);
  8064. formalParameter := procedureType.firstParameter;
  8065. FOR i := 0 TO x.parameters.Length() - 1 DO
  8066. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8067. formalParameter := formalParameter.nextParameter
  8068. END;
  8069. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8070. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8071. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8072. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8073. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8074. IF procedureType.returnType # NIL THEN
  8075. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8076. Emit(Result(position, res));
  8077. (*InitOperand(result,ModeValue);
  8078. result.op := res;
  8079. *)
  8080. IF ~conditional THEN
  8081. InitOperand(result,ModeValue); result.op := res;
  8082. ELSE
  8083. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8084. BrL(falseLabel);
  8085. ReleaseIntermediateOperand(res);
  8086. END;
  8087. END
  8088. ELSE (* function not yet implemented *)
  8089. Error(position,"not yet implemented");
  8090. END;
  8091. destination := dest;
  8092. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8093. END VisitBuiltinCallDesignator;
  8094. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8095. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8096. BEGIN
  8097. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8098. dest := destination; destination := emptyOperand;
  8099. Expression(x.left);
  8100. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8101. ELSIF isUnchecked THEN (* no check *)
  8102. ELSE
  8103. trueL := NewLabel();
  8104. falseL := NewLabel();
  8105. IF IsPointerToRecord(x.left.type,recordType) THEN
  8106. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8107. Emit(Mov(position,tag, result.op));
  8108. IF result.mode # ModeValue THEN
  8109. ptr := tag;
  8110. IntermediateCode.MakeMemory(ptr,addressType);
  8111. Emit(Mov(position,tag, ptr));
  8112. END;
  8113. IF ~backend.cooperative THEN
  8114. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8115. END;
  8116. IntermediateCode.MakeMemory(tag,addressType);
  8117. ELSE
  8118. tag := result.tag;
  8119. UseIntermediateOperand(tag);
  8120. END;
  8121. TypeTest(tag,x.type,trueL,falseL);
  8122. ReleaseIntermediateOperand(tag);
  8123. SetLabel(falseL);
  8124. EmitTrap(position,TypeCheckTrap);
  8125. SetLabel(trueL);
  8126. END;
  8127. destination := dest;
  8128. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8129. END VisitTypeGuardDesignator;
  8130. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8131. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8132. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8133. VAR label: Label; pc: LONGINT;
  8134. BEGIN
  8135. IF backend.cooperative & ~isUnchecked THEN
  8136. pc := section.pc;
  8137. label := NewLabel();
  8138. BrneL(label, operand.op, nil);
  8139. EmitTrap(position, NilPointerTrap);
  8140. SetLabel(label);
  8141. INC(statCoopNilCheck, section.pc - pc);
  8142. END;
  8143. END NilCheck;
  8144. BEGIN
  8145. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8146. ReuseCopy(dereferenced,operand.op);
  8147. ReleaseOperand(operand);
  8148. operand.mode := ModeReference;
  8149. operand.op := dereferenced;
  8150. operand.tag := dereferenced;
  8151. UseIntermediateOperand(operand.tag);
  8152. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8153. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8154. ReleaseIntermediateOperand(operand.tag);
  8155. operand.tag := TypeDescriptorAdr(type);
  8156. IF ~newObjectFile THEN
  8157. IntermediateCode.MakeMemory(operand.tag,addressType);
  8158. END;
  8159. ELSE
  8160. IF ~backend.cooperative THEN
  8161. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8162. END;
  8163. IntermediateCode.MakeMemory(operand.tag,addressType);
  8164. END;
  8165. NilCheck(operand.op);
  8166. ELSIF type IS SyntaxTree.ArrayType THEN
  8167. IF isUnsafe THEN
  8168. NilCheck(operand.op);
  8169. ReleaseIntermediateOperand(operand.tag);
  8170. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8171. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8172. ELSE
  8173. operand.tag := emptyOperand;
  8174. END;
  8175. ELSE
  8176. NilCheck(operand.op);
  8177. IF backend.cooperative THEN
  8178. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8179. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8180. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8181. ELSE
  8182. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8183. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8184. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8185. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8186. END;
  8187. END;
  8188. ELSIF type IS SyntaxTree.MathArrayType THEN
  8189. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8190. IntermediateCode.MakeMemory(operand.op,addressType);
  8191. ELSE HALT(100);
  8192. END;
  8193. END Dereference;
  8194. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8195. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8196. BEGIN
  8197. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8198. dest := destination; destination := emptyOperand;
  8199. Evaluate(x.left,d);
  8200. type := x.type.resolved;
  8201. prevIsUnchecked := isUnchecked;
  8202. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8203. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8204. END;
  8205. Dereference(d,type,IsUnsafePointer(x.left.type));
  8206. isUnchecked := prevIsUnchecked;
  8207. result := d;
  8208. 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
  8209. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8210. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8211. END;
  8212. END;
  8213. destination := dest;
  8214. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8215. END VisitDereferenceDesignator;
  8216. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8217. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8218. BEGIN
  8219. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8220. dest := destination; destination := emptyOperand;
  8221. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8222. tag := result.op;
  8223. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8224. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8225. StaticCallOperand(result,procedure.super);
  8226. ReleaseIntermediateOperand(result.tag);
  8227. UseIntermediateOperand(tag); (* necessary ? *)
  8228. result.tag := tag;
  8229. destination := dest;
  8230. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8231. END VisitSupercallDesignator;
  8232. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8233. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8234. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8235. name: Basic.SegmentedName;
  8236. procedure: SyntaxTree.Procedure;
  8237. procedureType: SyntaxTree.ProcedureType;
  8238. BEGIN
  8239. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8240. dest := destination; destination := emptyOperand;
  8241. scope := currentScope;
  8242. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8243. scope := scope.outerScope;
  8244. END;
  8245. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8246. IF newObjectFile THEN
  8247. moduleSection := meta.ModuleSection();
  8248. IF backend.cooperative THEN
  8249. moduleOffset := 0;
  8250. ELSE
  8251. moduleOffset := moduleSection.pc;
  8252. END;
  8253. result.mode := ModeValue;
  8254. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8255. ELSE
  8256. Symbol(moduleSelf,result);
  8257. IntermediateCode.MakeMemory(result.op,addressType);
  8258. END
  8259. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8260. result.mode := ModeValue;
  8261. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8262. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8263. ELSE
  8264. GetBaseRegister(basereg,currentScope,scope);
  8265. InitOperand(result,ModeReference);
  8266. result.op := basereg;
  8267. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8268. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8269. parametersSize := ProcedureParametersSize(system,procedure);
  8270. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8271. IF backend.cooperative THEN
  8272. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8273. END;
  8274. (* tag must be loaded when dereferencing SELF pointer *)
  8275. END;
  8276. destination := dest;
  8277. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8278. END VisitSelfDesignator;
  8279. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8280. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; map: SymbolMap;
  8281. BEGIN
  8282. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8283. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8284. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8285. parameter := procedureType.returnParameter;
  8286. IF currentIsInline THEN
  8287. map := currentMapper.Get(NIL);
  8288. IF map # NIL THEN
  8289. Designate(map.to, result);
  8290. ELSE
  8291. HALT(200);
  8292. END;
  8293. RETURN;
  8294. END;
  8295. VisitParameter(parameter);
  8296. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8297. END VisitResultDesignator;
  8298. (** values *)
  8299. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8300. BEGIN
  8301. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8302. IF conditional THEN
  8303. IF x.value THEN BrL(trueLabel)
  8304. ELSE BrL(falseLabel)
  8305. END;
  8306. ELSE
  8307. InitOperand(result,ModeValue);
  8308. IF x.value THEN result.op := true ELSE result.op := false END;
  8309. END;
  8310. END VisitBooleanValue;
  8311. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8312. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8313. BEGIN
  8314. Global.GetModuleSegmentedName(module.module, name);
  8315. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8316. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8317. RETURN section
  8318. END GetDataSection;
  8319. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8320. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8321. BEGIN
  8322. type := vop.type;
  8323. data := GetDataSection();
  8324. pc := EnterImmediate(data,vop);
  8325. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8326. IntermediateCode.MakeMemory(vop, type);
  8327. END GetImmediateMem;
  8328. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8329. BEGIN
  8330. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8331. InitOperand(result,ModeValue);
  8332. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8333. IF ~supportedImmediate(result.op) &~inData THEN
  8334. GetImmediateMem(result.op)
  8335. END;
  8336. END VisitIntegerValue;
  8337. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8338. BEGIN
  8339. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8340. InitOperand(result,ModeValue);
  8341. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8342. END VisitCharacterValue;
  8343. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8344. BEGIN
  8345. IF Trace THEN TraceEnter("VisitSetValue") END;
  8346. InitOperand(result,ModeValue);
  8347. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8348. END VisitSetValue;
  8349. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8350. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8351. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8352. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8353. BEGIN
  8354. numberElements := x.elements.Length();
  8355. FOR i := 0 TO numberElements-1 DO
  8356. expression := x.elements.GetExpression(i);
  8357. IF expression IS SyntaxTree.MathArrayExpression THEN
  8358. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8359. ELSE
  8360. inData := TRUE;
  8361. Evaluate(expression,op);
  8362. irv.Emit(Data(position,op.op));
  8363. inData := FALSE;
  8364. ReleaseOperand(op);
  8365. END;
  8366. END;
  8367. END RecursiveData;
  8368. BEGIN
  8369. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8370. IF ~TryConstantDeclaration() THEN
  8371. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8372. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8373. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8374. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8375. ELSE
  8376. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8377. END;
  8378. RecursiveData(x.array);
  8379. InitOperand(result,ModeReference);
  8380. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8381. END
  8382. END VisitMathArrayValue;
  8383. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8384. VAR constant: Sections.Section;
  8385. BEGIN
  8386. IF constantDeclaration = NIL THEN
  8387. RETURN FALSE
  8388. ELSE
  8389. (* Is a constant in this module: did we generate it already? *)
  8390. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8391. IF constant # NIL THEN
  8392. InitOperand(result,ModeReference);
  8393. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8394. RETURN TRUE;
  8395. END;
  8396. END;
  8397. RETURN FALSE
  8398. END TryConstantDeclaration;
  8399. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8400. BEGIN
  8401. constantDeclaration := x;
  8402. x.value.resolved.Accept(SELF);
  8403. END VisitConstant;
  8404. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8405. BEGIN
  8406. IF Trace THEN TraceEnter("VisitRealValue") END;
  8407. InitOperand(result,ModeValue);
  8408. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8409. END VisitRealValue;
  8410. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8411. VAR
  8412. componentType: SyntaxTree.Type;
  8413. BEGIN
  8414. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8415. ASSERT(x.type IS SyntaxTree.ComplexType);
  8416. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8417. InitOperand(result,ModeValue);
  8418. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8419. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8420. END VisitComplexValue;
  8421. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8422. VAR i: LONGINT; name: Basic.SegmentedName;
  8423. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8424. BEGIN
  8425. IF Trace THEN TraceEnter("VisitStringValue") END;
  8426. IF ~TryConstantDeclaration() THEN
  8427. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8428. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8429. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8430. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8431. ELSE
  8432. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8433. END;
  8434. FOR i := 0 TO x.length-1 DO
  8435. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8436. irv.Emit(Data(position,op));
  8437. END;
  8438. InitOperand(result,ModeReference);
  8439. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8440. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8441. END
  8442. END VisitStringValue;
  8443. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8444. BEGIN
  8445. IF Trace THEN TraceEnter("VisitNilValue") END;
  8446. InitOperand(result,ModeValue);
  8447. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8448. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8449. END VisitNilValue;
  8450. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8451. BEGIN
  8452. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8453. InitOperand(result,ModeValue);
  8454. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8455. END VisitEnumerationValue;
  8456. (** symbols *)
  8457. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8458. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8459. END VisitImport;
  8460. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8461. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8462. BEGIN
  8463. IF scope # baseScope THEN
  8464. (* left := [fp+8] *)
  8465. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8466. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8467. ReuseCopy(left,right);
  8468. ReleaseIntermediateOperand(right);
  8469. scope := scope.outerScope; DEC(level);
  8470. (* { left := [left+8] } *)
  8471. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8472. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8473. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8474. Emit(Mov(position,left,right));
  8475. scope := scope.outerScope; DEC(level);
  8476. END;
  8477. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8478. result := left;
  8479. ELSE
  8480. result := fp;
  8481. END;
  8482. END GetBaseRegister;
  8483. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8484. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8485. BEGIN
  8486. IF Trace THEN TraceEnter("VisitVariable"); END;
  8487. type := x.type.resolved;
  8488. IF (x.useRegister) THEN
  8489. InitOperand(result, ModeValue);
  8490. IF x.registerNumber < 0 THEN
  8491. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8492. reg := x.registerNumber;
  8493. ELSE
  8494. reg := registerUsageCount.Map(x.registerNumber);
  8495. UseRegister(reg);
  8496. END;
  8497. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8498. ELSIF x.externalName # NIL THEN
  8499. InitOperand(result,ModeReference);
  8500. Basic.ToSegmentedName(x.externalName^, name);
  8501. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8502. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8503. InitOperand(result,ModeReference);
  8504. GetBaseRegister(result.op,currentScope,x.scope);
  8505. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8506. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8507. InitOperand(result,ModeReference);
  8508. GetCodeSectionNameForSymbol(x,name);
  8509. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8510. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8511. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8512. InitOperand(result,ModeReference);
  8513. GetCodeSectionNameForSymbol(x,name);
  8514. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8515. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8516. ELSE (* field, left designator must have been emitted *)
  8517. ASSERT(result.mode = ModeReference);
  8518. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8519. ReuseCopy(temp,result.op);
  8520. ReleaseIntermediateOperand(result.op);
  8521. result.op := temp;
  8522. END;
  8523. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8524. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8525. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8526. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8527. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8528. END;
  8529. END;
  8530. END;
  8531. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8532. ValueToCondition(result);
  8533. ELSIF type IS SyntaxTree.ProcedureType THEN
  8534. ReleaseIntermediateOperand(result.tag);
  8535. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8536. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8537. UseIntermediateOperand(result.tag);
  8538. ELSE
  8539. result.tag := nil; (* nil *)
  8540. END;
  8541. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8542. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8543. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8544. ReleaseIntermediateOperand(result.tag);
  8545. Global.GetSymbolSegmentedName(x,name);
  8546. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8547. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8548. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8549. ELSE
  8550. END;
  8551. ELSE
  8552. ReleaseIntermediateOperand(result.tag);
  8553. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8554. END;
  8555. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8556. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8557. ReleaseIntermediateOperand(result.tag);
  8558. result.tag := result.op;
  8559. UseIntermediateOperand(result.tag);
  8560. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8561. IntermediateCode.MakeMemory(result.op,addressType);
  8562. END;
  8563. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8564. ReleaseIntermediateOperand(result.tag);
  8565. result.tag := TypeDescriptorAdr(type);
  8566. IF ~newObjectFile THEN
  8567. IntermediateCode.MakeMemory(result.tag,addressType);
  8568. END;
  8569. UseIntermediateOperand(result.tag);
  8570. (* tag for pointer type computed not here but during dereferencing *)
  8571. END;
  8572. IF Trace THEN TraceExit("VisitVariable") END;
  8573. END VisitVariable;
  8574. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8575. BEGIN
  8576. VisitVariable(property);
  8577. END VisitProperty;
  8578. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8579. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8580. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8581. BEGIN
  8582. type := x.type.resolved;
  8583. IF Trace THEN TraceEnter("VisitParameter") END;
  8584. IF x.ownerType IS SyntaxTree.CellType THEN
  8585. ptype := x.type.resolved;
  8586. IF backend.cellsAreObjects THEN
  8587. ASSERT(result.mode = ModeReference);
  8588. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8589. ReuseCopy(temp,result.op);
  8590. ReleaseIntermediateOperand(result.op);
  8591. result.op := temp;
  8592. END;
  8593. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8594. RETURN;
  8595. ELSE
  8596. InitOperand(result,ModeReference);
  8597. GetCodeSectionNameForSymbol(x,name);
  8598. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8599. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8600. RETURN;
  8601. END;
  8602. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8603. InitOperand(result,ModeReference);
  8604. GetCodeSectionNameForSymbol(x,name);
  8605. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8606. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8607. RETURN
  8608. ELSE
  8609. GetBaseRegister(basereg,currentScope,x.scope);
  8610. InitOperand(result,ModeReference);
  8611. result.op := basereg;
  8612. END;
  8613. IF IsOpenArray(type) THEN
  8614. result.tag := basereg;
  8615. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8616. IntermediateCode.MakeMemory(result.op,addressType);
  8617. IF Global.IsOberonProcedure(x.ownerType) THEN
  8618. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8619. UseIntermediateOperand(result.tag);
  8620. ELSE
  8621. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8622. END;
  8623. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8624. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8625. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8626. ELSIF IsStaticArray(type) THEN
  8627. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8628. IntermediateCode.MakeMemory(result.op,addressType);
  8629. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8630. ELSIF type IS SyntaxTree.MathArrayType THEN
  8631. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8632. WITH type: SyntaxTree.MathArrayType DO
  8633. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8634. IF type.form = SyntaxTree.Tensor THEN
  8635. ELSIF type.form = SyntaxTree.Open THEN
  8636. result.tag := result.op;
  8637. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8638. IntermediateCode.MakeMemory(result.op,addressType);
  8639. UseIntermediateOperand(result.tag);
  8640. ELSIF type.form = SyntaxTree.Static THEN
  8641. IF x.kind = SyntaxTree.ConstParameter THEN
  8642. IntermediateCode.MakeMemory(result.op,addressType);
  8643. END;
  8644. ELSE HALT(100)
  8645. END;
  8646. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8647. IF type.form = SyntaxTree.Tensor THEN
  8648. ToMemory(result.op,addressType,0);
  8649. ELSIF type.form = SyntaxTree.Open THEN
  8650. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8651. ReuseCopy(result.tag, mem);
  8652. ReleaseIntermediateOperand(mem);
  8653. ReleaseIntermediateOperand(result.op);
  8654. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8655. ELSIF type.form = SyntaxTree.Static THEN
  8656. IntermediateCode.MakeMemory(result.op,addressType);
  8657. ELSE HALT(100)
  8658. END;
  8659. ELSE HALT(100)
  8660. END;
  8661. END;
  8662. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8663. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8664. IntermediateCode.MakeMemory(result.op,addressType);
  8665. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8666. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8667. END;
  8668. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8669. ValueToCondition(result);
  8670. ELSIF type IS SyntaxTree.ProcedureType THEN
  8671. ReleaseIntermediateOperand(result.tag);
  8672. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8673. IF x.kind = SyntaxTree.VarParameter THEN
  8674. ReuseCopy(result.tag,result.op);
  8675. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8676. IntermediateCode.MakeMemory(result.tag,addressType);
  8677. ELSE
  8678. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8679. UseIntermediateOperand(result.tag);
  8680. END;
  8681. ELSE
  8682. result.tag := nil;
  8683. END;
  8684. (* tag for pointer type computed not here but during dereferencing *)
  8685. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) & ~(x.selfParameter) THEN
  8686. ReleaseIntermediateOperand(result.tag);
  8687. result.tag := basereg;
  8688. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8689. IntermediateCode.MakeMemory(result.tag,addressType);
  8690. UseIntermediateOperand(result.tag);
  8691. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & ((x.kind = SyntaxTree.ValueParameter) OR x.selfParameter) THEN
  8692. ReleaseIntermediateOperand(result.tag);
  8693. result.tag := TypeDescriptorAdr(type);
  8694. IF ~newObjectFile THEN
  8695. IntermediateCode.MakeMemory(result.tag,addressType);
  8696. END;
  8697. UseIntermediateOperand(result.tag);
  8698. END;
  8699. IF Trace THEN TraceExit("VisitParameter") END;
  8700. END VisitParameter;
  8701. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8702. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8703. BEGIN
  8704. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8705. (* left.p: left already emitted *)
  8706. tag := result.op; (* value of pointer to left *)
  8707. (* get type desc *)
  8708. tmp := result.tag;
  8709. IntermediateCode.MakeMemory(tmp,addressType);
  8710. (* get method adr *)
  8711. Reuse1(reg,tmp);
  8712. ReleaseIntermediateOperand(tmp);
  8713. IF backend.cooperative THEN
  8714. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8715. WHILE recordType.baseType # NIL DO
  8716. recordType := recordType.GetBaseRecord ();
  8717. END;
  8718. GetRecordTypeName (recordType,name);
  8719. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8720. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8721. offset := 0;
  8722. ELSE
  8723. offset := 2;
  8724. END;
  8725. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8726. ELSE
  8727. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8728. END;
  8729. InitOperand(operand,ModeReference);
  8730. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8731. operand.op := reg;
  8732. operand.tag := tag;
  8733. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8734. END DynamicCallOperand;
  8735. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8736. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8737. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8738. BEGIN
  8739. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8740. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8741. tag := operand.op;
  8742. ReleaseIntermediateOperand(operand.tag);
  8743. ELSE tag := nil
  8744. END;
  8745. IF x.isInline THEN
  8746. sectionType := Sections.InlineCodeSection;
  8747. ELSE
  8748. sectionType := Sections.CodeSection;
  8749. END;
  8750. IF x.externalName # NIL THEN
  8751. Basic.ToSegmentedName(x.externalName^, name);
  8752. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8753. ELSE
  8754. GetCodeSectionNameForSymbol(x, name);
  8755. IF (x.scope.ownerModule = module.module) THEN
  8756. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8757. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8758. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8759. IF source.pc = 0 THEN (* no code yet *)
  8760. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8761. END;
  8762. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8763. bits := x.procedureScope.body.code.inlineCode;
  8764. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8765. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8766. binary.CopyBits(bits, 0, bits.GetSize());
  8767. source.SetResolved(binary);
  8768. ELSE
  8769. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8770. END;
  8771. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8772. END;
  8773. InitOperand(operand,ModeValue);
  8774. operand.op := reg;
  8775. operand.tag := tag;
  8776. IF Trace THEN TraceExit("StaticCallOperand") END;
  8777. END StaticCallOperand;
  8778. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8779. (* handle expressions of the form designator.procedure or procedure *)
  8780. BEGIN
  8781. IF Trace THEN TraceEnter("VisitProcedure") END;
  8782. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) & ~(result.tag.mode = IntermediateCode.ModeImmediate) THEN
  8783. DynamicCallOperand(result,x);
  8784. ELSIF x.isInline THEN
  8785. StaticCallOperand(result,x);
  8786. ELSE
  8787. StaticCallOperand(result,x);
  8788. END;
  8789. IF Trace THEN TraceExit("VisitProcedure") END;
  8790. END VisitProcedure;
  8791. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8792. BEGIN
  8793. VisitProcedure(x);
  8794. END VisitOperator;
  8795. (** statements *)
  8796. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8797. BEGIN
  8798. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8799. Expression(x.call);
  8800. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8801. ReleaseOperand(result)
  8802. END;
  8803. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8804. END VisitProcedureCallStatement;
  8805. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8806. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8807. leftBase, rightBase: SyntaxTree.Type;
  8808. procedureName,s: SyntaxTree.IdentifierString;
  8809. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8810. size: LONGINT; rightKind: LONGINT;
  8811. dummy: IntermediateCode.Operand;
  8812. CONST moduleName = "FoxArrayBase";
  8813. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8814. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8815. BEGIN
  8816. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8817. IF base IS SyntaxTree.MathArrayType THEN
  8818. base := OpenArray(base(SyntaxTree.MathArrayType));
  8819. END;
  8820. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8821. result.SetArrayBase(base);
  8822. RETURN result
  8823. END OpenArray;
  8824. BEGIN
  8825. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8826. SaveRegisters();ReleaseUsedRegisters(saved);
  8827. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8828. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8829. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8830. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8831. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8832. IF leftType.form = SyntaxTree.Tensor THEN
  8833. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8834. ELSIF leftType.form = SyntaxTree.Open THEN
  8835. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8836. ELSIF leftType.form = SyntaxTree.Static THEN
  8837. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8838. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8839. END;
  8840. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8841. IF procedure = NIL THEN
  8842. s := "Instruction not supported on target, emulation procedure ";
  8843. Strings.Append(s,moduleName);
  8844. Strings.Append(s,".");
  8845. Strings.Append(s,procedureName);
  8846. Strings.Append(s," not present");
  8847. Error(position,s);
  8848. ELSE
  8849. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8850. parameter.SetType(leftType);
  8851. parameter.SetAccess(SyntaxTree.Internal);
  8852. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8853. parameter.SetKind(rightKind);
  8854. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8855. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8856. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8857. StaticCallOperand(result,procedure);
  8858. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8859. ReleaseOperand(result);
  8860. END;
  8861. RestoreRegisters(saved);
  8862. END;
  8863. END AssignMathArray;
  8864. VAR modifyAssignmentCounter := 0: LONGINT;
  8865. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8866. VAR processor,mem,dst: IntermediateCode.Operand;
  8867. BEGIN
  8868. IF value.intValue = true.intValue THEN
  8869. INC(modifyAssignmentCounter);
  8870. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8871. modifyAssignmentsPC := section.pc;
  8872. ELSE
  8873. DEC(modifyAssignmentCounter);
  8874. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8875. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8876. END;
  8877. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8878. dst := NewRegisterOperand (addressType);
  8879. Emit(Mov(position,dst, processor));
  8880. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8881. Emit(Mov(position,mem, value));
  8882. ReleaseIntermediateOperand(dst);
  8883. END ModifyAssignments;
  8884. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8885. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8886. BEGIN
  8887. type := left.type.resolved;
  8888. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8889. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8890. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8891. IF procedureType.returnParameter = parameter THEN
  8892. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8893. END;
  8894. END;
  8895. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8896. END CopySize;
  8897. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8898. VAR
  8899. leftO, rightO: Operand;
  8900. mem, sizeOp: IntermediateCode.Operand;
  8901. leftType, rightType, componentType: SyntaxTree.Type;
  8902. size: LONGINT;
  8903. parameters: SyntaxTree.ExpressionList;
  8904. procedure: SyntaxTree.Procedure;
  8905. call: SyntaxTree.ProcedureCallDesignator;
  8906. designator: SyntaxTree.Designator;
  8907. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8908. VAR procedureType: SyntaxTree.ProcedureType;
  8909. BEGIN
  8910. IF SemanticChecker.ReturnedAsParameter(right.type) THEN
  8911. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8912. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8913. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8914. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8915. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8916. IF right.id = Global.Reshape THEN RETURN TRUE
  8917. END;
  8918. END;
  8919. END;
  8920. END;
  8921. RETURN FALSE
  8922. END CanPassAsResultParameter;
  8923. BEGIN
  8924. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8925. leftType := left.type.resolved; rightType:= right.type.resolved;
  8926. IF backend.cooperative & left.NeedsTrace() THEN
  8927. ModifyAssignments(true);
  8928. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8929. Designate(right, rightO);
  8930. Designate(left, leftO);
  8931. ASSERT(leftO.mode = ModeReference);
  8932. TransferToRegister(leftO.op, leftO.op);
  8933. TransferToRegister(rightO.op, rightO.op);
  8934. Emit(Push(position, leftO.op));
  8935. Emit(Push(position, rightO.op));
  8936. CallAssignMethod(leftO.op, rightO.op, left.type);
  8937. Emit(Pop(position, rightO.op));
  8938. Emit(Pop(position, leftO.op));
  8939. sizeOp := CopySize(left);
  8940. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8941. ReleaseOperand(leftO);
  8942. ReleaseOperand(rightO);
  8943. ELSE
  8944. Evaluate(right,rightO);
  8945. Designate(left,leftO);
  8946. ASSERT(leftO.mode = ModeReference);
  8947. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8948. (* copy procedure address first *)
  8949. MakeMemory(mem,leftO.op,addressType,0);
  8950. Emit(Mov(position,mem,rightO.op));
  8951. ReleaseIntermediateOperand(mem);
  8952. (* copy pointer address *)
  8953. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8954. CallAssignPointer(leftO.tag, rightO.tag);
  8955. ELSE
  8956. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8957. CallAssignPointer(leftO.op, rightO.op);
  8958. END;
  8959. ReleaseOperand(leftO);
  8960. ReleaseOperand(rightO);
  8961. END;
  8962. ModifyAssignments(false);
  8963. RETURN;
  8964. END;
  8965. IF CanPassAsResultParameter(right) THEN
  8966. procedureResultDesignator := left(SyntaxTree.Designator);
  8967. Expression(right);
  8968. procedureResultDesignator := NIL;
  8969. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8970. (* left <-- ALIAS OF right: zerocopy *)
  8971. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8972. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8973. designator.SetType(procedure.type);
  8974. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8975. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8976. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8977. END;
  8978. ELSIF leftType IS SyntaxTree.RangeType THEN
  8979. (* LHS is of array range type *)
  8980. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8981. Evaluate(right, rightO);
  8982. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8983. (* first *)
  8984. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8985. Emit(Mov(position,mem, rightO.op));
  8986. ReleaseIntermediateOperand(mem);
  8987. (* last *)
  8988. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8989. Emit(Mov(position,mem, rightO.tag));
  8990. ReleaseIntermediateOperand(mem);
  8991. (* step *)
  8992. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8993. Emit(Mov(position,mem, rightO.extra));
  8994. ReleaseIntermediateOperand(mem);
  8995. ReleaseOperand(rightO);
  8996. ReleaseOperand(leftO)
  8997. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8998. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8999. Evaluate(right, rightO);
  9000. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  9001. componentType := leftType(SyntaxTree.ComplexType).componentType;
  9002. (* real part *)
  9003. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  9004. Emit(Mov(position,mem, rightO.op));
  9005. ReleaseIntermediateOperand(mem);
  9006. (* imaginary part *)
  9007. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9008. Emit(Mov(position,mem, rightO.tag));
  9009. ReleaseIntermediateOperand(mem);
  9010. ReleaseOperand(rightO);
  9011. ReleaseOperand(leftO)
  9012. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9013. OR (leftType IS SyntaxTree.PortType) THEN
  9014. (* rightO := leftO;*)
  9015. Evaluate(right,rightO);
  9016. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9017. Designate(left,leftO);
  9018. IF leftO.mode = ModeReference THEN
  9019. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9020. destination := mem;
  9021. ELSE
  9022. destination := leftO.op;
  9023. END;
  9024. ReleaseOperand(leftO);
  9025. IF destination.mode # IntermediateCode.Undefined THEN
  9026. Emit(Mov(position,destination,rightO.op));
  9027. END;
  9028. ReleaseOperand(rightO);
  9029. ReleaseIntermediateOperand(mem);
  9030. IntermediateCode.InitOperand(destination);
  9031. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9032. Evaluate(right,rightO);
  9033. Designate(left,leftO);
  9034. MakeMemory(mem,leftO.op,addressType,0);
  9035. Emit(Mov(position,mem,rightO.op));
  9036. ReleaseIntermediateOperand(mem);
  9037. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9038. (* delegate *)
  9039. (*
  9040. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9041. *)
  9042. Emit(Mov(position,leftO.tag,rightO.tag));
  9043. END;
  9044. ReleaseOperand(leftO);
  9045. ReleaseOperand(rightO);
  9046. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9047. Designate(right,rightO);
  9048. Designate(left,leftO);
  9049. sizeOp := CopySize(left);
  9050. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9051. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9052. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9053. IF (rightType IS SyntaxTree.StringType) THEN
  9054. CopyString(left,right);
  9055. 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
  9056. Designate(right,rightO);
  9057. Designate(left,leftO);
  9058. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9059. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9060. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9061. ELSE
  9062. HALT(201)
  9063. END;
  9064. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9065. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9066. Designate(right,rightO);
  9067. Designate(left,leftO);
  9068. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9069. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9070. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9071. END;
  9072. AssignMathArray(left,right);
  9073. ELSE
  9074. HALT(200);
  9075. END;
  9076. END Assign;
  9077. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9078. BEGIN
  9079. IF Trace THEN TraceEnter("VisitAssignment") END;
  9080. Assign(x.left,x.right);
  9081. IF Trace THEN TraceExit("VisitAssignment") END;
  9082. END VisitAssignment;
  9083. PROCEDURE EmitCooperativeSwitch;
  9084. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9085. BEGIN
  9086. ASSERT (cooperativeSwitches);
  9087. pc := section.pc;
  9088. IF lastSwitchPC = section.pc THEN RETURN END;
  9089. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9090. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9091. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9092. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9093. INC(statCoopSwitch, section.pc - pc);
  9094. END EmitCooperativeSwitch;
  9095. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9096. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9097. BEGIN
  9098. p0 := communication.left; p1 := communication.right;
  9099. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9100. Evaluate(p0,s0);
  9101. Evaluate(p1,s1);
  9102. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9103. Emit(Push(position,s0.op));
  9104. Emit(Push(position,s1.op));
  9105. (*
  9106. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9107. *)
  9108. IF ~backend.cellsAreObjects THEN
  9109. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9110. END;
  9111. ReleaseOperand(s0);
  9112. ReleaseOperand(s1);
  9113. IF backend.cellsAreObjects THEN
  9114. CallThis(position,"ActiveCellsRuntime","Send",2);
  9115. ELSE
  9116. CallThis(position,ChannelModuleName,"Send",2);
  9117. END;
  9118. (* ----- RECEIVE ------*)
  9119. ELSE
  9120. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9121. tmp := p0; p0 := p1; p1 := tmp;
  9122. END;
  9123. Evaluate(p0,s0);
  9124. Emit(Push(position,s0.op));
  9125. Designate(p1,s1);
  9126. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9127. Emit(Push(position,s1.op));
  9128. (*
  9129. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9130. *)
  9131. IF ~backend.cellsAreObjects THEN
  9132. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9133. END;
  9134. ReleaseOperand(s0);
  9135. ReleaseOperand(s1);
  9136. IF backend.cellsAreObjects THEN
  9137. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9138. ELSE
  9139. CallThis(position,ChannelModuleName,"Receive",2)
  9140. END;
  9141. END;
  9142. END VisitCommunicationStatement;
  9143. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9144. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9145. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9146. VAR true, false: Label; condition, value: BOOLEAN;
  9147. BEGIN
  9148. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9149. IF condition THEN
  9150. true := NewLabel();
  9151. false := NewLabel();
  9152. Condition(if.condition,true,false);
  9153. SetLabel(true);
  9154. StatementSequence(if.statements);
  9155. BrL(end);
  9156. SetLabel(false);
  9157. ELSE
  9158. IF value THEN (* always true *)
  9159. escape := TRUE;
  9160. StatementSequence(if.statements);
  9161. (* no branch necessary -- rest skipped *)
  9162. END;
  9163. END;
  9164. END IfPart;
  9165. BEGIN
  9166. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9167. end := NewLabel();
  9168. elsifs := x.ElsifParts();
  9169. IfPart(x.ifPart);
  9170. FOR i := 0 TO elsifs-1 DO
  9171. IF ~escape THEN
  9172. elsif := x.GetElsifPart(i);
  9173. IfPart(elsif);
  9174. END;
  9175. END;
  9176. IF (x.elsePart # NIL) & ~escape THEN
  9177. StatementSequence(x.elsePart);
  9178. END;
  9179. SetLabel(end);
  9180. IF Trace THEN TraceExit("VisitIfStatement") END;
  9181. END VisitIfStatement;
  9182. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9183. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9184. BEGIN
  9185. (*IF x.variable.type.resolved = x.type.resolved THEN
  9186. (* always true, do nothing *)
  9187. ELSE*)
  9188. Designate(x.variable,res);
  9189. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9190. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9191. END;
  9192. trueL := NewLabel();
  9193. TypeTest(res.tag,x.type,trueL,falseL);
  9194. ReleaseOperand(res);
  9195. SetLabel(trueL);
  9196. StatementSequence(x.statements);
  9197. BrL(endL);
  9198. END WithPart;
  9199. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9200. VAR endL,falseL: Label;i: LONGINT;
  9201. BEGIN
  9202. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9203. endL := NewLabel();
  9204. FOR i := 0 TO x.WithParts()-1 DO
  9205. falseL := NewLabel();
  9206. WithPart(x.GetWithPart(i),falseL,endL);
  9207. SetLabel(falseL);
  9208. END;
  9209. IF x.elsePart = NIL THEN
  9210. IF ~isUnchecked THEN
  9211. EmitTrap(position,WithTrap);
  9212. END;
  9213. ELSE
  9214. StatementSequence(x.elsePart)
  9215. END;
  9216. SetLabel(endL);
  9217. IF Trace THEN TraceExit("VisitWithStatement") END;
  9218. END VisitWithStatement;
  9219. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9220. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9221. out,else: Label; label: Label;
  9222. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9223. symbol: SyntaxTree.Symbol;
  9224. BEGIN
  9225. (*! split case statement into if-elsif statements for large case label lists *)
  9226. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9227. size := x.max-x.min+1;
  9228. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9229. END;
  9230. Evaluate(x.variable,var);
  9231. ReuseCopy(tmp,var.op);
  9232. ReleaseIntermediateOperand(var.op);
  9233. var.op := tmp;
  9234. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9235. Convert(var.op,addressType);
  9236. size := x.max-x.min+1;
  9237. else := NewLabel();
  9238. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9239. (*
  9240. UniqueId(name,module.module,"case",caseId);
  9241. *)
  9242. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9243. Global.GetModuleSegmentedName(module.module, name);
  9244. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9245. symbol := SyntaxTree.NewSymbol(name[1]);
  9246. symbol.SetScope(moduleScope);
  9247. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9248. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9249. section.isCaseTable := TRUE;
  9250. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9251. ReuseCopy(res,var.op);
  9252. ReleaseOperand(var);
  9253. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9254. Emit(Add(position,res,res,jmp));
  9255. IntermediateCode.MakeMemory(res,addressType);
  9256. Emit(Br(position,res));
  9257. ReleaseIntermediateOperand(res);
  9258. out := NewLabel();
  9259. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9260. part := x.GetCasePart(i);
  9261. constant := part.firstConstant;
  9262. label := NewLabel();
  9263. SetLabel(label);
  9264. WHILE(constant # NIL) DO (* case labels for this case part *)
  9265. FOR j := constant.min TO constant.max DO
  9266. fixups[j-x.min] := label;
  9267. END;
  9268. constant := constant.next;
  9269. END;
  9270. StatementSequence(part.statements);
  9271. BrL(out);
  9272. END;
  9273. SetLabel(else);
  9274. FOR i := 0 TO size-1 DO
  9275. IF fixups[i] = NIL THEN
  9276. fixups[i] := else;
  9277. END;
  9278. END;
  9279. IF x.elsePart # NIL THEN
  9280. StatementSequence(x.elsePart);
  9281. ELSIF ~isUnchecked THEN
  9282. EmitTrap(position,CaseTrap);
  9283. END;
  9284. SetLabel(out);
  9285. FOR i := 0 TO size-1 DO
  9286. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9287. section.Emit(Data(position,op));
  9288. END;
  9289. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9290. END VisitCaseStatement;
  9291. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9292. VAR start: Label; true,false: Label;
  9293. BEGIN
  9294. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9295. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9296. start := NewLabel();
  9297. true := NewLabel();
  9298. false := NewLabel();
  9299. SetLabel(start);
  9300. Condition(x.condition,true,false);
  9301. SetLabel(true);
  9302. StatementSequence(x.statements);
  9303. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9304. BrL(start);
  9305. SetLabel(false);
  9306. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9307. END VisitWhileStatement;
  9308. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9309. VAR false,true: Label;
  9310. BEGIN
  9311. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9312. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9313. true := NewLabel();
  9314. false := NewLabel();
  9315. SetLabel(false);
  9316. StatementSequence(x.statements);
  9317. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9318. Condition(x.condition,true,false);
  9319. SetLabel(true);
  9320. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9321. END VisitRepeatStatement;
  9322. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9323. VAR
  9324. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9325. temporaryVariable: SyntaxTree.Variable;
  9326. temporaryVariableDesignator : SyntaxTree.Designator;
  9327. BEGIN
  9328. IF Trace THEN TraceEnter("VisitForStatement") END;
  9329. true := NewLabel();
  9330. false := NewLabel();
  9331. start := NewLabel();
  9332. Assign(x.variable,x.from);
  9333. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9334. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9335. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9336. Assign(temporaryVariableDesignator,x.to);
  9337. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9338. IF by > 0 THEN
  9339. cmp := Scanner.LessEqual
  9340. ELSE
  9341. cmp := Scanner.GreaterEqual
  9342. END;
  9343. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9344. binary.SetType(system.booleanType);
  9345. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9346. SetLabel(start);
  9347. Condition(binary,true,false);
  9348. SetLabel(true);
  9349. StatementSequence(x.statements);
  9350. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9351. binary.SetType(x.variable.type);
  9352. Assign(x.variable,binary);
  9353. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9354. BrL(start);
  9355. SetLabel(false);
  9356. IF Trace THEN TraceExit("VisitForStatement") END;
  9357. END VisitForStatement;
  9358. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9359. VAR prevLoop: Label;
  9360. BEGIN
  9361. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9362. prevLoop := currentLoop;
  9363. currentLoop := NewLabel();
  9364. StatementSequence(x.statements);
  9365. SetLabel(currentLoop);
  9366. currentLoop := prevLoop;
  9367. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9368. END VisitExitableBlock;
  9369. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9370. VAR prevLoop,start: Label;
  9371. BEGIN
  9372. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9373. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9374. start := NewLabel();
  9375. prevLoop := currentLoop;
  9376. SetLabel(start);
  9377. currentLoop := NewLabel();
  9378. StatementSequence(x.statements);
  9379. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9380. BrL(start);
  9381. SetLabel(currentLoop);
  9382. currentLoop := prevLoop;
  9383. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9384. END VisitLoopStatement;
  9385. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9386. VAR outer: SyntaxTree.Statement;
  9387. BEGIN
  9388. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9389. IF locked THEN (* r if we jump out of an exclusive block *)
  9390. outer := x.outer;
  9391. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9392. outer := outer.outer;
  9393. END;
  9394. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9395. Lock(FALSE);
  9396. END;
  9397. END;
  9398. BrL(currentLoop);
  9399. IF Trace THEN TraceExit("VisitExitStatement") END;
  9400. END VisitExitStatement;
  9401. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9402. VAR
  9403. expression, parameterDesignator: SyntaxTree.Expression;
  9404. type, componentType: SyntaxTree.Type;
  9405. res, right: Operand;
  9406. left, mem, reg: IntermediateCode.Operand;
  9407. parameter: SyntaxTree.Parameter;
  9408. procedure: SyntaxTree.Procedure;
  9409. procedureType: SyntaxTree.ProcedureType;
  9410. returnTypeOffset: LONGINT;
  9411. delegate: BOOLEAN;
  9412. map: SymbolMap;
  9413. cc, parametersSize: LONGINT;
  9414. BEGIN
  9415. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9416. expression := x.returnValue;
  9417. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9418. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9419. IF currentIsInline THEN
  9420. IF expression # NIL THEN
  9421. map := currentMapper.Get(NIL);
  9422. IF map # NIL THEN
  9423. Assign(map.to, expression);
  9424. ELSE
  9425. Evaluate(expression,res);
  9426. Emit(Return(position,res.op));
  9427. ReleaseOperand(res);
  9428. END;
  9429. END;
  9430. BrL(currentInlineExit);
  9431. RETURN;
  9432. END;
  9433. IF expression # NIL THEN
  9434. type := expression.type.resolved;
  9435. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9436. IF locked THEN Lock(FALSE) END;
  9437. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9438. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9439. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9440. (* return without structured return parameter *)
  9441. Evaluate(expression,res);
  9442. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9443. IF locked OR profile THEN
  9444. Emit(Push(position,res.op));
  9445. IF delegate THEN HALT(200) END;
  9446. ReleaseOperand(res);
  9447. IF locked THEN Lock(FALSE) END;
  9448. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9449. reg := NewRegisterOperand(res.op.type);
  9450. Emit(Pop(position,reg));
  9451. Emit(Return(position,reg));
  9452. ReleaseIntermediateOperand(reg);
  9453. ELSE
  9454. Emit(Return(position,res.op));
  9455. ReleaseOperand(res);
  9456. END;
  9457. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9458. THEN
  9459. (* return using structured return parameter *)
  9460. 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)
  9461. OR SemanticChecker.IsPointerType(type));
  9462. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9463. parameter :=procedureType.returnParameter;
  9464. ASSERT(parameter # NIL);
  9465. returnTypeOffset := parameter.offsetInBits;
  9466. (*
  9467. IF parameter# NIL THEN
  9468. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9469. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9470. ELSE
  9471. returnTypeOffset := system.offsetFirstParameter
  9472. END;
  9473. *)
  9474. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9475. IF type IS SyntaxTree.RangeType THEN
  9476. (* array range type *)
  9477. Evaluate(expression, right);
  9478. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9479. Emit(Mov(position,mem, right.op)); (* first *)
  9480. ReleaseIntermediateOperand(mem);
  9481. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9482. Emit(Mov(position,mem, right.tag)); (* last *)
  9483. ReleaseIntermediateOperand(mem);
  9484. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9485. Emit(Mov(position,mem, right.extra)); (* step *)
  9486. ReleaseIntermediateOperand(mem);
  9487. ReleaseOperand(right);
  9488. ELSIF type IS SyntaxTree.ComplexType THEN
  9489. Evaluate(expression, right);
  9490. componentType := type(SyntaxTree.ComplexType).componentType;
  9491. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9492. Emit(Mov(position,mem, right.op)); (* real part *)
  9493. ReleaseIntermediateOperand(mem);
  9494. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9495. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9496. ReleaseIntermediateOperand(mem);
  9497. ReleaseOperand(right);
  9498. ELSE (* covers cases: pointer / record / array *)
  9499. parameter := procedureType.returnParameter;
  9500. checker.SetCurrentScope(currentScope);
  9501. ASSERT(parameter # NIL);
  9502. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9503. Assign(parameterDesignator,expression);
  9504. END;
  9505. ReleaseIntermediateOperand(left);
  9506. IF locked THEN Lock(FALSE) END;
  9507. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9508. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9509. parameter := procedureType.returnParameter;
  9510. checker.SetCurrentScope(currentScope);
  9511. IF parameter = NIL THEN
  9512. Error(procedure.position, "structured return of parameter of procedure not found");
  9513. ELSE
  9514. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9515. Assign(parameterDesignator,expression);
  9516. END;
  9517. IF locked THEN Lock(FALSE) END;
  9518. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9519. ELSE
  9520. HALT(200);
  9521. END;
  9522. ELSE
  9523. IF locked THEN Lock(FALSE) END;
  9524. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9525. END;
  9526. IF backend.cooperative THEN
  9527. BrL(exitLabel);
  9528. ELSE
  9529. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9530. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9531. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9532. ELSE
  9533. parametersSize := 0;
  9534. END;
  9535. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9536. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9537. END;
  9538. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9539. END VisitReturnStatement;
  9540. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9541. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9542. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9543. statements: SyntaxTree.StatementSequence;
  9544. name, suffix: SyntaxTree.IdentifierString;
  9545. BEGIN
  9546. Strings.IntToStr(awaitProcCounter,suffix);
  9547. Strings.Concat("@AwaitProcedure",suffix,name);
  9548. identifier := SyntaxTree.NewIdentifier(name);
  9549. INC(awaitProcCounter);
  9550. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9551. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9552. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9553. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9554. procedure.SetAccess(SyntaxTree.Hidden);
  9555. procedure.SetScope(currentScope);
  9556. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9557. procedureType.SetReturnType(system.booleanType);
  9558. procedure.SetType(procedureType);
  9559. body := SyntaxTree.NewBody(x.position,procedureScope);
  9560. procedureScope.SetBody(body);
  9561. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9562. returnStatement.SetReturnValue(x.condition);
  9563. statements := SyntaxTree.NewStatementSequence();
  9564. statements.AddStatement(returnStatement);
  9565. body.SetStatementSequence(statements);
  9566. currentScope.AddProcedure(procedure);
  9567. RETURN procedure
  9568. END MakeAwaitProcedure;
  9569. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9570. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9571. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9572. BEGIN
  9573. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9574. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9575. IF backend.cooperative THEN
  9576. start := NewLabel();
  9577. true := NewLabel();
  9578. false := NewLabel();
  9579. SetLabel(start);
  9580. Condition(x.condition,true,false);
  9581. SetLabel(false);
  9582. PushSelfPointer();
  9583. CallThis(position,"ExclusiveBlocks","Await",1);
  9584. BrL(start);
  9585. SetLabel(true);
  9586. PushSelfPointer();
  9587. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9588. ELSE
  9589. proc := MakeAwaitProcedure(x);
  9590. Emit(Push(position,fp));
  9591. GetCodeSectionNameForSymbol(proc,name);
  9592. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9593. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9594. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9595. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9596. Emit(Result(position,res));
  9597. (*
  9598. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9599. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9600. *)
  9601. InitOperand(result,ModeValue);
  9602. result.op := res;
  9603. label := NewLabel();
  9604. BreqL(label, result.op, SELF.true);
  9605. ReleaseOperand(result);
  9606. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9607. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9608. Emit(Push(position,res));
  9609. Emit(Push(position,fp));
  9610. PushSelfPointer();
  9611. Emit(Push(position,nil));
  9612. CallThis(position,"Objects","Await",4);
  9613. SetLabel(label);
  9614. END;
  9615. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9616. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9617. END VisitAwaitStatement;
  9618. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9619. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9620. BEGIN
  9621. FOR i := 0 TO x.Length() - 1 DO
  9622. statement := x.GetStatement( i );
  9623. Statement(statement);
  9624. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9625. END;
  9626. END StatementSequence;
  9627. PROCEDURE PushSelfPointer;
  9628. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9629. procedureType: SyntaxTree.ProcedureType;
  9630. BEGIN
  9631. scope := currentScope;
  9632. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9633. scope := scope.outerScope;
  9634. END;
  9635. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9636. IF ~newObjectFile THEN
  9637. Symbol(moduleSelf,op);
  9638. IntermediateCode.MakeMemory(op.op,addressType);
  9639. ELSE
  9640. moduleSection := meta.ModuleSection();
  9641. IF backend.cooperative THEN
  9642. moduleOffset := 0;
  9643. ELSE
  9644. moduleOffset := moduleSection.pc;
  9645. END;
  9646. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9647. END;
  9648. ELSE
  9649. GetBaseRegister(op.op,currentScope,scope);
  9650. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9651. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9652. parametersSize := ProcedureParametersSize(system,procedure);
  9653. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9654. IF backend.cooperative THEN
  9655. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9656. END;
  9657. IntermediateCode.MakeMemory(op.op,addressType);
  9658. END;
  9659. Emit(Push(position,op.op));
  9660. ReleaseOperand(op);
  9661. END PushSelfPointer;
  9662. PROCEDURE Lock(lock: BOOLEAN);
  9663. BEGIN
  9664. IF Trace THEN TraceEnter("Lock") END;
  9665. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9666. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9667. ASSERT(modifyAssignmentCounter = 0);
  9668. IF dump # NIL THEN
  9669. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9670. dump.Ln;dump.Update;
  9671. END;
  9672. PushSelfPointer;
  9673. IF backend.cooperative THEN
  9674. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9675. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9676. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9677. END;
  9678. ELSE
  9679. Emit(Push(position,true));
  9680. IF lock THEN CallThis(position,"Objects","Lock",2)
  9681. ELSE CallThis(position,"Objects","Unlock",2);
  9682. END;
  9683. END;
  9684. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9685. IF Trace THEN TraceExit("Lock") END;
  9686. END Lock;
  9687. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9688. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9689. BEGIN
  9690. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9691. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9692. previouslyUnchecked := isUnchecked;
  9693. isUnchecked := isUnchecked OR x.isUnchecked;
  9694. previouslyCooperativeSwitches := cooperativeSwitches;
  9695. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9696. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9697. IF x.statements # NIL THEN
  9698. StatementSequence(x.statements);
  9699. END;
  9700. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9701. isUnchecked := previouslyUnchecked;
  9702. cooperativeSwitches := previouslyCooperativeSwitches;
  9703. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9704. END VisitStatementBlock;
  9705. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9706. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9707. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9708. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9709. procedure: SyntaxTree.Procedure;
  9710. map: SymbolMap;
  9711. cc, parametersSize: LONGINT;
  9712. BEGIN
  9713. scope := currentScope;
  9714. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9715. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9716. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9717. return := emptyOperand;
  9718. IF Trace THEN TraceEnter("VisitCode") END;
  9719. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9720. NEW(in, x.inRules.Length());
  9721. FOR i := 0 TO LEN(in)-1 DO
  9722. statement := x.inRules.GetStatement(i);
  9723. WITH statement: SyntaxTree.Assignment DO
  9724. Evaluate(statement.right, operand);
  9725. result := operand.op;
  9726. NEW(str, 64);
  9727. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9728. in[i] := result; IntermediateCode.SetString(in[i], str);
  9729. ReleaseIntermediateOperand(operand.tag);
  9730. END;
  9731. END;
  9732. ELSE in := NIL
  9733. END;
  9734. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9735. NEW(out, x.outRules.Length());
  9736. FOR i := 0 TO LEN(out)-1 DO
  9737. statement := x.outRules.GetStatement(i);
  9738. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9739. WITH statement: SyntaxTree.Assignment DO
  9740. Designate(statement.left, operand);
  9741. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9742. NEW(str, 64);
  9743. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9744. out[i] := result; IntermediateCode.SetString(out[i], str);
  9745. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9746. |statement: SyntaxTree.ReturnStatement DO
  9747. NEW(str, 64);
  9748. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9749. IF currentIsInline THEN
  9750. map := currentMapper.Get(NIL);
  9751. Designate(map.to, operand);
  9752. IF map.isAddress THEN
  9753. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9754. ELSE
  9755. result := operand.op;
  9756. END;
  9757. (*! only if it does not fit into register
  9758. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9759. *)
  9760. (*Evaluate(map.to, operand);*)
  9761. out[i] := result;
  9762. ELSE
  9763. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9764. END;
  9765. IntermediateCode.SetString(out[i], str);
  9766. ReleaseIntermediateOperand(operand.tag);
  9767. return := out[i];
  9768. ELSE
  9769. END;
  9770. END;
  9771. ELSE out := NIL
  9772. END;
  9773. Emit(Asm(x.position,x.sourceCode, in, out));
  9774. IF in # NIL THEN
  9775. FOR i := 0 TO LEN(in)-1 DO
  9776. ReleaseIntermediateOperand(in[i]);
  9777. END;
  9778. END;
  9779. IF out # NIL THEN
  9780. FOR i := 0 TO LEN(out)-1 DO
  9781. WITH statement: SyntaxTree.Assignment DO
  9782. ReleaseIntermediateOperand(out[i]);
  9783. |statement: SyntaxTree.ReturnStatement DO
  9784. (* release happens below *)
  9785. ELSE
  9786. END;
  9787. statement := x.outRules.GetStatement(i);
  9788. END;
  9789. END;
  9790. IF return.mode # IntermediateCode.Undefined THEN
  9791. IF currentIsInline THEN
  9792. ELSIF SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  9793. Symbol(procedureType.returnParameter, par);
  9794. MakeMemory(mem, par.op, return.type, 0);
  9795. ReleaseOperand(par);
  9796. Emit(Mov(position, mem, return));
  9797. ReleaseIntermediateOperand(mem);
  9798. ELSE
  9799. Emit(Return(position,return));
  9800. END;
  9801. ReleaseIntermediateOperand(return);
  9802. IF currentIsInline THEN RETURN END;
  9803. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9804. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9805. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9806. ELSE
  9807. parametersSize := 0;
  9808. END;
  9809. EmitLeave(section, position,NIL, cc);
  9810. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9811. END;
  9812. IF Trace THEN TraceExit("VisitCode") END;
  9813. END VisitCode;
  9814. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9815. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9816. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9817. const, call: IntermediateCode.Operand;
  9818. parameterDesignator: SyntaxTree.Expression;
  9819. saved: RegisterEntry;
  9820. name: Basic.SegmentedName;
  9821. BEGIN
  9822. IF Trace THEN TraceEnter("ParameterCopies") END;
  9823. parameter := x.firstParameter;
  9824. WHILE parameter # NIL DO
  9825. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9826. type := parameter.type.resolved;
  9827. IF IsOpenArray(type) THEN
  9828. VisitParameter(parameter);
  9829. op := result;
  9830. IF backend.cooperative & parameter.NeedsTrace() THEN
  9831. length := GetArrayLength(type, op.tag);
  9832. size := NewRegisterOperand(addressType);
  9833. base := ArrayBaseType(type);
  9834. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9835. Emit(Mul(position, size, length, const));
  9836. dst := NewRegisterOperand (addressType);
  9837. Emit(Mov(position, dst, size));
  9838. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9839. Emit(Sub(position,dst,sp,dst));
  9840. Emit(And(position,dst,dst,const));
  9841. Emit(Mov(position,sp,dst));
  9842. par := fp;
  9843. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9844. IntermediateCode.InitImmediate(null, byteType, 0);
  9845. Emit(Fill(position, dst, size, null));
  9846. ReleaseIntermediateOperand(dst);
  9847. ReleaseIntermediateOperand(length);
  9848. SaveRegisters();ReleaseUsedRegisters(saved);
  9849. (* register dst has been freed before SaveRegisters already *)
  9850. base := ArrayBaseType(type);
  9851. (* assign method of open array *)
  9852. IF base.IsRecordType() THEN
  9853. Emit (Push(position, length));
  9854. Emit (Push(position, dst));
  9855. Emit (Push(position, op.op));
  9856. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9857. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9858. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9859. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9860. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9861. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9862. Emit (Push(position,length));
  9863. Emit (Push(position, dst));
  9864. Emit (Push(position, length));
  9865. Emit (Push(position, op.op));
  9866. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9867. ELSE
  9868. Emit (Push(position, length));
  9869. Emit (Push(position, dst));
  9870. Emit (Push(position, length));
  9871. Emit (Push(position, op.op));
  9872. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9873. ASSERT(ArrayBaseType(type).IsPointer());
  9874. END;
  9875. RestoreRegisters(saved);
  9876. ELSE
  9877. temp := GetDynamicSize(type,op.tag);
  9878. ReuseCopy(size,temp);
  9879. ReleaseIntermediateOperand(temp);
  9880. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9881. Emit(Sub(position,size,sp,size));
  9882. Emit(And(position,size,size,const));
  9883. Emit(Mov(position,sp,size));
  9884. par := fp;
  9885. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9886. ReleaseIntermediateOperand(size);
  9887. size := GetDynamicSize(type,op.tag);
  9888. END;
  9889. Emit(Copy(position,sp,op.op,size));
  9890. ReleaseIntermediateOperand(size);
  9891. ReleaseOperand(op);
  9892. IntermediateCode.MakeMemory(par,addressType);
  9893. Emit(Mov(position,par,sp));
  9894. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9895. checker.SetCurrentScope(currentScope);
  9896. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9897. Assign(parameterDesignator,parameterDesignator);
  9898. END;
  9899. END;
  9900. parameter := parameter.nextParameter;
  9901. END;
  9902. IF Trace THEN TraceExit("ParameterCopies") END;
  9903. END ParameterCopies;
  9904. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9905. VAR x: SyntaxTree.Variable;
  9906. BEGIN
  9907. x := scope.firstVariable;
  9908. WHILE x # NIL DO
  9909. InitVariable(x);
  9910. x := x.nextVariable;
  9911. END;
  9912. END InitVariables;
  9913. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9914. BEGIN
  9915. IF (symbol # NIL) THEN
  9916. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9917. ELSE
  9918. RETURN 0
  9919. END;
  9920. END GetFingerprint;
  9921. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9922. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9923. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9924. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9925. name: Basic.SegmentedName;
  9926. offset: LONGINT;
  9927. BEGIN
  9928. IF Trace THEN TraceEnter("Body") END;
  9929. ReleaseUsedRegisters(saved); (* just in case ... *)
  9930. section := ir;
  9931. exitLabel := NewLabel ();
  9932. IF moduleBody THEN moduleBodySection := section END;
  9933. IF ir.comments # NIL THEN
  9934. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9935. commentPrintout.SingleStatement(TRUE);
  9936. dump := ir.comments;
  9937. ELSE
  9938. commentPrintout := NIL;
  9939. dump := NIL;
  9940. END;
  9941. prevScope := currentScope;
  9942. currentScope := scope;
  9943. lastSwitchPC := 0;
  9944. cooperativeSwitches := backend.cooperative;
  9945. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9946. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9947. IF x # NIL THEN
  9948. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9949. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9950. IF moduleBody THEN
  9951. ProfilerInit();
  9952. ELSE
  9953. Basic.SegmentedNameToString(ir.name, string);
  9954. ProfilerAddProcedure(numberProcedures,string);
  9955. ProfilerEnterExit(numberProcedures,TRUE);
  9956. END;
  9957. END;
  9958. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9959. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9960. END;
  9961. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9962. IF moduleBody & ~newObjectFile THEN
  9963. InitVariables(moduleScope)
  9964. END;
  9965. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9966. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9967. IF procedure = cellScope.bodyProcedure THEN
  9968. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9969. StaticCallOperand(op, cellScope.constructor);
  9970. Emit(Call(position,op.op,0));
  9971. END;
  9972. END;
  9973. END;
  9974. ParameterCopies(procedureType);
  9975. InitVariables(scope);
  9976. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9977. GetCodeSectionNameForSymbol(procedure, name);
  9978. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9979. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9980. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9981. IntermediateCode.SetOffset(right,offset); (* tag *)
  9982. IntermediateCode.InitMemory(left,addressType,fp,0);
  9983. Emit(Mov(position, left, right));
  9984. END;
  9985. IF x.code = NIL THEN
  9986. VisitStatementBlock(x);
  9987. ELSE
  9988. VisitCode(x.code)
  9989. END;
  9990. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9991. ir.SetFinally(ir.pc);
  9992. StatementSequence(x.finally)
  9993. END;
  9994. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9995. IF ~backend.cooperative THEN
  9996. ProfilerEnterExit(numberProcedures,FALSE);
  9997. END;
  9998. INC(numberProcedures);
  9999. END;
  10000. END;
  10001. IF backend.cooperative THEN
  10002. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  10003. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  10004. END;
  10005. IF x # NIL THEN
  10006. SELF.position := x.position;
  10007. END;
  10008. CheckRegistersFree();
  10009. ASSERT(modifyAssignmentCounter = 0);
  10010. currentScope := prevScope;
  10011. IF Trace THEN TraceExit("Body") END;
  10012. END Body;
  10013. END ImplementationVisitor;
  10014. MetaDataGenerator=OBJECT
  10015. VAR
  10016. implementationVisitor: ImplementationVisitor;
  10017. declarationVisitor: DeclarationVisitor;
  10018. module: Sections.Module;
  10019. moduleName: ARRAY 128 OF CHAR;
  10020. moduleNamePool: Basic.HashTableInt;
  10021. moduleNamePoolSection: IntermediateCode.Section;
  10022. modulePointerSection: IntermediateCode.Section;
  10023. modulePointerSizePC: LONGINT;
  10024. modulePointerSectionOffset: LONGINT;
  10025. modulePointers: LONGINT;
  10026. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10027. RecordBaseOffset: LONGINT;
  10028. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10029. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10030. TypeTags: LONGINT; (* type extension level support *)
  10031. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10032. patchInfoPC: LONGINT;
  10033. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10034. BEGIN
  10035. IF implementationVisitor.backend.cooperative THEN
  10036. TypeTags := MAX(LONGINT);
  10037. BaseTypesTableOffset := 0;
  10038. MethodTableOffset := 2;
  10039. TypeRecordBaseOffset := 0;
  10040. RecordBaseOffset := 0;
  10041. ELSIF simple THEN
  10042. TypeTags := 3; (* only 3 extensions allowed *)
  10043. BaseTypesTableOffset := 1;
  10044. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10045. TypeRecordBaseOffset := 0;
  10046. RecordBaseOffset := 1;
  10047. ELSE
  10048. TypeTags := 16;
  10049. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10050. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10051. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10052. RecordBaseOffset := 8;
  10053. END;
  10054. SELF.simple := simple;
  10055. SELF.implementationVisitor := implementationVisitor;
  10056. SELF.declarationVisitor := declarationVisitor;
  10057. implementationVisitor.meta := SELF;
  10058. declarationVisitor.meta := SELF;
  10059. END InitMetaDataGenerator;
  10060. PROCEDURE SetModule(module: Sections.Module);
  10061. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10062. BEGIN
  10063. SELF.module := module;
  10064. Global.GetModuleName(module.module,moduleName);
  10065. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10066. NEW(moduleNamePool, 32);
  10067. (*! require GC protection *)
  10068. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10069. name := "Heaps.AnyPtr";
  10070. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10071. (* set base pointer *)
  10072. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10073. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10074. modulePointers := 0;
  10075. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10076. AddPointer(moduleNamePoolSection, namePoolOffset);
  10077. END;
  10078. END SetModule;
  10079. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10080. BEGIN
  10081. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10082. INC(modulePointers);
  10083. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10084. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10085. END AddPointer;
  10086. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10087. BEGIN
  10088. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10089. END GetTypeRecordBaseOffset;
  10090. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10091. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10092. BEGIN
  10093. INC(dataAdrOffset,6);
  10094. Info(section,"headerAdr");
  10095. Address(section,0);
  10096. Info(section,"typeDesc");
  10097. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10098. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10099. NamedSymbol(section, name, symbol, 0, offset);
  10100. Info(section,"mark: LONGINT;");
  10101. Longint(section,-1);
  10102. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10103. Info(section,"dataAdr-: ADDRESS");
  10104. Symbol(section,section, dataAdrOffset,0);
  10105. Info(section,"size-: SIZE");
  10106. Address(section,0);
  10107. Info(section,"nextRealtime: HeapBlock;");
  10108. Address(section,0);
  10109. END HeapBlock;
  10110. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10111. VAR i: LONGINT;
  10112. BEGIN
  10113. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10114. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10115. Info(section,"count*: LONGINT");
  10116. Longint(section,0);
  10117. Info(section,"locked*: BOOLEAN");
  10118. Longint(section,0);
  10119. Info(section,"awaitingLock*: ProcessQueue");
  10120. Address(section,0);
  10121. Address(section,0);
  10122. Info(section,"awaitingCond*: ProcessQueue");
  10123. Address(section,0);
  10124. Address(section,0);
  10125. Info(section,"lockedBy*: ANY");
  10126. Address(section,0);
  10127. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10128. Longint(section,1);
  10129. FOR i := 2 TO 6 DO
  10130. Longint(section,0);
  10131. END;
  10132. Info(section,"lock*: ANY");
  10133. Address(section,0);
  10134. END ProtectedHeapBlock;
  10135. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10136. BEGIN
  10137. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10138. END Info;
  10139. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10140. VAR op: IntermediateCode.Operand;
  10141. BEGIN
  10142. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10143. section.Emit(Data(Basic.invalidPosition,op));
  10144. END Address;
  10145. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10146. VAR op: IntermediateCode.Operand;
  10147. BEGIN
  10148. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10149. section.Emit(Data(Basic.invalidPosition,op));
  10150. END Size;
  10151. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10152. VAR op: IntermediateCode.Operand;
  10153. BEGIN
  10154. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10155. section.Emit(Data(Basic.invalidPosition,op));
  10156. END Set;
  10157. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10158. VAR op: IntermediateCode.Operand;
  10159. BEGIN
  10160. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10161. section.Emit(Data(Basic.invalidPosition,op));
  10162. END Longint;
  10163. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10164. VAR op,noOperand: IntermediateCode.Operand;
  10165. BEGIN
  10166. IntermediateCode.InitOperand(noOperand);
  10167. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10168. section.PatchOperands(pc,op,noOperand,noOperand);
  10169. END PatchAddress;
  10170. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10171. VAR op,noOperand: IntermediateCode.Operand;
  10172. BEGIN
  10173. IntermediateCode.InitOperand(noOperand);
  10174. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10175. section.PatchOperands(pc,op,noOperand,noOperand);
  10176. END PatchSize;
  10177. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10178. VAR op,noOperand: IntermediateCode.Operand;
  10179. BEGIN
  10180. IntermediateCode.InitOperand(noOperand);
  10181. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10182. section.PatchOperands(pc,op,noOperand,noOperand);
  10183. END PatchLongint;
  10184. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10185. VAR op, noOperand: IntermediateCode.Operand;
  10186. BEGIN
  10187. IntermediateCode.InitOperand(noOperand);
  10188. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10189. section.PatchOperands(pc,op,noOperand,noOperand);
  10190. END PatchSymbol;
  10191. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10192. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10193. BEGIN
  10194. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10195. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10196. section.Emit(Data(Basic.invalidPosition,op));
  10197. END Boolean;
  10198. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10199. VAR op: IntermediateCode.Operand;
  10200. BEGIN
  10201. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10202. section.Emit(Data(Basic.invalidPosition,op));
  10203. END Char;
  10204. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10205. VAR op: IntermediateCode.Operand;
  10206. BEGIN
  10207. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10208. section.Emit(Data(Basic.invalidPosition,op));
  10209. END Integer;
  10210. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10211. VAR i: LONGINT;
  10212. BEGIN
  10213. Info(section,str);
  10214. i := 0;
  10215. WHILE(str[i] # 0X) DO
  10216. Char(section,str[i]);
  10217. INC(i);
  10218. END;
  10219. Char(section,0X);
  10220. END String;
  10221. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10222. VAR s: Basic.SectionName;
  10223. BEGIN
  10224. StringPool.GetString(str, s);
  10225. String(section, s);
  10226. END String0;
  10227. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10228. VAR op: IntermediateCode.Operand;
  10229. BEGIN
  10230. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10231. IntermediateCode.SetOffset(op,realOffset);
  10232. section.Emit(Data(Basic.invalidPosition,op));
  10233. END NamedSymbol;
  10234. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10235. VAR op: IntermediateCode.Operand;
  10236. BEGIN
  10237. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10238. IntermediateCode.SetOffset(op,realOffset);
  10239. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10240. END NamedSymbolAt;
  10241. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10242. BEGIN
  10243. IF symbol= NIL THEN
  10244. Address( section, realOffset);
  10245. ASSERT(virtualOffset = 0);
  10246. ELSE
  10247. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10248. END;
  10249. END Symbol;
  10250. (* OutPointers delivers
  10251. {pointerOffset}
  10252. *)
  10253. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10254. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10255. BEGIN
  10256. type := type.resolved;
  10257. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10258. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10259. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10260. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10261. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10262. ELSIF type IS SyntaxTree.PointerType THEN
  10263. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10264. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10265. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10266. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10267. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10268. ELSIF (type IS SyntaxTree.RecordType) THEN
  10269. (* never treat a record like a pointer, even if the pointer field is set! *)
  10270. WITH type: SyntaxTree.RecordType DO
  10271. base := type.GetBaseRecord();
  10272. IF base # NIL THEN
  10273. Pointers(offset,symbol,section, base,numberPointers);
  10274. END;
  10275. variable := type.recordScope.firstVariable;
  10276. WHILE(variable # NIL) DO
  10277. IF ~(variable.untraced) THEN
  10278. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10279. END;
  10280. variable := variable.nextVariable;
  10281. END;
  10282. END;
  10283. ELSIF (type IS SyntaxTree.CellType) THEN
  10284. WITH type: SyntaxTree.CellType DO
  10285. base := type.GetBaseRecord();
  10286. IF base # NIL THEN
  10287. Pointers(offset,symbol,section, base,numberPointers);
  10288. END;
  10289. variable := type.cellScope.firstVariable;
  10290. WHILE(variable # NIL) DO
  10291. IF ~(variable.untraced) THEN
  10292. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10293. END;
  10294. variable := variable.nextVariable;
  10295. END;
  10296. property := type.firstProperty;
  10297. WHILE(property # NIL) DO
  10298. IF ~(property.untraced) THEN
  10299. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10300. END;
  10301. property := property.nextProperty;
  10302. END;
  10303. parameter := type.firstParameter;
  10304. WHILE(parameter # NIL) DO
  10305. IF ~(parameter.untraced) THEN
  10306. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10307. END;
  10308. parameter := parameter.nextParameter;
  10309. END;
  10310. END;
  10311. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10312. WITH type: SyntaxTree.ArrayType DO
  10313. IF type.form= SyntaxTree.Static THEN
  10314. n := type.staticLength;
  10315. base := type.arrayBase.resolved;
  10316. WHILE(base IS SyntaxTree.ArrayType) DO
  10317. type := base(SyntaxTree.ArrayType);
  10318. n := n* type.staticLength;
  10319. base := type.arrayBase.resolved;
  10320. END;
  10321. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10322. IF SemanticChecker.ContainsPointer(base) THEN
  10323. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10324. FOR i := 0 TO n-1 DO
  10325. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10326. END;
  10327. END;
  10328. ELSE
  10329. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10330. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10331. END;
  10332. END;
  10333. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10334. WITH type: SyntaxTree.MathArrayType DO
  10335. IF type.form = SyntaxTree.Static THEN
  10336. n := type.staticLength;
  10337. base := type.arrayBase.resolved;
  10338. WHILE(base IS SyntaxTree.MathArrayType) DO
  10339. type := base(SyntaxTree.MathArrayType);
  10340. n := n* type.staticLength;
  10341. base := type.arrayBase.resolved;
  10342. END;
  10343. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10344. IF SemanticChecker.ContainsPointer(base) THEN
  10345. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10346. FOR i := 0 TO n-1 DO
  10347. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10348. END;
  10349. END;
  10350. ELSE
  10351. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10352. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10353. END
  10354. END;
  10355. (* ELSE no pointers in type *)
  10356. END;
  10357. END Pointers;
  10358. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10359. VAR position,i: LONGINT; ch: CHAR;
  10360. BEGIN
  10361. IF pool.Has(index) THEN
  10362. RETURN pool.GetInt(index)
  10363. ELSE
  10364. position := source.pc;
  10365. pool.PutInt(index, position);
  10366. Info(source, name);
  10367. i := 0;
  10368. REPEAT
  10369. ch := name[i]; INC(i);
  10370. Char( source, ch);
  10371. UNTIL ch = 0X;
  10372. END;
  10373. RETURN position;
  10374. END EnterDynamicName;
  10375. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10376. VAR name: Basic.SectionName; position: LONGINT;
  10377. BEGIN
  10378. IF pool.Has(index) THEN
  10379. RETURN pool.GetInt(index)
  10380. ELSE
  10381. StringPool.GetString(index, name);
  10382. position := EnterDynamicName(source,name,index, pool);
  10383. END;
  10384. RETURN position;
  10385. END DynamicName;
  10386. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10387. VAR section: IntermediateCode.Section;
  10388. BEGIN
  10389. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10390. IF implementationVisitor.backend.cooperative THEN
  10391. Info(section, "TypeDescriptor");
  10392. Basic.ToSegmentedName("BaseTypes.Array", name);
  10393. NamedSymbol(section, name,NIL, 0, 0);
  10394. BasePointer(section);
  10395. offset := 0;
  10396. ELSE
  10397. HeapBlock(mName,typeName,section,2);
  10398. Info(section, "HeapBlock");
  10399. (*
  10400. Symbol(section,section,2,0);
  10401. *)
  10402. Address(section,0); (* empty such that GC does not go on traversing *)
  10403. Info(section, "TypeDescriptor");
  10404. Address(section,0);
  10405. offset := section.pc;
  10406. END;
  10407. RETURN section
  10408. END NamedBlock;
  10409. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10410. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10411. BEGIN
  10412. COPY(moduleName,name);
  10413. Strings.Append(name,suffix);
  10414. Basic.ToSegmentedName(name, pooledName);
  10415. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10416. END Block;
  10417. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10418. VAR name: Basic.SegmentedName;
  10419. BEGIN
  10420. Info(source,"ArrayHeader");
  10421. IF implementationVisitor.backend.cooperative THEN
  10422. sizePC := source.pc;
  10423. Address(source,0);
  10424. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10425. IF baseType # "" THEN
  10426. Basic.ToSegmentedName(baseType, name);
  10427. NamedSymbol(source, name,NIL, 0, 0);
  10428. ELSE
  10429. Address(source,0);
  10430. END;
  10431. Address(source,0);
  10432. ELSE
  10433. Address(source,0);
  10434. Address(source,0);
  10435. (* first pointer for GC *)
  10436. IF hasPointer THEN
  10437. (* points to first element in the array, this is NOT the base type descriptor *)
  10438. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10439. ELSE
  10440. Address(source,0);
  10441. END;
  10442. sizePC := source.pc;
  10443. Address(source,0);
  10444. Info(source,"array data");
  10445. END;
  10446. END ArrayBlock;
  10447. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10448. BEGIN
  10449. IF implementationVisitor.backend.cooperative THEN
  10450. PatchSize(section, pc, size);
  10451. PatchSize(section, pc + 3, size);
  10452. ELSE
  10453. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10454. PatchSize(section, pc, size);
  10455. END;
  10456. END PatchArray;
  10457. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10458. VAR
  10459. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10460. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10461. poolMap: Basic.HashTableInt;
  10462. namePool: IntermediateCode.Section;
  10463. namePoolOffset: LONGINT;
  10464. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10465. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10466. BEGIN
  10467. Basic.SegmentedNameToString(s1.name,n1);
  10468. Basic.SegmentedNameToString(s2.name,n2);
  10469. index := 0;
  10470. ch1 := n1[index];
  10471. ch2 := n2[index];
  10472. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10473. INC(index);
  10474. ch1 := n1[index];
  10475. ch2 := n2[index];
  10476. END;
  10477. RETURN ch1 < ch2;
  10478. END Compare;
  10479. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10480. VAR
  10481. i, j: LONGINT;
  10482. x, t: Sections.Section;
  10483. BEGIN
  10484. IF lo < hi THEN
  10485. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10486. WHILE i <= j DO
  10487. WHILE Compare(list[i], x) DO INC(i) END;
  10488. WHILE Compare(x, list[j]) DO DEC(j) END;
  10489. IF i <= j THEN
  10490. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10491. INC(i); DEC(j)
  10492. END
  10493. END;
  10494. IF lo < j THEN QuickSort(list, lo, j) END;
  10495. IF i < hi THEN QuickSort(list, i, hi) END
  10496. END;
  10497. END QuickSort;
  10498. (*
  10499. ExportDesc* = RECORD
  10500. fp*: ADDRESS;
  10501. name* {UNTRACED}: DynamicName;
  10502. adr*: ADDRESS;
  10503. exports*: LONGINT;
  10504. dsc* {UNTRACED}: ExportArray
  10505. END;
  10506. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10507. *)
  10508. PROCEDURE ExportDesc2(
  10509. source: IntermediateCode.Section;
  10510. namePool: IntermediateCode.Section;
  10511. fingerPrinter: FingerPrinter.FingerPrinter;
  10512. symbol: Sections.Section;
  10513. name: StringPool.Index;
  10514. VAR patchAdr: LONGINT
  10515. ): BOOLEAN;
  10516. VAR fingerPrint: SyntaxTree.FingerPrint;
  10517. BEGIN
  10518. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10519. & (symbol.type # Sections.InlineCodeSection)
  10520. THEN
  10521. *)
  10522. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10523. & (symbol.type # Sections.InlineCodeSection))
  10524. THEN
  10525. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10526. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10527. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10528. Longint(source,fingerPrint.shallow);
  10529. ELSE
  10530. Longint(source, 0);
  10531. END;
  10532. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10533. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10534. Symbol(source, symbol,0,0);
  10535. patchAdr := source.pc;
  10536. Longint(source, 0);
  10537. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10538. Address(source,0);
  10539. END;
  10540. RETURN TRUE
  10541. ELSE
  10542. RETURN FALSE
  10543. END;
  10544. END ExportDesc2;
  10545. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10546. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10547. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10548. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10549. nextPatch: LONGINT;
  10550. TYPE
  10551. Scope = RECORD
  10552. elements: LONGINT;
  10553. gelements: LONGINT;
  10554. section: IntermediateCode.Section;
  10555. patchAdr: LONGINT;
  10556. arraySizePC: LONGINT;
  10557. beginPC: LONGINT; (* current scope start pc *)
  10558. END;
  10559. BEGIN
  10560. Basic.InitSegmentedName(prev);
  10561. olevel := -1;
  10562. scopes[0].section := source;
  10563. scopes[0].arraySizePC := MIN(LONGINT);
  10564. FOR s := 0 TO LEN(sections)-1 DO
  10565. symbol := sections[s];
  10566. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10567. this := sections[s].name;
  10568. level := 0;
  10569. WHILE (this[level] > 0) DO
  10570. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10571. INC(level);
  10572. END;
  10573. WHILE level < olevel DO
  10574. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10575. IF olevel > 0 THEN
  10576. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10577. nextPatch := scopes[olevel-1].patchAdr+1;
  10578. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10579. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10580. END;
  10581. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10582. DEC(olevel);
  10583. END;
  10584. IF (this[level] > 0) THEN
  10585. IF level > olevel THEN
  10586. (*TRACE("opening",level); *)
  10587. IF scopes[level].section = NIL THEN
  10588. arrayName := ".@ExportArray";
  10589. Strings.AppendInt(arrayName, level);
  10590. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10591. AddPointer(scopes[level].section,offset);
  10592. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10593. END;
  10594. scopes[level].beginPC := scopes[level].section.pc;
  10595. olevel := level;
  10596. scopes[olevel].elements := 0;
  10597. END;
  10598. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10599. sym := sections[s];
  10600. ELSE
  10601. sym := NIL;
  10602. END;
  10603. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10604. THEN
  10605. INC(scopes[olevel].elements);
  10606. END;
  10607. (* enter string in scope *)
  10608. INC(level);
  10609. END;
  10610. END;
  10611. Basic.SegmentedNameToString(this, name);
  10612. prev := this;
  10613. END;
  10614. END;
  10615. WHILE 0 <= olevel DO
  10616. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10617. IF olevel > 0 THEN
  10618. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10619. nextPatch := scopes[olevel-1].patchAdr+1;
  10620. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10621. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10622. END;
  10623. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10624. DEC(olevel);
  10625. END;
  10626. level := 0;
  10627. WHILE (level < LEN(scopes)) DO
  10628. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10629. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10630. END;
  10631. INC(level);
  10632. END;
  10633. END Export;
  10634. BEGIN
  10635. NEW(fingerPrinter);
  10636. NEW(poolMap, 64);
  10637. (* 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 *)
  10638. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10639. NEW(sectionArray, module.allSections.Length());
  10640. FOR i := 0 TO module.allSections.Length() - 1 DO
  10641. section := module.allSections.GetSection(i);
  10642. sectionArray[i] := section;
  10643. END;
  10644. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10645. Export(sectionArray^);
  10646. END ExportDesc;
  10647. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10648. VAR
  10649. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10650. BEGIN
  10651. Info(source, "exception table offsets array descriptor");
  10652. size := 0;
  10653. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10654. Info(source, "exception table content");
  10655. FOR i := 0 TO module.allSections.Length() - 1 DO
  10656. p := module.allSections.GetSection(i);
  10657. IF p.type = Sections.CodeSection THEN
  10658. finallyPC := p(IntermediateCode.Section).finally;
  10659. IF finallyPC>=0 THEN
  10660. Symbol( source, p, 0,0);
  10661. Symbol( source, p, finallyPC, 0);
  10662. Symbol( source, p, finallyPC,0);
  10663. INC(size);
  10664. END;
  10665. END
  10666. END;
  10667. PatchArray(source,sizePC,size);
  10668. END ExceptionArray;
  10669. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10670. VAR i: LONGINT; ch: CHAR;
  10671. BEGIN
  10672. i := 0;
  10673. REPEAT
  10674. ch := name[i]; INC(i);
  10675. Char( section, ch);
  10676. UNTIL ch = 0X;
  10677. WHILE i < 32 DO
  10678. Char( section, 0X); INC(i);
  10679. END;
  10680. END Name;
  10681. PROCEDURE References(section: IntermediateCode.Section);
  10682. CONST
  10683. sfTypeNone = 0X;
  10684. sfTypeCHAR = 01X;
  10685. sfTypeCHAR8 = 02X;
  10686. sfTypeCHAR16 = 03X;
  10687. sfTypeCHAR32 = 04X;
  10688. sfTypeRANGE = 05X;
  10689. sfTypeSHORTINT = 06X;
  10690. sfTypeINTEGER = 07X;
  10691. sfTypeLONGINT = 08X;
  10692. sfTypeHUGEINT = 09X;
  10693. sfTypeWORD = 0AX;
  10694. sfTypeLONGWORD = 0BX;
  10695. sfTypeSIGNED8 = 0CX;
  10696. sfTypeSIGNED16 = 0DX;
  10697. sfTypeSIGNED32 = 0EX;
  10698. sfTypeSIGNED64 = 0FX;
  10699. sfTypeUNSIGNED8 = 10X;
  10700. sfTypeUNSIGNED16 = 11X;
  10701. sfTypeUNSIGNED32 = 12X;
  10702. sfTypeUNSIGNED64 = 13X;
  10703. sfTypeREAL = 14X;
  10704. sfTypeLONGREAL = 15X;
  10705. sfTypeCOMPLEX = 16X;
  10706. sfTypeLONGCOMPLEX = 17X;
  10707. sfTypeBOOLEAN = 18X;
  10708. sfTypeSET = 19X;
  10709. sfTypeANY = 1AX;
  10710. sfTypeOBJECT = 1BX;
  10711. sfTypeBYTE = 1CX;
  10712. sfTypeADDRESS = 1DX;
  10713. sfTypeSIZE = 1EX;
  10714. sfTypeIndirect = 1FX;
  10715. sfTypeRecord = 20X;
  10716. sfTypePointerToRecord = 21X;
  10717. sfTypePointerToArray = 22X;
  10718. sfTypeOpenArray = 23X;
  10719. sfTypeStaticArray = 24X;
  10720. sfTypeDynamicArray = 25X;
  10721. sfTypeMathStaticArray = 26X;
  10722. sfTypeMathOpenArray = 27X;
  10723. sfTypeMathTensor = 28X;
  10724. sfTypeDelegate = 29X;
  10725. sfTypeENUM = 2AX;
  10726. sfTypeCELL = 2BX;
  10727. sfTypePORT = 2CX;
  10728. sfIN = 0X;
  10729. sfOUT = 1X;
  10730. flagDelegate = 0;
  10731. flagConstructor = 1;
  10732. (* variable / parameter addressing modes *)
  10733. sfAbsolute = 0X; (* global vars *)
  10734. sfRelative = 1X; (* variables, value parameters *)
  10735. sfIndirect = 2X; (* var parameters *)
  10736. sfScopeBegin = 0F0X;
  10737. sfScopeEnd = 0F1X;
  10738. sfProcedure = 0F2X;
  10739. sfVariable = 0F3X;
  10740. sfTypeDeclaration = 0F4X;
  10741. sfModule = 0FFX;
  10742. RefInfo = TRUE;
  10743. VAR
  10744. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10745. indirectTypes: Basic.HashTable;
  10746. PROCEDURE CurrentIndex(): SIZE;
  10747. VAR i: LONGINT;
  10748. BEGIN
  10749. FOR i := startPC TO section.pc -1 DO
  10750. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10751. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10752. END;
  10753. startPC := section.pc;
  10754. RETURN lastOffset;
  10755. END CurrentIndex;
  10756. (*
  10757. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10758. Module = sfModule prevSymbol:SIZE name:String Scope.
  10759. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10760. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10761. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10762. Type =
  10763. sfTypePointerToRecord
  10764. | sfTypePointerToArray Type
  10765. | sfTypeOpenArray Type
  10766. | sfTypeDynamicArray Type
  10767. | sfTypeStaticArray length:SIZE Type
  10768. | sfTypeMathOpenArray Type
  10769. | sfTypeMathStaticArray length:SIZE Type
  10770. | sfTypeMathTensor Type
  10771. | sfTypeRecord tdAdr:ADDRESS
  10772. | sfTypeDelegate {Parameter} return:Type
  10773. | sfTypePort (sfIN | sfOUT)
  10774. | sfTypeBOOLEAN
  10775. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10776. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10777. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10778. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10779. | sfTypeWORD | sfTypeLONGWORD
  10780. | sfTypeREAL | sfTypeLONGREAL
  10781. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10782. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10783. | sfTypeIndirect offset:SIZE.
  10784. *)
  10785. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10786. VAR offset: SIZE;
  10787. BEGIN
  10788. IF indirectTypes.Has(type) THEN
  10789. offset := indirectTypes.GetInt(type);
  10790. Char(section, sfTypeIndirect);
  10791. Size(section, offset);
  10792. RETURN TRUE;
  10793. ELSE
  10794. indirectTypes.PutInt(type, CurrentIndex());
  10795. RETURN FALSE;
  10796. END;
  10797. END Indirect;
  10798. PROCEDURE NType(type: SyntaxTree.Type);
  10799. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10800. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10801. BEGIN
  10802. IF type = NIL THEN
  10803. Char(section, sfTypeNone)
  10804. ELSE
  10805. type := type.resolved;
  10806. size := type.sizeInBits;
  10807. WITH type:SyntaxTree.PointerType DO
  10808. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10809. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10810. Char(section, sfTypePointerToRecord);
  10811. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10812. ELSE
  10813. IF RefInfo THEN Info(section,"PointerToArray") END;
  10814. Char(section, sfTypePointerToArray);
  10815. NType(type.pointerBase);
  10816. END;
  10817. | type: SyntaxTree.ArrayType DO
  10818. IF ~Indirect(type) THEN
  10819. IF type.form = SyntaxTree.Open THEN
  10820. IF RefInfo THEN Info(section,"OpenArray") END;
  10821. Char(section, sfTypeOpenArray);
  10822. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10823. IF RefInfo THEN Info(section,"DynamicArray") END;
  10824. Char(section, sfTypeDynamicArray);
  10825. ELSIF type.form = SyntaxTree.Static THEN
  10826. IF RefInfo THEN Info(section,"StaticArray") END;
  10827. Char(section, sfTypeStaticArray);
  10828. Size(section, type.staticLength);
  10829. ELSE
  10830. HALT(100);
  10831. END;
  10832. NType(type.arrayBase);
  10833. END;
  10834. | type: SyntaxTree.MathArrayType DO
  10835. IF ~Indirect(type) THEN
  10836. IF type.form = SyntaxTree.Open THEN
  10837. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10838. Char(section, sfTypeMathOpenArray);
  10839. ELSIF type.form = SyntaxTree.Static THEN
  10840. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10841. Char(section, sfTypeMathStaticArray);
  10842. Size(section, type.staticLength);
  10843. ELSIF type.form = SyntaxTree.Tensor THEN
  10844. IF RefInfo THEN Info(section,"MathTensor") END;
  10845. Char(section, sfTypeMathTensor);
  10846. ELSE
  10847. HALT(100);
  10848. END;
  10849. NType(type.arrayBase);
  10850. END;
  10851. | type: SyntaxTree.RecordType DO
  10852. IF ~Indirect(type) THEN
  10853. IF type.pointerType # NIL (* OBJECT *) THEN
  10854. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10855. Char(section, sfTypePointerToRecord)
  10856. ELSE
  10857. IF RefInfo THEN Info(section,"Record") END;
  10858. Char(section, sfTypeRecord);
  10859. td := type.typeDeclaration;
  10860. IF RefInfo THEN Info(section,"TD") END;
  10861. IF (td # NIL) THEN
  10862. Global.GetSymbolSegmentedName(td,segmentedName);
  10863. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10864. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10865. ELSE
  10866. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10867. END;
  10868. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10869. Symbol(section, tir, 0, offset);
  10870. ELSE
  10871. Address(section, 0);
  10872. END;
  10873. END;
  10874. END;
  10875. | type: SyntaxTree.CellType DO
  10876. IF ~Indirect(type) THEN
  10877. IF RefInfo THEN Info(section,"Record") END;
  10878. Char(section, sfTypeRecord);
  10879. td := type.typeDeclaration;
  10880. IF RefInfo THEN Info(section,"TD") END;
  10881. IF (td # NIL) THEN
  10882. Global.GetSymbolSegmentedName(td,segmentedName);
  10883. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10884. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10885. ELSE
  10886. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10887. END;
  10888. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10889. Symbol(section, tir, 0, offset);
  10890. ELSE
  10891. Address(section, 0);
  10892. END;
  10893. END;
  10894. | type: SyntaxTree.PortType DO
  10895. Char(section, sfTypePORT);
  10896. IF type.direction = SyntaxTree.OutPort THEN
  10897. Char(section, sfOUT)
  10898. ELSE
  10899. Char(section, sfIN)
  10900. END;
  10901. | type: SyntaxTree.ProcedureType DO
  10902. IF ~Indirect(type) THEN
  10903. Char(section, sfTypeDelegate);
  10904. parameter := type.firstParameter;
  10905. WHILE(parameter # NIL) DO
  10906. NParameter(parameter, -1);
  10907. parameter := parameter.nextParameter;
  10908. END;
  10909. NType(type.returnType);
  10910. END;
  10911. | type:SyntaxTree.EnumerationType DO
  10912. Char(section, sfTypeENUM);
  10913. | type: SyntaxTree.BasicType DO
  10914. WITH type: SyntaxTree.BooleanType DO
  10915. IF RefInfo THEN Info(section,"Boolean") END;
  10916. Char(section, sfTypeBOOLEAN);
  10917. | type: SyntaxTree.CharacterType DO
  10918. IF type = module.system.characterType THEN
  10919. IF RefInfo THEN Info(section,"CHAR") END;
  10920. Char(section, sfTypeCHAR);
  10921. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10922. IF RefInfo THEN Info(section,"CHAR8") END;
  10923. Char(section, sfTypeCHAR8)
  10924. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10925. IF RefInfo THEN Info(section,"CHAR16") END;
  10926. Char(section, sfTypeCHAR16);
  10927. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10928. IF RefInfo THEN Info(section,"CHAR32") END;
  10929. Char(section, sfTypeCHAR32);
  10930. ELSE
  10931. HALT(100);
  10932. END;
  10933. |type: SyntaxTree.IntegerType DO
  10934. IF type(SyntaxTree.IntegerType).signed THEN
  10935. IF (type = module.system.shortintType) THEN
  10936. IF RefInfo THEN Info(section,"SHORTINT") END;
  10937. Char(section, sfTypeSHORTINT)
  10938. ELSIF (type = module.system.integerType) THEN
  10939. IF RefInfo THEN Info(section,"INTEGER") END;
  10940. Char(section, sfTypeINTEGER)
  10941. ELSIF (type = module.system.longintType) THEN
  10942. IF RefInfo THEN Info(section,"LONGINT") END;
  10943. Char(section, sfTypeLONGINT)
  10944. ELSIF (type = module.system.hugeintType) THEN
  10945. IF RefInfo THEN Info(section,"HUGEINT") END;
  10946. Char(section, sfTypeHUGEINT)
  10947. ELSIF (type = module.system.wordType) THEN
  10948. IF RefInfo THEN Info(section,"WORD") END;
  10949. Char(section, sfTypeWORD)
  10950. ELSIF (type = module.system.longWordType) THEN
  10951. IF RefInfo THEN Info(section,"LONGWORD") END;
  10952. Char(section, sfTypeLONGWORD);
  10953. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10954. IF RefInfo THEN Info(section,"SIGNED8") END;
  10955. Char(section, sfTypeSIGNED8)
  10956. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10957. IF RefInfo THEN Info(section,"SIGNED16") END;
  10958. Char(section, sfTypeSIGNED16)
  10959. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10960. IF RefInfo THEN Info(section,"SIGNED32") END;
  10961. Char(section, sfTypeSIGNED32)
  10962. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10963. IF RefInfo THEN Info(section,"SIGNED64") END;
  10964. Char(section, sfTypeSIGNED64)
  10965. ELSE
  10966. HALT(100);
  10967. END
  10968. ELSE (* unsigned *)
  10969. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10970. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10971. Char(section, sfTypeUNSIGNED8)
  10972. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10973. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10974. Char(section, sfTypeUNSIGNED16)
  10975. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10976. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10977. Char(section, sfTypeUNSIGNED32)
  10978. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10979. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10980. Char(section, sfTypeUNSIGNED64)
  10981. ELSE
  10982. HALT(100)
  10983. END
  10984. END;
  10985. | type: SyntaxTree.FloatType DO
  10986. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10987. IF RefInfo THEN Info(section,"REAL") END;
  10988. Char(section, sfTypeREAL);
  10989. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  10990. IF RefInfo THEN Info(section,"LONGREAL") END;
  10991. Char(section, sfTypeLONGREAL);
  10992. ELSE
  10993. HALT(100);
  10994. END;
  10995. |type: SyntaxTree.ComplexType DO
  10996. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  10997. IF RefInfo THEN Info(section,"COMPLEX") END;
  10998. Char(section, sfTypeCOMPLEX);
  10999. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  11000. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  11001. Char(section, sfTypeLONGCOMPLEX);
  11002. ELSE
  11003. HALT(100);
  11004. END;
  11005. |type:SyntaxTree.SetType DO
  11006. IF RefInfo THEN Info(section,"SET") END;
  11007. Char(section, sfTypeSET);
  11008. |type:SyntaxTree.AnyType DO
  11009. IF RefInfo THEN Info(section,"ANY") END;
  11010. Char(section, sfTypeANY);
  11011. |type:SyntaxTree.ObjectType DO
  11012. IF RefInfo THEN Info(section,"OBJECT") END;
  11013. Char(section, sfTypeOBJECT);
  11014. |type:SyntaxTree.ByteType DO
  11015. IF RefInfo THEN Info(section,"BYTE") END;
  11016. Char(section, sfTypeBYTE);
  11017. |type:SyntaxTree.RangeType DO
  11018. IF RefInfo THEN Info(section,"RANGE") END;
  11019. Char(section, sfTypeRANGE)
  11020. |type:SyntaxTree.AddressType DO
  11021. IF RefInfo THEN Info(section,"ADDRESS") END;
  11022. Char(section, sfTypeADDRESS)
  11023. |type:SyntaxTree.SizeType DO
  11024. IF RefInfo THEN Info(section,"SIZE") END;
  11025. Char(section, sfTypeSIZE)
  11026. ELSE
  11027. HALT(100)
  11028. END;
  11029. ELSE HALT(101);
  11030. END;
  11031. END;
  11032. END NType;
  11033. (*
  11034. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11035. *)
  11036. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11037. VAR pos: LONGINT; type: SyntaxTree.Type;
  11038. BEGIN
  11039. IF RefInfo THEN Info(section, "Parameter") END;
  11040. Char(section, sfVariable);
  11041. Size(section, procOffset);
  11042. String0(section, parameter.name);
  11043. type := parameter.type.resolved;
  11044. IF parameter.kind = SyntaxTree.VarParameter THEN
  11045. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11046. ELSE Char(section, sfIndirect)
  11047. END;
  11048. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11049. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11050. Char(section, sfIndirect);
  11051. ELSE
  11052. Char(section, sfRelative);
  11053. END;
  11054. ELSE
  11055. Char(section, sfRelative);
  11056. END;
  11057. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11058. NType(parameter.type);
  11059. END NParameter;
  11060. (*
  11061. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11062. *)
  11063. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11064. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11065. name: Basic.SegmentedName; flags: SET;
  11066. BEGIN
  11067. IF RefInfo THEN Info(section, "Procedure") END;
  11068. pos := CurrentIndex();
  11069. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11070. Char(section, sfProcedure);
  11071. Size(section, scopeOffset);
  11072. String0(section,procedure.name);
  11073. s := module.allSections.FindBySymbol(procedure);
  11074. Symbol(section,s,0,0); (* start *)
  11075. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11076. flags := {};
  11077. IF procedureType.isDelegate THEN
  11078. INCL(flags, flagDelegate);
  11079. END;
  11080. IF procedure.isConstructor THEN
  11081. INCL(flags, flagConstructor);
  11082. END;
  11083. Set(section, flags);
  11084. Global.GetSymbolSegmentedName(procedure,name);
  11085. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11086. IF RefInfo THEN Info(section, "Parameters") END;
  11087. parameter := procedureType.firstParameter;
  11088. WHILE(parameter # NIL) DO
  11089. NParameter(parameter, pos);
  11090. parameter := parameter.nextParameter;
  11091. END;
  11092. IF RefInfo THEN Info(section, "ReturnType") END;
  11093. NType(procedureType.returnType);
  11094. NScope(procedure.procedureScope, pos);
  11095. END NProcedure;
  11096. (*
  11097. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11098. *)
  11099. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11100. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11101. BEGIN
  11102. IF RefInfo THEN Info(section, "Variable") END;
  11103. pos := CurrentIndex();
  11104. Char(section, sfVariable);
  11105. Size(section, scopeOffset);
  11106. String0(section, variable.name);
  11107. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11108. Char(section, sfAbsolute);
  11109. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11110. NamedSymbol(section, sn,variable, 0,0);
  11111. ELSE
  11112. Char(section, sfRelative);
  11113. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11114. END;
  11115. NType(variable.type);
  11116. s := module.allSections.FindBySymbol(variable);
  11117. END NVariable;
  11118. (*
  11119. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11120. *)
  11121. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11122. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11123. BEGIN
  11124. IF typeDeclaration = NIL THEN RETURN END;
  11125. pos := CurrentIndex();
  11126. s := module.allSections.FindBySymbol(typeDeclaration);
  11127. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11128. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11129. Char(section, sfTypeDeclaration);
  11130. Size(section, scopeOffset);
  11131. String0(section, typeDeclaration.name);
  11132. declared := typeDeclaration.declaredType.resolved;
  11133. IF (declared IS SyntaxTree.PointerType) THEN
  11134. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11135. END;
  11136. WITH declared: SyntaxTree.RecordType DO
  11137. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11138. Symbol(section, s, 0, offset);
  11139. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11140. Basic.AppendToSegmentedName(name,".@Info");
  11141. s := module.allSections.FindByName(name);
  11142. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11143. NScope(declared.recordScope, pos);
  11144. |declared: SyntaxTree.CellType DO
  11145. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11146. Symbol(section, s, 0, offset);
  11147. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11148. Basic.AppendToSegmentedName(name,".@Info");
  11149. s := module.allSections.FindByName(name);
  11150. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11151. NScope(declared.cellScope, pos);
  11152. ELSE
  11153. Address(section, 0);
  11154. END;
  11155. END NTypeDeclaration;
  11156. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11157. VAR pos: LONGINT;
  11158. BEGIN
  11159. pos := CurrentIndex();
  11160. Char(section,sfModule);
  11161. Size(section, prevSymbol);
  11162. String0(section, module.name);
  11163. NScope(module.moduleScope, pos);
  11164. END NModule;
  11165. (*
  11166. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11167. *)
  11168. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11169. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11170. BEGIN
  11171. IF scope = NIL THEN RETURN END;
  11172. IF RefInfo THEN Info(section, "Scope") END;
  11173. Char(section, sfScopeBegin);
  11174. variable := scope.firstVariable;
  11175. WHILE (variable # NIL) DO
  11176. NVariable(variable, prevSymbol);
  11177. variable := variable.nextVariable;
  11178. END;
  11179. WITH scope: SyntaxTree.ModuleScope DO
  11180. bodyProcedure := scope.bodyProcedure;
  11181. |scope: SyntaxTree.RecordScope DO
  11182. bodyProcedure := scope.bodyProcedure;
  11183. ELSE
  11184. bodyProcedure := NIL;
  11185. END;
  11186. IF bodyProcedure # NIL THEN
  11187. NProcedure(bodyProcedure, prevSymbol)
  11188. END;
  11189. procedure := scope.firstProcedure;
  11190. WHILE procedure # NIL DO
  11191. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11192. procedure := procedure.nextProcedure;
  11193. END;
  11194. typeDeclaration := scope.firstTypeDeclaration;
  11195. WHILE typeDeclaration # NIL DO
  11196. NTypeDeclaration(typeDeclaration, prevSymbol);
  11197. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11198. END;
  11199. Char(section, sfScopeEnd); (* scope ends *)
  11200. END NScope;
  11201. BEGIN
  11202. NEW(indirectTypes, 32);
  11203. ArrayBlock(section,sizePC,"", FALSE);
  11204. startPC := section.pc;
  11205. NModule(module.module, -1);
  11206. PatchArray(section,sizePC,CurrentIndex());
  11207. END References;
  11208. (*
  11209. Command* = RECORD
  11210. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11211. name*: Name; (* name of the procedure *)
  11212. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11213. entryAdr* : ADDRESS; (* entry address of procedure *)
  11214. END;
  11215. *)
  11216. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11217. VAR
  11218. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11219. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11220. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11221. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11222. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11223. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11224. BEGIN
  11225. RETURN
  11226. (type = NIL) OR
  11227. (type.resolved IS SyntaxTree.RecordType) OR
  11228. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11229. (type.resolved IS SyntaxTree.AnyType);
  11230. END TypeAllowed;
  11231. BEGIN
  11232. numberParameters := procedureType.numberParameters;
  11233. RETURN
  11234. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11235. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11236. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11237. END GetProcedureAllowed;
  11238. PROCEDURE WriteType(type : SyntaxTree.Type);
  11239. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11240. name: Basic.SegmentedName; offset: LONGINT;
  11241. BEGIN
  11242. IF type = NIL THEN
  11243. Address(source,0);
  11244. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11245. Address(source,1);
  11246. ELSE
  11247. type := type.resolved;
  11248. IF type IS SyntaxTree.PointerType THEN
  11249. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11250. END;
  11251. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11252. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11253. name[0] := typeDeclaration.name; name[1] := -1;
  11254. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11255. ASSERT(section # NIL);
  11256. ELSE
  11257. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11258. (* TODO *)
  11259. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11260. END;
  11261. IF implementationVisitor.backend.cooperative THEN
  11262. offset := 0;
  11263. ELSE
  11264. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11265. END;
  11266. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11267. END;
  11268. END WriteType;
  11269. BEGIN
  11270. Info(source, "command array descriptor");
  11271. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11272. numberCommands := 0;
  11273. Info(source, "command array content");
  11274. FOR i := 0 TO module.allSections.Length() - 1 DO
  11275. p := module.allSections.GetSection(i);
  11276. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11277. procedure := p.symbol(SyntaxTree.Procedure);
  11278. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11279. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11280. procedure.GetName(name);
  11281. Name(source,name);
  11282. numberParameters := procedureType.numberParameters;
  11283. (* offset of type of first parameter *)
  11284. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11285. ELSE WriteType(procedureType.firstParameter.type)
  11286. END;
  11287. (* offset of type of return parameter *)
  11288. WriteType(procedureType.returnType);
  11289. (* command name *)
  11290. (* command code offset *)
  11291. Symbol(source,p,0,0);
  11292. INC(numberCommands);
  11293. IF Trace THEN
  11294. D.Ln;
  11295. END;
  11296. END;
  11297. END
  11298. END;
  11299. PatchArray(source,sizePC,numberCommands);
  11300. END CommandArray;
  11301. (* to prevent from double import of different module aliases *)
  11302. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11303. VAR i: SyntaxTree.Import;
  11304. BEGIN
  11305. i := module.module.moduleScope.firstImport;
  11306. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11307. i := i.nextImport;
  11308. END;
  11309. RETURN i = import
  11310. END IsFirstDirectOccurence;
  11311. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11312. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11313. BEGIN
  11314. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11315. ArrayBlock(source,pc,"", FALSE);
  11316. Info(source, "import module array data");
  11317. IF implementationVisitor.backend.cooperative THEN
  11318. offset := 0;
  11319. ELSE
  11320. IF module.system.addressType.sizeInBits = 64 THEN
  11321. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11322. ELSE
  11323. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11324. END;
  11325. END;
  11326. import := module.module.moduleScope.firstImport;
  11327. numberImports := 0;
  11328. WHILE import # NIL DO
  11329. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11330. Global.GetModuleSegmentedName(import.module,name);
  11331. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11332. NamedSymbol(source, name, NIL, 0, offset);
  11333. INC(numberImports);
  11334. END;
  11335. import := import.nextImport
  11336. END;
  11337. PatchArray(source,pc,numberImports);
  11338. END ImportsArray;
  11339. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11340. VAR
  11341. p: Sections.Section; sizePC, size, i: LONGINT;
  11342. BEGIN
  11343. Info(source, "Type info section");
  11344. size := 0;
  11345. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11346. FOR i := 0 TO module.allSections.Length() - 1 DO
  11347. p := module.allSections.GetSection(i);
  11348. WITH p: IntermediateCode.Section DO
  11349. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11350. Symbol(source,p,0,0);
  11351. INC(size);
  11352. END;
  11353. END
  11354. END;
  11355. PatchArray(source,sizePC,size);
  11356. END TypeInfoSection;
  11357. (*
  11358. ProcTableEntry* = RECORD
  11359. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11360. noPtr*: LONGINT;
  11361. END;
  11362. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11363. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11364. *)
  11365. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11366. VAR
  11367. numberPointers: SIZE;
  11368. procedure: SyntaxTree.Procedure;
  11369. BEGIN
  11370. Info(section,"pcFrom");
  11371. Symbol(section,procedureSection,0,0);
  11372. Info(section,"pcTo");
  11373. Symbol(section, procedureSection, procedureSection.pc, 0);
  11374. Info(section,"pointer to offsets array");
  11375. Symbol(section, section,section.pc+1,0);
  11376. Info(section,"offsets array");
  11377. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11378. PointerArray(section, procedure.procedureScope, numberPointers);
  11379. END ProcedureDescriptor;
  11380. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11381. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11382. BEGIN
  11383. name := procedureSection.name;
  11384. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11385. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11386. ProcedureDescriptor(dest, procedureSection);
  11387. Symbol(section, dest, offset, 0);
  11388. END ProcedureDescriptorPointer;
  11389. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11390. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11391. BEGIN
  11392. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11393. numberProcs := 0;
  11394. FOR i := 0 TO module.allSections.Length() - 1 DO
  11395. destination := module.allSections.GetSection(i);
  11396. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11397. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11398. INC(numberProcs);
  11399. END
  11400. END;
  11401. PatchArray(section, sizePC, numberProcs);
  11402. END ProcedureDescriptorArray;
  11403. (*
  11404. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11405. VAR
  11406. next*: Module; (** once a module is published, all fields are read-only *)
  11407. name*: Name;
  11408. init, published: BOOLEAN;
  11409. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11410. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11411. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11412. command*: POINTER TO ARRAY OF Command;
  11413. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11414. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11415. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11416. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11417. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11418. data*, code*: Bytes;
  11419. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11420. export*: ExportDesc;
  11421. term*: TerminationHandler;
  11422. exTable*: ExceptionTable;
  11423. noProcs*: LONGINT;
  11424. firstProc*: ADDRESS; <- done by loader
  11425. maxPtrs*: LONGINT;
  11426. crc*: LONGINT;
  11427. *)
  11428. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11429. BEGIN
  11430. Info(section, "cycle");
  11431. Size(section,0);
  11432. Info(section, "references");
  11433. Size(section,0);
  11434. Info(section, "nextMarked");
  11435. Address(section,0);
  11436. Info(section, "nextWatched");
  11437. Address(section,0);
  11438. END BasePointer;
  11439. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11440. BEGIN
  11441. BasePointer(section);
  11442. Info(section, "action");
  11443. Address(section,0);
  11444. Info(section, "monitor");
  11445. Address(section,0);
  11446. END BaseObject;
  11447. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11448. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11449. pooledName: Basic.SegmentedName;
  11450. symbol: SyntaxTree.Symbol;
  11451. BEGIN
  11452. Global.GetModuleName(module.module,name);
  11453. Strings.Append(name,".@Module.@Descriptor");
  11454. Basic.ToSegmentedName(name, pooledName);
  11455. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11456. Symbol(section,descriptorSection,0,0);
  11457. Info(descriptorSection, "descriptor");
  11458. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11459. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11460. Address(descriptorSection,0);
  11461. Global.GetModuleName(module.module,name);
  11462. Strings.Append(name,".@Trace");
  11463. Basic.ToSegmentedName(name, pooledName);
  11464. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11465. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11466. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11467. END ModuleDescriptor;
  11468. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11469. VAR name: ARRAY 128 OF CHAR;
  11470. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11471. symbol: SyntaxTree.Symbol;
  11472. BEGIN
  11473. Global.GetModuleName(module.module,name);
  11474. Strings.Append(name,".@Module");
  11475. Basic.ToSegmentedName(name, pooledName);
  11476. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11477. moduleSection.SetExported(TRUE);
  11478. IF moduleSection.pc = 0 THEN
  11479. IF implementationVisitor.backend.cooperative THEN
  11480. Info(moduleSection, "descriptor");
  11481. ModuleDescriptor(moduleSection);
  11482. BaseObject(moduleSection);
  11483. implementationVisitor.CreateTraceModuleMethod(module.module);
  11484. ELSE
  11485. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11486. Info(moduleSection, "HeapBlock");
  11487. Symbol(moduleSection,moduleSection,2,0);
  11488. Info(moduleSection, "TypeDescriptor");
  11489. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11490. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11491. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11492. END;
  11493. END;
  11494. RETURN moduleSection;
  11495. END ModuleSection;
  11496. PROCEDURE NewModuleInfo();
  11497. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11498. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11499. sectionName: Basic.SectionName;
  11500. CONST MPO=-40000000H;
  11501. BEGIN
  11502. (*
  11503. TypeDesc* = POINTER TO RECORD
  11504. descSize: LONGINT;
  11505. sentinel: LONGINT; (* = MPO-4 *)
  11506. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11507. flags*: SET;
  11508. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11509. name*: Name;
  11510. END;
  11511. *)
  11512. Global.GetSymbolSegmentedName(module.module,name);
  11513. Basic.AppendToSegmentedName(name,".@Info");
  11514. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11515. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11516. Address(source,MPO-4);
  11517. Info(source, "type tag pointer");
  11518. Address( source,0);
  11519. Info(source, "type flags");
  11520. flags := {};
  11521. Set( source, flags);
  11522. Info(source, "pointer to module");
  11523. moduleSection := ModuleSection();
  11524. Symbol( source, moduleSection, moduleSection.pc,0);
  11525. Info(source, "type name");
  11526. i := 0;
  11527. sectionName := "@Self";
  11528. (*
  11529. Global.GetSymbolSegmentedName(td,name);
  11530. Basic.SegmentedNameToString(name, sectionName);
  11531. *)
  11532. Name(source,sectionName);
  11533. source.SetReferenced(FALSE);
  11534. patchInfoPC := source.pc;
  11535. Size(source, 0);
  11536. END NewModuleInfo;
  11537. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11538. VAR
  11539. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11540. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11541. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11542. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11543. referenceSectionOffset : LONGINT;
  11544. name: Basic.SegmentedName; offset: LONGINT;
  11545. BEGIN
  11546. NewModuleInfo();
  11547. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11548. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11549. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11550. ImportsArray(importSection);
  11551. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11552. CommandArray(commandsSection);
  11553. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11554. ExceptionArray(exceptionSection);
  11555. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11556. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11557. TypeInfoSection(typeInfoSection);
  11558. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11559. References(referenceSection);
  11560. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11561. ProcedureDescriptorArray(procTableSection, numberProcs);
  11562. name := "Heaps.AnyPtr";
  11563. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11564. (* set base pointer *)
  11565. NamedSymbolAt(procTableSection, procTableSectionOffset -1, name, NIL, 0, offset);
  11566. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11567. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11568. moduleSection := ModuleSection();
  11569. Info(moduleSection, "nextRoot*: RootObject");
  11570. Address(moduleSection,0);
  11571. Info(moduleSection, "next*: Module");
  11572. Address(moduleSection,0);
  11573. Info(moduleSection, "name*: Name");
  11574. Name(moduleSection,moduleName);
  11575. Info(moduleSection, "init, published: BOOLEAN");
  11576. Boolean(moduleSection,FALSE);
  11577. Boolean(moduleSection,FALSE);
  11578. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11579. Boolean(moduleSection,FALSE);
  11580. Boolean(moduleSection,FALSE);
  11581. Info(moduleSection, "refcnt*: LONGINT");
  11582. Longint(moduleSection,0);
  11583. Info(moduleSection, "sb*: ADDRESS");
  11584. Address(moduleSection,0);
  11585. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11586. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11587. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11588. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11589. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11590. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11591. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11592. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11593. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11594. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11595. Info(moduleSection, "procTable*: ProcTable");
  11596. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11597. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11598. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11599. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11600. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11601. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11602. Info(moduleSection, "export*: ExportDesc");
  11603. ExportDesc(moduleSection);
  11604. Info(moduleSection, "term*: TerminationHandler");
  11605. Address(moduleSection,0);
  11606. Info(moduleSection, "exTable*: ExceptionTable");
  11607. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11608. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11609. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11610. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11611. Info(moduleSection, "crc*: LONGINT");
  11612. Longint(moduleSection, 0); (*! must be implemented *)
  11613. Info(moduleSection, "body*: ADDRESS");
  11614. Symbol(moduleSection, bodyProc, 0,0);
  11615. IF implementationVisitor.backend.cooperative THEN
  11616. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11617. Info(moduleSection, "monitor.owner");
  11618. Address(moduleSection,0);
  11619. Info(moduleSection, "monitor.nestingLevel");
  11620. Address(moduleSection,0);
  11621. Info(moduleSection, "monitor.blockedQueue");
  11622. Address(moduleSection,0); Address(moduleSection,0);
  11623. Info(moduleSection, "monitor.waitingQueue");
  11624. Address(moduleSection,0); Address(moduleSection,0);
  11625. Info(moduleSection, "monitor.waitingSentinel");
  11626. Address(moduleSection,0);
  11627. END;
  11628. END Module;
  11629. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11630. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11631. BEGIN
  11632. ArrayBlock(source,pc,"",FALSE);
  11633. Info(source, "pointer offsets array data");
  11634. IF scope IS SyntaxTree.RecordScope THEN
  11635. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11636. ELSIF scope IS SyntaxTree.CellScope THEN
  11637. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11638. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11639. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11640. WHILE variable # NIL DO
  11641. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11642. symbol := module.allSections.FindBySymbol(variable);
  11643. ASSERT(symbol # NIL);
  11644. Pointers(0,symbol, source,variable.type,numberPointers);
  11645. END;
  11646. variable := variable.nextVariable;
  11647. END;
  11648. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11649. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11650. WHILE parameter # NIL DO
  11651. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11652. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11653. END;
  11654. parameter := parameter.nextParameter;
  11655. END;
  11656. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11657. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11658. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11659. IF implementationVisitor.backend.preciseGC THEN
  11660. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11661. END;
  11662. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11663. END;
  11664. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11665. WHILE(variable # NIL) DO
  11666. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11667. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11668. END;
  11669. variable := variable.nextVariable
  11670. END;
  11671. END;
  11672. PatchArray(source,pc,numberPointers);
  11673. END PointerArray;
  11674. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11675. VAR
  11676. name: Basic.SegmentedName;
  11677. section: IntermediateCode.Section;
  11678. BEGIN
  11679. ASSERT(implementationVisitor.newObjectFile);
  11680. Global.GetSymbolSegmentedName(symbol,name);
  11681. Basic.AppendToSegmentedName(name,suffix);
  11682. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11683. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11684. Info(section, "HeapBlock");
  11685. Address(section,0); (* empty such that GC does not go on traversing *)
  11686. Info(section, suffix);
  11687. Address(section,0);
  11688. pc := section.pc;
  11689. RETURN section;
  11690. END SymbolSection;
  11691. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11692. VAR recordType: SyntaxTree.RecordType;
  11693. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11694. section: Sections.Section; cellType: SyntaxTree.CellType;
  11695. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11696. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11697. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11698. sectionName: Basic.SectionName;
  11699. CONST MPO=-40000000H;
  11700. BEGIN
  11701. (*
  11702. TypeDesc* = POINTER TO RECORD
  11703. descSize: LONGINT;
  11704. sentinel: LONGINT; (* = MPO-4 *)
  11705. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11706. flags*: SET;
  11707. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11708. name*: Name;
  11709. END;
  11710. *)
  11711. (* source := module.sections.FindByName(...) *)
  11712. Global.GetSymbolSegmentedName(td,name);
  11713. Basic.AppendToSegmentedName(name,".@Info");
  11714. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11715. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11716. Address(source,MPO-4);
  11717. Info(source, "type tag pointer");
  11718. Symbol( source, tag, offset, 0);
  11719. Info(source, "type flags");
  11720. flags := {};
  11721. IF isProtected THEN INCL(flags,31) END;
  11722. Set( source, flags);
  11723. Info(source, "pointer to module");
  11724. moduleSection := ModuleSection();
  11725. Symbol( source, moduleSection, moduleSection.pc,0);
  11726. Info(source, "type name");
  11727. i := 0;
  11728. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11729. (*
  11730. Global.GetSymbolSegmentedName(td,name);
  11731. Basic.SegmentedNameToString(name, sectionName);
  11732. *)
  11733. Name(source,sectionName);
  11734. source.SetReferenced(FALSE);
  11735. Size(source, 0);
  11736. RETURN source;
  11737. END NewTypeDescriptorInfo;
  11738. PROCEDURE NewTypeDescriptor;
  11739. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11740. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11741. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11742. numberPointers: LONGINT; padding, i: LONGINT;
  11743. CONST MPO=-40000000H;
  11744. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11745. VAR i: LONGINT;
  11746. PROCEDURE Td(record: SyntaxTree.RecordType);
  11747. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11748. BEGIN
  11749. IF record # NIL THEN
  11750. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11751. baseTD := record.typeDeclaration;
  11752. Global.GetSymbolSegmentedName(baseTD,name);
  11753. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11754. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11755. ELSE
  11756. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11757. END;
  11758. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11759. Symbol(source, tir, 0, offset);
  11760. IF reverse THEN Td(record.GetBaseRecord()) END;
  11761. END;
  11762. END Td;
  11763. BEGIN
  11764. Info(source, "tag table");
  11765. baseRecord := recordType;
  11766. i := 0;
  11767. WHILE baseRecord # NIL DO
  11768. INC(i);
  11769. baseRecord := baseRecord.GetBaseRecord();
  11770. END;
  11771. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11772. IF ~reverse THEN Td(recordType) END;
  11773. WHILE i < size DO
  11774. Address(source,0);
  11775. INC(i);
  11776. END;
  11777. IF reverse THEN Td(recordType) END;
  11778. END TdTable;
  11779. PROCEDURE MethodTable(reverse: BOOLEAN);
  11780. VAR i,methods: LONGINT;
  11781. BEGIN
  11782. Info(source, "method table");
  11783. IF recordType # NIL THEN
  11784. methods := recordType.recordScope.numberMethods;
  11785. IF reverse THEN
  11786. FOR i := methods-1 TO 0 BY -1 DO
  11787. procedure := recordType.recordScope.FindMethod(i);
  11788. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11789. NamedSymbol(source, name,procedure, 0,0);
  11790. END;
  11791. ELSE
  11792. FOR i := 0 TO methods-1 DO
  11793. procedure := recordType.recordScope.FindMethod(i);
  11794. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11795. NamedSymbol(source, name,procedure, 0,0);
  11796. END;
  11797. END;
  11798. END;
  11799. END MethodTable;
  11800. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11801. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11802. BEGIN
  11803. Info(source, "method table");
  11804. baseRecord := recordType;
  11805. WHILE baseRecord.baseType # NIL DO
  11806. baseRecord := baseRecord.GetBaseRecord ();
  11807. END;
  11808. GetRecordTypeName (baseRecord, name);
  11809. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11810. IF name = stackFrame THEN
  11811. start := 0;
  11812. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11813. baseRecord := recordType;
  11814. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11815. baseRecord := baseRecord.GetBaseRecord ();
  11816. END;
  11817. IF baseRecord # NIL THEN
  11818. GetRecordTypeName (baseRecord, name);
  11819. IF pointer & ~baseRecord.isObject THEN
  11820. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11821. END;
  11822. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11823. ELSIF recordType.isObject THEN
  11824. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11825. ELSIF pointer THEN
  11826. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11827. ELSE
  11828. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11829. END;
  11830. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11831. Symbol(source, tir, 0, 0);
  11832. start := 0;
  11833. baseRecord := recordType;
  11834. WHILE (baseRecord # NIL) DO
  11835. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11836. baseRecord := baseRecord.GetBaseRecord ();
  11837. END;
  11838. ELSE
  11839. (* explicit trace method: *)
  11840. procedure := recordType.recordScope.FindMethod(0);
  11841. IF ~procedure.isFinalizer THEN
  11842. Global.GetSymbolSegmentedName(procedure, name);
  11843. NamedSymbol(source, name,procedure, 0,0);
  11844. END;
  11845. start := 1;
  11846. END;
  11847. IF (name # stackFrame) & recordType.isObject THEN
  11848. baseRecord := recordType;
  11849. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11850. baseRecord := baseRecord.GetBaseRecord ();
  11851. END;
  11852. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11853. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11854. ELSE
  11855. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11856. END;
  11857. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11858. Symbol(source, tir, 0, 0);
  11859. END;
  11860. methods := recordType.recordScope.numberMethods;
  11861. FOR i := start TO methods-1 DO
  11862. procedure := recordType.recordScope.FindMethod(i);
  11863. IF ~procedure.isFinalizer THEN
  11864. Global.GetSymbolSegmentedName(procedure, name);
  11865. NamedSymbol(source, name,procedure, 0,0);
  11866. END;
  11867. END;
  11868. END CooperativeMethodTable;
  11869. BEGIN
  11870. Global.GetSymbolSegmentedName(td,name);
  11871. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11872. source.SetExported(IsExported(td));
  11873. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11874. IF implementationVisitor.backend.cooperative THEN
  11875. base := NIL;
  11876. baseRecord := recordType.GetBaseRecord();
  11877. IF baseRecord # NIL THEN
  11878. baseTD := baseRecord.typeDeclaration;
  11879. END;
  11880. IF ~recordType.isObject THEN
  11881. Info(source, "parent");
  11882. IF baseRecord # NIL THEN
  11883. Global.GetSymbolSegmentedName(baseTD,name);
  11884. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11885. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11886. ELSE
  11887. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11888. END;
  11889. Symbol(source, tir, 0, 0);
  11890. ELSE
  11891. Address(source,0);
  11892. END;
  11893. Info(source, "record size");
  11894. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11895. source.SetReferenced(FALSE);
  11896. CooperativeMethodTable(FALSE);
  11897. base := source;
  11898. Global.GetSymbolSegmentedName(td,name);
  11899. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11900. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11901. source.SetExported(IsExported(td));
  11902. source.SetReferenced(FALSE);
  11903. END;
  11904. Info(source, "parent");
  11905. IF baseRecord # NIL THEN
  11906. Global.GetSymbolSegmentedName(baseTD,name);
  11907. sym := baseTD;
  11908. IF ~recordType.isObject THEN
  11909. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11910. sym := NIL;
  11911. END;
  11912. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11913. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11914. ELSE
  11915. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11916. END;
  11917. Symbol(source, tir, 0, 0);
  11918. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11919. Address(source,0);
  11920. ELSE
  11921. IF recordType.isObject THEN
  11922. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11923. ELSE
  11924. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11925. END;
  11926. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11927. Symbol(source, tir, 0, 0);
  11928. END;
  11929. Info(source, "base record descriptor");
  11930. Symbol(source, base, 0, 0);
  11931. CooperativeMethodTable(TRUE);
  11932. source.SetReferenced(FALSE);
  11933. IF recordType.hasPointers THEN
  11934. IF ~HasExplicitTraceMethod (recordType) THEN
  11935. implementationVisitor.CreateTraceMethod(recordType);
  11936. END;
  11937. implementationVisitor.CreateResetProcedure(recordType);
  11938. implementationVisitor.CreateAssignProcedure(recordType);
  11939. END;
  11940. ELSIF ~simple THEN
  11941. (*
  11942. MethodEnd = MPO
  11943. ---
  11944. methods (# methods)
  11945. ---
  11946. tags (16)
  11947. ---
  11948. TypeDesc = TypeInfoAdr
  11949. ---
  11950. td adr ---> rec size
  11951. ----
  11952. pointer offsets
  11953. ----
  11954. (padding)
  11955. -----
  11956. empty [2 addresses aligned]
  11957. empty
  11958. empty
  11959. numPtrs
  11960. ---
  11961. pointer offsets
  11962. ---
  11963. *)
  11964. Info(source, "MethodEnd = MPO");
  11965. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11966. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11967. MethodTable(TRUE);
  11968. TdTable(TypeTags, TRUE);
  11969. Info(source, "type descriptor info pointer");
  11970. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11971. IF (cellType # NIL) THEN
  11972. IF cellType.sizeInBits < 0 THEN
  11973. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11974. END;
  11975. Info(source, "cell size");
  11976. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11977. ELSE
  11978. Info(source, "record size");
  11979. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11980. END;
  11981. Info(source, "pointer offsets pointer");
  11982. padding := 1- source.pc MOD 2;
  11983. Symbol(source, source, source.pc+1+padding,0);
  11984. IF padding >0 THEN
  11985. Info(source, "padding");
  11986. FOR i := 1 TO padding DO Address(source,0) END;
  11987. END;
  11988. IF cellType # NIL THEN
  11989. PointerArray(source, cellType.cellScope, numberPointers);
  11990. ELSE
  11991. PointerArray(source, recordType.recordScope, numberPointers);
  11992. END;
  11993. ELSE
  11994. (*
  11995. simple:
  11996. td adr --> size
  11997. tag(1)
  11998. tag(2)
  11999. tag(3)
  12000. methods ->
  12001. *)
  12002. Info(source, "record size");
  12003. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12004. TdTable(TypeTags, FALSE);
  12005. MethodTable(FALSE);
  12006. source.SetReferenced(FALSE);
  12007. END;
  12008. END NewTypeDescriptor;
  12009. BEGIN
  12010. x := x.resolved;
  12011. IF (x IS SyntaxTree.PointerType) THEN
  12012. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12013. END;
  12014. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12015. recordType := x(SyntaxTree.RecordType);
  12016. td := x.typeDeclaration;
  12017. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12018. ASSERT(td # NIL);
  12019. section := module.allSections.FindBySymbol(td); (* TODO *)
  12020. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12021. IF implementationVisitor.newObjectFile THEN
  12022. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12023. NewTypeDescriptor
  12024. END;
  12025. ELSE
  12026. (* data section in intermediate code *)
  12027. Global.GetSymbolSegmentedName(td,name);
  12028. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12029. Basic.SuffixSegmentedName (name, module.module.name);
  12030. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12031. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12032. tir.Emit(Data(Basic.invalidPosition,op));
  12033. END;
  12034. END;
  12035. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12036. cellType := x(SyntaxTree.CellType);
  12037. td := x.typeDeclaration;
  12038. section := module.allSections.FindBySymbol(td); (* TODO *)
  12039. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12040. IF implementationVisitor.newObjectFile THEN
  12041. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12042. NewTypeDescriptor
  12043. END;
  12044. END;
  12045. END;
  12046. END
  12047. END CheckTypeDeclaration
  12048. END MetaDataGenerator;
  12049. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12050. VAR
  12051. trace-: BOOLEAN;
  12052. traceString-: SyntaxTree.IdentifierString;
  12053. traceModuleName-: SyntaxTree.IdentifierString;
  12054. newObjectFile-: BOOLEAN;
  12055. profile-: BOOLEAN;
  12056. noRuntimeChecks: BOOLEAN;
  12057. simpleMetaData-: BOOLEAN;
  12058. noAsserts: BOOLEAN;
  12059. optimize-: BOOLEAN;
  12060. cooperative-: BOOLEAN;
  12061. preregisterStatic-: BOOLEAN;
  12062. dump-: Basic.Writer;
  12063. cellsAreObjects: BOOLEAN;
  12064. preciseGC: BOOLEAN;
  12065. PROCEDURE &InitIntermediateBackend*;
  12066. BEGIN
  12067. simpleMetaData := FALSE;
  12068. newObjectFile := FALSE;
  12069. InitBackend;
  12070. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12071. SetTraceModuleName(DefaultTraceModuleName);
  12072. END InitIntermediateBackend;
  12073. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12074. VAR
  12075. declarationVisitor: DeclarationVisitor;
  12076. implementationVisitor: ImplementationVisitor;
  12077. module: Sections.Module;
  12078. name, platformName: SyntaxTree.IdentifierString;
  12079. meta: MetaDataGenerator;
  12080. BEGIN
  12081. ResetError;
  12082. Global.GetSymbolName(x,name);
  12083. NEW(module,x,system); (* backend structures *)
  12084. Global.GetModuleName(x, name);
  12085. module.SetModuleName(name);
  12086. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12087. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12088. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12089. declarationVisitor.Module(x,module);
  12090. IF newObjectFile & ~meta.simple THEN
  12091. meta.Module(implementationVisitor.moduleBodySection);
  12092. END;
  12093. GetDescription(platformName);
  12094. module.SetPlatformName(platformName);
  12095. RETURN module
  12096. END GenerateIntermediate;
  12097. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12098. BEGIN RETURN TRUE
  12099. END SupportedImmediate;
  12100. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12101. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12102. END ProcessSyntaxTreeModule;
  12103. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12104. VAR
  12105. result: Sections.Module;
  12106. traceName: Basic.MessageString;
  12107. BEGIN
  12108. ASSERT(intermediateCodeModule IS Sections.Module);
  12109. result := intermediateCodeModule(Sections.Module);
  12110. IF trace THEN
  12111. traceName := "intermediate code trace: ";
  12112. Strings.Append(traceName,traceString);
  12113. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12114. IF (traceString="") OR (traceString="*") THEN
  12115. result.Dump(dump);
  12116. dump.Update
  12117. ELSE
  12118. Sections.DumpFiltered(dump, result, traceString);
  12119. END
  12120. END;
  12121. RETURN result
  12122. END ProcessIntermediateCodeModule;
  12123. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12124. BEGIN instructionSet := "Intermediate";
  12125. END GetDescription;
  12126. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12127. BEGIN
  12128. SELF.newObjectFile := newObjectFile;
  12129. SELF.simpleMetaData := simpleMetaData;
  12130. END SetNewObjectFile;
  12131. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12132. BEGIN COPY(name, traceModuleName)
  12133. END SetTraceModuleName;
  12134. PROCEDURE DefineOptions(options: Options.Options);
  12135. BEGIN
  12136. DefineOptions^(options);
  12137. options.Add(0X,"trace",Options.String);
  12138. options.Add(0X,"runtime",Options.String);
  12139. options.Add(0X,"newObjectFile",Options.Flag);
  12140. options.Add(0X,"traceModule",Options.String);
  12141. options.Add(0X,"profile",Options.Flag);
  12142. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12143. options.Add(0X,"noAsserts",Options.Flag);
  12144. options.Add(0X,"metaData",Options.String);
  12145. options.Add('o',"optimize", Options.Flag);
  12146. options.Add(0X,"preregisterStatic", Options.Flag);
  12147. options.Add(0X,"cellsAreObjects", Options.Flag);
  12148. options.Add(0X,"preciseGC", Options.Flag);
  12149. END DefineOptions;
  12150. PROCEDURE GetOptions(options: Options.Options);
  12151. VAR name,string: SyntaxTree.IdentifierString;
  12152. BEGIN
  12153. GetOptions^(options);
  12154. trace := options.GetString("trace",traceString);
  12155. profile := options.GetFlag("profile");
  12156. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12157. noAsserts := options.GetFlag("noAsserts");
  12158. cooperative := options.GetFlag("cooperative");
  12159. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12160. IF cooperative THEN
  12161. SetRuntimeModuleName("CPU") END
  12162. END;
  12163. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12164. simpleMetaData := TRUE
  12165. END;
  12166. IF options.GetString("metaData",string) THEN
  12167. IF string = "simple" THEN simpleMetaData := TRUE
  12168. ELSIF string ="full" THEN simpleMetaData := FALSE
  12169. END;
  12170. END;
  12171. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12172. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12173. optimize := options.GetFlag("optimize");
  12174. preregisterStatic := options.GetFlag("preregisterStatic");
  12175. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12176. preciseGC := options.GetFlag("preciseGC");
  12177. END GetOptions;
  12178. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12179. BEGIN RETURN SymbolFileFormat.Get()
  12180. END DefaultSymbolFileFormat;
  12181. END IntermediateBackend;
  12182. (* ----------------------------------- register allocation ------------------------------------- *)
  12183. (* register mapping scheme
  12184. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12185. spill offset
  12186. part(n) --> ticket <--> physical register
  12187. spill offset
  12188. *)
  12189. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12190. emptyOperand: IntermediateCode.Operand;
  12191. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12192. statCoopResetVariables: LONGINT;
  12193. statCoopModifyAssignments: LONGINT;
  12194. modifyAssignmentsPC : LONGINT;
  12195. statCoopNilCheck: LONGINT;
  12196. statCoopSwitch: LONGINT;
  12197. statCoopAssignProcedure: LONGINT;
  12198. statCoopTraceMethod: LONGINT;
  12199. statCoopResetProcedure: LONGINT;
  12200. statCoopTraceModule: LONGINT;
  12201. PROCEDURE ResetStatistics*;
  12202. BEGIN
  12203. statCoopResetVariables := 0;
  12204. statCoopModifyAssignments := 0;
  12205. statCoopNilCheck:= 0;
  12206. statCoopSwitch:= 0;
  12207. statCoopAssignProcedure:= 0;
  12208. statCoopTraceMethod:= 0;
  12209. statCoopResetProcedure:= 0;
  12210. statCoopTraceModule:= 0;
  12211. END ResetStatistics;
  12212. PROCEDURE Statistics*;
  12213. BEGIN
  12214. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12215. TRACE(statCoopNilCheck, statCoopSwitch);
  12216. TRACE(statCoopAssignProcedure,
  12217. statCoopTraceMethod,
  12218. statCoopResetProcedure,
  12219. statCoopTraceModule)
  12220. END Statistics;
  12221. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12222. VAR h: LONGINT;
  12223. BEGIN
  12224. WHILE b # 0 DO
  12225. h := a MOD b;
  12226. a := b;
  12227. b := h;
  12228. END;
  12229. RETURN a
  12230. END GCD;
  12231. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12232. BEGIN
  12233. RETURN a*b DIV GCD(a,b)
  12234. END SCM;
  12235. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12236. BEGIN
  12237. (*TRACE(a,b);*)
  12238. IF a = 0 THEN RETURN b
  12239. ELSIF b = 0 THEN RETURN a
  12240. ELSE RETURN SCM(a,b)
  12241. END;
  12242. END CommonAlignment;
  12243. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12244. BEGIN
  12245. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12246. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12247. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12248. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12249. 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)
  12250. END
  12251. END PassBySingleReference;
  12252. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12253. BEGIN
  12254. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12255. END PassInRegister;
  12256. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12257. VAR new: RegisterEntry;
  12258. BEGIN
  12259. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12260. IF queue = NIL THEN
  12261. queue := new
  12262. ELSE
  12263. new.next := queue;
  12264. IF queue#NIL THEN queue.prev := new END;
  12265. queue := new
  12266. END;
  12267. END AddRegisterEntry;
  12268. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12269. VAR this: RegisterEntry;
  12270. BEGIN
  12271. this := queue;
  12272. WHILE (this # NIL) & (this.register # register) DO
  12273. this := this.next;
  12274. END;
  12275. IF this = NIL THEN
  12276. RETURN FALSE
  12277. END;
  12278. ASSERT(this # NIL);
  12279. IF this = queue THEN queue := queue.next END;
  12280. IF this.prev # NIL THEN this.prev.next := this.next END;
  12281. IF this.next # NIL THEN this.next.prev := this.prev END;
  12282. RETURN TRUE
  12283. END RemoveRegisterEntry;
  12284. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12285. BEGIN ASSERT(cond);
  12286. END Assert;
  12287. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12288. BEGIN
  12289. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12290. END ReusableRegister;
  12291. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12292. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12293. BEGIN
  12294. procedure := moduleScope.bodyProcedure;
  12295. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12296. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12297. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12298. procedure.SetScope(moduleScope);
  12299. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12300. procedure.SetAccess(SyntaxTree.Hidden);
  12301. moduleScope.SetBodyProcedure(procedure);
  12302. moduleScope.AddProcedure(procedure);
  12303. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12304. END;
  12305. END EnsureBodyProcedure;
  12306. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12307. VAR import: SyntaxTree.Import;
  12308. selfName: SyntaxTree.IdentifierString;
  12309. module: SyntaxTree.Module;
  12310. BEGIN
  12311. scope.ownerModule.GetName(selfName);
  12312. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12313. module := scope.ownerModule
  12314. ELSE
  12315. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12316. IF import = NIL THEN
  12317. RETURN NIL
  12318. ELSIF import.module = NIL THEN
  12319. RETURN NIL
  12320. ELSE module := import.module
  12321. END;
  12322. END;
  12323. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12324. END GetSymbol;
  12325. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12326. BEGIN
  12327. op.mode := mode;
  12328. IntermediateCode.InitOperand(op.op);
  12329. IntermediateCode.InitOperand(op.tag);
  12330. IntermediateCode.InitOperand(op.extra);
  12331. op.dimOffset := 0;
  12332. END InitOperand;
  12333. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12334. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12335. BEGIN RETURN IntermediateCode.GetType(system, type)
  12336. END GetType;
  12337. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12338. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12339. BEGIN
  12340. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12341. (*
  12342. UniqueId(name,module,name,adr);
  12343. *)
  12344. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12345. constant.SetValue(value);
  12346. constant.SetAccess(SyntaxTree.Hidden);
  12347. module.moduleScope.AddConstant(constant);
  12348. constant.SetScope(module.moduleScope);
  12349. RETURN constant
  12350. END BuildConstant;
  12351. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12352. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12353. BEGIN
  12354. variable := scope.firstVariable;
  12355. WHILE variable # NIL DO
  12356. IF variable.NeedsTrace() THEN
  12357. RETURN TRUE;
  12358. END;
  12359. variable := variable.nextVariable;
  12360. END;
  12361. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12362. WHILE parameter # NIL DO
  12363. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12364. RETURN TRUE;
  12365. END;
  12366. parameter := parameter.nextParameter;
  12367. END;
  12368. RETURN FALSE;
  12369. END HasPointers;
  12370. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12371. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12372. END IsVariableParameter;
  12373. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12374. VAR parameter: SyntaxTree.Parameter;
  12375. BEGIN
  12376. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12377. WHILE parameter # NIL DO
  12378. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12379. IF parameter.movable THEN RETURN TRUE END;
  12380. parameter := parameter.nextParameter;
  12381. END;
  12382. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12383. END HasVariableParameters;
  12384. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12385. BEGIN
  12386. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12387. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12388. END HasExplicitTraceMethod;
  12389. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12390. BEGIN
  12391. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12392. IF (expression IS SyntaxTree.IntegerValue) THEN
  12393. val := expression(SyntaxTree.IntegerValue).value;
  12394. RETURN TRUE
  12395. ELSE
  12396. RETURN FALSE
  12397. END;
  12398. END IsIntegerConstant;
  12399. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12400. BEGIN
  12401. IF val <= 0 THEN RETURN FALSE END;
  12402. exp := 0;
  12403. WHILE ~ODD(val) DO
  12404. val := val DIV 2;
  12405. INC(exp)
  12406. END;
  12407. RETURN val = 1
  12408. END PowerOf2;
  12409. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12410. VAR procedure: SyntaxTree.Procedure;
  12411. BEGIN
  12412. procedure := record.recordScope.constructor;
  12413. IF procedure = NIL THEN
  12414. record := record.GetBaseRecord();
  12415. IF record # NIL THEN
  12416. procedure := GetConstructor(record)
  12417. END;
  12418. END;
  12419. RETURN procedure;
  12420. END GetConstructor;
  12421. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12422. BEGIN
  12423. value := SHORT(op.intValue);
  12424. RETURN op.mode = IntermediateCode.ModeImmediate;
  12425. END IsIntegerImmediate;
  12426. (** whether a type strictily is a pointer to record or object type
  12427. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12428. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12429. BEGIN
  12430. IF type = NIL THEN
  12431. RETURN FALSE
  12432. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12433. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12434. ELSE
  12435. RETURN FALSE
  12436. END
  12437. END IsStrictlyPointerToRecord;
  12438. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12439. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12440. END IsUnsafePointer;
  12441. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12442. BEGIN type := type.resolved;
  12443. IF type IS SyntaxTree.PointerType THEN
  12444. type := type(SyntaxTree.PointerType).pointerBase;
  12445. type := type.resolved;
  12446. IF type IS SyntaxTree.RecordType THEN
  12447. recordType := type(SyntaxTree.RecordType);
  12448. RETURN TRUE
  12449. ELSE
  12450. RETURN FALSE
  12451. END
  12452. ELSIF type IS SyntaxTree.RecordType THEN
  12453. recordType := type(SyntaxTree.RecordType);
  12454. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12455. ELSIF type IS SyntaxTree.ObjectType THEN
  12456. RETURN TRUE
  12457. ELSIF type IS SyntaxTree.AnyType THEN
  12458. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12459. ELSE
  12460. RETURN FALSE
  12461. END;
  12462. END IsPointerToRecord;
  12463. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12464. BEGIN
  12465. type := type.resolved;
  12466. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12467. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12468. END IsArrayOfSystemByte;
  12469. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12470. BEGIN
  12471. IF type = NIL THEN RETURN FALSE END;
  12472. type := type.resolved;
  12473. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12474. END IsOpenArray;
  12475. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12476. BEGIN
  12477. IF type = NIL THEN RETURN FALSE END;
  12478. type := type.resolved;
  12479. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12480. END IsSemiDynamicArray;
  12481. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12482. BEGIN
  12483. IF type = NIL THEN RETURN FALSE END;
  12484. type := type.resolved;
  12485. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12486. END IsStaticArray;
  12487. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12488. VAR size: LONGINT;
  12489. BEGIN
  12490. size := 1;
  12491. WHILE (IsStaticArray(type)) DO
  12492. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12493. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12494. END;
  12495. RETURN size;
  12496. END StaticArrayNumElements;
  12497. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12498. BEGIN
  12499. WHILE (IsStaticArray(type)) DO
  12500. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12501. END;
  12502. RETURN type;
  12503. END StaticArrayBaseType;
  12504. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12505. BEGIN
  12506. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12507. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12508. END;
  12509. RETURN type;
  12510. END ArrayBaseType;
  12511. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12512. BEGIN
  12513. IF type = NIL THEN RETURN FALSE END;
  12514. type := type.resolved;
  12515. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12516. END IsDelegate;
  12517. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12518. VAR i: LONGINT;
  12519. BEGIN
  12520. i := 0; type := type.resolved;
  12521. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12522. INC(i);
  12523. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12524. END;
  12525. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12526. INC(i);
  12527. type := type(SyntaxTree.MathArrayType).arrayBase;
  12528. IF type # NIL THEN type := type.resolved END;
  12529. END;
  12530. RETURN i
  12531. END DynamicDim;
  12532. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12533. BEGIN
  12534. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12535. type := type(SyntaxTree.ArrayType).arrayBase;
  12536. END;
  12537. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12538. type := type(SyntaxTree.MathArrayType).arrayBase;
  12539. END;
  12540. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12541. END StaticSize;
  12542. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12543. BEGIN
  12544. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12545. END IsImmediate;
  12546. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12547. BEGIN
  12548. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12549. END IsAddress;
  12550. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12551. BEGIN
  12552. RETURN (x.mode = IntermediateCode.ModeRegister);
  12553. END IsRegister;
  12554. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12555. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12556. BEGIN
  12557. typeDeclaration := recordType.typeDeclaration;
  12558. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12559. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12560. END GetRecordTypeName;
  12561. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12562. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12563. BEGIN
  12564. parSize := 0;
  12565. IF SemanticChecker.StructuredReturnType(procedureType) THEN
  12566. parameter := procedureType.returnParameter;
  12567. INC(parSize,system.SizeOfParameter(parameter));
  12568. parSize := parSize + (-parSize) MOD system.addressSize;
  12569. END;
  12570. parameter :=procedureType.lastParameter;
  12571. WHILE (parameter # NIL) DO
  12572. INC(parSize,system.SizeOfParameter(parameter));
  12573. parSize := parSize + (-parSize) MOD system.addressSize;
  12574. parameter := parameter.prevParameter;
  12575. END;
  12576. IF procedureType.selfParameter # NIL THEN
  12577. parameter := procedureType.selfParameter;
  12578. INC(parSize,system.SizeOfParameter(parameter));
  12579. parSize := parSize + (-parSize) MOD system.addressSize;
  12580. ELSIF procedureType.isDelegate THEN INC(parSize,system.addressSize)
  12581. END; (* method => self pointer *)
  12582. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12583. RETURN ToMemoryUnits(system,parSize)
  12584. END ParametersSize;
  12585. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12586. BEGIN
  12587. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12588. END IsNested;
  12589. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12590. BEGIN
  12591. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12592. scope := scope.outerScope;
  12593. END;
  12594. RETURN scope # NIL;
  12595. END InCellScope;
  12596. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12597. BEGIN
  12598. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12599. RETURN 0
  12600. ELSE
  12601. *)
  12602. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12603. (*END;*)
  12604. END ProcedureParametersSize;
  12605. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12606. VAR dataUnit: LONGINT;
  12607. BEGIN dataUnit := system.dataUnit;
  12608. ASSERT(size MOD system.dataUnit = 0);
  12609. RETURN size DIV system.dataUnit
  12610. END ToMemoryUnits;
  12611. PROCEDURE Get*(): Backend.Backend;
  12612. VAR backend: IntermediateBackend;
  12613. BEGIN NEW(backend); RETURN backend
  12614. END Get;
  12615. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12616. VAR instruction: IntermediateCode.Instruction;
  12617. BEGIN
  12618. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12619. RETURN instruction
  12620. END Nop;
  12621. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12622. VAR instruction: IntermediateCode.Instruction;
  12623. BEGIN
  12624. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12625. RETURN instruction
  12626. END Mov;
  12627. (* like Mov but ensures that no new register will be allocated for dest *)
  12628. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12629. VAR instruction: IntermediateCode.Instruction;
  12630. BEGIN
  12631. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12632. RETURN instruction
  12633. END MovReplace;
  12634. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12635. VAR instruction: IntermediateCode.Instruction;
  12636. BEGIN
  12637. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12638. RETURN instruction
  12639. END Conv;
  12640. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12641. BEGIN
  12642. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12643. END SysvABI;
  12644. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12645. BEGIN
  12646. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12647. END SysvABIorWINAPI;
  12648. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12649. VAR instruction: IntermediateCode.Instruction;
  12650. BEGIN
  12651. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12652. RETURN instruction
  12653. END Call;
  12654. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12655. VAR op1, op2, op3: IntermediateCode.Operand;
  12656. VAR instruction: IntermediateCode.Instruction;
  12657. BEGIN
  12658. IntermediateCode.InitNumber(op1,pcOffset);
  12659. IntermediateCode.InitNumber(op2,callingConvention);
  12660. IntermediateCode.InitNumber(op3,unwind);
  12661. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12662. RETURN instruction
  12663. END Exit;
  12664. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12665. VAR instruction: IntermediateCode.Instruction;
  12666. BEGIN
  12667. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12668. RETURN instruction
  12669. END Return;
  12670. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12671. VAR instruction: IntermediateCode.Instruction;
  12672. BEGIN
  12673. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12674. RETURN instruction
  12675. END Result;
  12676. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12677. VAR op1: IntermediateCode.Operand;
  12678. VAR instruction: IntermediateCode.Instruction;
  12679. BEGIN
  12680. IntermediateCode.InitNumber(op1,nr);
  12681. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12682. RETURN instruction
  12683. END Trap;
  12684. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12685. VAR instruction: IntermediateCode.Instruction;
  12686. BEGIN
  12687. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12688. RETURN instruction
  12689. END Br;
  12690. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12691. VAR instruction: IntermediateCode.Instruction;
  12692. BEGIN
  12693. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12694. RETURN instruction
  12695. END Breq;
  12696. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12697. VAR instruction: IntermediateCode.Instruction;
  12698. BEGIN
  12699. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12700. RETURN instruction
  12701. END Brne;
  12702. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12703. VAR instruction: IntermediateCode.Instruction;
  12704. BEGIN
  12705. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12706. RETURN instruction
  12707. END Brge;
  12708. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12709. VAR instruction: IntermediateCode.Instruction;
  12710. BEGIN
  12711. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12712. RETURN instruction
  12713. END Brlt;
  12714. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12715. VAR instruction: IntermediateCode.Instruction;
  12716. BEGIN
  12717. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12718. RETURN instruction
  12719. END Pop;
  12720. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12721. VAR instruction: IntermediateCode.Instruction;
  12722. BEGIN
  12723. ASSERT(op.mode # IntermediateCode.Undefined);
  12724. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12725. RETURN instruction
  12726. END Push;
  12727. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12728. VAR instruction: IntermediateCode.Instruction;
  12729. BEGIN
  12730. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12731. RETURN instruction
  12732. END Neg;
  12733. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12734. VAR instruction: IntermediateCode.Instruction;
  12735. BEGIN
  12736. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12737. RETURN instruction
  12738. END Not;
  12739. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12740. VAR instruction: IntermediateCode.Instruction;
  12741. BEGIN
  12742. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12743. RETURN instruction
  12744. END Abs;
  12745. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12746. VAR instruction: IntermediateCode.Instruction;
  12747. BEGIN
  12748. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12749. ASSERT(~IsImmediate(instruction.op1));
  12750. RETURN instruction
  12751. END Mul;
  12752. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12753. VAR instruction: IntermediateCode.Instruction;
  12754. BEGIN
  12755. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12756. RETURN instruction
  12757. END Div;
  12758. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12759. VAR instruction: IntermediateCode.Instruction;
  12760. BEGIN
  12761. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12762. RETURN instruction
  12763. END Mod;
  12764. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12765. VAR instruction: IntermediateCode.Instruction;
  12766. BEGIN
  12767. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12768. RETURN instruction
  12769. END Sub;
  12770. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12771. VAR instruction: IntermediateCode.Instruction;
  12772. BEGIN
  12773. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12774. RETURN instruction
  12775. END Add;
  12776. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12777. VAR instruction: IntermediateCode.Instruction;
  12778. BEGIN
  12779. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12780. RETURN instruction
  12781. END And;
  12782. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12783. VAR instruction: IntermediateCode.Instruction;
  12784. BEGIN
  12785. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12786. RETURN instruction
  12787. END Or;
  12788. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12789. VAR instruction: IntermediateCode.Instruction;
  12790. BEGIN
  12791. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12792. RETURN instruction
  12793. END Xor;
  12794. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12795. VAR instruction: IntermediateCode.Instruction;
  12796. BEGIN
  12797. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12798. RETURN instruction
  12799. END Shl;
  12800. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12801. VAR instruction: IntermediateCode.Instruction;
  12802. BEGIN
  12803. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12804. RETURN instruction
  12805. END Shr;
  12806. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12807. VAR instruction: IntermediateCode.Instruction;
  12808. BEGIN
  12809. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12810. RETURN instruction
  12811. END Rol;
  12812. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12813. VAR instruction: IntermediateCode.Instruction;
  12814. BEGIN
  12815. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12816. RETURN instruction
  12817. END Ror;
  12818. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12819. VAR instruction: IntermediateCode.Instruction;
  12820. BEGIN
  12821. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12822. RETURN instruction
  12823. END Cas;
  12824. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12825. VAR instruction: IntermediateCode.Instruction;
  12826. BEGIN
  12827. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12828. RETURN instruction
  12829. END Copy;
  12830. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12831. VAR instruction: IntermediateCode.Instruction;
  12832. BEGIN
  12833. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12834. RETURN instruction
  12835. END Fill;
  12836. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12837. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12838. BEGIN
  12839. string := IntermediateCode.String(s);
  12840. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12841. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12842. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12843. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12844. RETURN instruction
  12845. END Asm;
  12846. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12847. VAR instruction: IntermediateCode.Instruction;
  12848. BEGIN
  12849. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12850. RETURN instruction
  12851. END Data;
  12852. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12853. VAR instruction: IntermediateCode.Instruction;
  12854. BEGIN
  12855. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12856. IntermediateCode.SetSubType(instruction, subtype);
  12857. RETURN instruction
  12858. END SpecialInstruction;
  12859. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12860. VAR op1: IntermediateCode.Operand;
  12861. VAR instruction: IntermediateCode.Instruction;
  12862. BEGIN
  12863. (*! generate a warning if size exceeds a certain limit *)
  12864. (*
  12865. ASSERT(bytes < 1000000); (* sanity check *)
  12866. *)
  12867. ASSERT(0 <= units); (* sanity check *)
  12868. IntermediateCode.InitNumber(op1,units);
  12869. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12870. RETURN instruction
  12871. END Reserve;
  12872. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12873. VAR op1: IntermediateCode.Operand;
  12874. VAR instruction: IntermediateCode.Instruction;
  12875. BEGIN
  12876. IntermediateCode.InitNumber(op1,position.start);
  12877. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12878. RETURN instruction
  12879. END LabelInstruction;
  12880. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12881. VAR pc: LONGINT;
  12882. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12883. BEGIN
  12884. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12885. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12886. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12887. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12888. IF instr.op1.type.form = IntermediateCode.Float THEN
  12889. RETURN instr.op1.floatValue = op.floatValue
  12890. ELSE
  12891. RETURN instr.op1.intValue = op.intValue
  12892. END;
  12893. END ProvidesValue;
  12894. BEGIN
  12895. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12896. pc := 0;
  12897. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12898. INC(pc);
  12899. END;
  12900. IF pc = data.pc THEN
  12901. data.Emit(Data(Basic.invalidPosition,vop));
  12902. END;
  12903. RETURN pc
  12904. END EnterImmediate;
  12905. PROCEDURE Init;
  12906. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12907. BEGIN
  12908. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12909. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12910. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12911. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12912. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12913. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12914. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12915. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12916. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12917. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12918. IntermediateCode.InitOperand(emptyOperand);
  12919. FOR i := 0 TO NumberSystemCalls-1 DO
  12920. name := "@SystemCall";
  12921. Basic.AppendNumber(name,i);
  12922. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12923. END;
  12924. END Init;
  12925. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12926. BEGIN
  12927. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12928. END IsExported;
  12929. BEGIN
  12930. Init;
  12931. END FoxIntermediateBackend.
  12932. Compiler.Compile FoxIntermediateBackend.Mod ~
  12933. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12934. # 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 ~
  12935. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12936. # 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 ~
  12937. FSTools.CloseFiles A2Z.exe ~
  12938. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12939. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12940. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12941. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12942. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12943. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12944. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12945. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12946. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12947. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12948. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod