FoxIntermediateBackend.Mod 557 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187
  1. MODULE FoxIntermediateBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, Backend := FoxBackend, Global := FoxGlobal,
  3. Scanner := FoxScanner, IntermediateCode := FoxIntermediateCode, Sections := FoxSections, BinaryCode := FoxBinaryCode, Printout := FoxPrintout,
  4. SYSTEM, Diagnostics, Strings, Options, Streams, Compiler, Formats := FoxFormats, SymbolFileFormat := FoxTextualSymbolFile, D := Debugging,
  5. FingerPrinter := FoxFingerPrinter, StringPool;
  6. CONST
  7. (* operand modes *)
  8. ModeUndefined = 0;
  9. ModeReference = 1;
  10. ModeValue = 2;
  11. (* heap data offsets *)
  12. ArrayDimTable = 3; (* dimension table in dyn arrays *)
  13. (* math array offsets *)
  14. MathPtrOffset=0;
  15. MathAdrOffset=1;
  16. MathFlagsOffset=2;
  17. MathDimOffset=3;
  18. MathElementSizeOffset=4;
  19. MathLenOffset=5;
  20. MathIncrOffset=6;
  21. SysDataArrayOffset* = 0; (* array offset in system bl ock, for 32 byte alignment *)
  22. ArrDataArrayOffset*= 16*8; (* 16 bytes array offset in array block, to be compatible with the GC scheme of POINTER TO ARRAY OF ... *)
  23. TensorFlag* = 0; (* flag indicating a tensor array *)
  24. RangeFlag* = 1; (* flag indicating a range, e.g. an array derived from A[..,..] *)
  25. StackFlag* = 2; (* flag indicates temporary result *)
  26. StaticFlag* = 1; (* flag indicating a static array, may not be reallocated *)
  27. (** compiler generated traps *)
  28. WithTrap* = 1; (* generated when a WITH statement fails *)
  29. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  30. ReturnTrap* = 3;
  31. TypeEqualTrap* = 5;
  32. TypeCheckTrap* = 6;
  33. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  34. AssertTrap* = 8; (* generated when an assert fails *)
  35. ArraySizeTrap* = 9;
  36. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  37. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  38. NegativeDivisorTrap*=12;
  39. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  40. NilPointerTrap*=17; (* indicates that a nil pointer was being dereferenced *)
  41. Trace = FALSE;
  42. TraceRegisterUsageCount=TRUE;
  43. ArrayAlignment = 8*8; (* first array element of ArrayBlock and first data element of SystemBlock must be aligned to 0 MOD ArrayAlignment *)
  44. (** system call numbers *)
  45. NumberSystemCalls* = 12;
  46. SysNewRec* = 0; SysNewArr* = 1; SysNewSys* = 2; SysCaseTable* = 3; SysProcAddr* = 4;
  47. SysLock* = 5; SysUnlock* = 6; SysStart* = 7; SysAwait* = 8; SysInterfaceLookup* = 9;
  48. SysRegisterInterface* = 10; SysGetProcedure* = 11;
  49. DefaultRuntimeModuleName ="Runtime";
  50. DefaultTraceModuleName ="KernelLog";
  51. ChannelModuleName = "Channels";
  52. NonPointer = -1; (* special pointer values *)
  53. NoType = 0; (* special type info values *)
  54. LhsIsPointer = 0; (* for the operator kind *)
  55. RhsIsPointer = 1;
  56. (* priority values, lower means higher priority *)
  57. EntryPriority=-4;
  58. FirstPriority=-3;
  59. InitPriority=-2;
  60. ExitPriority=-1;
  61. BasePointerTypeSize = 5;
  62. BaseArrayTypeSize = BasePointerTypeSize + 3;
  63. LengthOffset = BasePointerTypeSize + 0;
  64. DataOffset = BasePointerTypeSize + 1;
  65. DescriptorOffset = BasePointerTypeSize + 2;
  66. BaseRecordTypeSize = BasePointerTypeSize + 2;
  67. ActionOffset = BasePointerTypeSize + 0;
  68. MonitorOffset = BasePointerTypeSize + 1;
  69. BaseObjectTypeSize = BaseRecordTypeSize;
  70. ActionTypeSize = 3;
  71. MonitorTypeSize = 7;
  72. ProcessorOffset = BaseObjectTypeSize + 1;
  73. StackLimitOffset* = BaseObjectTypeSize + 3;
  74. QuantumOffset = BaseObjectTypeSize + 4;
  75. (* flags for optimizations with small matricies and vectors (Alexey Morozov) *)
  76. SmallMatrixFlag = 3; (* flag for identification of a small matrix *)
  77. SmallVectorFlag = 3; (* flag for identification of a small vector *)
  78. Size2Flag = 4; (* size = 2 *)
  79. Size3Flag = 5; (* size = 3 *)
  80. Size4Flag = 6; (* size = 4 *)
  81. Size5Flag = 7; (* size = 5 *)
  82. Size6Flag = 8; (* size = 6 *)
  83. Size7Flag = 9; (* size = 7 *)
  84. Size8Flag = 10; (* size = 8 *)
  85. ReflectionSupport = TRUE;
  86. (* Solution for identifying procedure descriptors on the stack and for being able to differentiate "old school" stack frames from the underlying operating system stack frames:
  87. push a procedure desriptor plus one to where the BP pointer would be located. The misalignment of the procedure descriptor makes it possible to identify that it is not
  88. a base pointer but a procedure descriptor. The base pointer itself is in such cases located at BP + address size.
  89. *)
  90. TYPE
  91. Position=SyntaxTree.Position;
  92. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  93. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  94. Operand = RECORD
  95. mode: SHORTINT;
  96. op: IntermediateCode.Operand;
  97. tag: IntermediateCode.Operand;
  98. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  99. dimOffset: LONGINT;
  100. END;
  101. Fixup= POINTER TO RECORD
  102. pc: LONGINT;
  103. nextFixup: Fixup;
  104. END;
  105. WriteBackCall = POINTER TO RECORD
  106. call: SyntaxTree.ProcedureCallDesignator;
  107. next: WriteBackCall;
  108. END;
  109. Label= OBJECT
  110. VAR
  111. fixups: Fixup;
  112. section: IntermediateCode.Section;
  113. pc: LONGINT;
  114. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  115. BEGIN
  116. SELF.section := section; pc := -1;
  117. END InitLabel;
  118. PROCEDURE Resolve(pc: LONGINT);
  119. VAR at: LONGINT;
  120. BEGIN
  121. SELF.pc := pc;
  122. WHILE(fixups # NIL) DO
  123. at := fixups.pc;
  124. section.PatchAddress(at,pc);
  125. fixups := fixups.nextFixup;
  126. END;
  127. END Resolve;
  128. PROCEDURE AddFixup(at: LONGINT);
  129. VAR fixup: Fixup;
  130. BEGIN
  131. ASSERT(pc=-1);
  132. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  133. END AddFixup;
  134. END Label;
  135. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  136. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  137. VAR
  138. backend: IntermediateBackend;
  139. implementationVisitor: ImplementationVisitor;
  140. meta: MetaDataGenerator;
  141. system: Global.System;
  142. currentScope: SyntaxTree.Scope;
  143. module: Sections.Module;
  144. moduleSelf: SyntaxTree.Variable;
  145. dump: BOOLEAN;
  146. forceModuleBody: BOOLEAN;
  147. addressType: IntermediateCode.Type;
  148. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  149. BEGIN
  150. currentScope := NIL; module := NIL; moduleSelf := NIL;
  151. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  152. SELF.dump := dump;
  153. SELF.backend := backend;
  154. SELF.forceModuleBody := forceModuleBody;
  155. addressType := IntermediateCode.GetType(system,system.addressType)
  156. END Init;
  157. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  158. BEGIN
  159. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  160. END Error;
  161. PROCEDURE Type(x: SyntaxTree.Type);
  162. BEGIN
  163. x.Accept(SELF);
  164. END Type;
  165. (** types **)
  166. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  167. BEGIN (* no code emission *) END VisitBasicType;
  168. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  169. BEGIN (* no code emission *) END VisitCharacterType;
  170. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  171. BEGIN (* no code emission *) END VisitIntegerType;
  172. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  173. BEGIN (* no code emission *) END VisitFloatType;
  174. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  175. BEGIN (* no code emission *) END VisitComplexType;
  176. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  177. VAR type: SyntaxTree.Type;
  178. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  179. type := x.resolved;
  180. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  181. meta.CheckTypeDeclaration(type);
  182. END;
  183. END VisitQualifiedType;
  184. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  185. BEGIN (* no code emission *) END VisitStringType;
  186. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  187. BEGIN (* no code emission *)
  188. END VisitArrayRangeType;
  189. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  190. BEGIN (* no code emission *) END VisitArrayType;
  191. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  192. BEGIN (* no code emission *) END VisitPortType;
  193. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  194. BEGIN
  195. meta.CheckTypeDeclaration(x);
  196. END VisitMathArrayType;
  197. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  198. BEGIN
  199. meta.CheckTypeDeclaration(x);
  200. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  201. END VisitPointerType;
  202. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  203. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  204. BEGIN (* no code emission *)
  205. meta.CheckTypeDeclaration(x);
  206. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  207. IF x.pointerType.typeDeclaration # NIL THEN
  208. td := x.pointerType.typeDeclaration
  209. ELSE
  210. td := x.typeDeclaration
  211. END;
  212. Global.GetSymbolName(td,name);
  213. (* code section for object *)
  214. END;
  215. Scope(x.recordScope);
  216. END VisitRecordType;
  217. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  218. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  219. BEGIN
  220. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  221. WHILE (this # NIL) & (this.identifier# id) DO
  222. this := this.nextModifier;
  223. END;
  224. RETURN this # NIL
  225. END HasFlag;
  226. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  227. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  228. BEGIN
  229. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  230. capabilities := {};
  231. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  232. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  233. backend.SetCapabilities(capabilities);
  234. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  235. Scope(x.cellScope);
  236. END;
  237. END VisitCellType;
  238. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  239. BEGIN (* no code emission *) END VisitProcedureType;
  240. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  241. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  242. END VisitEnumerationType;
  243. (* symbols *)
  244. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  245. BEGIN
  246. Procedure(x);
  247. END VisitProcedure;
  248. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  249. BEGIN
  250. Procedure(x);
  251. END VisitOperator;
  252. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  253. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  254. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  255. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  256. BEGIN
  257. type := type.resolved;
  258. IF type IS SyntaxTree.RecordType THEN
  259. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  260. ELSIF (type IS SyntaxTree.ArrayType) THEN
  261. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  262. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  263. END;
  264. ELSIF type IS SyntaxTree.MathArrayType THEN
  265. WITH type: SyntaxTree.MathArrayType DO
  266. IF type.form = SyntaxTree.Open THEN
  267. RETURN TRUE
  268. ELSIF type.form = SyntaxTree.Static THEN
  269. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  270. END;
  271. END;
  272. END;
  273. RETURN FALSE
  274. END TypeNeedsInitialization;
  275. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  276. VAR variable: SyntaxTree.Variable;
  277. BEGIN
  278. variable := scope.firstVariable;
  279. WHILE variable # NIL DO
  280. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  281. IF variable.initializer # NIL THEN RETURN TRUE END;
  282. variable := variable.nextVariable;
  283. END;
  284. RETURN FALSE
  285. END ScopeNeedsInitialization;
  286. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  287. BEGIN
  288. size := offset - lastUpdated;
  289. IF size > 0 THEN
  290. irv.Emit(Reserve(x.position,size));
  291. END;
  292. irv.Emit(Data(x.position, op));
  293. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  294. END SingleInitialize;
  295. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  296. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  297. BEGIN
  298. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  299. WITH type: SyntaxTree.RecordType DO
  300. baseType := type.baseType;
  301. IF baseType # NIL THEN
  302. baseType := baseType.resolved;
  303. IF baseType IS SyntaxTree.PointerType THEN
  304. baseType := baseType(SyntaxTree.PointerType).pointerBase
  305. END;
  306. Initialize(baseType,NIL, offset);
  307. END;
  308. variable := type.recordScope.firstVariable;
  309. WHILE variable # NIL DO
  310. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  311. variable := variable.nextVariable
  312. END;
  313. | type: SyntaxTree.ArrayType DO
  314. IF type.form = SyntaxTree.Static THEN
  315. baseType := type.arrayBase;
  316. IF TypeNeedsInitialization(baseType) THEN
  317. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  318. FOR i := 0 TO type.staticLength-1 DO
  319. Initialize(baseType,NIL,offset+i*size);
  320. END;
  321. END;
  322. END;
  323. | type: SyntaxTree.MathArrayType DO
  324. IF type.form = SyntaxTree.Open THEN
  325. dim := DynamicDim(type);
  326. baseType := SemanticChecker.ArrayBase(type,dim);
  327. imm := IntermediateCode.Immediate(addressType,dim);
  328. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  329. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  330. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  331. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  332. (* flags remain empty (=0) for open array *)
  333. ELSIF type.form = SyntaxTree.Static THEN
  334. baseType := type.arrayBase;
  335. IF TypeNeedsInitialization(baseType) THEN
  336. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  337. ASSERT(type.staticLength < 1024*1024*1024);
  338. FOR i := 0 TO type.staticLength-1 DO
  339. Initialize(baseType,NIL,offset+i*size);
  340. END;
  341. END;
  342. END;
  343. ELSE
  344. IF initializer # NIL THEN
  345. implementationVisitor.Evaluate(initializer, op);
  346. SingleInitialize(op.op, offset);
  347. END;
  348. END;
  349. END Initialize;
  350. BEGIN
  351. IF x.externalName # NIL THEN RETURN END;
  352. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  353. (* code section for variable *)
  354. Global.GetSymbolSegmentedName(x,name);
  355. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  356. irv.SetExported(IsExported(x));
  357. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  358. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  359. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  360. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  361. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  362. FOR i := 0 TO DynamicDim(x.type)-1 DO
  363. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  364. END;
  365. ELSE
  366. lastUpdated:= 0;
  367. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  368. Initialize(x.type, x.initializer, 0);
  369. END;
  370. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  371. IF size > 0 THEN
  372. irv.Emit(Reserve(x.position,size));
  373. END;
  374. IF ~x.fixed THEN
  375. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  376. ELSE
  377. align := x.alignment;
  378. END;
  379. irv.SetPositionOrAlignment(x.fixed, align);
  380. meta.CheckTypeDeclaration(x.type);
  381. END;
  382. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  383. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  384. END;
  385. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  386. END VisitVariable;
  387. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  388. BEGIN
  389. VisitVariable(x)
  390. END VisitProperty;
  391. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  392. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  393. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  394. BEGIN
  395. ASSERT(currentScope IS SyntaxTree.CellScope);
  396. Global.GetSymbolSegmentedName(x,name);
  397. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  398. irv.SetExported(IsExported(x));
  399. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  400. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  401. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  403. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  404. FOR i := 0 TO DynamicDim(x.type)-1 DO
  405. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  406. END;
  407. ELSE
  408. lastUpdated:= 0;
  409. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  410. IF size > 0 THEN
  411. irv.Emit(Reserve(x.position,size));
  412. END;
  413. IF ~x.fixed THEN
  414. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  415. ELSE
  416. align := x.alignment;
  417. END;
  418. irv.SetPositionOrAlignment(x.fixed, align);
  419. meta.CheckTypeDeclaration(x.type);
  420. END;
  421. END VisitParameter;
  422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  423. BEGIN
  424. Type(x.declaredType); (* => code in objects *)
  425. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  426. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  427. END;
  428. END VisitTypeDeclaration;
  429. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  430. BEGIN
  431. IF (SyntaxTree.Public * x.access # {}) THEN
  432. implementationVisitor.VisitConstant(x);
  433. END;
  434. END VisitConstant;
  435. PROCEDURE Scope(x: SyntaxTree.Scope);
  436. VAR procedure: SyntaxTree.Procedure;
  437. constant: SyntaxTree.Constant;
  438. variable: SyntaxTree.Variable;
  439. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  440. cell: SyntaxTree.CellType;
  441. parameter: SyntaxTree.Parameter;
  442. property: SyntaxTree.Property;
  443. BEGIN
  444. prevScope := currentScope;
  445. currentScope := x;
  446. (* constants treated in implementation visitor *)
  447. WITH x: SyntaxTree.CellScope DO
  448. cell := x.ownerCell;
  449. parameter := cell.firstParameter;
  450. WHILE parameter # NIL DO
  451. VisitParameter(parameter);
  452. parameter := parameter.nextParameter;
  453. END;
  454. property := cell.firstProperty;
  455. WHILE property # NIL DO
  456. VisitProperty(property);
  457. property := property.nextProperty;
  458. END;
  459. ELSE
  460. END;
  461. typeDeclaration := x.firstTypeDeclaration;
  462. WHILE typeDeclaration # NIL DO
  463. VisitTypeDeclaration(typeDeclaration);
  464. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  465. END;
  466. variable := x.firstVariable;
  467. WHILE variable # NIL DO
  468. VisitVariable(variable);
  469. variable := variable.nextVariable;
  470. END;
  471. procedure := x.firstProcedure;
  472. WHILE procedure # NIL DO
  473. VisitProcedure(procedure);
  474. procedure := procedure.nextProcedure;
  475. END;
  476. constant := x.firstConstant;
  477. WHILE constant # NIL DO
  478. VisitConstant(constant);
  479. constant := constant.nextConstant;
  480. END;
  481. currentScope := prevScope;
  482. END Scope;
  483. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  484. VAR parameter: SyntaxTree.Parameter;
  485. BEGIN
  486. parameter := first;
  487. WHILE parameter # NIL DO
  488. VisitParameter(parameter);
  489. parameter := parameter.nextParameter;
  490. END;
  491. END Parameters;
  492. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  493. VAR scope: SyntaxTree.ProcedureScope;
  494. prevScope: SyntaxTree.Scope;
  495. inline, finalizer: BOOLEAN;
  496. procedureType: SyntaxTree.ProcedureType;
  497. pc: LONGINT;
  498. stackSize: LONGINT;
  499. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  500. null,size,src,dest,fp,res: IntermediateCode.Operand;
  501. cc: LONGINT;
  502. cellType: SyntaxTree.CellType;
  503. registerNumber: LONGINT;
  504. registerParameter: Backend.Registers;
  505. registerParameters: LONGINT;
  506. registerClass: IntermediateCode.RegisterClass;
  507. type: IntermediateCode.Type;
  508. formalParameter: SyntaxTree.Parameter;
  509. recordType: SyntaxTree.RecordType;
  510. isModuleBody: BOOLEAN;
  511. parametersSize: LONGINT;
  512. PROCEDURE Signature;
  513. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  514. BEGIN
  515. procedureType := x.type(SyntaxTree.ProcedureType);
  516. returnType := procedureType.returnType;
  517. IF returnType # NIL THEN
  518. meta.CheckTypeDeclaration(returnType)
  519. END;
  520. parameter := procedureType.firstParameter;
  521. WHILE parameter # NIL DO
  522. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  523. parameter := parameter.nextParameter;
  524. END;
  525. END Signature;
  526. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  527. VAR result: BOOLEAN;
  528. BEGIN
  529. result := FALSE;
  530. IF x = SyntaxTree.invalidExpression THEN
  531. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  532. result := TRUE;
  533. value := x.resolved(SyntaxTree.IntegerValue).value;
  534. ELSE
  535. Error(x.position,"expression is not an integer constant");
  536. END;
  537. RETURN result;
  538. END CheckIntegerValue;
  539. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  540. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  541. BEGIN
  542. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  543. WHILE (this # NIL) & (this.identifier # id) DO
  544. this := this.nextModifier;
  545. END;
  546. IF this # NIL THEN
  547. IF this.expression = NIL THEN
  548. Error(this.position,"expected expression value");
  549. ELSIF CheckIntegerValue(this.expression,value) THEN
  550. END;
  551. RETURN TRUE
  552. ELSE RETURN FALSE
  553. END;
  554. END HasValue;
  555. CONST DefaultDataMemorySize=512;
  556. BEGIN
  557. IF x.externalName # NIL THEN RETURN END;
  558. (*
  559. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  560. *)
  561. (* code section for this procedure *)
  562. scope := x.procedureScope;
  563. prevScope := currentScope;
  564. currentScope := scope;
  565. procedureType := x.type(SyntaxTree.ProcedureType);
  566. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  567. implementationVisitor.temporaries.Init;
  568. implementationVisitor.usedRegisters := NIL;
  569. implementationVisitor.registerUsageCount.Init;
  570. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  571. IF (scope.body # NIL) & (x.isInline) THEN
  572. inline := TRUE;
  573. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  574. ir.SetExported(IsExported(x));
  575. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  576. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  577. IF backend.cellsAreObjects THEN
  578. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  579. ir.SetExported(IsExported(x));
  580. ELSE
  581. RETURN; (* cellnet cannot be compiled for final static hardware *)
  582. END;
  583. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  584. inline := FALSE;
  585. AddBodyCallStub(x);
  586. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  587. ir.SetExported(IsExported(x));
  588. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  589. inline := FALSE;
  590. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  591. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  592. AddBodyCallStub(x);
  593. AddStackAllocation(x,stackSize);
  594. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  595. ir.SetExported(IsExported(x));
  596. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  597. inline := FALSE;
  598. Parameters(procedureType.firstParameter);
  599. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  600. ir.SetExported(IsExported(x));
  601. ELSE
  602. inline := FALSE;
  603. IF x.isEntry OR x.isExit THEN
  604. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  605. ir.SetExported(TRUE);
  606. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  607. ELSE
  608. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  609. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  610. END;
  611. END;
  612. cc := procedureType.callingConvention;
  613. IF cc = SyntaxTree.WinAPICallingConvention THEN
  614. parametersSize := ProcedureParametersSize(backend.system,x);
  615. ELSE
  616. parametersSize := 0;
  617. END;
  618. IF scope.body # NIL THEN
  619. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  620. registerNumber := 0;
  621. IF ~inline THEN
  622. IF scope.lastVariable = NIL THEN
  623. stackSize := 0
  624. ELSE
  625. stackSize := scope.lastVariable.offsetInBits;
  626. IF stackSize <0 THEN stackSize := -stackSize END;
  627. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  628. END;
  629. (*
  630. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  632. *)
  633. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  634. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  635. END;
  636. pc := ir.pc-1;
  637. (*
  638. ir.Emit(Nop(position)); (* placeholder for fill *)
  639. *)
  640. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  641. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  642. IF registerParameter = NIL THEN registerParameters := 0
  643. ELSE registerParameters := LEN(registerParameter)
  644. END;
  645. formalParameter := procedureType.lastParameter;
  646. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  647. IF ~PassInRegister(formalParameter) THEN
  648. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  649. ELSE
  650. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  651. type := GetType(system, formalParameter.type);
  652. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  653. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  654. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  655. implementationVisitor.ReleaseIntermediateOperand(src);
  656. INC(registerNumber);
  657. formalParameter := formalParameter.prevParameter;
  658. END;
  659. END;
  660. END;
  661. END;
  662. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  663. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  664. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  665. IF scope.lastVariable # NIL THEN
  666. stackSize := scope.lastVariable.offsetInBits;
  667. IF stackSize <0 THEN stackSize := -stackSize END;
  668. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  669. END;
  670. END;
  671. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  672. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  673. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  674. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  675. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  676. ir.EmitAt(pc,Push(size));
  677. implementationVisitor.StaticCallOperand(result,procedure);
  678. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  679. END;
  680. *)
  681. END;
  682. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  683. IF stackSize > 0 THEN
  684. IF (stackSize MOD system.addressSize = 0) THEN
  685. null := IntermediateCode.Immediate(addressType,0);
  686. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  687. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  688. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  689. ELSE
  690. null := IntermediateCode.Immediate(int8,0);
  691. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  692. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  693. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  694. END;
  695. (*! should potentially be called by backend -- enter might initialize
  696. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  697. *)
  698. END;
  699. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  700. parametersSize := ProcedureParametersSize(backend.system,x);
  701. ELSE
  702. parametersSize := 0;
  703. END;
  704. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  705. finalizer := FALSE;
  706. IF backend.cooperative & x.isFinalizer THEN
  707. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  708. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  709. GetRecordTypeName(recordType,name);
  710. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  711. END;
  712. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  713. IF backend.cooperative THEN
  714. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  715. IF implementationVisitor.profile & ~isModuleBody THEN
  716. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  717. END;
  718. END;
  719. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  720. IF finalizer THEN
  721. IF backend.hasLinkRegister THEN
  722. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  723. END;
  724. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  725. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  726. ir.Emit(Br(x.position,dest));
  727. ELSE
  728. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  729. END;
  730. ELSE
  731. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  732. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  733. END;
  734. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  735. IF backend.cooperative THEN
  736. IF HasPointers (scope) THEN
  737. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  738. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  739. ir.Emit(Result(x.position, res));
  740. ir.Emit(Push(x.position, res));
  741. implementationVisitor.ResetVariables(scope);
  742. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  743. ir.Emit(Pop(x.position, res));
  744. ir.Emit(Return(x.position, res));
  745. ELSE
  746. implementationVisitor.ResetVariables(scope);
  747. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  748. END;
  749. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  750. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  751. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  752. ir.Emit(Result(x.position, res));
  753. ir.Emit(Push(x.position, res));
  754. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  755. ir.Emit(Pop(x.position, res));
  756. ir.Emit(Return(x.position, res));
  757. ELSE
  758. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  759. END;
  760. END;
  761. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  762. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  763. ELSE
  764. ir.Emit(Nop(x.position));
  765. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  766. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  767. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  768. END;
  769. END;
  770. END
  771. END;
  772. ELSE (* force body for procedures *)
  773. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  774. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  775. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  776. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  777. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  778. END;
  779. Scope(scope);
  780. Signature;
  781. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  782. currentScope := prevScope;
  783. END Procedure;
  784. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  785. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  786. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  787. BEGIN
  788. ASSERT (bodyProcedure # NIL);
  789. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  790. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  791. procedure.SetScope(bodyProcedure.scope);
  792. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  793. procedure.SetAccess(SyntaxTree.Hidden);
  794. Global.GetSymbolSegmentedName (procedure,name);
  795. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  796. ir.SetExported(TRUE);
  797. ir.SetPriority(InitPriority);
  798. Global.GetSymbolSegmentedName (bodyProcedure,name);
  799. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  800. implementationVisitor.currentScope := module.module.moduleScope;
  801. implementationVisitor.section := ir;
  802. implementationVisitor.PushSelfPointer();
  803. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  804. ELSIF backend.preregisterStatic THEN
  805. implementationVisitor.currentScope := module.module.moduleScope;
  806. implementationVisitor.section := ir;
  807. implementationVisitor.PushSelfPointer();
  808. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  809. ELSE
  810. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  811. ir.Emit(Call(bodyProcedure.position,op, 0));
  812. END;
  813. END AddBodyCallStub;
  814. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  815. VAR name: Basic.SegmentedName;
  816. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  817. BEGIN
  818. Global.GetSymbolSegmentedName (symbol,name);
  819. Basic.RemoveSuffix(name);
  820. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  821. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  822. ir.SetExported(TRUE);
  823. ir.SetPriority(FirstPriority);
  824. IntermediateCode.InitImmediate(op,addressType,initStack);
  825. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  826. END AddStackAllocation;
  827. (** entry function to visit a complete module *)
  828. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  829. VAR
  830. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  831. hasDynamicOperatorDeclarations: BOOLEAN;
  832. operator: SyntaxTree.Operator;
  833. import: SyntaxTree.Import;
  834. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  835. BEGIN
  836. type := type.resolved;
  837. IF type IS SyntaxTree.RecordType THEN
  838. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  839. ELSIF (type IS SyntaxTree.ArrayType) THEN
  840. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  841. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  842. END;
  843. ELSIF type IS SyntaxTree.MathArrayType THEN
  844. WITH type: SyntaxTree.MathArrayType DO
  845. IF type.form = SyntaxTree.Open THEN
  846. RETURN TRUE
  847. ELSIF type.form = SyntaxTree.Static THEN
  848. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  849. END;
  850. END;
  851. END;
  852. RETURN FALSE
  853. END TypeNeedsInitialization;
  854. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  855. VAR variable: SyntaxTree.Variable;
  856. BEGIN
  857. variable := scope.firstVariable;
  858. WHILE variable # NIL DO
  859. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  860. IF variable.initializer # NIL THEN RETURN TRUE END;
  861. variable := variable.nextVariable;
  862. END;
  863. RETURN FALSE
  864. END ScopeNeedsInitialization;
  865. BEGIN
  866. ASSERT(x # NIL); ASSERT(module # NIL);
  867. SELF.module := module;
  868. (* add import names to the generated Sections.Module *)
  869. import := x.moduleScope.firstImport;
  870. WHILE import # NIL DO
  871. import.module.GetName(idstr);
  872. module.imports.AddName(idstr);
  873. import := import.nextImport
  874. END;
  875. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  876. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  877. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  878. moduleSelf.SetType(system.anyType);
  879. moduleSelf.SetScope(x.moduleScope);
  880. moduleSelf.SetUntraced(TRUE);
  881. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  882. ir.SetExported(TRUE);
  883. IntermediateCode.InitImmediate(op,addressType,0);
  884. ir.Emit(Data(Basic.invalidPosition,op));
  885. END;
  886. implementationVisitor.module := module;
  887. implementationVisitor.moduleScope := x.moduleScope;
  888. implementationVisitor.moduleSelf := moduleSelf;
  889. implementationVisitor.canBeLoaded := TRUE;
  890. meta.SetModule(module);
  891. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  892. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  893. END;
  894. IF backend.profile THEN
  895. EnsureBodyProcedure(x.moduleScope);
  896. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  897. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  898. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  899. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  900. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  901. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  902. Global.GetModuleName(module.module,idstr);
  903. implementationVisitor.ProfilerAddModule(idstr);
  904. implementationVisitor.numberProcedures := 0;
  905. END;
  906. implementationVisitor.profile := backend.profile;
  907. (* check if there is at least one dynamic operator locally defined *)
  908. hasDynamicOperatorDeclarations := FALSE;
  909. operator := x.moduleScope.firstOperator;
  910. WHILE operator # NIL DO
  911. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  912. operator := operator.nextOperator
  913. END;
  914. (* add operator initialization code section *)
  915. IF hasDynamicOperatorDeclarations THEN
  916. EnsureBodyProcedure(x.moduleScope);
  917. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  918. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  919. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  920. END;
  921. Scope(x.moduleScope);
  922. IF hasDynamicOperatorDeclarations THEN
  923. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  924. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  925. END;
  926. IF backend.profile THEN
  927. implementationVisitor.ProfilerPatchInit;
  928. END;
  929. END Module;
  930. END DeclarationVisitor;
  931. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  932. RegisterUsageCount*=OBJECT
  933. VAR used: UsedArray; count: LONGINT;
  934. PROCEDURE &Init;
  935. VAR i: LONGINT;
  936. BEGIN
  937. count := 0;
  938. IF used = NIL THEN NEW(used,64); END;
  939. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  940. END Init;
  941. PROCEDURE Grow;
  942. VAR new: UsedArray; size,i: LONGINT;
  943. BEGIN
  944. size := LEN(used)*2;
  945. NEW(new,size);
  946. FOR i := 0 TO LEN(used)-1 DO
  947. new[i].count := used[i].count;
  948. new[i].type := used[i].type;
  949. new[i].map := used[i].map
  950. END;
  951. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  952. used := new
  953. END Grow;
  954. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  955. BEGIN
  956. INC(count);
  957. IF count = LEN(used) THEN Grow END;
  958. used[count].type := type;
  959. used[count].class := class;
  960. used[count].map := count;
  961. RETURN count;
  962. END Next;
  963. PROCEDURE IncUse(register: LONGINT);
  964. BEGIN
  965. INC(used[register].count);
  966. (*
  967. IF (register = 1) & (count > 30) THEN
  968. D.TraceBack;
  969. END;
  970. *)
  971. END IncUse;
  972. PROCEDURE DecUse(register: LONGINT);
  973. BEGIN
  974. DEC(used[register].count);
  975. END DecUse;
  976. PROCEDURE Map(register: LONGINT): LONGINT;
  977. VAR map : LONGINT;
  978. BEGIN
  979. IF register > 0 THEN
  980. map := used[register].map;
  981. WHILE register # map DO register := map; map := used[register].map END;
  982. END;
  983. RETURN register
  984. END Map;
  985. PROCEDURE Use(register: LONGINT): LONGINT;
  986. BEGIN
  987. IF register< LEN(used) THEN
  988. RETURN used[register].count
  989. ELSE
  990. RETURN 0
  991. END
  992. END Use;
  993. END RegisterUsageCount;
  994. RegisterEntry = POINTER TO RECORD
  995. prev,next: RegisterEntry;
  996. register: LONGINT;
  997. registerClass: IntermediateCode.RegisterClass;
  998. type: IntermediateCode.Type;
  999. END;
  1000. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1001. Variables = OBJECT (Basic.List)
  1002. VAR
  1003. inUse: VariableUse;
  1004. registerIndex: LONGINT;
  1005. PROCEDURE & Init;
  1006. VAR i: LONGINT;
  1007. BEGIN
  1008. InitList(16);
  1009. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1010. registerIndex := 1024;
  1011. END Init;
  1012. PROCEDURE GetUsage(VAR use: VariableUse);
  1013. BEGIN
  1014. use := inUse;
  1015. END GetUsage;
  1016. PROCEDURE SetUsage(CONST use: VariableUse);
  1017. BEGIN
  1018. inUse := use;
  1019. END SetUsage;
  1020. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1021. VAR any: ANY;
  1022. BEGIN
  1023. any := Get(i);;
  1024. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1025. END GetVariable;
  1026. PROCEDURE Occupy(pos: LONGINT);
  1027. BEGIN
  1028. INCL(inUse[pos DIV 32], pos MOD 32);
  1029. END Occupy;
  1030. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1031. BEGIN
  1032. Occupy(Length());
  1033. Add(v);
  1034. END AddVariable;
  1035. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1036. VAR var : SyntaxTree.Variable;
  1037. BEGIN
  1038. FOR pos := 0 TO Length()-1 DO
  1039. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1040. var := GetVariable(pos);
  1041. IF type.SameType(var.type) THEN
  1042. Occupy(pos); RETURN var
  1043. END;
  1044. END;
  1045. END;
  1046. pos := Length();
  1047. RETURN NIL
  1048. END GetFreeVariable;
  1049. END Variables;
  1050. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  1051. SymbolMapper = OBJECT
  1052. VAR
  1053. first: SymbolMap;
  1054. PROCEDURE & Init;
  1055. BEGIN
  1056. first := NIL;
  1057. END Init;
  1058. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  1059. VAR new: SymbolMap;
  1060. BEGIN
  1061. NEW(new); new.this := this; new.to := to; new.tag := tag;
  1062. new.next := first; first := new;
  1063. END Add;
  1064. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1065. VAR s: SymbolMap;
  1066. BEGIN
  1067. s := first;
  1068. WHILE (s # NIL) & (s.this # this) DO
  1069. s := s.next
  1070. END;
  1071. RETURN s
  1072. END Get;
  1073. END SymbolMapper;
  1074. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1075. VAR
  1076. system: Global.System;
  1077. section: IntermediateCode.Section;
  1078. module: Sections.Module;
  1079. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1080. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1081. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1082. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1083. checker: SemanticChecker.Checker;
  1084. backend: IntermediateBackend;
  1085. meta: MetaDataGenerator;
  1086. position: Position;
  1087. moduleSelf: SyntaxTree.Variable;
  1088. (* variables for hand over of variables / temporary state *)
  1089. currentScope: SyntaxTree.Scope;
  1090. constantDeclaration : SyntaxTree.Symbol;
  1091. result: Operand; (* result of the most recent expression / statement *)
  1092. destination: IntermediateCode.Operand;
  1093. arrayDestinationTag: IntermediateCode.Operand;
  1094. arrayDestinationDimension:LONGINT;
  1095. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1096. conditional: BOOLEAN;
  1097. trueLabel, falseLabel, exitLabel: Label;
  1098. locked: BOOLEAN;
  1099. (*
  1100. usedRegisters: Registers;
  1101. *)
  1102. registerUsageCount: RegisterUsageCount;
  1103. usedRegisters: RegisterEntry;
  1104. (* useful operands and types *)
  1105. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1106. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1107. commentPrintout: Printout.Printer;
  1108. dump: Streams.Writer;
  1109. tagsAvailable : BOOLEAN;
  1110. supportedInstruction: SupportedInstructionProcedure;
  1111. supportedImmediate: SupportedImmediateProcedure;
  1112. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1113. emitLabels: BOOLEAN;
  1114. runtimeModuleName : SyntaxTree.IdentifierString;
  1115. newObjectFile: BOOLEAN;
  1116. indexCounter: LONGINT;
  1117. profile: BOOLEAN;
  1118. profileId, profileInit: IntermediateCode.Section;
  1119. profileInitPatchPosition: LONGINT;
  1120. numberProcedures: LONGINT;
  1121. procedureResultDesignator : SyntaxTree.Designator;
  1122. operatorInitializationCodeSection: IntermediateCode.Section;
  1123. fingerPrinter: FingerPrinter.FingerPrinter;
  1124. temporaries: Variables;
  1125. canBeLoaded : BOOLEAN;
  1126. currentIsInline: BOOLEAN;
  1127. currentMapper: SymbolMapper;
  1128. currentInlineExit: Label;
  1129. moduleBodySection: IntermediateCode.Section;
  1130. NeedDescriptor : BOOLEAN;
  1131. cooperativeSwitches: BOOLEAN;
  1132. lastSwitchPC: LONGINT;
  1133. isUnchecked: BOOLEAN;
  1134. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1135. newObjectFile: BOOLEAN);
  1136. BEGIN
  1137. SELF.system := system;
  1138. SELF.runtimeModuleName := runtime;
  1139. currentScope := NIL;
  1140. hiddenPointerType := NIL;
  1141. delegatePointerType := NIL;
  1142. awaitProcCounter := 0;
  1143. labelId := 0; constId := 0; labelId := 0;
  1144. SELF.checker := checker;
  1145. SELF.backend := backend;
  1146. position := Basic.invalidPosition;
  1147. conditional := FALSE;
  1148. locked := FALSE;
  1149. InitOperand(result,ModeUndefined);
  1150. addressType := IntermediateCode.GetType(system,system.addressType);
  1151. setType := IntermediateCode.GetType(system,system.setType);
  1152. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1153. byteType := IntermediateCode.GetType(system, system.byteType);
  1154. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1155. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1156. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1157. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1158. nil := IntermediateCode.Immediate(addressType,0);
  1159. one := IntermediateCode.Immediate(addressType,1);
  1160. IntermediateCode.InitOperand(destination);
  1161. tagsAvailable := TRUE;
  1162. supportedInstruction := supportedInstructionProcedure;
  1163. supportedImmediate := supportedImmediateProcedure;
  1164. inData := FALSE;
  1165. SELF.emitLabels := emitLabels;
  1166. IntermediateCode.InitOperand(arrayDestinationTag);
  1167. bool := IntermediateCode.GetType(system,system.booleanType);
  1168. IntermediateCode.InitImmediate(false,bool,0);
  1169. IntermediateCode.InitImmediate(true,bool,1);
  1170. SELF.newObjectFile := newObjectFile;
  1171. indexCounter := 0;
  1172. NEW(registerUsageCount);
  1173. usedRegisters := NIL;
  1174. procedureResultDesignator := NIL;
  1175. NEW(fingerPrinter, system);
  1176. NEW(temporaries);
  1177. currentIsInline := FALSE;
  1178. NeedDescriptor := FALSE;
  1179. isUnchecked := backend.noRuntimeChecks;
  1180. END Init;
  1181. TYPE Context = RECORD
  1182. section: IntermediateCode.Section;
  1183. registerUsageCount: RegisterUsageCount;
  1184. usedRegisters: RegisterEntry;
  1185. END;
  1186. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1187. VAR context: Context;
  1188. BEGIN
  1189. context.section := section;
  1190. context.registerUsageCount := registerUsageCount;
  1191. context.usedRegisters := usedRegisters;
  1192. section := new;
  1193. NEW(registerUsageCount);
  1194. usedRegisters := NIL;
  1195. RETURN context;
  1196. END SwitchContext;
  1197. PROCEDURE ReturnToContext(context: Context);
  1198. BEGIN
  1199. section := context.section;
  1200. registerUsageCount := context.registerUsageCount;
  1201. usedRegisters := context.usedRegisters;
  1202. END ReturnToContext;
  1203. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1204. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1205. BEGIN
  1206. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1207. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1208. END;
  1209. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1210. section.SetExported(IsExported(syntaxTreeSymbol));
  1211. RETURN section
  1212. END NewSection;
  1213. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1214. VAR new: LONGINT;
  1215. BEGIN
  1216. new := registerUsageCount.Next(type,class);
  1217. UseRegister(new);
  1218. RETURN new
  1219. END AcquireRegister;
  1220. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1221. VAR
  1222. fingerPrint: SyntaxTree.FingerPrint;
  1223. fingerPrintString: ARRAY 32 OF CHAR;
  1224. BEGIN
  1225. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1226. string := "[";
  1227. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1228. Strings.Append(string, fingerPrintString);
  1229. Strings.Append(string, "]");
  1230. END GetFingerprintString;
  1231. (** get the name for the code section that represens a certain symbol
  1232. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1233. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1234. VAR string: ARRAY 32 OF CHAR;
  1235. BEGIN
  1236. Global.GetSymbolSegmentedName(symbol, name);
  1237. (* if the symbol is an operator, then append the fingerprint to the name *)
  1238. IF symbol IS SyntaxTree.Operator THEN
  1239. GetFingerprintString(symbol, string);
  1240. Basic.AppendToSegmentedName(name,string);
  1241. END
  1242. END GetCodeSectionNameForSymbol;
  1243. (** get the name for the code section that represens a certain symbol
  1244. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1245. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1246. VAR string: ARRAY 32 OF CHAR;
  1247. BEGIN
  1248. Global.GetSymbolNameInScope(symbol, scope, name);
  1249. (* if the symbol is an operator, then append the fingerprint to the name *)
  1250. IF symbol IS SyntaxTree.Operator THEN
  1251. GetFingerprintString(symbol, string);
  1252. Strings.Append(name, string);
  1253. END
  1254. END GetCodeSectionNameForSymbolInScope;
  1255. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1256. BEGIN
  1257. IF dump # NIL THEN
  1258. dump.String("enter "); dump.String(s); dump.Ln;
  1259. END;
  1260. END TraceEnter;
  1261. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1262. BEGIN
  1263. IF dump # NIL THEN
  1264. dump.String("exit "); dump.String(s); dump.Ln;
  1265. END;
  1266. END TraceExit;
  1267. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1268. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1269. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1270. VAR i: LONGINT;
  1271. BEGIN
  1272. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1273. IF op.rule # NIL THEN
  1274. FOR i := 0 TO LEN(op.rule)-1 DO
  1275. CheckRegister(op.rule[i])
  1276. END;
  1277. END;
  1278. END CheckRegister;
  1279. BEGIN
  1280. CheckRegister(instruction.op1);
  1281. CheckRegister(instruction.op2);
  1282. CheckRegister(instruction.op3);
  1283. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1284. ELSE section.Emit(instruction);
  1285. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1286. END;
  1287. END Emit;
  1288. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1289. BEGIN
  1290. IF backend.cooperative THEN
  1291. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1292. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1293. ELSE
  1294. Emit(Trap(position,trapNo));
  1295. END;
  1296. END EmitTrap;
  1297. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1298. VAR name: Basic.SegmentedName;
  1299. VAR op1, op2, reg: IntermediateCode.Operand;
  1300. VAR call, nocall: Label;
  1301. VAR parametersSize: LONGINT;
  1302. VAR prevSection: IntermediateCode.Section;
  1303. VAR prevDump: Streams.Writer;
  1304. VAR body: SyntaxTree.Body;
  1305. VAR procedureType: SyntaxTree.ProcedureType;
  1306. BEGIN
  1307. IF procedure # NIL THEN
  1308. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1309. ELSE procedureType := NIL;
  1310. END;
  1311. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1312. prevSection := SELF.section;
  1313. SELF.section := section;
  1314. prevDump := dump;
  1315. dump := section.comments;
  1316. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1317. IF backend.hasLinkRegister THEN
  1318. Emit(Push(Basic.invalidPosition, lr));
  1319. END;
  1320. Emit(Push(Basic.invalidPosition,fp));
  1321. END;
  1322. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1323. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1324. GetCodeSectionNameForSymbol(procedure, name);
  1325. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1326. ELSE
  1327. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1328. END;
  1329. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1330. Emit(Push(Basic.invalidPosition,op1));
  1331. Emit(Mov(Basic.invalidPosition,fp, sp));
  1332. body := procedure.procedureScope.body;
  1333. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1334. NEW(call, section);
  1335. NEW(nocall, section);
  1336. reg := NewRegisterOperand(addressType);
  1337. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1338. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1339. BrltL(call, sp, reg);
  1340. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1341. BrgeL(nocall, sp, op2);
  1342. call.Resolve(section.pc);
  1343. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1344. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1345. Emit(Push(Basic.invalidPosition, op2));
  1346. Emit(Push(Basic.invalidPosition, reg));
  1347. ReleaseIntermediateOperand(reg);
  1348. CallThis(position, "Activities","ExpandStack",2);
  1349. Emit(Result(Basic.invalidPosition, sp));
  1350. nocall.Resolve(section.pc);
  1351. END;
  1352. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1353. IF procedure # NIL THEN
  1354. body := procedure.procedureScope.body;
  1355. ELSE
  1356. body := NIL;
  1357. END;
  1358. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1359. Emit(Push(Basic.invalidPosition, one)) ;
  1360. procedureType.SetParametersOffset(1);
  1361. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1362. END;
  1363. Emit(Mov(Basic.invalidPosition, fp, sp));
  1364. END;
  1365. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1366. SELF.section := prevSection;
  1367. dump := prevDump;
  1368. END EmitEnter;
  1369. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1370. VAR op1,op2: IntermediateCode.Operand;
  1371. VAR instruction: IntermediateCode.Instruction;
  1372. BEGIN
  1373. IntermediateCode.InitNumber(op1,callconv);
  1374. IntermediateCode.InitNumber(op2,varSize);
  1375. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1376. RETURN instruction
  1377. END Enter;
  1378. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1379. VAR op1: IntermediateCode.Operand;
  1380. VAR instruction: IntermediateCode.Instruction;
  1381. BEGIN
  1382. IntermediateCode.InitNumber(op1,callconv);
  1383. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1384. RETURN instruction
  1385. END Leave;
  1386. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1387. VAR prevSection: IntermediateCode.Section;
  1388. VAR op2, size: IntermediateCode.Operand;
  1389. VAR body: SyntaxTree.Body;
  1390. BEGIN
  1391. prevSection := SELF.section;
  1392. SELF.section := section;
  1393. Emit(Leave(position, callconv));
  1394. IF procedure # NIL THEN
  1395. body := procedure.procedureScope.body;
  1396. ELSE
  1397. body := NIL;
  1398. END;
  1399. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1400. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1401. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1402. Emit(Add(position, sp, fp, op2));
  1403. ELSE
  1404. Emit(Mov(position, sp, fp));
  1405. END;
  1406. Emit(Pop(position, fp));
  1407. END;
  1408. SELF.section := prevSection;
  1409. END EmitLeave;
  1410. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1411. VAR m: SymbolMap;
  1412. BEGIN
  1413. position := x.position;
  1414. IF currentIsInline THEN
  1415. m := currentMapper.Get(x);
  1416. IF m # NIL THEN
  1417. (*
  1418. Printout.Info("mapping from", x);
  1419. Printout.Info("mapping to ", m.to);
  1420. *)
  1421. m.to.Accept(SELF);
  1422. op := result;
  1423. IF m.tag # NIL THEN
  1424. ReleaseIntermediateOperand(result.tag);
  1425. m.tag.Accept(SELF);
  1426. op.tag := result.op;
  1427. ReleaseIntermediateOperand(result.tag);
  1428. END;
  1429. RETURN
  1430. END;
  1431. END;
  1432. x.Accept(SELF);
  1433. op := result;
  1434. END Symbol;
  1435. PROCEDURE Expression(x: SyntaxTree.Expression);
  1436. BEGIN
  1437. position := x.position;
  1438. constantDeclaration := NIL;
  1439. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1440. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1441. END;
  1442. IF x.resolved # NIL THEN
  1443. x.resolved.Accept(SELF)
  1444. ELSE
  1445. x.Accept(SELF)
  1446. END;
  1447. (* check this, was commented out in ActiveCells3 *)
  1448. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1449. Error(x.position, "unsupported modifier");
  1450. END;
  1451. END Expression;
  1452. (*
  1453. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1454. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1455. BEGIN
  1456. temporaries.GetUsage(current);
  1457. FOR i := 0 TO LEN(current)-1 DO
  1458. set := current[i] - previous[i];
  1459. IF set # {} THEN
  1460. FOR j := 0 TO MAX(SET)-1 DO
  1461. IF j IN set THEN
  1462. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1463. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1464. Symbol(variable, op);
  1465. MakeMemory(tmp,op.op,addressType,0);
  1466. ReleaseOperand(op);
  1467. Emit(Mov(position,tmp, nil ) );
  1468. ReleaseIntermediateOperand(tmp);
  1469. END;
  1470. END;
  1471. END;
  1472. END;
  1473. END;
  1474. END ResetUsedTemporaries;
  1475. *)
  1476. PROCEDURE Statement(x: SyntaxTree.Statement);
  1477. VAR use: VariableUse;
  1478. BEGIN
  1479. temporaries.GetUsage(use);
  1480. position := x.position;
  1481. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1482. IF commentPrintout # NIL THEN
  1483. commentPrintout.Statement(x);
  1484. dump.Ln;
  1485. (*dump.Update;*)
  1486. END;
  1487. x.Accept(SELF);
  1488. (*
  1489. CheckRegistersFree();
  1490. *)
  1491. (*ResetUsedTemporaries(use);*)
  1492. temporaries.SetUsage(use);
  1493. END Statement;
  1494. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1495. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1496. *)
  1497. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1498. BEGIN
  1499. ASSERT(op.mode # IntermediateCode.Undefined);
  1500. IF op.mode = IntermediateCode.ModeMemory THEN
  1501. ReuseCopy(res,op);
  1502. ELSE
  1503. res := op;
  1504. UseIntermediateOperand(res);
  1505. END;
  1506. IntermediateCode.AddOffset(res,offset);
  1507. IntermediateCode.MakeMemory(res,type);
  1508. END MakeMemory;
  1509. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1510. VAR mem: IntermediateCode.Operand;
  1511. BEGIN
  1512. MakeMemory(mem,res,type,offset);
  1513. ReleaseIntermediateOperand(res);
  1514. res := mem;
  1515. END ToMemory;
  1516. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1517. VAR mem: IntermediateCode.Operand;
  1518. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1519. componentType: SyntaxTree.Type;
  1520. BEGIN
  1521. type := type.resolved;
  1522. IF operand.mode = ModeReference THEN
  1523. IF type IS SyntaxTree.RangeType THEN
  1524. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1525. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1526. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1527. ReleaseOperand(operand);
  1528. operand.op := firstOp;
  1529. operand.tag := lastOp;
  1530. operand.extra := stepOp;
  1531. ELSIF type IS SyntaxTree.ComplexType THEN
  1532. componentType := type(SyntaxTree.ComplexType).componentType;
  1533. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1534. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1535. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1536. ReleaseOperand(operand);
  1537. operand.op := firstOp;
  1538. operand.tag := lastOp
  1539. ELSE
  1540. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1541. ReleaseIntermediateOperand(operand.op);
  1542. operand.op := mem;
  1543. END;
  1544. operand.mode := ModeValue;
  1545. END;
  1546. ASSERT(operand.mode = ModeValue);
  1547. END LoadValue;
  1548. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1549. VAR prevConditional: BOOLEAN;
  1550. BEGIN
  1551. prevConditional := conditional;
  1552. conditional := FALSE;
  1553. InitOperand(result, ModeUndefined);
  1554. Expression(x);
  1555. op := result;
  1556. LoadValue(op,x.type.resolved);
  1557. conditional := prevConditional;
  1558. END Evaluate;
  1559. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1560. VAR prevConditional: BOOLEAN;
  1561. BEGIN
  1562. prevConditional := conditional;
  1563. conditional := FALSE;
  1564. InitOperand(result,ModeUndefined);
  1565. Expression(x);
  1566. op := result;
  1567. (*
  1568. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1569. *)
  1570. conditional := prevConditional;
  1571. END Designate;
  1572. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1573. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1574. BEGIN
  1575. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1576. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1577. conditional := TRUE;
  1578. trueLabel := trueL; falseLabel := falseL;
  1579. Expression(x);
  1580. trueL := trueLabel; falseL := falseLabel;
  1581. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1582. END Condition;
  1583. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1584. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1585. BEGIN
  1586. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1587. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1588. RETURN op
  1589. END NewRegisterOperand;
  1590. PROCEDURE UnuseRegister(register: LONGINT);
  1591. BEGIN
  1592. IF (register > 0) THEN
  1593. register := registerUsageCount.Map(register);
  1594. registerUsageCount.DecUse(register);
  1595. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1596. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1597. END;
  1598. IF registerUsageCount.Use(register)=0 THEN
  1599. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1600. Warning(position, "register cannot be removed");
  1601. END;
  1602. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1603. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1604. END;
  1605. ELSIF registerUsageCount.Use(register)<0 THEN
  1606. Warning(position, "register removed too often");
  1607. IF dump # NIL THEN
  1608. dump.String("register removed too often"); dump.Ln; dump.Update;
  1609. END;
  1610. D.TraceBack;
  1611. END;
  1612. END;
  1613. END UnuseRegister;
  1614. PROCEDURE UseRegister(register: LONGINT);
  1615. BEGIN
  1616. IF (register > 0) THEN
  1617. register := registerUsageCount.Map(register);
  1618. registerUsageCount.IncUse(register);
  1619. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1620. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1621. END;
  1622. IF registerUsageCount.Use(register)=1 THEN
  1623. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1624. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1625. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1626. END;
  1627. END;
  1628. END;
  1629. END UseRegister;
  1630. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1631. BEGIN
  1632. UnuseRegister(op.register)
  1633. END ReleaseIntermediateOperand;
  1634. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1635. BEGIN
  1636. UseRegister(op.register)
  1637. END UseIntermediateOperand;
  1638. PROCEDURE ReleaseOperand(CONST op: Operand);
  1639. BEGIN
  1640. UnuseRegister(op.op.register);
  1641. UnuseRegister(op.tag.register);
  1642. UnuseRegister(op.extra.register);
  1643. END ReleaseOperand;
  1644. (* save registers marked in array "markedRegisters" to the stack
  1645. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1646. *)
  1647. PROCEDURE SaveRegisters();
  1648. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1649. BEGIN
  1650. entry := usedRegisters;
  1651. WHILE entry # NIL DO
  1652. type := registerUsageCount.used[entry.register].type;
  1653. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1654. Emit(Push(position,op));
  1655. entry := entry.next;
  1656. END;
  1657. END SaveRegisters;
  1658. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1659. BEGIN
  1660. saved := usedRegisters;
  1661. usedRegisters := NIL;
  1662. END ReleaseUsedRegisters;
  1663. (** remove parameter registers from used queue *)
  1664. PROCEDURE ReleaseParameterRegisters;
  1665. VAR entry,prev,next: RegisterEntry;
  1666. BEGIN
  1667. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1668. WHILE entry # NIL DO
  1669. next := entry.next;
  1670. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1671. registerUsageCount.DecUse(entry.register);
  1672. ASSERT(registerUsageCount.Use(entry.register)=0);
  1673. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1674. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1675. END;
  1676. ELSIF prev = NIL THEN
  1677. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1678. ELSE
  1679. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1680. END;
  1681. entry := next;
  1682. END;
  1683. END ReleaseParameterRegisters;
  1684. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1685. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1686. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1687. BEGIN
  1688. entry := saved;
  1689. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1690. entry := prev;
  1691. WHILE entry # NIL DO
  1692. prev := entry.prev;
  1693. type := entry.type;
  1694. class := entry.registerClass;
  1695. IntermediateCode.InitRegister(op,type,class,entry.register);
  1696. (*
  1697. new := registerUsageCount.Next(type,class);
  1698. registerUsageCount.Remap(entry.register,new);
  1699. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1700. dump.String("remap register "); dump.Int(entry.register,1);
  1701. dump.String("to "); dump.Int(new,1);
  1702. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1703. END;
  1704. entry.register := new;
  1705. *)
  1706. Emit(Pop(position,op));
  1707. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1708. entry := prev;
  1709. END;
  1710. (*
  1711. usedRegisters := saved;
  1712. *)
  1713. END RestoreRegisters;
  1714. PROCEDURE CheckRegistersFree;
  1715. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1716. BEGIN
  1717. IF usedRegisters # NIL THEN
  1718. r := usedRegisters;
  1719. WHILE r # NIL DO
  1720. warning := "register ";
  1721. Strings.AppendInt(warning, r.register);
  1722. Strings.Append(warning, " not released.");
  1723. Warning(position,warning);
  1724. r := r .next;
  1725. END;
  1726. END;
  1727. FOR i := 0 TO registerUsageCount.count-1 DO
  1728. IF registerUsageCount.used[i].count < 0 THEN
  1729. warning := "register ";
  1730. Strings.AppendInt(warning, i);
  1731. Strings.Append(warning, " unused too often.");
  1732. Warning(position,warning);
  1733. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1734. warning := "register ";
  1735. Strings.AppendInt(warning, i);
  1736. Strings.Append(warning, " not unused often enough.");
  1737. Warning(position,warning);
  1738. END;
  1739. END;
  1740. END CheckRegistersFree;
  1741. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1742. Otherwise allocate a new register.
  1743. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1744. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1745. BEGIN
  1746. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1747. UseIntermediateOperand(result);
  1748. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1749. UseIntermediateOperand(result);
  1750. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1751. END;
  1752. END Reuse2;
  1753. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1754. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1755. If not then allocate a new register.
  1756. Does NOT necessarily keep the content of src1 or src2 in result!
  1757. *)
  1758. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1759. BEGIN
  1760. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1761. UseIntermediateOperand(result);
  1762. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1765. result := alternative; alternative := emptyOperand;
  1766. UseIntermediateOperand(result);
  1767. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1768. END;
  1769. END Reuse2a;
  1770. (* like reuse2 but only one source *)
  1771. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1772. BEGIN
  1773. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1774. UseIntermediateOperand(result);
  1775. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1776. END;
  1777. END Reuse1;
  1778. (* like reuse2a but only one source *)
  1779. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1780. BEGIN
  1781. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1782. UseIntermediateOperand(result);
  1783. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1784. UseIntermediateOperand(result);
  1785. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1786. END;
  1787. END Reuse1a;
  1788. (* like reuse1 but guarantees that content of src1 is in result *)
  1789. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1790. BEGIN
  1791. IF ReusableRegister(src1) THEN
  1792. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1793. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1794. UseIntermediateOperand(result);
  1795. ELSE
  1796. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1797. Emit(Mov(position,result,src1));
  1798. END
  1799. END ReuseCopy;
  1800. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1801. BEGIN
  1802. IF ReusableRegister(src) THEN
  1803. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1804. ELSE
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1806. Emit(Mov(position,result,src));
  1807. ReleaseIntermediateOperand(src);
  1808. END
  1809. END TransferToRegister;
  1810. (** labels and branches **)
  1811. PROCEDURE NewLabel(): Label;
  1812. VAR label: Label;
  1813. BEGIN
  1814. NEW(label,section); RETURN label;
  1815. END NewLabel;
  1816. PROCEDURE SetLabel(label: Label);
  1817. BEGIN label.Resolve(section.pc);
  1818. END SetLabel;
  1819. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1820. BEGIN
  1821. ASSERT(label # NIL);
  1822. IF label.pc < 0 THEN (* label not yet set *)
  1823. label.AddFixup(section.pc);
  1824. END;
  1825. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1826. END LabelOperand;
  1827. PROCEDURE BrL(label: Label);
  1828. BEGIN
  1829. Emit(Br(position,LabelOperand(label)));
  1830. END BrL;
  1831. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1832. BEGIN
  1833. Emit(Brge(position,LabelOperand(label),left,right));
  1834. END BrgeL;
  1835. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1836. BEGIN
  1837. Emit(Brlt(position,LabelOperand(label),left,right));
  1838. END BrltL;
  1839. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1840. BEGIN
  1841. Emit(Breq(position,LabelOperand(label),left,right));
  1842. END BreqL;
  1843. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1844. BEGIN
  1845. Emit(Brne(position,LabelOperand(label),left,right));
  1846. END BrneL;
  1847. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1848. VAR new: IntermediateCode.Operand;
  1849. BEGIN
  1850. IF Trace THEN TraceEnter("Convert") END;
  1851. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1852. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1853. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1854. & (operand.offset = 0)
  1855. THEN
  1856. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1857. Emit(Conv(position,new,operand));
  1858. ELSE
  1859. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1860. Emit(Conv(position,new,operand));
  1861. ReleaseIntermediateOperand(operand);
  1862. END;
  1863. operand := new;
  1864. ELSIF (operand.mode = IntermediateCode.ModeImmediate) & (operand.symbol.name = "") & (operand.type.sizeInBits <= type.sizeInBits) & (operand.type.form IN IntermediateCode.Integer) & (type.form IN IntermediateCode.Integer) THEN
  1865. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1866. ELSE
  1867. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1868. Emit(Conv(position,new,operand));
  1869. ReleaseIntermediateOperand(operand);
  1870. operand := new;
  1871. END;
  1872. IF Trace THEN TraceExit("Convert") END;
  1873. END Convert;
  1874. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1875. VAR exit: Label;
  1876. BEGIN
  1877. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1878. exit := NewLabel();
  1879. br(exit,left,right);
  1880. EmitTrap(position,trapNo);
  1881. SetLabel(exit);
  1882. END TrapC;
  1883. (** expressions *)
  1884. (** emit necessary runtime check for set elements **)
  1885. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1886. VAR max: IntermediateCode.Operand;
  1887. BEGIN
  1888. IF isUnchecked THEN RETURN END;
  1889. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1890. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1891. TrapC(BrgeL, max, o, SetElementTrap);
  1892. END;
  1893. END CheckSetElement;
  1894. (** the set that a range represents **)
  1895. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1896. VAR
  1897. operand: Operand;
  1898. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1899. BEGIN
  1900. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1901. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1902. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1903. one := IntermediateCode.Immediate(setType, 1);
  1904. Evaluate(x, operand);
  1905. Convert(operand.op, setType);
  1906. Convert(operand.tag, setType);
  1907. CheckSetElement(operand.op);
  1908. CheckSetElement(operand.tag);
  1909. (* create mask for lower bound
  1910. i.e. shift 11111111 to the left by the value of the lower bound
  1911. *)
  1912. Reuse1(temp, operand.op);
  1913. Emit(Shl(position,temp, allBits, operand.op));
  1914. ReleaseIntermediateOperand(operand.op);
  1915. operand.op := temp;
  1916. (* create mask for upper bound
  1917. i.e. shift 11111111 to the right by the difference between the
  1918. upper bound and the maximum number of set elements
  1919. *)
  1920. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1921. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1922. Reuse1(temp, operand.tag);
  1923. ELSE
  1924. Reuse1(temp, operand.tag);
  1925. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1926. Emit(Sub(position,temp, size, operand.tag));
  1927. END;
  1928. Emit(Shr(position,temp, allBits, operand.tag));
  1929. ReleaseIntermediateOperand(operand.tag);
  1930. operand.tag := temp;
  1931. Reuse2(resultingSet, operand.op, operand.tag);
  1932. (* intersect the two masks *)
  1933. Emit(And(position,resultingSet, operand.op, operand.tag));
  1934. ReleaseOperand(operand);
  1935. RETURN resultingSet
  1936. END SetFromRange;
  1937. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1938. VAR
  1939. res, operand: Operand;
  1940. temp, one, noBits, dest: IntermediateCode.Operand;
  1941. expression: SyntaxTree.Expression;
  1942. i: LONGINT;
  1943. BEGIN
  1944. IF Trace THEN TraceEnter("VisitSet") END;
  1945. dest := destination;
  1946. destination := emptyOperand;
  1947. noBits := IntermediateCode.Immediate(setType, 0);
  1948. one := IntermediateCode.Immediate(setType, 1);
  1949. (* start off with the empty set *)
  1950. InitOperand(res, ModeValue);
  1951. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1952. Emit(Mov(position,res.op, noBits));
  1953. FOR i := 0 TO x.elements.Length() - 1 DO
  1954. expression := x.elements.GetExpression(i);
  1955. IF expression IS SyntaxTree.RangeExpression THEN
  1956. (* range of set elements *)
  1957. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1958. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1959. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1960. ReleaseIntermediateOperand(temp)
  1961. ELSE
  1962. (* singelton element *)
  1963. Evaluate(expression, operand);
  1964. Convert(operand.op, setType);
  1965. CheckSetElement(operand.op);
  1966. (* create subset containing single element *)
  1967. Reuse1(temp, operand.op);
  1968. Emit(Shl(position,temp, one, operand.op));
  1969. ReleaseOperand(operand);
  1970. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1971. ReleaseIntermediateOperand(temp);
  1972. END
  1973. END;
  1974. result := res;
  1975. destination := dest;
  1976. IF Trace THEN TraceExit("VisitSet") END;
  1977. END VisitSet;
  1978. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1979. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1980. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1981. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1982. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1983. element: SyntaxTree.IntegerValue;
  1984. BEGIN
  1985. numberElements := x.elements.Length();
  1986. expression := index.parameters.GetExpression(dim);
  1987. element := expression(SyntaxTree.IntegerValue);
  1988. FOR i := 0 TO numberElements-1 DO
  1989. expression := x.elements.GetExpression(i);
  1990. element.SetValue(i);
  1991. IF expression IS SyntaxTree.MathArrayExpression THEN
  1992. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1993. ELSE
  1994. Assign(index,expression);
  1995. END;
  1996. END;
  1997. END RecursiveAssignment;
  1998. BEGIN
  1999. variable := GetTemporaryVariable(x.type, FALSE);
  2000. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2001. designator.SetType(variable.type);
  2002. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2003. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2004. FOR i := 0 TO dim-1 DO
  2005. element := SyntaxTree.NewIntegerValue(x.position,0);
  2006. element.SetType(system.longintType);
  2007. index.parameters.AddExpression(element);
  2008. END;
  2009. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2010. RecursiveAssignment(x,0);
  2011. Expression(designator);
  2012. END VisitMathArrayExpression;
  2013. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2014. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2015. BEGIN
  2016. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2017. dest := destination; destination := emptyOperand;
  2018. IF x.operator = Scanner.Not THEN
  2019. IF conditional THEN
  2020. Condition(x.left,falseLabel,trueLabel)
  2021. ELSE
  2022. Evaluate(x.left,operand);
  2023. InitOperand(result,ModeValue);
  2024. Reuse1a(result.op,operand.op,dest);
  2025. Emit(Xor(position,result.op,operand.op,true));
  2026. ReleaseOperand(operand);
  2027. END;
  2028. ELSIF x.operator = Scanner.Minus THEN
  2029. Evaluate(x.left,operand);
  2030. InitOperand(result,ModeValue);
  2031. Reuse1a(result.op,operand.op,dest);
  2032. type := x.left.type.resolved;
  2033. IF type IS SyntaxTree.SetType THEN
  2034. Emit(Not(position,result.op,operand.op));
  2035. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2036. Reuse1(result.tag,operand.tag);
  2037. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2038. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2039. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2040. Emit(Neg(position,result.op,operand.op));
  2041. ELSE HALT(200)
  2042. END;
  2043. ReleaseOperand(operand);
  2044. ELSIF x.operator = Scanner.Address THEN
  2045. Designate(x.left,operand);
  2046. operand.mode := ModeValue;
  2047. t0 := x.left.type.resolved;
  2048. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2049. ReleaseIntermediateOperand(operand.op);
  2050. operand.op := operand.tag;
  2051. IntermediateCode.InitOperand(operand.tag);
  2052. END;
  2053. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2054. result := operand;
  2055. ELSE HALT(100)
  2056. END;
  2057. destination := dest;
  2058. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2059. END VisitUnaryExpression;
  2060. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2061. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2062. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2063. BEGIN
  2064. type := type.resolved;
  2065. originalType := type;
  2066. IF type IS SyntaxTree.PointerType THEN
  2067. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2068. END;
  2069. IF type IS SyntaxTree.ObjectType THEN
  2070. BrL(trueL);
  2071. ELSE
  2072. ASSERT(type IS SyntaxTree.RecordType);
  2073. (*
  2074. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2075. *)
  2076. ReuseCopy(left,tag);
  2077. right := TypeDescriptorAdr(type);
  2078. IF ~newObjectFile THEN
  2079. IntermediateCode.MakeMemory(right,addressType);
  2080. END;
  2081. IF backend.cooperative THEN
  2082. repeatL := NewLabel();
  2083. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2084. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2085. END;
  2086. SetLabel(repeatL);
  2087. BreqL(trueL,left,right);
  2088. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2089. BrneL(repeatL,left,nil);
  2090. ELSIF meta.simple THEN
  2091. level := type(SyntaxTree.RecordType).Level();
  2092. (* get type desc tag of level relative to base tag *)
  2093. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2094. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2095. IntermediateCode.MakeMemory(left,addressType);
  2096. BreqL(trueL,left,right);
  2097. ELSE
  2098. level := type(SyntaxTree.RecordType).Level();
  2099. (* get type desc tag of level relative to base tag *)
  2100. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2101. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2102. IntermediateCode.MakeMemory(left,addressType);
  2103. BreqL(trueL,left,right);
  2104. END;
  2105. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2106. BrL(falseL);
  2107. END;
  2108. END TypeTest;
  2109. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2110. BEGIN
  2111. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2112. IF dump # NIL THEN
  2113. dump.String(s); dump.Ln;
  2114. END;
  2115. END Error;
  2116. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2117. BEGIN
  2118. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2119. IF dump # NIL THEN
  2120. dump.String(s); dump.Ln; dump.Update;
  2121. END;
  2122. END Warning;
  2123. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2124. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2125. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2126. operand:Operand;
  2127. BEGIN
  2128. previousSection := section;
  2129. Global.GetModuleName(mod,name);
  2130. Strings.Append(name,".@Trace");
  2131. Basic.ToSegmentedName(name, pooledName);
  2132. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2133. IF dump # NIL THEN dump := section.comments END;
  2134. IF backend.hasLinkRegister THEN
  2135. Emit(Push(Basic.invalidPosition, lr));
  2136. END;
  2137. variable := mod.moduleScope.firstVariable;
  2138. WHILE variable # NIL DO
  2139. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2140. Symbol(variable, operand);
  2141. register := operand.op;
  2142. CallTraceMethod(register, variable.type);
  2143. ReleaseIntermediateOperand(register);
  2144. END;
  2145. variable := variable.nextVariable;
  2146. END;
  2147. IF backend.hasLinkRegister THEN
  2148. Emit(Pop(Basic.invalidPosition, lr));
  2149. END;
  2150. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2151. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2152. Emit(Br(position,op));
  2153. INC(statCoopTraceModule, section.pc);
  2154. section := previousSection;
  2155. IF dump # NIL THEN dump := section.comments END;
  2156. END CreateTraceModuleMethod;
  2157. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2158. BEGIN
  2159. Emit (Push(position, dst));
  2160. Emit (Push(position, src));
  2161. CallThis(position,"GarbageCollector","Assign", 2);
  2162. END CallAssignPointer;
  2163. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2164. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2165. BEGIN
  2166. IF SemanticChecker.IsPointerType (type) THEN
  2167. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2168. ELSIF type.IsRecordType() THEN
  2169. Emit (Push(position,dst));
  2170. Emit (Push(position,src));
  2171. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2172. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2173. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2174. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2175. ELSIF IsStaticArray(type) THEN
  2176. size := StaticArrayNumElements(type);
  2177. base := StaticArrayBaseType(type);
  2178. IF base.IsRecordType() THEN
  2179. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2180. Emit (Push(position, dst));
  2181. Emit (Push(position, src));
  2182. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2183. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2184. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2185. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2186. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2187. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2188. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2189. Emit (Push(position, dst));
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, src));
  2192. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2193. ELSE
  2194. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2195. Emit (Push(position, dst));
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, src));
  2198. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2199. ASSERT(StaticArrayBaseType(type).IsPointer());
  2200. END;
  2201. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2202. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2203. Emit (Push(position, dst));
  2204. Emit (Push(position, src));
  2205. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2206. ELSE HALT(100); (* missing ? *)
  2207. END;
  2208. END CallAssignMethod;
  2209. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2210. VAR name: Basic.SegmentedName;
  2211. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2212. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2213. context: Context;
  2214. BEGIN
  2215. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2216. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2217. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2218. GetRecordTypeName (recordType,name);
  2219. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2220. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2221. IF dump # NIL THEN dump := section.comments END;
  2222. IF backend.hasLinkRegister THEN
  2223. Emit(Push(Basic.invalidPosition, lr));
  2224. END;
  2225. variable := recordType.recordScope.firstVariable;
  2226. WHILE variable # NIL DO
  2227. IF variable.NeedsTrace() THEN
  2228. dst := NewRegisterOperand (addressType);
  2229. src := NewRegisterOperand (addressType);
  2230. Emit (Mov(position, dst, parameter1));
  2231. Emit (Mov(position, src, parameter2));
  2232. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2233. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2234. CallAssignMethod(dst, src, variable.type);
  2235. ReleaseIntermediateOperand(src);
  2236. ReleaseIntermediateOperand(dst);
  2237. END;
  2238. variable := variable.nextVariable;
  2239. END;
  2240. recordBase := recordType.GetBaseRecord();
  2241. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2242. IF backend.hasLinkRegister THEN
  2243. Emit(Pop(Basic.invalidPosition, lr));
  2244. END;
  2245. GetRecordTypeName (recordBase,name);
  2246. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2247. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2248. Emit(Br(position,op));
  2249. ELSE
  2250. Emit(Exit(position,0,0, 0));
  2251. END;
  2252. IF ~recordType.isObject THEN
  2253. GetRecordTypeName (recordType,name);
  2254. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2255. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2256. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2257. NEW(registerUsageCount);
  2258. usedRegisters := NIL;
  2259. dst := NewRegisterOperand (addressType);
  2260. src := NewRegisterOperand (addressType);
  2261. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2262. Emit(Mov(position, dst, parameter1));
  2263. Emit(Mov(position, src, parameter2));
  2264. label := NewLabel();
  2265. SetLabel(label);
  2266. Emit(Push(position, dst));
  2267. Emit(Push(position, src));
  2268. GetRecordTypeName (recordType,name);
  2269. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2270. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2271. Emit(Call(position, op, 0));
  2272. Emit(Pop(position, src));
  2273. Emit(Pop(position, dst));
  2274. Emit(Add(position, dst, dst, ofs));
  2275. Emit(Add(position, src, src, ofs));
  2276. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2277. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2278. Emit(Exit(position,0,0, 0));
  2279. END;
  2280. INC(statCoopAssignProcedure, section.pc);
  2281. ReturnToContext(context);
  2282. IF dump # NIL THEN dump := section.comments END;
  2283. END CreateAssignProcedure;
  2284. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2285. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2286. BEGIN
  2287. IF IsUnsafePointer (type) THEN
  2288. skip := NewLabel();
  2289. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2290. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2291. BreqL (skip, op, nil);
  2292. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2293. SetLabel (skip);
  2294. ELSIF SemanticChecker.IsPointerType (type) THEN
  2295. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2296. CallThis(position,"GarbageCollector","Mark", 1);
  2297. ELSIF type.IsRecordType() THEN
  2298. Emit (Push(position,register));
  2299. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2300. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2301. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2302. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2303. ELSIF IsStaticArray(type) THEN
  2304. size := StaticArrayNumElements(type);
  2305. base := StaticArrayBaseType(type);
  2306. IF base.IsRecordType() THEN
  2307. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2308. Emit (Push(position, register));
  2309. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2310. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2311. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2312. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2313. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2314. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2315. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2316. Emit (Push(position, register));
  2317. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2318. ELSE
  2319. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2320. Emit (Push(position, register));
  2321. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2322. ASSERT(base.IsPointer());
  2323. END;
  2324. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2325. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2326. CallThis(position,"GarbageCollector","Mark", 1);
  2327. ELSE HALT(100); (* missing ? *)
  2328. END;
  2329. END CallTraceMethod;
  2330. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2331. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2332. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2333. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2334. BEGIN
  2335. previousSection := section;
  2336. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2337. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2338. GetRecordTypeName (recordType,name);
  2339. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2340. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2341. IF dump # NIL THEN dump := section.comments END;
  2342. IF backend.hasLinkRegister THEN
  2343. Emit(Push(Basic.invalidPosition, lr));
  2344. END;
  2345. variable := recordType.recordScope.firstVariable;
  2346. WHILE variable # NIL DO
  2347. IF variable.NeedsTrace() THEN
  2348. register := NewRegisterOperand (addressType);
  2349. Emit (Mov(position,register,parameter1));
  2350. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2351. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2352. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2353. END;
  2354. CallTraceMethod(register, variable.type);
  2355. ReleaseIntermediateOperand(register);
  2356. END;
  2357. variable := variable.nextVariable;
  2358. END;
  2359. recordBase := recordType.GetBaseRecord();
  2360. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2361. recordBase := recordBase.GetBaseRecord();
  2362. END;
  2363. IF recordBase # NIL THEN
  2364. IF backend.hasLinkRegister THEN
  2365. Emit(Pop(Basic.invalidPosition, lr));
  2366. END;
  2367. GetRecordTypeName (recordBase,name);
  2368. IF HasExplicitTraceMethod (recordBase) THEN
  2369. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2370. ELSE
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2372. END;
  2373. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2374. Emit(Br(position,op));
  2375. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2376. Emit(Exit(position,0,0,0));
  2377. ELSE
  2378. IF backend.hasLinkRegister THEN
  2379. Emit(Pop(Basic.invalidPosition, lr));
  2380. END;
  2381. IF recordType.isObject THEN
  2382. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2383. ELSE
  2384. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2385. END;
  2386. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2387. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2388. Emit(Br(position,op));
  2389. END;
  2390. IF ~recordType.isObject THEN
  2391. GetRecordTypeName (recordType,name);
  2392. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2393. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2394. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2395. NEW(registerUsageCount);
  2396. usedRegisters := NIL;
  2397. IF dump # NIL THEN dump := section.comments END;
  2398. register := NewRegisterOperand (addressType);
  2399. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2400. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2401. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2402. END;
  2403. Emit (Push(position,register));
  2404. GetRecordTypeName (recordType,name);
  2405. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2406. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2407. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2408. ReleaseIntermediateOperand(register);
  2409. Emit(Exit(position,0,0,0));
  2410. GetRecordTypeName (recordType,name);
  2411. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2412. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2413. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2414. NEW(registerUsageCount);
  2415. usedRegisters := NIL;
  2416. register := NewRegisterOperand (addressType);
  2417. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2418. Emit(Mov(position, register, parameter1));
  2419. label := NewLabel();
  2420. SetLabel(label);
  2421. Emit(Push(position, register));
  2422. GetRecordTypeName (recordType,name);
  2423. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2424. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2425. Emit(Call(position, op, 0));
  2426. Emit(Pop(position, register));
  2427. Emit(Add(position, register, register, ofs));
  2428. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2429. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2430. Emit(Exit(position,0,0,0));
  2431. END;
  2432. INC(statCoopTraceMethod, section.pc);
  2433. ReturnToContext(context);
  2434. IF dump # NIL THEN dump := section.comments END;
  2435. END CreateTraceMethod;
  2436. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2437. VAR name: Basic.SegmentedName;
  2438. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2439. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2440. context: Context;
  2441. BEGIN
  2442. IF recordType.isObject THEN RETURN END;
  2443. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2444. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2445. GetRecordTypeName (recordType,name);
  2446. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2447. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2448. IF dump # NIL THEN dump := section.comments END;
  2449. IF backend.hasLinkRegister THEN
  2450. Emit(Push(Basic.invalidPosition, lr));
  2451. END;
  2452. variable := recordType.recordScope.firstVariable;
  2453. WHILE variable # NIL DO
  2454. IF variable.NeedsTrace() THEN
  2455. dst := NewRegisterOperand (addressType);
  2456. Emit (Mov(position, dst, parameter1));
  2457. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2458. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2459. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2460. END;
  2461. CallResetProcedure(dst, nil, variable.type);
  2462. ReleaseIntermediateOperand(dst);
  2463. END;
  2464. variable := variable.nextVariable;
  2465. END;
  2466. recordBase := recordType.GetBaseRecord();
  2467. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2468. IF backend.hasLinkRegister THEN
  2469. Emit(Pop(Basic.invalidPosition, lr));
  2470. END;
  2471. GetRecordTypeName (recordBase,name);
  2472. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2473. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2474. Emit(Br(position,op));
  2475. ELSE
  2476. Emit(Exit(position,0,0, 0));
  2477. END;
  2478. GetRecordTypeName (recordType,name);
  2479. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2480. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2481. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2482. NEW(registerUsageCount);
  2483. usedRegisters := NIL;
  2484. dst := NewRegisterOperand (addressType);
  2485. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2486. Emit(Mov(position, dst, parameter1));
  2487. label := NewLabel();
  2488. SetLabel(label);
  2489. Emit(Push(position, dst));
  2490. GetRecordTypeName (recordType,name);
  2491. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2492. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2493. Emit(Call(position, op, 0));
  2494. Emit(Pop(position, dst));
  2495. Emit(Add(position, dst, dst, ofs));
  2496. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2497. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2498. Emit(Exit(position,0,0, 0));
  2499. INC(statCoopResetProcedure, section.pc);
  2500. ReturnToContext(context);
  2501. IF dump # NIL THEN dump := section.comments END;
  2502. END CreateResetProcedure;
  2503. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2504. VAR name: Basic.SegmentedName; context: Context;
  2505. BEGIN
  2506. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2507. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2508. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2509. IF dump # NIL THEN dump := section.comments END;
  2510. IF backend.hasLinkRegister THEN
  2511. Emit(Push(Basic.invalidPosition, lr));
  2512. END;
  2513. Emit(Push(position,fp));
  2514. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2515. ResetVariables(scope);
  2516. Emit(Pop(position,fp));
  2517. Emit(Exit(position,0,0, 0));
  2518. ReturnToContext(context);
  2519. IF dump # NIL THEN dump := section.comments END;
  2520. END CreateResetMethod;
  2521. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2522. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2523. BEGIN
  2524. IF SemanticChecker.IsPointerType (type) THEN
  2525. Emit (Push(position, dest));
  2526. CallThis(position,"GarbageCollector","Reset", 1);
  2527. ELSIF type.IsRecordType() THEN
  2528. Emit (Push(position, dest));
  2529. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2530. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2531. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2532. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2533. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2534. size := GetArrayLength(type, tag);
  2535. base := ArrayBaseType(type);
  2536. IF base.IsRecordType() THEN
  2537. Emit (Push(position, size));
  2538. Emit (Push(position, dest));
  2539. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2540. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2541. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2542. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2543. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2544. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2545. Emit (Push(position, size));
  2546. Emit (Push(position, dest));
  2547. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2548. ELSE
  2549. Emit (Push(position, size));
  2550. Emit (Push(position, dest));
  2551. CallThis(position,"GarbageCollector","ResetArray", 2);
  2552. ASSERT(ArrayBaseType(type).IsPointer());
  2553. END;
  2554. ReleaseIntermediateOperand(size);
  2555. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2556. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2557. Emit (Push(position, dest));
  2558. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2559. ELSE HALT(100); (* missing ? *)
  2560. END;
  2561. END CallResetProcedure;
  2562. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2563. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2564. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2565. VAR operand: Operand;
  2566. BEGIN
  2567. Symbol (symbol, operand);
  2568. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2569. ReleaseOperand(operand);
  2570. END Reset;
  2571. BEGIN
  2572. previousScope := currentScope;
  2573. currentScope := scope;
  2574. pc := section.pc;
  2575. variable := scope.firstVariable;
  2576. WHILE variable # NIL DO
  2577. IF variable.NeedsTrace() THEN
  2578. Reset (variable);
  2579. END;
  2580. variable := variable.nextVariable;
  2581. END;
  2582. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2583. WHILE parameter # NIL DO
  2584. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2585. Reset (parameter);
  2586. END;
  2587. parameter := parameter.nextParameter;
  2588. END;
  2589. INC(statCoopResetVariables, section.pc - pc);
  2590. currentScope := previousScope;
  2591. END ResetVariables;
  2592. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2593. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2594. VAR op: IntermediateCode.Operand; context: Context;
  2595. BEGIN
  2596. previousSection := section;
  2597. GetCodeSectionNameForSymbol(procedure, name);
  2598. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2599. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2600. IF dump # NIL THEN dump := section.comments END;
  2601. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2602. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2603. Emit(Data(position,op));
  2604. Emit(Data(position,nil));
  2605. IF HasPointers (procedure.procedureScope) THEN
  2606. GetCodeSectionNameForSymbol(procedure, name);
  2607. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2608. ELSE
  2609. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2610. END;
  2611. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2612. Emit(Data(position,op));
  2613. ReturnToContext(context);
  2614. IF dump # NIL THEN dump := section.comments END;
  2615. END CreateProcedureDescriptor;
  2616. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2617. VAR import: SyntaxTree.Import;
  2618. s: Basic.MessageString;
  2619. selfName: SyntaxTree.IdentifierString;
  2620. BEGIN
  2621. moduleScope.ownerModule.GetName(selfName);
  2622. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2623. module := moduleScope.ownerModule
  2624. ELSE
  2625. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2626. IF import = NIL THEN
  2627. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2628. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2629. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2630. s := "Module ";
  2631. Strings.Append(s,moduleName);
  2632. Strings.Append(s," cannot be imported.");
  2633. IF force THEN
  2634. Error(position,s);
  2635. ELSIF canBeLoaded THEN
  2636. Strings.Append(s, "=> no dynamic linking.");
  2637. Warning(position, s);
  2638. canBeLoaded := FALSE;
  2639. END;
  2640. RETURN FALSE
  2641. ELSE
  2642. SELF.module.imports.AddName(moduleName)
  2643. END;
  2644. ELSIF import.module = NIL THEN (* already tried *)
  2645. RETURN FALSE
  2646. END;
  2647. module := import.module;
  2648. END;
  2649. RETURN TRUE
  2650. END AddImport;
  2651. (* needed for old binary object file format*)
  2652. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2653. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2654. BEGIN
  2655. IF AddImport(moduleName,module,TRUE) THEN
  2656. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2657. IF procedure = NIL THEN
  2658. s := "Instruction not supported on target, emulation procedure ";
  2659. Strings.Append(s,moduleName);
  2660. Strings.Append(s,".");
  2661. Strings.Append(s,procedureName);
  2662. Strings.Append(s," not present");
  2663. Error(position,s);
  2664. ELSE
  2665. StaticCallOperand(r,procedure);
  2666. ReleaseOperand(r);
  2667. fp := GetFingerprint(procedure);
  2668. END;
  2669. END;
  2670. END EnsureSymbol;
  2671. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2672. VAR exit: Label; trueL,falseL: Label;
  2673. BEGIN
  2674. trueL := NewLabel();
  2675. falseL := NewLabel();
  2676. exit := NewLabel();
  2677. Condition(x,trueL,falseL);
  2678. InitOperand(result,ModeValue);
  2679. SetLabel(trueL);
  2680. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2681. Emit(Mov(position,result.op,true));
  2682. BrL(exit);
  2683. SetLabel(falseL);
  2684. Emit(MovReplace(position,result.op,false));
  2685. SetLabel(exit);
  2686. END ConditionToValue;
  2687. PROCEDURE ValueToCondition(VAR op: Operand);
  2688. BEGIN
  2689. LoadValue(op,system.booleanType);
  2690. BrneL(trueLabel,op.op, false);
  2691. ReleaseOperand(op);
  2692. BrL(falseLabel);
  2693. END ValueToCondition;
  2694. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2695. VAR size: LONGINT;
  2696. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2697. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2698. BEGIN
  2699. ASSERT(type.form = SyntaxTree.Open);
  2700. baseType := type.arrayBase.resolved;
  2701. IF IsOpenArray(baseType) THEN
  2702. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2703. ELSE
  2704. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2705. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2706. END;
  2707. len := tag;
  2708. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2709. IntermediateCode.MakeMemory(len,addressType);
  2710. UseIntermediateOperand(len);
  2711. Reuse2(res,sizeOperand,len);
  2712. Emit(Mul(position,res,sizeOperand,len));
  2713. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2714. RETURN res
  2715. END GetArraySize;
  2716. BEGIN
  2717. type := type.resolved;
  2718. IF IsOpenArray(type) THEN
  2719. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2720. RETURN tag
  2721. ELSE
  2722. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2723. END;
  2724. ELSE
  2725. size := ToMemoryUnits(system,system.SizeOf(type));
  2726. RETURN IntermediateCode.Immediate(addressType,size)
  2727. END;
  2728. END GetDynamicSize;
  2729. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2730. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2731. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2732. BEGIN
  2733. ASSERT(type.form = SyntaxTree.Open);
  2734. baseType := type.arrayBase.resolved;
  2735. IF IsOpenArray(baseType) THEN
  2736. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2737. ELSE
  2738. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2739. END;
  2740. len := tag;
  2741. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2742. IntermediateCode.MakeMemory(len,addressType);
  2743. UseIntermediateOperand(len);
  2744. Reuse2(res,sizeOperand,len);
  2745. Emit(Mul(position,res,sizeOperand,len));
  2746. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2747. RETURN res
  2748. END GetLength;
  2749. BEGIN
  2750. type := type.resolved;
  2751. IF IsOpenArray(type) THEN
  2752. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2753. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2754. ELSIF type IS SyntaxTree.StringType THEN
  2755. RETURN tag;
  2756. ELSE
  2757. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2758. END;
  2759. END GetArrayLength;
  2760. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2761. VAR result: IntermediateCode.Operand;
  2762. BEGIN
  2763. IF backend.cooperative THEN
  2764. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2765. ELSE
  2766. MakeMemory(result, tag, addressType, 0);
  2767. END;
  2768. RETURN result
  2769. END GetSizeFromTag;
  2770. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2771. VAR parameter: SyntaxTree.Parameter;
  2772. BEGIN
  2773. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2774. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2775. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2776. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2777. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. END;
  2779. RETURN GetDynamicSize(e.type, tag);
  2780. END GetArrayOfBytesSize;
  2781. (*
  2782. to find imported symbol. not needed ?
  2783. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2784. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2785. BEGIN
  2786. IF AddImport(moduleName,importedModule,FALSE) THEN
  2787. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2788. RETURN symbol
  2789. ELSE
  2790. RETURN NIL
  2791. END
  2792. END SymbolByName;
  2793. *)
  2794. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2795. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2796. BEGIN
  2797. IF AddImport(moduleName,runtimeModule,force) THEN
  2798. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2799. IF procedure = NIL THEN
  2800. s := "Procedure ";
  2801. Strings.Append(s,moduleName);
  2802. Strings.Append(s,".");
  2803. Strings.Append(s,procedureName);
  2804. Strings.Append(s," not present");
  2805. Error(position,s);
  2806. RETURN FALSE
  2807. ELSE
  2808. RETURN TRUE
  2809. END;
  2810. ELSE RETURN FALSE
  2811. END;
  2812. END GetRuntimeProcedure;
  2813. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2814. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2815. s: Basic.MessageString;
  2816. BEGIN
  2817. Basic.InitSegmentedName(name);
  2818. name[0] := Basic.MakeString(moduleName);
  2819. name[1] := Basic.MakeString(typeName);
  2820. name[2] := -1;
  2821. IF AddImport(moduleName,importedModule, FALSE) THEN
  2822. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2823. IF symbol = NIL THEN
  2824. s := "type ";
  2825. Strings.Append(s,moduleName);
  2826. Strings.Append(s,".");
  2827. Strings.Append(s,typeName);
  2828. Strings.Append(s," not present");
  2829. Error(position,s);
  2830. END;
  2831. ELSE symbol := NIL;
  2832. END;
  2833. IF importedModule = moduleScope.ownerModule THEN
  2834. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2835. ELSE
  2836. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. END;
  2838. RETURN symbol
  2839. END GetTypeDescriptor;
  2840. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2841. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2842. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2843. pooledName: Basic.SegmentedName; size: LONGINT;
  2844. BEGIN
  2845. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2846. StaticCallOperand(result,procedure);
  2847. IF numberParameters < 0 THEN
  2848. size := ProcedureParametersSize(system,procedure);
  2849. ELSE
  2850. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2851. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2852. Error(position,"runtime call parameter count mismatch");
  2853. END;
  2854. END;
  2855. Emit(Call(position, result.op, size));
  2856. ReleaseOperand(result);
  2857. ELSE (* only static linking possible *)
  2858. ASSERT(numberParameters >= 0);
  2859. Basic.InitSegmentedName(pooledName);
  2860. pooledName[0] := Basic.MakeString(moduleName);
  2861. pooledName[1] := Basic.MakeString(procedureName);
  2862. pooledName[2] := -1;
  2863. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2864. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2865. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2866. END;
  2867. END CallThisChecked;
  2868. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2869. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2870. BEGIN
  2871. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2872. END CallThis;
  2873. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2874. VAR
  2875. left,right: Operand;
  2876. leftSize, rightSize: IntermediateCode.Operand;
  2877. saved: RegisterEntry;
  2878. reg: IntermediateCode.Operand;
  2879. procedureName: SyntaxTree.IdentifierString;
  2880. BEGIN
  2881. procedureName := "CompareString";
  2882. SaveRegisters();ReleaseUsedRegisters(saved);
  2883. Designate(leftExpression,left);
  2884. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2885. Emit(Push(position,leftSize));
  2886. ReleaseIntermediateOperand(leftSize);
  2887. Emit(Push(position,left.op));
  2888. ReleaseOperand(left);
  2889. Designate(rightExpression,right);
  2890. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2891. Emit(Push(position,rightSize));
  2892. ReleaseIntermediateOperand(rightSize);
  2893. Emit(Push(position,right.op));
  2894. ReleaseOperand(right);
  2895. IF backend.cooperative THEN
  2896. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2897. ELSE
  2898. CallThis(position,runtimeModuleName,procedureName, 4);
  2899. END;
  2900. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2901. Emit(Result(position,reg));
  2902. (*
  2903. AcquireThisRegister(int8,IntermediateCode.Result);
  2904. *)
  2905. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2906. (*
  2907. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2908. *)
  2909. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2910. ReleaseIntermediateOperand(reg);
  2911. BrL(falseLabel);
  2912. END CompareString;
  2913. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2914. VAR
  2915. left,right: Operand;
  2916. leftSize, rightSize: IntermediateCode.Operand;
  2917. saved: RegisterEntry;
  2918. procedureName: SyntaxTree.IdentifierString;
  2919. BEGIN
  2920. procedureName := "CopyString";
  2921. SaveRegisters();ReleaseUsedRegisters(saved);
  2922. Designate(leftExpression,left);
  2923. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2924. Emit(Push(position,leftSize));
  2925. ReleaseIntermediateOperand(leftSize);
  2926. Emit(Push(position,left.op));
  2927. ReleaseOperand(left);
  2928. Designate(rightExpression,right);
  2929. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2930. Emit(Push(position,rightSize));
  2931. ReleaseIntermediateOperand(rightSize);
  2932. Emit(Push(position,right.op));
  2933. ReleaseOperand(right);
  2934. IF backend.cooperative THEN
  2935. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2936. ELSE
  2937. CallThis(position,runtimeModuleName,procedureName,4);
  2938. END;
  2939. RestoreRegisters(saved);
  2940. END CopyString;
  2941. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2942. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2943. leftType,rightType: SyntaxTree.Type;
  2944. leftExpression,rightExpression : SyntaxTree.Expression;
  2945. componentType: IntermediateCode.Type;
  2946. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2947. size: LONGINT;
  2948. BEGIN
  2949. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2950. dest := destination; destination := emptyOperand;
  2951. leftType := x.left.type.resolved;
  2952. rightType := x.right.type.resolved;
  2953. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2954. CASE x.operator OF
  2955. Scanner.Or:
  2956. (* shortcut evaluation of left OR right *)
  2957. IF ~conditional THEN ConditionToValue(x);
  2958. ELSE
  2959. next := NewLabel();
  2960. Condition(x.left,trueLabel,next);
  2961. SetLabel(next);
  2962. Condition(x.right,trueLabel,falseLabel);
  2963. END;
  2964. |Scanner.And:
  2965. (* shortcut evaluation of left & right *)
  2966. IF ~conditional THEN ConditionToValue(x);
  2967. ELSE
  2968. next := NewLabel();
  2969. Condition(x.left,next,falseLabel);
  2970. SetLabel(next);
  2971. Condition(x.right,trueLabel,falseLabel);
  2972. END;
  2973. |Scanner.Is:
  2974. IF ~conditional THEN ConditionToValue(x);
  2975. ELSE
  2976. (* get type desc tag *)
  2977. IF IsPointerToRecord(leftType,recordType) THEN
  2978. Evaluate(x.left,left);
  2979. Dereference(left,recordType,IsUnsafePointer(leftType))
  2980. ELSE
  2981. Designate(x.left,left);
  2982. END;
  2983. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2984. ReleaseOperand(left);
  2985. END;
  2986. |Scanner.Plus:
  2987. Evaluate(x.left,left);
  2988. Evaluate(x.right,right);
  2989. IF leftType IS SyntaxTree.SetType THEN
  2990. InitOperand(result,ModeValue);
  2991. Reuse2a(result.op,left.op,right.op,dest);
  2992. Emit(Or(position,result.op,left.op,right.op));
  2993. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2994. InitOperand(result,ModeValue);
  2995. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2996. Reuse2(result.tag,left.tag,right.tag);
  2997. Emit(Add(position,result.op,left.op,right.op));
  2998. Emit(Add(position,result.tag,left.tag,right.tag))
  2999. ELSE
  3000. InitOperand(result,ModeValue);
  3001. Reuse2a(result.op,left.op,right.op,dest);
  3002. Emit(Add(position,result.op,left.op,right.op));
  3003. END;
  3004. ReleaseOperand(left); ReleaseOperand(right);
  3005. |Scanner.Minus:
  3006. Evaluate(x.left,left);
  3007. Evaluate(x.right,right);
  3008. IF leftType IS SyntaxTree.SetType THEN
  3009. InitOperand(result,ModeValue);
  3010. Reuse1(result.op,right.op);
  3011. Emit(Not(position,result.op,right.op));
  3012. ReleaseOperand(right);
  3013. Emit(And(position,result.op,result.op,left.op));
  3014. ReleaseOperand(left);
  3015. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3016. InitOperand(result,ModeValue);
  3017. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3018. Reuse2(result.tag,left.tag,right.tag);
  3019. Emit(Sub(position,result.op,left.op,right.op));
  3020. Emit(Sub(position,result.tag,left.tag,right.tag));
  3021. ReleaseOperand(left); ReleaseOperand(right)
  3022. ELSE
  3023. InitOperand(result,ModeValue);
  3024. Reuse2a(result.op,left.op,right.op,dest);
  3025. Emit(Sub(position,result.op,left.op,right.op));
  3026. ReleaseOperand(left); ReleaseOperand(right);
  3027. END;
  3028. |Scanner.Times:
  3029. Evaluate(x.left,left);
  3030. Evaluate(x.right,right);
  3031. IF leftType IS SyntaxTree.SetType THEN
  3032. InitOperand(result,ModeValue);
  3033. Reuse2a(result.op,left.op,right.op,dest);
  3034. Emit(And(position,result.op,left.op,right.op));
  3035. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3036. InitOperand(result, ModeValue);
  3037. componentType := left.op.type;
  3038. (* TODO: review this *)
  3039. (*
  3040. result.op = left.op * right.op - left.tag * right.tag
  3041. result.tag = left.tag * right.op + left.op * right.tag
  3042. *)
  3043. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3044. Emit(Mul(position,result.op, left.op, right.op));
  3045. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3046. Emit(Mul(position,tempReg, left.tag, right.tag));
  3047. Emit(Sub(position,result.op, result.op, tempReg));
  3048. Reuse2(result.tag, left.tag, right.op);
  3049. Emit(Mul(position,result.tag, left.tag, right.op));
  3050. Emit(Mul(position,tempReg, left.op, right.tag));
  3051. Emit(Add(position,result.tag, result.tag, tempReg));
  3052. ReleaseIntermediateOperand(tempReg)
  3053. ELSE
  3054. InitOperand(result,ModeValue);
  3055. Reuse2a(result.op,left.op,right.op,dest);
  3056. Emit(Mul(position,result.op,left.op,right.op));
  3057. END;
  3058. ReleaseOperand(left); ReleaseOperand(right);
  3059. |Scanner.Div:
  3060. Evaluate(x.left,left);
  3061. Evaluate(x.right,right);
  3062. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3063. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3064. IF ~isUnchecked THEN
  3065. exit := NewLabel();
  3066. BrltL(exit,zero,right.op);
  3067. EmitTrap(position,NegativeDivisorTrap);
  3068. SetLabel(exit);
  3069. END;
  3070. END;
  3071. InitOperand(result,ModeValue);
  3072. Reuse2a(result.op,left.op,right.op,dest);
  3073. Emit(Div(position,result.op,left.op,right.op));
  3074. ReleaseOperand(left); ReleaseOperand(right);
  3075. |Scanner.Mod:
  3076. Evaluate(x.left,left);
  3077. Evaluate(x.right,right);
  3078. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3079. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3080. IF ~isUnchecked THEN
  3081. exit := NewLabel();
  3082. BrltL(exit,zero,right.op);
  3083. EmitTrap(position,NegativeDivisorTrap);
  3084. SetLabel(exit);
  3085. END;
  3086. END;
  3087. InitOperand(result,ModeValue);
  3088. Reuse2a(result.op,left.op,right.op,dest);
  3089. Emit(Mod(position,result.op,left.op,right.op));
  3090. ReleaseOperand(left); ReleaseOperand(right);
  3091. |Scanner.Slash:
  3092. Evaluate(x.left,left);
  3093. Evaluate(x.right,right);
  3094. IF leftType IS SyntaxTree.SetType THEN
  3095. InitOperand(result,ModeValue);
  3096. Reuse2a(result.op,left.op,right.op,dest);
  3097. Emit(Xor(position,result.op,left.op,right.op));
  3098. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3099. InitOperand(result,ModeValue);
  3100. componentType := left.op.type;
  3101. (* review this *)
  3102. (*
  3103. divisor = right.op * right.op + right.tag * right.tag
  3104. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3105. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3106. *)
  3107. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3108. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3109. Emit(Mul(position,tempReg, right.op, right.op));
  3110. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3111. Emit(Add(position,tempReg, tempReg, tempReg2));
  3112. result.op := tempReg2;
  3113. Emit(Mul(position,result.op, left.op, right.op));
  3114. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3115. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3116. Emit(Add(position,result.op, result.op, tempReg2));
  3117. Emit(Div(position,result.op, result.op, tempReg));
  3118. Reuse2(result.tag, left.tag, right.op);
  3119. Emit(Mul(position,result.tag, left.tag, right.op));
  3120. Emit(Mul(position,tempReg2, left.op, right.tag));
  3121. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3122. Emit(Div(position,result.tag, result.tag, tempReg));
  3123. ReleaseIntermediateOperand(tempReg);
  3124. ReleaseIntermediateOperand(tempReg2)
  3125. ELSE
  3126. InitOperand(result,ModeValue);
  3127. Reuse2a(result.op,left.op,right.op,dest);
  3128. Emit(Div(position,result.op,left.op,right.op));
  3129. END;
  3130. ReleaseOperand(left); ReleaseOperand(right);
  3131. |Scanner.Equal:
  3132. IF ~conditional THEN ConditionToValue(x);
  3133. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3134. CompareString(BreqL,x.left,x.right);
  3135. ELSE
  3136. Evaluate(x.left,left);
  3137. Evaluate(x.right,right);
  3138. IF leftType IS SyntaxTree.RangeType THEN
  3139. ASSERT(rightType IS SyntaxTree.RangeType);
  3140. BrneL(falseLabel, left.op, right.op); (* first *)
  3141. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3142. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3143. ReleaseOperand(left); ReleaseOperand(right);
  3144. BrL(trueLabel)
  3145. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3146. BrneL(falseLabel, left.op, right.op); (* first *)
  3147. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3148. ReleaseOperand(left); ReleaseOperand(right);
  3149. BrL(trueLabel)
  3150. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3151. (* TODO: review this *)
  3152. BrneL(falseLabel, left.op, right.op); (* real part *)
  3153. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3154. ReleaseOperand(left); ReleaseOperand(right);
  3155. BrL(trueLabel)
  3156. ELSE
  3157. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3158. ReleaseOperand(left); ReleaseOperand(right);
  3159. BrL(trueLabel);
  3160. END;
  3161. END;
  3162. |Scanner.LessEqual:
  3163. IF ~conditional THEN ConditionToValue(x);
  3164. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3165. CompareString(BrgeL,x.right,x.left);
  3166. ELSE
  3167. Evaluate(x.left,left);
  3168. Evaluate(x.right,right);
  3169. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3170. Reuse1(temp.op,right.op);
  3171. Emit(And(position,temp.op,left.op,right.op));
  3172. ReleaseOperand(right);
  3173. BreqL(trueLabel,temp.op,left.op);
  3174. BrL(falseLabel);
  3175. ReleaseOperand(temp);ReleaseOperand(left);
  3176. ELSE
  3177. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3178. ReleaseOperand(left); ReleaseOperand(right);
  3179. BrL(trueLabel);
  3180. END;
  3181. END;
  3182. |Scanner.Less:
  3183. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3184. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3185. leftExpression.SetType(system.booleanType);
  3186. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3187. rightExpression.SetType(system.booleanType);
  3188. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3189. leftExpression.SetType(system.booleanType);
  3190. Expression(leftExpression);
  3191. ELSIF ~conditional THEN ConditionToValue(x);
  3192. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3193. CompareString(BrltL,x.left,x.right);
  3194. ELSE
  3195. Evaluate(x.left,left);
  3196. Evaluate(x.right,right);
  3197. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3198. ReleaseOperand(left); ReleaseOperand(right);
  3199. BrL(trueLabel);
  3200. END;
  3201. |Scanner.Greater:
  3202. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3203. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3204. leftExpression.SetType(system.booleanType);
  3205. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3206. rightExpression.SetType(system.booleanType);
  3207. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3208. leftExpression.SetType(system.booleanType);
  3209. Expression(leftExpression);
  3210. ELSIF ~conditional THEN ConditionToValue(x);
  3211. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3212. CompareString(BrltL,x.right,x.left);
  3213. ELSE
  3214. Evaluate(x.left,left);
  3215. Evaluate(x.right,right);
  3216. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3217. ReleaseOperand(left); ReleaseOperand(right);
  3218. BrL(trueLabel);
  3219. END;
  3220. |Scanner.GreaterEqual:
  3221. IF ~conditional THEN ConditionToValue(x);
  3222. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3223. CompareString(BrgeL,x.left,x.right);
  3224. ELSE
  3225. Evaluate(x.left,left);
  3226. Evaluate(x.right,right);
  3227. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3228. Reuse1(temp.op,left.op);
  3229. Emit(And(position,temp.op,left.op,right.op));
  3230. ReleaseOperand(left);
  3231. BreqL(trueLabel, temp.op,right.op);
  3232. ReleaseOperand(temp); ReleaseOperand(right);
  3233. BrL(falseLabel);
  3234. ELSE
  3235. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3236. ReleaseOperand(left); ReleaseOperand(right);
  3237. BrL(trueLabel);
  3238. END;
  3239. END;
  3240. |Scanner.Unequal:
  3241. IF ~conditional THEN ConditionToValue(x);
  3242. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3243. CompareString(BrneL,x.left,x.right);
  3244. ELSE
  3245. Evaluate(x.left,left);
  3246. Evaluate(x.right,right);
  3247. IF leftType IS SyntaxTree.RangeType THEN
  3248. ASSERT(rightType IS SyntaxTree.RangeType);
  3249. BrneL(trueLabel, left.op, right.op); (* first *)
  3250. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3251. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3252. ReleaseOperand(left); ReleaseOperand(right);
  3253. BrL(falseLabel)
  3254. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3255. BrneL(trueLabel, left.op, right.op); (* first *)
  3256. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3257. ReleaseOperand(left); ReleaseOperand(right);
  3258. BrL(falseLabel)
  3259. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3260. (* TODO: review this *)
  3261. BrneL(trueLabel, left.op, right.op); (* real part *)
  3262. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3263. ReleaseOperand(left); ReleaseOperand(right);
  3264. BrL(falseLabel)
  3265. ELSE
  3266. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3267. ReleaseOperand(left); ReleaseOperand(right);
  3268. BrL(trueLabel);
  3269. END;
  3270. END;
  3271. |Scanner.In:
  3272. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3273. Evaluate(x.left,left);
  3274. Evaluate(x.right,right);
  3275. Convert(left.op,setType);
  3276. ReuseCopy(temp.op,right.op);
  3277. Emit(Shr(position,temp.op,temp.op,left.op));
  3278. ReleaseOperand(right); ReleaseOperand(left);
  3279. IntermediateCode.InitImmediate(one,setType,1);
  3280. Emit(And(position,temp.op,temp.op,one));
  3281. IF conditional THEN
  3282. IntermediateCode.InitImmediate(zero,setType,0);
  3283. BrneL(trueLabel,temp.op,zero);
  3284. ReleaseOperand(temp);
  3285. BrL(falseLabel);
  3286. ELSE
  3287. Convert(temp.op,bool);
  3288. result.mode := ModeValue;
  3289. result.op := temp.op;
  3290. result.tag := nil; (* may be left over from calls to evaluate *)
  3291. END;
  3292. ELSE
  3293. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3294. IF x.operator = Scanner.Questionmarks THEN
  3295. leftExpression := x.left;
  3296. rightExpression := x.right;
  3297. ELSE
  3298. leftExpression := x.right;
  3299. rightExpression := x.left;
  3300. END;
  3301. Evaluate(leftExpression, left);
  3302. Emit(Push(position,left.op));
  3303. ReleaseOperand(left);
  3304. Designate(rightExpression, right);
  3305. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3306. IF ~backend.cellsAreObjects THEN
  3307. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3308. END;
  3309. Emit(Push(position,right.op));
  3310. ReleaseOperand(right);
  3311. IF backend.cellsAreObjects THEN
  3312. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3313. ELSE
  3314. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3315. END;
  3316. InitOperand(result, ModeValue);
  3317. result.op := NewRegisterOperand(bool);
  3318. Emit(Result(position,result.op));
  3319. IF conditional THEN
  3320. IntermediateCode.InitImmediate(zero,setType,0);
  3321. BrneL(trueLabel,result.op,zero);
  3322. ReleaseOperand(result);
  3323. BrL(falseLabel);
  3324. END;
  3325. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3326. leftExpression := x.left;
  3327. rightExpression := x.right;
  3328. Evaluate(leftExpression, left);
  3329. Emit(Push(position,left.op));
  3330. ReleaseOperand(left);
  3331. Evaluate(rightExpression, right);
  3332. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3333. IF ~backend.cellsAreObjects THEN
  3334. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3335. END;
  3336. Emit(Push(position,right.op));
  3337. ReleaseOperand(right);
  3338. IF backend.cellsAreObjects THEN
  3339. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3340. ELSE
  3341. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3342. END;
  3343. InitOperand(result, ModeValue);
  3344. result.op := NewRegisterOperand(bool);
  3345. Emit(Result(position,result.op));
  3346. IF conditional THEN
  3347. IntermediateCode.InitImmediate(zero,setType,0);
  3348. BrneL(trueLabel,result.op,zero);
  3349. ReleaseOperand(result);
  3350. BrL(falseLabel);
  3351. END;
  3352. ELSE
  3353. HALT(100);
  3354. END;
  3355. END;
  3356. destination := dest;
  3357. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3358. END VisitBinaryExpression;
  3359. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3360. VAR localResult, operand: Operand;
  3361. BEGIN
  3362. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3363. InitOperand(localResult, ModeValue);
  3364. ASSERT(x.first # NIL);
  3365. Evaluate(x.first, operand);
  3366. localResult.op := operand.op;
  3367. ReleaseOperand(operand);
  3368. UseIntermediateOperand(localResult.op);
  3369. ASSERT(x.last # NIL);
  3370. Evaluate(x.last, operand);
  3371. localResult.tag := operand.op;
  3372. ReleaseOperand(operand);
  3373. UseIntermediateOperand(localResult.tag);
  3374. IF x.step # NIL THEN
  3375. Evaluate(x.step, operand);
  3376. localResult.extra := operand.op;
  3377. ReleaseOperand(operand);
  3378. UseIntermediateOperand(localResult.extra);
  3379. END;
  3380. result := localResult;
  3381. IF Trace THEN TraceExit("VisitRangeExpression") END
  3382. END VisitRangeExpression;
  3383. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3384. BEGIN
  3385. HALT(100); (* should never be evaluated *)
  3386. END VisitTensorRangeExpression;
  3387. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3388. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3389. BEGIN
  3390. IF Trace THEN TraceEnter("VisitConversion") END;
  3391. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3392. dest := destination; destination := emptyOperand;
  3393. Evaluate(x.expression,old);
  3394. InitOperand(result,ModeValue);
  3395. result.op := old.op;
  3396. ASSERT(result.op.mode # 0);
  3397. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3398. (* convert TO a complex number *)
  3399. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3400. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3401. ASSERT(result.op.mode # 0);
  3402. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3403. (* convert FROM a complex number TO a complex number*)
  3404. result.tag := old.tag;
  3405. ASSERT(result.tag.mode # 0);
  3406. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3407. ASSERT(result.tag.mode # 0)
  3408. ELSE
  3409. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3410. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3411. END
  3412. ELSE
  3413. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3414. ASSERT(result.op.mode # 0);
  3415. result.tag := old.tag; (*! probably never used *)
  3416. END;
  3417. destination := dest;
  3418. IF Trace THEN TraceExit("VisitConversion") END;
  3419. END VisitConversion;
  3420. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3421. BEGIN
  3422. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3423. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3424. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3425. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3426. END VisitTypeDeclaration;
  3427. (** designators (expressions) *)
  3428. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3429. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3430. BEGIN
  3431. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3432. IF x.left # NIL THEN Expression(x.left) END;
  3433. Symbol(x.symbol,result);
  3434. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3435. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3436. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3437. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3438. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3439. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3440. END;
  3441. END;
  3442. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3443. END VisitSymbolDesignator;
  3444. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3445. BEGIN
  3446. IF isUnchecked THEN RETURN END;
  3447. IF tagsAvailable THEN
  3448. TrapC(BrltL,index,length,IndexCheckTrap);
  3449. END;
  3450. END BoundCheck;
  3451. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3452. VAR d: IntermediateCode.Operand;
  3453. BEGIN
  3454. IF isUnchecked THEN RETURN END;
  3455. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3456. TrapC(op,dim,d,ArraySizeTrap);
  3457. ReleaseIntermediateOperand(d);
  3458. END DimensionCheck;
  3459. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3460. VAR
  3461. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3462. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3463. expression: SyntaxTree.Expression;
  3464. resultingType, leftType, baseType: SyntaxTree.Type;
  3465. skipLabel1: Label;
  3466. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3467. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3468. variableOp: Operand;
  3469. variable: SyntaxTree.Variable;
  3470. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3471. VAR expression: SyntaxTree.Expression;
  3472. BEGIN
  3473. (* count the number of indices, ranges and tensorRanges in the index list *)
  3474. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3475. FOR i := 0 TO parameters.Length() - 1 DO
  3476. expression := parameters.GetExpression(i);
  3477. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3478. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3479. ELSE INC(indexCount)
  3480. END
  3481. END;
  3482. END CountIndices;
  3483. BEGIN
  3484. ASSERT(tagsAvailable);
  3485. resultingType := x.type.resolved; (* resulting type *)
  3486. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3487. InitOperand(localResult, ModeReference);
  3488. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3489. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3490. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3491. (* a globally defined array destination tag is available -> use and invalidate it*)
  3492. localResult.tag := arrayDestinationTag;
  3493. IntermediateCode.InitOperand(arrayDestinationTag)
  3494. ELSE
  3495. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3496. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3497. Symbol(variable, variableOp);
  3498. ReuseCopy(localResult.tag, variableOp.op);
  3499. ReleaseOperand(variableOp);
  3500. END
  3501. END;
  3502. indexListSize := x.parameters.Length();
  3503. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3504. ASSERT(tensorRangeCount <= 1);
  3505. (* designate the array to be indexed over, perform tensor range check if known *)
  3506. Designate(x.left, array);
  3507. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3508. Dereference(array, leftType,FALSE);
  3509. IF tensorRangeCount=0 THEN
  3510. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3511. END
  3512. END;
  3513. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3514. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3515. srcDimOffset := -indexListSize;
  3516. destDimOffset := -rangeCount
  3517. ELSE
  3518. srcDimOffset := 0;
  3519. destDimOffset := 0
  3520. END;
  3521. indexDim := 0;
  3522. (* use address of source array as basis *)
  3523. (*
  3524. ReuseCopy(localResult.op, array.op);
  3525. *)
  3526. localResult.op := array.op;
  3527. UseIntermediateOperand(localResult.op);
  3528. (* go through the index list *)
  3529. FOR i := 0 TO indexListSize - 1 DO
  3530. expression := x.parameters.GetExpression(i);
  3531. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3532. (* nothing to do *)
  3533. ELSE
  3534. (* determine which dimension of source array is currently looked at *)
  3535. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3536. (* get the memory operand pointing to array descriptor dimension *)
  3537. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3538. (* make a reusable register from it *)
  3539. ReuseCopy(srcDim, tmp);
  3540. ReleaseIntermediateOperand(tmp);
  3541. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3542. ELSE
  3543. srcDim := IntermediateCode.Immediate(int32, i)
  3544. END;
  3545. (* get length and increment of source array for current dimension *)
  3546. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3547. Convert(sourceLength.op, sizeType);
  3548. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3549. Convert(sourceIncrement.op, sizeType);
  3550. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3551. ReleaseIntermediateOperand(srcDim);
  3552. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3553. (* SINGLE INDEX *)
  3554. Evaluate(expression, index);
  3555. ReleaseIntermediateOperand(index.tag);
  3556. index.tag := emptyOperand;
  3557. Convert(index.op, sizeType);
  3558. (* lower bound check *)
  3559. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3560. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3561. ELSIF isUnchecked THEN (* do nothing *)
  3562. ELSE
  3563. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3564. END;
  3565. (* upper bound check *)
  3566. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3567. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3568. ELSIF isUnchecked THEN (* do nothing *)
  3569. ELSE
  3570. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3571. END;
  3572. ReleaseOperand(sourceLength);
  3573. Convert(index.op, addressType);
  3574. summand := index.op;
  3575. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3576. (* RANGE OF INDICES *)
  3577. Evaluate(expression, range);
  3578. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3579. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3580. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3581. ReleaseOperand(range);
  3582. Convert(firstIndex, sizeType);
  3583. Convert(lastIndex, sizeType);
  3584. Convert(stepSize, sizeType);
  3585. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3586. if it is 'MAX(LONGINT)' *)
  3587. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3588. TransferToRegister(lastIndex, lastIndex);
  3589. skipLabel1 := NewLabel();
  3590. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3591. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3592. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3593. SetLabel(skipLabel1)
  3594. END;
  3595. (* check if step size is valid *)
  3596. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3597. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3598. ELSIF isUnchecked THEN (* do nothing *)
  3599. ELSE
  3600. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3601. END;
  3602. (* check lower bound check *)
  3603. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3604. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3605. ELSIF isUnchecked THEN (* do nothing *)
  3606. ELSE
  3607. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3608. END;
  3609. (* check upper bound check *)
  3610. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3611. (* statically open range: nothing to do *)
  3612. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3613. ASSERT(staticLastIndex < staticSourceLength)
  3614. ELSIF isUnchecked THEN (* do nothing *)
  3615. ELSE
  3616. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3617. END;
  3618. (* determine length of target array for current dimension *)
  3619. (* 1. incorporate last index: *)
  3620. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3621. (* last index is static *)
  3622. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3623. targetLength := sourceLength.op
  3624. ELSE
  3625. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3626. END;
  3627. UseIntermediateOperand(targetLength);
  3628. ELSE
  3629. (* targetLength := lastIndex + 1
  3630. Reuse1(targetLength, lastIndex);
  3631. *)
  3632. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3633. END;
  3634. ReleaseOperand(sourceLength);
  3635. ReleaseIntermediateOperand(lastIndex);
  3636. (* 2. incorporate first index: *)
  3637. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3638. (* first index and current target length are static *)
  3639. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3640. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3641. (* first index = 0: nothing to do *)
  3642. ELSE
  3643. (* targetLength := targetLength - firstIndex *)
  3644. TransferToRegister(targetLength, targetLength);
  3645. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3646. END;
  3647. (* clip negative lengths to 0 *)
  3648. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3649. IF staticTargetLength < 0 THEN
  3650. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3651. END
  3652. ELSE
  3653. skipLabel1 := NewLabel();
  3654. TransferToRegister(targetLength, targetLength);
  3655. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3656. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3657. SetLabel(skipLabel1)
  3658. END;
  3659. (* 3. incorporate index step size: *)
  3660. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3661. (*step size and current target length are static *)
  3662. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3663. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3664. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3665. (* step size = 1: nothing to do *)
  3666. ELSE
  3667. (* emit code for this:
  3668. targetLength := (targetLength-1) DIV stepSize +1;
  3669. *)
  3670. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3671. DivInt(targetLength, targetLength, stepSize);
  3672. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3673. END;
  3674. (* determine increment of target array for current dimension *)
  3675. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3676. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3677. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3678. (* step size = 1 *)
  3679. targetIncrement := sourceIncrement.op;
  3680. UseIntermediateOperand(targetIncrement)
  3681. ELSE
  3682. (* targetIncrement := sourceIncrement * stepSize *)
  3683. Reuse1(targetIncrement, stepSize);
  3684. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3685. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3686. END;
  3687. ReleaseIntermediateOperand(stepSize);
  3688. (* write length and increment of target array to descriptor *)
  3689. IF destDimOffset < 0 THEN
  3690. (* determine which dimension of target array is currently looked at *)
  3691. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3692. TransferToRegister(destDim, tmp);
  3693. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3694. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3695. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3696. ReleaseIntermediateOperand(destDim)
  3697. ELSE
  3698. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3699. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3700. END;
  3701. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3702. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3703. INC(indexDim);
  3704. Convert(firstIndex, addressType);
  3705. TransferToRegister(summand, firstIndex);
  3706. ELSE HALT(100);
  3707. END;
  3708. (*
  3709. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3710. *)
  3711. Convert(sourceIncrement.op, addressType);
  3712. Convert(summand, addressType);
  3713. MulInt(summand, summand, sourceIncrement.op);
  3714. ReleaseIntermediateOperand(sourceIncrement.op);
  3715. AddInt(localResult.op, localResult.op, summand);
  3716. ReleaseIntermediateOperand(summand);
  3717. END
  3718. END;
  3719. result := localResult;
  3720. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3721. ReleaseIntermediateOperand(result.tag);
  3722. result.tag := TypeDescriptorAdr(resultingType);
  3723. IF ~newObjectFile THEN
  3724. IntermediateCode.MakeMemory(result.tag,addressType);
  3725. END;
  3726. ELSIF IsDelegate(resultingType) THEN
  3727. ReleaseIntermediateOperand(result.tag);
  3728. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3729. UseIntermediateOperand(result.tag);
  3730. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3731. ReleaseIntermediateOperand(result.tag);
  3732. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3733. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3734. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3735. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3736. (* finalize target array descriptor *)
  3737. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3738. (* write lengths and increments of target array for remaining dimensions *)
  3739. i := indexListSize;
  3740. WHILE indexDim < targetArrayDimensionality DO
  3741. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3742. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3743. ReleaseOperand(sourceLength);
  3744. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3745. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3746. ReleaseOperand(sourceIncrement);
  3747. INC(i); INC(indexDim);
  3748. END;
  3749. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3750. tmp := nil;
  3751. ELSE
  3752. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3753. END;
  3754. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3755. ReleaseIntermediateOperand(tmp);
  3756. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3757. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3758. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3759. ELSE
  3760. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3761. END;
  3762. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3763. ReleaseIntermediateOperand(tmp);
  3764. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3765. (* write dimensionality *)
  3766. IF targetArrayDimensionality # 0 THEN
  3767. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3768. END;
  3769. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3770. END;
  3771. ReleaseOperand(array);
  3772. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3773. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3774. END;
  3775. END MathIndexDesignator;
  3776. (* get the length of an array , trying to make use of static information *)
  3777. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3778. VAR res: IntermediateCode.Operand; size: LONGINT;
  3779. BEGIN
  3780. type := type.resolved;
  3781. IF type IS SyntaxTree.ArrayType THEN
  3782. WITH type: SyntaxTree.ArrayType DO
  3783. IF type.form = SyntaxTree.Static THEN
  3784. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3785. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3786. Evaluate(type.length, op);
  3787. ReleaseIntermediateOperand(op.tag);
  3788. RETURN op.op;*)
  3789. ELSE
  3790. res := tag;
  3791. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3792. IntermediateCode.MakeMemory(res,addressType);
  3793. UseIntermediateOperand(res);
  3794. RETURN res
  3795. END
  3796. END;
  3797. ELSE
  3798. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3799. RETURN IntermediateCode.Immediate(addressType,size);
  3800. END;
  3801. END ArrayLength;
  3802. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3803. BEGIN
  3804. IF IsImmediate(x) THEN
  3805. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3806. ELSE
  3807. IF ~ReusableRegister(res) THEN
  3808. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3809. ELSE
  3810. UseIntermediateOperand(res);
  3811. END;
  3812. Emit(Mov(position,res,x))
  3813. END;
  3814. END CopyInt;
  3815. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3816. BEGIN
  3817. ReleaseIntermediateOperand(res);
  3818. IF IsImmediate(x) & IsImmediate(y) THEN
  3819. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3820. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3821. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3822. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3823. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3824. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3825. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3826. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3827. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3828. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3829. UseIntermediateOperand(res);
  3830. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3831. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3832. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3833. UseIntermediateOperand(res);
  3834. ELSE
  3835. IF ~ReusableRegister(res) THEN
  3836. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3837. ELSE
  3838. UseIntermediateOperand(res);
  3839. END;
  3840. IF IsImmediate(x) & (x.intValue = 0) THEN
  3841. Emit(Mov(position,res,y))
  3842. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3843. Emit(Mov(position,res,x))
  3844. ELSE
  3845. Emit(Add(position,res, x, y));
  3846. END;
  3847. END;
  3848. END AddInt;
  3849. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3850. BEGIN
  3851. ReleaseIntermediateOperand(res);
  3852. IF IsImmediate(x) & IsImmediate(y) THEN
  3853. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3854. ELSE
  3855. IF ~ReusableRegister(res) THEN
  3856. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3857. ELSE UseIntermediateOperand(res);
  3858. END;
  3859. IF IsImmediate(x) & (x.intValue = 1) THEN
  3860. Emit(Mov(position,res,y))
  3861. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3862. Emit(Mov(position,res,x))
  3863. ELSE
  3864. Emit(Mul(position,res, x, y));
  3865. END;
  3866. END;
  3867. END MulInt;
  3868. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3869. BEGIN
  3870. ReleaseIntermediateOperand(res);
  3871. IF IsImmediate(x) & IsImmediate(y) THEN
  3872. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3873. ELSE
  3874. IF ~ReusableRegister(res) THEN
  3875. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3876. ELSE UseIntermediateOperand(res);
  3877. END;
  3878. IF IsImmediate(x) & (x.intValue = 1) THEN
  3879. Emit(Mov(position,res,y))
  3880. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3881. Emit(Mov(position,res,x))
  3882. ELSE
  3883. Emit(Div(position,res, x, y));
  3884. END;
  3885. END;
  3886. END DivInt;
  3887. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3888. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3889. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3890. BEGIN
  3891. type := x.left.type.resolved;
  3892. IF type IS SyntaxTree.StringType THEN
  3893. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3894. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3895. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3896. type := atype;
  3897. x.left.SetType(type);
  3898. END;
  3899. IntermediateCode.InitImmediate(res,addressType,0);
  3900. maxDim := x.parameters.Length()-1;
  3901. (*
  3902. computation rule:
  3903. a: ARRAY X,Y,Z OF Element with size S
  3904. a[i,j,k] -->
  3905. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3906. *)
  3907. FOR i := 0 TO maxDim DO
  3908. e := x.parameters.GetExpression(i);
  3909. Evaluate(e,index);
  3910. Convert(index.op,addressType);
  3911. AddInt(res, res, index.op);
  3912. IF i = 0 THEN
  3913. (*
  3914. ReuseCopy(res, index.op);
  3915. *)
  3916. Designate(x.left,array);
  3917. type := x.left.type.resolved;
  3918. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3919. Dereference(array, type, FALSE);
  3920. END;
  3921. (*
  3922. ELSE AddInt(res, res, index.op);
  3923. *)
  3924. END;
  3925. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3926. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3927. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3928. BoundCheck(index.op, length);
  3929. END;
  3930. ReleaseIntermediateOperand(length);
  3931. END;
  3932. ReleaseOperand(index);
  3933. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3934. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3935. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3936. MulInt(res,res,length);
  3937. END;
  3938. ReleaseIntermediateOperand(length);
  3939. END;
  3940. (* remaining open dimensions -- compute address *)
  3941. i := maxDim+1;
  3942. IF type IS SyntaxTree.ArrayType THEN
  3943. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3944. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3945. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3946. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3947. MulInt(res,res,length);
  3948. END;
  3949. ReleaseIntermediateOperand(length);
  3950. INC(i);
  3951. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3952. END;
  3953. END;
  3954. IF (type IS SyntaxTree.ArrayType) THEN
  3955. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3956. size := StaticSize(system, type);
  3957. IF size # 1 THEN
  3958. length := IntermediateCode.Immediate(addressType,size);
  3959. MulInt(res,res,length);
  3960. END;
  3961. ELSE
  3962. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3963. IF size # 1 THEN
  3964. length := IntermediateCode.Immediate(addressType,size);
  3965. MulInt(res,res,length);
  3966. END;
  3967. END;
  3968. END;
  3969. AddInt(res,res,array.op);
  3970. InitOperand(result,ModeReference);
  3971. result.op := res;
  3972. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3973. ReleaseIntermediateOperand(result.tag);
  3974. result.tag := TypeDescriptorAdr(type);
  3975. IF ~newObjectFile THEN
  3976. IntermediateCode.MakeMemory(result.tag,addressType);
  3977. END
  3978. ELSIF IsDelegate(type) THEN
  3979. ReleaseIntermediateOperand(result.tag);
  3980. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3981. UseIntermediateOperand(result.tag);
  3982. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3983. ReleaseIntermediateOperand(result.tag);
  3984. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3985. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3986. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3987. END;
  3988. ReleaseOperand(array);
  3989. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3990. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3991. END;
  3992. END IndexDesignator;
  3993. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3994. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3995. BEGIN
  3996. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3997. dest := destination; destination := emptyOperand;
  3998. type := x.left.type.resolved;
  3999. IF type IS SyntaxTree.MathArrayType THEN
  4000. MathIndexDesignator(x);
  4001. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4002. IndexDesignator(x);
  4003. END;
  4004. destination := dest;
  4005. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4006. END VisitIndexDesignator;
  4007. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4008. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4009. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4010. procedure: SyntaxTree.Procedure;
  4011. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4012. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4013. BEGIN
  4014. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4015. parameters := expression.parameters;
  4016. moduleName := "FoxArrayBase";
  4017. procedureName := "CopyDescriptor";
  4018. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4019. SaveRegisters();ReleaseUsedRegisters(saved);
  4020. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4021. IF procedure = NIL THEN
  4022. s := "procedure ";
  4023. Strings.Append(s,moduleName);
  4024. Strings.Append(s,".");
  4025. Strings.Append(s,procedureName);
  4026. Strings.Append(s," not present");
  4027. Error(position,s);
  4028. ELSE
  4029. (* push address of temporary variable *)
  4030. Symbol(variable,destOperand);
  4031. Emit(Push(position,destOperand.op));
  4032. ReleaseOperand(destOperand);
  4033. (* push src *)
  4034. Evaluate(expression.left,srcOperand);
  4035. (*
  4036. Dereference(srcOperand,expression.type.resolved);
  4037. Emit(Push(position,srcOperand.tag));
  4038. *)
  4039. Emit(Push(position,srcOperand.op));
  4040. ReleaseOperand(srcOperand);
  4041. tensorFound := FALSE;
  4042. FOR i := 0 TO parameters.Length()-1 DO
  4043. e := parameters.GetExpression(i);
  4044. IF e IS SyntaxTree.TensorRangeExpression THEN
  4045. tensorFound := TRUE;
  4046. ELSIF e IS SyntaxTree.RangeExpression THEN
  4047. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4048. ELSE
  4049. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4050. END;
  4051. END;
  4052. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4053. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4054. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4055. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4056. StaticCallOperand(procOp,procedure);
  4057. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4058. ReleaseOperand(procOp);
  4059. END;
  4060. RestoreRegisters(saved);
  4061. END;
  4062. RETURN variable
  4063. END PrepareTensorDescriptor;
  4064. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4065. VAR
  4066. type, descriptorType, baseType: SyntaxTree.Type;
  4067. operand, tmpOperand, variableOp, variable2Op: Operand;
  4068. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4069. variable, variable2: SyntaxTree.Variable;
  4070. dim, i, size: LONGINT;
  4071. (* TODO: needed? *)
  4072. oldArrayDestinationTag: IntermediateCode.Operand;
  4073. oldArrayDestinationDimension: LONGINT;
  4074. position: Position;
  4075. saved: RegisterEntry;
  4076. arrayFlags: SET;
  4077. m, n: LONGINT;
  4078. PROCEDURE Pass(op: IntermediateCode.Operand);
  4079. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4080. BEGIN
  4081. IF numberRegister >= 0 THEN
  4082. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4083. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4084. Emit(Mov(position,parameterRegister, op));
  4085. ELSE
  4086. Emit(Push(position,op))
  4087. END
  4088. END Pass;
  4089. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4090. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4091. BEGIN
  4092. formalType := formalType.resolved; actualType := actualType.resolved;
  4093. IF IsOpenArray(formalType)THEN
  4094. IF actualType IS SyntaxTree.StringType THEN
  4095. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4096. RETURN;
  4097. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4098. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4099. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4100. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4101. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4102. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4103. ELSE
  4104. tmp := baseReg;
  4105. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4106. IntermediateCode.MakeMemory(tmp,addressType);
  4107. Pass((tmp));
  4108. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4109. END;
  4110. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4111. END;
  4112. END PushArrayLens;
  4113. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4114. BEGIN
  4115. CASE size OF
  4116. |2: INCL(flags,Size2Flag);
  4117. |3: INCL(flags,Size3Flag);
  4118. |4: INCL(flags,Size4Flag);
  4119. |5: INCL(flags,Size5Flag);
  4120. |6: INCL(flags,Size6Flag);
  4121. |7: INCL(flags,Size7Flag);
  4122. |8: INCL(flags,Size8Flag);
  4123. END;
  4124. END SetSmallArraySizeFlag;
  4125. BEGIN
  4126. IF Trace THEN TraceEnter("PushParameter") END;
  4127. position := expression.position;
  4128. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4129. type := expression.type.resolved;
  4130. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4131. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4132. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4133. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4134. );
  4135. (* TODO: needed? *)
  4136. oldArrayDestinationTag := arrayDestinationTag;
  4137. oldArrayDestinationDimension := arrayDestinationDimension;
  4138. IF IsArrayOfSystemByte(parameter.type) THEN
  4139. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4140. Designate(expression,operand);
  4141. ELSE
  4142. Evaluate(expression, tmpOperand);
  4143. variable := GetTemporaryVariable(expression.type, FALSE);
  4144. Symbol(variable, operand);
  4145. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4146. Emit(Mov(position,tmp, tmpOperand.op));
  4147. ReleaseOperand(tmpOperand);
  4148. END;
  4149. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4150. ReleaseIntermediateOperand(operand.tag);
  4151. operand.tag := tmp;
  4152. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4153. Pass((operand.tag));
  4154. END;
  4155. Pass((operand.op));
  4156. ELSIF IsOpenArray(parameter.type) THEN
  4157. Designate(expression,operand);
  4158. baseReg := operand.tag;
  4159. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4160. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4161. END;
  4162. Pass((operand.op)); (* address of the array *)
  4163. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4164. (* case 1 *)
  4165. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4166. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4167. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4168. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4169. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4170. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4171. arrayDestinationTag := sp;
  4172. (* case 1b *)
  4173. IF expression IS SyntaxTree.IndexDesignator THEN
  4174. (*
  4175. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4176. descriptorType := GetMathArrayDescriptorType(dim);
  4177. variable := GetTemporaryVariable(descriptorType,expression.position);
  4178. Symbol(variable,variableOp);
  4179. arrayDestinationTag := variableOp.op;
  4180. *)
  4181. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4182. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4183. arrayDestinationDimension := dim;
  4184. Designate(expression,operand);
  4185. (* case 1a *)
  4186. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4187. Designate(expression,operand);
  4188. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4189. i := 0;
  4190. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4191. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4192. INC(i);
  4193. END;
  4194. type := expression.type.resolved;
  4195. WHILE (i<dim) DO (* remaining static dimensions *)
  4196. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4197. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4198. ReleaseOperand(tmpOperand);
  4199. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4200. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4201. ReleaseOperand(tmpOperand);
  4202. INC(i);
  4203. END;
  4204. dimOp := IntermediateCode.Immediate(addressType,dim);
  4205. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4206. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4207. (* case 1d *)
  4208. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4209. Designate(expression,operand);
  4210. Dereference(operand,type.resolved,FALSE);
  4211. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4212. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4213. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4214. (* case 1f *)
  4215. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4216. Designate(expression,operand);
  4217. FOR i := 0 TO dim-1 DO
  4218. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4219. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4220. ReleaseOperand(tmpOperand);
  4221. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4222. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4223. ReleaseOperand(tmpOperand);
  4224. END;
  4225. (*******
  4226. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4227. *)
  4228. arrayFlags := {StaticFlag};
  4229. IF dim = 1 THEN
  4230. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4231. ReleaseOperand(tmpOperand);
  4232. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4233. m := LONGINT(tmpOperand.op.intValue);
  4234. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4235. ELSIF dim = 2 THEN
  4236. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4237. ReleaseOperand(tmpOperand);
  4238. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4239. m := LONGINT(tmpOperand.op.intValue);
  4240. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4241. ReleaseOperand(tmpOperand);
  4242. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4243. n := LONGINT(tmpOperand.op.intValue);
  4244. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4245. INCL(arrayFlags,SmallMatrixFlag);
  4246. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4247. END;
  4248. END;
  4249. (*******)
  4250. dimOp := IntermediateCode.Immediate(addressType,dim);
  4251. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4252. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4253. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4254. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4255. baseType := SemanticChecker.ArrayBase(type,dim);
  4256. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4257. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4258. ELSE HALT(100);
  4259. END;
  4260. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4261. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4262. (* case 2b *)
  4263. IF expression IS SyntaxTree.IndexDesignator THEN
  4264. descriptorType := GetMathArrayDescriptorType(dim);
  4265. variable := GetTemporaryVariable(descriptorType, FALSE);
  4266. Symbol(variable,variableOp);
  4267. arrayDestinationTag := variableOp.op;
  4268. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4269. arrayDestinationDimension := dim;
  4270. NeedDescriptor := TRUE;
  4271. Designate(expression,operand);
  4272. Pass((operand.tag));
  4273. NeedDescriptor := FALSE;
  4274. (* case 2a *)
  4275. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4276. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4277. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4278. INC(i);
  4279. END;
  4280. IF i = dim THEN
  4281. Designate(expression,operand);
  4282. Pass((operand.tag));
  4283. ELSE (* open-static *)
  4284. type := expression.type.resolved;
  4285. descriptorType := GetMathArrayDescriptorType(dim);
  4286. variable := GetTemporaryVariable(descriptorType, FALSE);
  4287. Symbol(variable,variableOp);
  4288. arrayDestinationTag := variableOp.op;
  4289. Designate(expression,operand);
  4290. FOR i := 0 TO dim-1 DO
  4291. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4292. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4293. ReleaseOperand(tmpOperand);
  4294. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4295. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4296. ReleaseOperand(tmpOperand);
  4297. END;
  4298. dimOp := IntermediateCode.Immediate(addressType,dim);
  4299. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4300. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4301. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4302. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4303. baseType := SemanticChecker.ArrayBase(type,dim);
  4304. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4305. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4306. Pass((arrayDestinationTag));
  4307. END;
  4308. (* case 2d *)
  4309. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4310. Designate(expression,operand);
  4311. Dereference(operand,type.resolved,FALSE);
  4312. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4313. Pass((operand.tag));
  4314. (* case 2f *)
  4315. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4316. descriptorType := GetMathArrayDescriptorType(dim);
  4317. variable := GetTemporaryVariable(descriptorType, FALSE);
  4318. Symbol(variable,variableOp);
  4319. arrayDestinationTag := variableOp.op;
  4320. Designate(expression,operand);
  4321. FOR i := 0 TO dim-1 DO
  4322. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4323. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4324. ReleaseOperand(tmpOperand);
  4325. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4326. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4327. ReleaseOperand(tmpOperand);
  4328. END;
  4329. (*
  4330. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4331. *)
  4332. arrayFlags := {StaticFlag};
  4333. IF dim = 1 THEN
  4334. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4335. ReleaseOperand(tmpOperand);
  4336. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4337. m := LONGINT(tmpOperand.op.intValue);
  4338. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4339. ELSIF dim = 2 THEN
  4340. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4341. ReleaseOperand(tmpOperand);
  4342. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4343. m := LONGINT(tmpOperand.op.intValue);
  4344. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4345. ReleaseOperand(tmpOperand);
  4346. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4347. n := LONGINT(tmpOperand.op.intValue);
  4348. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4349. INCL(arrayFlags,SmallMatrixFlag);
  4350. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4351. END;
  4352. END;
  4353. (*******)
  4354. dimOp := IntermediateCode.Immediate(addressType,dim);
  4355. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4356. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4357. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4358. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4359. baseType := SemanticChecker.ArrayBase(type,dim);
  4360. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4361. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4362. Pass((arrayDestinationTag));
  4363. ELSE HALT(100);
  4364. END;
  4365. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4366. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4367. (* case 3b *)
  4368. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4369. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4370. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4371. Symbol(variable,variableOp);
  4372. LoadValue(variableOp,system.addressType);
  4373. ELSE
  4374. descriptorType := GetMathArrayDescriptorType(dim);
  4375. variable := GetTemporaryVariable(descriptorType, FALSE);
  4376. Symbol(variable,variableOp);
  4377. END;
  4378. arrayDestinationTag := variableOp.op;
  4379. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4380. arrayDestinationDimension := 0;
  4381. Designate(expression,operand);
  4382. Pass((operand.tag));
  4383. (* case 3a *)
  4384. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4385. i := 0;
  4386. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4387. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4388. INC(i);
  4389. END;
  4390. IF i = dim THEN
  4391. Designate(expression,operand);
  4392. Pass((operand.tag));
  4393. ELSE (* open-static *)
  4394. type := expression.type.resolved;
  4395. descriptorType := GetMathArrayDescriptorType(dim);
  4396. variable := GetTemporaryVariable(descriptorType, FALSE);
  4397. Symbol(variable,variableOp);
  4398. arrayDestinationTag := variableOp.op;
  4399. Designate(expression,operand);
  4400. FOR i := 0 TO dim-1 DO
  4401. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4402. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4403. ReleaseOperand(tmpOperand);
  4404. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4405. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4406. ReleaseOperand(tmpOperand);
  4407. END;
  4408. dimOp := IntermediateCode.Immediate(addressType,dim);
  4409. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4410. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4411. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4412. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4413. baseType := SemanticChecker.ArrayBase(type,dim);
  4414. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4415. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4416. Pass((arrayDestinationTag));
  4417. END;
  4418. (* case 3d *)
  4419. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4420. Designate(expression,operand);
  4421. Dereference(operand,type.resolved,FALSE);
  4422. (*
  4423. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4424. *)
  4425. Pass((operand.tag));
  4426. (* case 3f *)
  4427. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4428. descriptorType := GetMathArrayDescriptorType(dim);
  4429. variable := GetTemporaryVariable(descriptorType, FALSE);
  4430. Symbol(variable,variableOp);
  4431. arrayDestinationTag := variableOp.op;
  4432. Designate(expression,operand);
  4433. IF operand.op.type.length >1 THEN (* vector register *)
  4434. variable2 := GetTemporaryVariable(type, FALSE);
  4435. Symbol(variable2, variable2Op);
  4436. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4437. Emit(Mov(position,tmp, operand.op));
  4438. ReleaseOperand(operand);
  4439. Symbol(variable2, operand);
  4440. END;
  4441. FOR i := 0 TO dim-1 DO
  4442. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4443. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4444. ReleaseOperand(tmpOperand);
  4445. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4446. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4447. ReleaseOperand(tmpOperand);
  4448. END;
  4449. dimOp := IntermediateCode.Immediate(addressType,dim);
  4450. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4451. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4452. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4453. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4454. baseType := SemanticChecker.ArrayBase(type,dim);
  4455. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4456. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4457. Pass((arrayDestinationTag));
  4458. ELSE HALT(100);
  4459. END;
  4460. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4461. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4462. (* case 4b *)
  4463. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4464. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4465. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4466. Symbol(variable,variableOp);
  4467. LoadValue(variableOp,system.addressType);
  4468. ELSE
  4469. descriptorType := GetMathArrayDescriptorType(dim);
  4470. variable := GetTemporaryVariable(descriptorType, FALSE);
  4471. Symbol(variable,variableOp);
  4472. END;
  4473. arrayDestinationTag := variableOp.op;
  4474. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4475. arrayDestinationDimension := 0;
  4476. Designate(expression,operand);
  4477. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4478. Symbol(variable,variableOp);
  4479. ELSE
  4480. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4481. Symbol(variable,variableOp);
  4482. MakeMemory(tmp,variableOp.op,addressType,0);
  4483. Emit(Mov(position,tmp,operand.tag));
  4484. ReleaseIntermediateOperand(tmp);
  4485. END;
  4486. Pass((variableOp.op));
  4487. ReleaseOperand(variableOp);
  4488. (* case 4a *)
  4489. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4490. i := 0;
  4491. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4492. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4493. INC(i);
  4494. END;
  4495. IF i = dim THEN
  4496. Designate(expression,operand);
  4497. arrayDestinationTag := operand.tag;
  4498. ELSE (* open-static *)
  4499. type := expression.type.resolved;
  4500. descriptorType := GetMathArrayDescriptorType(dim);
  4501. variable := GetTemporaryVariable(descriptorType, FALSE);
  4502. Symbol(variable,variableOp);
  4503. arrayDestinationTag := variableOp.op;
  4504. Designate(expression,operand);
  4505. FOR i := 0 TO dim-1 DO
  4506. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4507. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4508. ReleaseOperand(tmpOperand);
  4509. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4510. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4511. ReleaseOperand(tmpOperand);
  4512. END;
  4513. dimOp := IntermediateCode.Immediate(addressType,dim);
  4514. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4515. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4516. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4517. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4518. baseType := SemanticChecker.ArrayBase(type,dim);
  4519. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4520. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4521. END;
  4522. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4523. Symbol(variable,variableOp);
  4524. MakeMemory(tmp,variableOp.op,addressType,0);
  4525. Emit(Mov(position,tmp,arrayDestinationTag));
  4526. ReleaseIntermediateOperand(tmp);
  4527. Pass((variableOp.op));
  4528. ReleaseOperand(variableOp);
  4529. (* case 4d *)
  4530. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4531. Designate(expression,operand);
  4532. (*
  4533. Dereference(operand,type.resolved,FALSE);
  4534. *)
  4535. (*
  4536. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4537. *)
  4538. Pass((operand.op));
  4539. (* case 4f *)
  4540. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4541. descriptorType := GetMathArrayDescriptorType(dim);
  4542. variable := GetTemporaryVariable(descriptorType, FALSE);
  4543. Symbol(variable,variableOp);
  4544. arrayDestinationTag := variableOp.op;
  4545. Designate(expression,operand);
  4546. FOR i := 0 TO dim-1 DO
  4547. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4548. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4549. ReleaseOperand(tmpOperand);
  4550. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4551. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4552. ReleaseOperand(tmpOperand);
  4553. END;
  4554. dimOp := IntermediateCode.Immediate(addressType,dim);
  4555. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4556. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4557. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4558. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4559. baseType := SemanticChecker.ArrayBase(type,dim);
  4560. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4561. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4562. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4563. Symbol(variable,variableOp);
  4564. MakeMemory(tmp,variableOp.op,addressType,0);
  4565. Emit(Mov(position,tmp,arrayDestinationTag));
  4566. ReleaseIntermediateOperand(tmp);
  4567. Pass((variableOp.op));
  4568. ReleaseOperand(variableOp);
  4569. ELSE HALT(100);
  4570. END;
  4571. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4572. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4573. Designate(expression,operand);
  4574. IF operand.op.type.length > 1 THEN
  4575. Emit(Push(position, operand.op));
  4576. ReleaseOperand(operand);
  4577. ELSE
  4578. size := system.SizeOf(type);
  4579. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4580. size := ToMemoryUnits(system,size);
  4581. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4582. arrayDestinationTag := sp;
  4583. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4584. END;
  4585. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4586. Designate(expression,operand);
  4587. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4588. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4589. Symbol(variable,variableOp);
  4590. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4591. Emit(Mov(position,tmp,operand.op));
  4592. Emit(Push(position,variableOp.op));
  4593. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4594. Pass((operand.op));
  4595. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4596. END;
  4597. ELSE HALT(200)
  4598. END;
  4599. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4600. IF parameter.kind = SyntaxTree.VarParameter THEN
  4601. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4602. Designate(expression, operand);
  4603. Pass((operand.op));
  4604. ELSE
  4605. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4606. Evaluate(expression, operand);
  4607. Pass((operand.extra)); (* step *)
  4608. Pass((operand.tag)); (* last *)
  4609. Pass((operand.op)) (* first *)
  4610. END
  4611. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4612. IF parameter.kind = SyntaxTree.VarParameter THEN
  4613. Designate(expression, operand);
  4614. Pass((operand.op));
  4615. ELSE
  4616. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4617. Evaluate(expression, operand);
  4618. Pass((operand.tag)); (* real part *)
  4619. Pass((operand.op)) (* imaginary part *)
  4620. END
  4621. ELSE
  4622. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4623. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4624. Designate(expression,operand);
  4625. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4626. (* stack allocation *)
  4627. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4628. (*! parameter alignment to be discussed ... *)
  4629. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4630. size := type(SyntaxTree.StringType).length;
  4631. END;
  4632. IF backend.cooperative & parameter.NeedsTrace() THEN
  4633. dst := NewRegisterOperand (addressType);
  4634. Emit(Mov(position,dst, sp));
  4635. IntermediateCode.InitImmediate(null, byteType, 0);
  4636. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4637. ReleaseIntermediateOperand(dst);
  4638. SaveRegisters();ReleaseUsedRegisters(saved);
  4639. (* register dst has been freed before SaveRegisters already *)
  4640. CallAssignMethod(dst, operand.op, parameter.type);
  4641. RestoreRegisters(saved);
  4642. END;
  4643. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4644. ELSIF IsOpenArray(parameter.type) THEN
  4645. Designate(expression,operand);
  4646. baseReg := operand.tag;
  4647. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4648. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4649. END;
  4650. Pass((operand.op)); (* address of the array *)
  4651. ELSIF IsDelegate(parameter.type) THEN
  4652. Evaluate(expression,operand);
  4653. IF backend.cooperative & parameter.NeedsTrace() THEN
  4654. Emit(Push(position, nil));
  4655. dst := NewRegisterOperand (addressType);
  4656. Emit(Mov(position,dst, sp));
  4657. ReleaseIntermediateOperand(dst);
  4658. SaveRegisters();ReleaseUsedRegisters(saved);
  4659. Emit(Push(position, dst));
  4660. (* register dst has been freed before SaveRegisters already *)
  4661. Emit(Push(position, operand.tag));
  4662. CallThis(position,"GarbageCollector","Assign",2);
  4663. RestoreRegisters(saved);
  4664. ELSE
  4665. Pass((operand.tag));
  4666. END;
  4667. Pass((operand.op));
  4668. ELSE
  4669. Evaluate(expression,operand);
  4670. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4671. Emit(Push(position, nil));
  4672. dst := NewRegisterOperand (addressType);
  4673. Emit(Mov(position,dst, sp));
  4674. ReleaseIntermediateOperand(dst);
  4675. SaveRegisters();ReleaseUsedRegisters(saved);
  4676. Emit(Push(position, dst));
  4677. (* register dst has been freed before SaveRegisters already *)
  4678. Emit(Push(position, operand.op));
  4679. CallThis(position,"GarbageCollector","Assign",2);
  4680. RestoreRegisters(saved);
  4681. ELSE
  4682. Pass((operand.op));
  4683. END;
  4684. END;
  4685. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4686. Evaluate(expression,operand);
  4687. Pass((operand.op));
  4688. ELSE (* var parameter *)
  4689. Designate(expression,operand);
  4690. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4691. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4692. Pass((operand.tag));
  4693. END;
  4694. END;
  4695. Pass((operand.op));
  4696. END;
  4697. END;
  4698. (* TODO: needed? *)
  4699. arrayDestinationTag := oldArrayDestinationTag;
  4700. arrayDestinationDimension := oldArrayDestinationDimension;
  4701. IF needsParameterBackup THEN
  4702. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4703. ReuseCopy(parameterBackup, operand.op)
  4704. END;
  4705. ReleaseOperand(operand);
  4706. IF Trace THEN TraceExit("PushParameter") END;
  4707. END PushParameter;
  4708. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4709. VAR prevConditional: BOOLEAN;
  4710. BEGIN
  4711. prevConditional := conditional;
  4712. conditional := FALSE;
  4713. IF (x.result # NIL) & ( x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4714. Expression(x.result); (* use register *)
  4715. END;
  4716. Statement(x.statement);
  4717. conditional := prevConditional;
  4718. IF x.result # NIL THEN Expression(x.result) END;
  4719. IF (x.result # NIL) & (x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4720. ReleaseIntermediateOperand(result.op);
  4721. END;
  4722. END VisitStatementDesignator;
  4723. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4724. VAR
  4725. procedure: SyntaxTree.Procedure;
  4726. procedureType: SyntaxTree.ProcedureType;
  4727. wasInline: BOOLEAN;
  4728. actualParameters: SyntaxTree.ExpressionList;
  4729. formalParameter: SyntaxTree.Parameter;
  4730. actualParameter: SyntaxTree.Expression;
  4731. i: LONGINT;
  4732. localVariable: SyntaxTree.Variable;
  4733. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4734. src, dest: Operand;
  4735. prevInlineExit : Label;
  4736. prevMapper: SymbolMapper;
  4737. tooComplex: BOOLEAN;
  4738. resultDesignator: SyntaxTree.Expression;
  4739. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4740. BEGIN
  4741. IF e = NIL THEN RETURN TRUE
  4742. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4743. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4744. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4745. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4746. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4747. ELSE RETURN FALSE
  4748. END;
  4749. END SimpleExpression;
  4750. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4751. BEGIN
  4752. RETURN checker.CanPassInRegister(type)
  4753. END FitsInRegister;
  4754. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4755. VAR
  4756. variable: SyntaxTree.Variable;
  4757. variableDesignator: SyntaxTree.Designator;
  4758. BEGIN
  4759. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4760. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4761. variableDesignator.SetType(type);
  4762. RETURN variableDesignator
  4763. END GetTemp;
  4764. BEGIN
  4765. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4766. wasInline := currentIsInline;
  4767. prevInlineExit := currentInlineExit;
  4768. prevMapper := currentMapper;
  4769. currentInlineExit := NewLabel();
  4770. tooComplex := FALSE;
  4771. NEW(currentMapper);
  4772. currentIsInline := TRUE;
  4773. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4774. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4775. formalParameter := procedureType.firstParameter;
  4776. actualParameters := x.parameters;
  4777. i := 0;
  4778. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4779. actualParameter := actualParameters.GetExpression(i);
  4780. IF actualParameter.resolved # NIL THEN
  4781. actualParameter := actualParameter.resolved
  4782. END;
  4783. (*
  4784. if expression is simple and can be passed immediately
  4785. or if type fits in register then we can proceed
  4786. otherwise we escape to ordinary procedure call.
  4787. *)
  4788. (* cases where the expression can be mapped identically *)
  4789. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4790. currentMapper.Add(formalParameter, actualParameter, NIL);
  4791. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4792. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4793. (*
  4794. Assign(variableDesignator, actualParameter);
  4795. *)
  4796. Evaluate(actualParameter, src);
  4797. Designate(variableDesignator, dest);
  4798. Emit(Mov(x.position, dest.op, src.op));
  4799. ReleaseOperand(dest);
  4800. ReleaseOperand(src);
  4801. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4802. ELSE tooComplex := TRUE
  4803. END;
  4804. (*
  4805. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4806. currentMapper.Add(formalParameter, actualParameter, NIL);
  4807. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4808. variableDesignator := GetTemp(system.addressType, FALSE);
  4809. Designate(actualParameter, src);
  4810. Designate(variableDesignator, dest);
  4811. IntermediateCode.MakeMemory(dest.op,addressType);
  4812. Emit(Mov(x.position, dest.op, src.op));
  4813. ReleaseOperand(dest);
  4814. IF src.tag.mode # IntermediateCode.Undefined THEN
  4815. tagDesignator := GetTemp(system.addressType, FALSE);
  4816. Designate(tagDesignator, dest);
  4817. IntermediateCode.MakeMemory(dest.op,addressType);
  4818. Emit(Mov(x.position, dest.op, src.op));
  4819. END;
  4820. ReleaseOperand(dest); ReleaseOperand(src);
  4821. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4822. END;
  4823. *)
  4824. formalParameter := formalParameter.nextParameter;
  4825. INC(i);
  4826. END;
  4827. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4828. IF resultDesignator # NIL THEN
  4829. returnDesignator := resultDesignator
  4830. ELSE
  4831. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4832. END;
  4833. currentMapper.Add(NIL, returnDesignator, NIL);
  4834. END;
  4835. localVariable := procedure.procedureScope.firstVariable;
  4836. WHILE ~tooComplex & (localVariable # NIL) DO
  4837. variableDesignator := GetTemp(localVariable.type, FALSE);
  4838. currentMapper.Add(localVariable, variableDesignator, NIL);
  4839. localVariable := localVariable.nextVariable;
  4840. END;
  4841. IF ~tooComplex THEN
  4842. VisitStatementBlock(procedure.procedureScope.body);
  4843. SetLabel(currentInlineExit);
  4844. IF procedureType.returnType # NIL THEN
  4845. Designate(returnDesignator, result);
  4846. IF conditional THEN
  4847. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4848. ValueToCondition(result)
  4849. END;
  4850. END;
  4851. END;
  4852. currentMapper := prevMapper;
  4853. currentInlineExit := prevInlineExit;
  4854. currentIsInline := wasInline;
  4855. RETURN ~tooComplex
  4856. END InlineProcedureCall;
  4857. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4858. VAR
  4859. parameters: SyntaxTree.ExpressionList;
  4860. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4861. designator: SyntaxTree.Designator;
  4862. procedureType: SyntaxTree.ProcedureType;
  4863. formalParameter: SyntaxTree.Parameter;
  4864. operand, returnValue: Operand;
  4865. reg, size, mask, dest: IntermediateCode.Operand;
  4866. saved: RegisterEntry;
  4867. symbol: SyntaxTree.Symbol;
  4868. variable: SyntaxTree.Variable;
  4869. i, parametersSize, returnTypeSize : LONGINT;
  4870. structuredReturnType: BOOLEAN;
  4871. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4872. tempVariableDesignator: SyntaxTree.Designator;
  4873. gap, alignment: LONGINT; (*fld*)
  4874. (* TODO: remove unnecessary backup variables *)
  4875. oldResult: Operand;
  4876. oldCurrentScope: SyntaxTree.Scope;
  4877. oldArrayDestinationTag: IntermediateCode.Operand;
  4878. oldArrayDestinationDimension: LONGINT;
  4879. oldConstantDeclaration: SyntaxTree.Symbol;
  4880. oldDestination: IntermediateCode.Operand;
  4881. oldCurrentLoop: Label;
  4882. oldConditional: BOOLEAN;
  4883. oldTrueLabel, oldFalseLabel: Label;
  4884. oldLocked: BOOLEAN;
  4885. usedRegisters,oldUsedRegisters: RegisterEntry;
  4886. return: IntermediateCode.Operand;
  4887. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4888. arg: IntermediateCode.Operand;
  4889. dummy: IntermediateCode.Operand;
  4890. recordType: SyntaxTree.RecordType;
  4891. operatorSelectionProcedureOperand: Operand;
  4892. operatorSelectionProcedure: SyntaxTree.Procedure;
  4893. fingerPrint: SyntaxTree.FingerPrint;
  4894. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4895. identifierNumber: LONGINT;
  4896. parameterRegister: Backend.Registers;
  4897. parameterRegisters: LONGINT;
  4898. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4899. procedure: SyntaxTree.Procedure;
  4900. PROCEDURE BackupGlobalState;
  4901. BEGIN
  4902. oldResult := result;
  4903. oldCurrentScope := currentScope;
  4904. oldArrayDestinationTag := arrayDestinationTag;
  4905. oldArrayDestinationDimension := arrayDestinationDimension;
  4906. oldConstantDeclaration := constantDeclaration;
  4907. oldDestination := destination;
  4908. oldCurrentLoop := currentLoop;
  4909. oldConditional := conditional;
  4910. oldTrueLabel := trueLabel;
  4911. oldFalseLabel := falseLabel;
  4912. oldLocked := locked;
  4913. oldUsedRegisters := usedRegisters
  4914. END BackupGlobalState;
  4915. PROCEDURE RestoreGlobalState;
  4916. BEGIN
  4917. result := oldResult;
  4918. currentScope := oldCurrentScope;
  4919. arrayDestinationTag := oldArrayDestinationTag;
  4920. arrayDestinationDimension := oldArrayDestinationDimension;
  4921. constantDeclaration := oldConstantDeclaration;
  4922. destination := oldDestination;
  4923. currentLoop := oldCurrentLoop;
  4924. conditional := oldConditional;
  4925. trueLabel := oldTrueLabel;
  4926. falseLabel := oldFalseLabel;
  4927. locked := oldLocked;
  4928. usedRegisters := oldUsedRegisters
  4929. END RestoreGlobalState;
  4930. (** do preparations before parameter push for array-structured object types (ASOTs):
  4931. if ASOT is passed as VAR parameter:
  4932. - allocate temporary variable of math array type
  4933. - copy contents of ASOT to be passed to temporary variable
  4934. - use temporary variable as the actual parameter instead
  4935. - 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)
  4936. **)
  4937. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4938. VAR
  4939. expression: SyntaxTree.Expression;
  4940. BEGIN
  4941. IF actualParameter IS SyntaxTree.Designator THEN
  4942. designator := actualParameter(SyntaxTree.Designator);
  4943. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4944. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4945. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4946. ASSERT(checker # NIL);
  4947. checker.SetCurrentScope(currentScope);
  4948. (* allocate temporary variable *)
  4949. ASSERT(actualParameter.type # NIL);
  4950. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4951. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4952. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4953. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4954. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4955. ASSERT(tempVariableDesignator.type # NIL);
  4956. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4957. (* copy math array stored in actual parameter to temporary variable *)
  4958. BackupGlobalState;
  4959. AssignMathArray(tempVariableDesignator, actualParameter);
  4960. RestoreGlobalState;
  4961. (* use temporary variable as actual parameter instead of the original one *)
  4962. actualParameter := tempVariableDesignator;
  4963. (* create write-back call and store it in linked list *)
  4964. (* create new list entry *)
  4965. IF firstWriteBackCall = NIL THEN
  4966. NEW(firstWriteBackCall);
  4967. currentWriteBackCall := firstWriteBackCall
  4968. ELSE
  4969. ASSERT(currentWriteBackCall # NIL);
  4970. NEW(currentWriteBackCall.next);
  4971. currentWriteBackCall := currentWriteBackCall.next
  4972. END;
  4973. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4974. ASSERT(expression.type = NIL);
  4975. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4976. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4977. (* prepare writeback for any other "normal" indexer *)
  4978. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4979. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4980. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4981. Assign(tempVariableDesignator, actualParameter);
  4982. actualParameter := tempVariableDesignator;
  4983. IF firstWriteBackCall = NIL THEN
  4984. NEW(firstWriteBackCall);
  4985. currentWriteBackCall := firstWriteBackCall
  4986. ELSE
  4987. ASSERT(currentWriteBackCall # NIL);
  4988. NEW(currentWriteBackCall.next);
  4989. currentWriteBackCall := currentWriteBackCall.next
  4990. END;
  4991. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  4992. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4993. END
  4994. END
  4995. END PrepareParameter;
  4996. BEGIN
  4997. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4998. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4999. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5000. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5001. IF InlineProcedureCall(x) THEN
  5002. RETURN
  5003. ELSE
  5004. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5005. END
  5006. END;
  5007. END;
  5008. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5009. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5010. dest := destination; destination := emptyOperand;
  5011. SaveRegisters();ReleaseUsedRegisters(saved);
  5012. parameters := x.parameters;
  5013. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5014. (* an operator is called *)
  5015. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5016. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5017. (* check if a dynamic operator call should be performed *)
  5018. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5019. ELSE
  5020. isCallOfDynamicOperator := FALSE
  5021. END;
  5022. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5023. Emit(Push(position, ap));
  5024. END;
  5025. alignment := procedureType.stackAlignment;
  5026. IF alignment > 1 THEN
  5027. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5028. Emit(Mov(position,reg, sp));
  5029. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5030. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5031. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5032. Emit(And(position,sp, sp, mask));
  5033. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5034. Emit(Push(position,reg));
  5035. (*
  5036. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5037. Emit(Mov(position,mem,reg));
  5038. *)
  5039. ReleaseIntermediateOperand(reg);
  5040. END;
  5041. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5042. (* align stack to 16-byte boundary *)
  5043. IntermediateCode.InitImmediate(mask,addressType,-16);
  5044. Emit(And(position,sp, sp, mask));
  5045. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5046. IF gap # 0 THEN
  5047. IntermediateCode.InitImmediate(size,addressType,gap);
  5048. Emit(Sub(position,sp,sp,size))
  5049. END;
  5050. END;
  5051. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5052. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5053. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5054. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5055. ELSE
  5056. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5057. END;
  5058. Evaluate(x.left, operand);
  5059. IF symbol IS SyntaxTree.Procedure THEN
  5060. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5061. Emit(Push(position,operand.tag));
  5062. ELSIF (procedureType.isDelegate) THEN
  5063. Emit(Push(position,operand.tag));
  5064. END;
  5065. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5066. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5067. Emit(Push(position,operand.tag));
  5068. END;
  5069. ELSE HALT(200);
  5070. END;
  5071. ReleaseIntermediateOperand(operand.tag);
  5072. operand.tag := emptyOperand;
  5073. (* determine if a structured return type is needed *)
  5074. structuredReturnType := StructuredReturnType(procedureType);
  5075. IF structuredReturnType THEN
  5076. IF resultDesignator # NIL THEN
  5077. d := resultDesignator;
  5078. ELSE
  5079. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5080. variable.SetUntraced(procedureType.hasUntracedReturn);
  5081. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5082. d.SetType(variable.type);
  5083. END;
  5084. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5085. Designate(d,returnValue);
  5086. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5087. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5088. Emit(Push(position,size));
  5089. Emit(Push(position,returnValue.op));
  5090. ReleaseOperand(returnValue);
  5091. ELSE
  5092. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5093. END;
  5094. END;
  5095. firstWriteBackCall := NIL; (* reset write-back call list *)
  5096. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5097. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5098. IF parameterRegister = NIL THEN parameterRegisters := 0
  5099. ELSE parameterRegisters := LEN(parameterRegister)
  5100. END;
  5101. passByRegister := parameterRegisters > 0;
  5102. registerNumber := 0;
  5103. formalParameter := procedureType.lastParameter;
  5104. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5105. actualParameter := parameters.GetExpression(i);
  5106. PrepareParameter(actualParameter, formalParameter);
  5107. IF passByRegister & (i < parameterRegisters) THEN
  5108. IF ~PassInRegister(formalParameter) THEN
  5109. Error(actualParameter.position,"cannot be passed by register")
  5110. ELSE
  5111. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5112. END;
  5113. INC(registerNumber);
  5114. ELSE
  5115. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5116. END;
  5117. formalParameter := formalParameter.prevParameter;
  5118. END;
  5119. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5120. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5121. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5122. END;
  5123. ELSE
  5124. hasDynamicOperands := FALSE;
  5125. formalParameter := procedureType.firstParameter;
  5126. FOR i := 0 TO parameters.Length() - 1 DO
  5127. actualParameter := parameters.GetExpression(i);
  5128. IF formalParameter # NIL THEN (* TENTATIVE *)
  5129. PrepareParameter(actualParameter, formalParameter);
  5130. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5131. ASSERT(i < 2);
  5132. hasDynamicOperands := TRUE;
  5133. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5134. ELSE
  5135. IF passByRegister & (registerNumber > 0) THEN
  5136. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5137. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5138. END;
  5139. passByRegister := FALSE;
  5140. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5141. END;
  5142. formalParameter := formalParameter.nextParameter;
  5143. END;
  5144. END;
  5145. END;
  5146. IF symbol IS SyntaxTree.Procedure THEN
  5147. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5148. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5149. Emit(Push(position,reg));
  5150. ReleaseIntermediateOperand(reg);
  5151. END;
  5152. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5153. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5154. parametersSize := ParametersSize(system,procedureType,FALSE);
  5155. END;
  5156. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5157. (*
  5158. dynamic operator overloading:
  5159. first push parameters, regularly:
  5160. [self]
  5161. par1
  5162. par2
  5163. then push parameters for GetOperator
  5164. identifier
  5165. ptr1
  5166. tag
  5167. ptr2
  5168. tag
  5169. call GetOperatorRuntimeProc
  5170. call Operator
  5171. *)
  5172. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5173. (* push ID *)
  5174. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5175. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5176. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5177. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5178. formalParameter := procedureType.firstParameter;
  5179. FOR i := 0 TO parameters.Length() - 1 DO
  5180. IF formalParameter.access # SyntaxTree.Hidden THEN
  5181. ASSERT(i < 2);
  5182. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5183. (* push pointer *)
  5184. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5185. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5186. (* add dereference *)
  5187. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5188. (*! better: do refer to stack above than using parameter backups !!*)
  5189. ReleaseIntermediateOperand(parameterBackups[i]);
  5190. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5191. END;
  5192. Emit(Push(position,parameterBackups[i]));
  5193. ReleaseIntermediateOperand(parameterBackups[i]);
  5194. (* push typetag *)
  5195. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5196. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5197. arg := TypeDescriptorAdr(recordType);
  5198. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5199. Emit(Push(position,arg));
  5200. ELSE
  5201. (* push 'NonPointer' *)
  5202. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5203. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5204. (* push fingerprint *)
  5205. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5206. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5207. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5208. END
  5209. END;
  5210. formalParameter := formalParameter.nextParameter
  5211. END;
  5212. (* for unary operators: complete the information for the second parameter *)
  5213. IF procedureType.numberParameters < 2 THEN
  5214. (* push 'NonPointer' *)
  5215. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5216. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5217. (* push 'NoType' *)
  5218. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5219. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5220. END;
  5221. (* call operator selection procedure *)
  5222. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5223. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5224. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5225. ReleaseOperand(operatorSelectionProcedureOperand);
  5226. (* use the address that the operator selection procedure returned as the target address of the call *)
  5227. InitOperand(operand, ModeValue);
  5228. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5229. Emit(Result(position,operand.op))
  5230. END
  5231. END;
  5232. ReleaseParameterRegisters();
  5233. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5234. Emit(Call(position,operand.op,0));
  5235. ELSE
  5236. Emit(Call(position,operand.op,parametersSize));
  5237. END;
  5238. ReleaseOperand(operand);
  5239. IF procedureType.noReturn THEN
  5240. EmitTrap(position,NoReturnTrap);
  5241. END;
  5242. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5243. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5244. Emit(Result(position,return));
  5245. END;
  5246. (* === return parameter space === *)
  5247. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5248. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5249. IF parametersSize < 32 THEN
  5250. (* allocated space for all parameter registers *)
  5251. parametersSize := 32
  5252. END;
  5253. size := IntermediateCode.Immediate(addressType,parametersSize);
  5254. Emit(Add(position,sp,sp,size))
  5255. END;
  5256. IF SysvABI(procedureType.callingConvention) THEN
  5257. IF passByRegister THEN
  5258. IF parameters.Length() > parameterRegisters THEN
  5259. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5260. ELSE
  5261. parametersSize := 0
  5262. END;
  5263. ELSE
  5264. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5265. INC( parametersSize, (-parametersSize) MOD 16 )
  5266. END;
  5267. IF parametersSize > 0 THEN
  5268. size := IntermediateCode.Immediate(addressType,parametersSize);
  5269. Emit(Add(position,sp,sp,size))
  5270. END;
  5271. END;
  5272. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5273. IF structuredReturnType THEN
  5274. (* stack pointer rewinding done by callee
  5275. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5276. Emit(Add(position,sp,sp,size));
  5277. *)
  5278. RestoreRegisters(saved);
  5279. InitOperand(result,ModeReference);
  5280. Symbol(variable,result);
  5281. ELSE
  5282. RestoreRegisters(saved);
  5283. InitOperand(result,ModeValue);
  5284. result.op := return;
  5285. END;
  5286. END;
  5287. IF alignment > 1 THEN
  5288. Emit(Pop(position,sp));
  5289. END;
  5290. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5291. Emit(Pop(position, ap));
  5292. END;
  5293. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5294. ValueToCondition(result);
  5295. END;
  5296. destination := dest;
  5297. (* perform all write-back calls in the list *)
  5298. BackupGlobalState;
  5299. currentWriteBackCall := firstWriteBackCall;
  5300. WHILE currentWriteBackCall # NIL DO
  5301. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5302. currentWriteBackCall := currentWriteBackCall.next
  5303. END;
  5304. RestoreGlobalState;
  5305. (* TENATIVE *)
  5306. (*
  5307. IF isOperatorCall THEN
  5308. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5309. END;
  5310. *)
  5311. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5312. END VisitProcedureCallDesignator;
  5313. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5314. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5315. td: SyntaxTree.Symbol;
  5316. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5317. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5318. BEGIN
  5319. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5320. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5321. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5322. variable.SetType(system.anyType);
  5323. scope.AddVariable(variable);
  5324. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5325. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5326. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5327. typeDeclaration.SetScope(module.module.moduleScope);
  5328. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5329. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5330. END;
  5331. RETURN hiddenPointerType;
  5332. END GetHiddenPointerType;
  5333. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5334. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5335. BEGIN
  5336. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5337. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5338. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5339. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5340. scope.AddVariable(variable);
  5341. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5342. variable.SetType(system.anyType);
  5343. scope.AddVariable(variable);
  5344. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5345. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5346. typeDeclaration.SetDeclaredType(delegatePointerType);
  5347. typeDeclaration.SetScope(module.module.moduleScope);
  5348. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5349. delegatePointerType.SetState(SyntaxTree.Resolved);
  5350. END;
  5351. RETURN delegatePointerType
  5352. END GetDelegateType;
  5353. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5354. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5355. such as in VAR a: RECORD ... END;
  5356. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5357. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5358. *)
  5359. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5360. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5361. BEGIN (* no code emission *)
  5362. source := NIL;
  5363. x := x.resolved;
  5364. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5365. x := GetHiddenPointerType();
  5366. ELSIF IsDelegate(x) THEN
  5367. x := GetDelegateType();
  5368. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5369. ELSE HALT(200);
  5370. END;
  5371. td := x.typeDeclaration;
  5372. IF td = NIL THEN
  5373. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5374. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5375. ASSERT(td # NIL);
  5376. END;
  5377. IF newObjectFile THEN
  5378. GetCodeSectionNameForSymbol(td,name);
  5379. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5380. meta.CheckTypeDeclaration(x);
  5381. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5382. ELSE
  5383. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5384. END;
  5385. IF backend.cooperative OR meta.simple THEN
  5386. offset := 0;
  5387. ELSE
  5388. IF x IS SyntaxTree.CellType THEN
  5389. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5390. IF baseRecord = NIL THEN
  5391. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5392. ELSE
  5393. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5394. END;
  5395. ELSE
  5396. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5397. END;
  5398. END;
  5399. ELSE
  5400. offset := 0;
  5401. source := module.allSections.FindBySymbol(td); (*TODO*)
  5402. IF source = NIL THEN
  5403. null := 0;
  5404. GetCodeSectionNameForSymbol(td,name);
  5405. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5406. Basic.SuffixSegmentedName (name, module.module.name);
  5407. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5408. IntermediateCode.InitImmediate(op,addressType,0);
  5409. source(IntermediateCode.Section).Emit(Data(position,op));
  5410. source.SetReferenced(FALSE)
  5411. ELSE
  5412. name := source.name;
  5413. END;
  5414. END;
  5415. RETURN td
  5416. END GetBackendType;
  5417. BEGIN
  5418. (*td := t.typeDeclaration;*)
  5419. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5420. (*
  5421. IF t IS SyntaxTree.PointerType THEN
  5422. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5423. ELSE
  5424. source := GetBackendType(t);
  5425. END;
  5426. *)
  5427. IF newObjectFile THEN
  5428. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5429. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5430. IntermediateCode.SetOffset(res,offset);
  5431. ELSE
  5432. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5433. END;
  5434. (*
  5435. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5436. make memory should be used when tag is used, not earlier
  5437. *)
  5438. RETURN res
  5439. END TypeDescriptorAdr;
  5440. (*
  5441. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5442. VAR result: IntermediateCode.Operand;
  5443. BEGIN
  5444. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5445. operand.tag := TypeDescriptorAdr(operand.type);
  5446. IntermediateCode.MakeMemory(operand.tag,addressType);
  5447. UseIntermediateOperand(operand.tag);
  5448. END;
  5449. END MakeTypeTag;
  5450. *)
  5451. PROCEDURE ProfilerInit;
  5452. VAR reg: IntermediateCode.Operand;
  5453. BEGIN
  5454. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5455. Emit(Call(position,reg,0));
  5456. END ProfilerInit;
  5457. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5458. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5459. BEGIN
  5460. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5461. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5462. THEN
  5463. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5464. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5465. Emit(Push(position,reg));
  5466. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5467. Emit(Push(position,reg));
  5468. StaticCallOperand(result,procedure);
  5469. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5470. ReleaseOperand(result);
  5471. END;
  5472. END ProfilerEnterExit;
  5473. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5474. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5475. BEGIN
  5476. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5477. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5478. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5479. profileInit.Emit(Push(position,reg));
  5480. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5481. profileInit.Emit(Push(position,reg));
  5482. NEW(string, LEN(name)); COPY(name, string^);
  5483. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5484. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5485. sv.SetType(type);
  5486. Designate(sv,result);
  5487. profileInit.Emit(Push(position,result.tag));
  5488. profileInit.Emit(Push(position,result.op));
  5489. StaticCallOperand(result,procedure);
  5490. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5491. ReleaseOperand(result);
  5492. END;
  5493. END ProfilerAddProcedure;
  5494. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5495. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5496. BEGIN
  5497. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5498. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5499. profileInit.Emit(Push(position,reg));
  5500. profileInitPatchPosition := profileInit.pc;
  5501. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5502. NEW(string, LEN(name)); COPY(name, string^);
  5503. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5504. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5505. sv.SetType(type);
  5506. Designate(sv,result);
  5507. profileInit.Emit(Push(position,result.tag));
  5508. profileInit.Emit(Push(position,result.op));
  5509. StaticCallOperand(result,procedure);
  5510. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5511. ReleaseOperand(result);
  5512. END;
  5513. END ProfilerAddModule;
  5514. PROCEDURE ProfilerPatchInit;
  5515. VAR reg: IntermediateCode.Operand;
  5516. BEGIN
  5517. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5518. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5519. EmitLeave(profileInit,position,NIL,0);
  5520. profileInit.Emit(Exit(position,0,0,0));
  5521. END ProfilerPatchInit;
  5522. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5523. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5524. VAR
  5525. id: LONGINT;
  5526. leftType, rightType: SyntaxTree.Type;
  5527. procedureType: SyntaxTree.ProcedureType;
  5528. runtimeProcedure: SyntaxTree.Procedure;
  5529. runtimeProcedureOperand, operatorOperand: Operand;
  5530. kind: SET;
  5531. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5532. VAR
  5533. arg: IntermediateCode.Operand;
  5534. recordType: SyntaxTree.RecordType;
  5535. fingerPrint: SyntaxTree.FingerPrint;
  5536. BEGIN
  5537. IF type = NIL THEN
  5538. (* no type: push 'NoType' *)
  5539. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5540. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5541. ELSIF IsStrictlyPointerToRecord(type) THEN
  5542. (* pointer to record type: push typetag *)
  5543. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5544. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5545. arg := TypeDescriptorAdr(recordType);
  5546. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5547. ELSE
  5548. (* non-pointer to record type: push fingerprint *)
  5549. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5550. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5551. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5552. END;
  5553. operatorInitializationCodeSection.Emit(Push(position,arg))
  5554. END PushTypeInfo;
  5555. BEGIN
  5556. ASSERT(operatorInitializationCodeSection # NIL);
  5557. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5558. procedureType := operator.type(SyntaxTree.ProcedureType);
  5559. (* determine types *)
  5560. leftType := procedureType.firstParameter.type;
  5561. IF procedureType.numberParameters = 2 THEN
  5562. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5563. rightType := procedureType.firstParameter.nextParameter.type;
  5564. ELSE
  5565. rightType := NIL
  5566. END;
  5567. (* determine operator kind *)
  5568. IF IsStrictlyPointerToRecord(leftType) THEN
  5569. kind := {LhsIsPointer}
  5570. ELSE
  5571. kind := {}
  5572. END;
  5573. IF IsStrictlyPointerToRecord(rightType) THEN
  5574. kind := kind + {RhsIsPointer}
  5575. END;
  5576. IF kind # {} THEN (* TODO: to be removed later on *)
  5577. (* at least one of the types is a pointer to record *)
  5578. (* emit a code that registers this specific operator in the runtime *)
  5579. dump := operatorInitializationCodeSection.comments;
  5580. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5581. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5582. (* push ID *)
  5583. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5584. id := Global.GetSymbol(module.module.case, operator.name);
  5585. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5586. (* push kind *)
  5587. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5588. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5589. (* push type infos *)
  5590. PushTypeInfo(leftType);
  5591. PushTypeInfo(rightType);
  5592. (* push operator address *)
  5593. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5594. StaticCallOperand(operatorOperand, operator);
  5595. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5596. ReleaseOperand(operatorOperand);
  5597. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5598. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5599. ReleaseOperand(runtimeProcedureOperand)
  5600. END
  5601. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5602. END
  5603. END RegisterDynamicOperator;
  5604. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5605. VAR
  5606. traceModule: SyntaxTree.Module;
  5607. procedure: SyntaxTree.Procedure;
  5608. procedureVariable: SyntaxTree.Variable;
  5609. s,msg: Basic.MessageString;
  5610. res: Operand;
  5611. i: LONGINT;
  5612. sv: SyntaxTree.StringValue;
  5613. type: SyntaxTree.Type;
  5614. recordType: SyntaxTree.RecordType;
  5615. printout: Printout.Printer;
  5616. stringWriter: Streams.StringWriter;
  5617. expression: SyntaxTree.Expression;
  5618. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5619. BEGIN
  5620. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5621. IF procedure = NIL THEN
  5622. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5623. END;
  5624. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5625. s := "procedure ";
  5626. Strings.Append(s,backend.traceModuleName);
  5627. Strings.Append(s,".");
  5628. Strings.Append(s,procedureName);
  5629. Strings.Append(s," not present");
  5630. Error(position,s);
  5631. RETURN FALSE
  5632. ELSE
  5633. RETURN TRUE
  5634. END;
  5635. END GetProcedure;
  5636. PROCEDURE CallProcedure;
  5637. VAR size: LONGINT;
  5638. BEGIN
  5639. IF procedure # NIL THEN
  5640. StaticCallOperand(result,procedure);
  5641. size := ProcedureParametersSize(system,procedure);
  5642. ELSE
  5643. Symbol(procedureVariable, result);
  5644. LoadValue(result, procedureVariable.type.resolved);
  5645. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5646. END;
  5647. Emit(Call(position,result.op,size));
  5648. END CallProcedure;
  5649. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5650. VAR res: Operand; string: SyntaxTree.String;
  5651. BEGIN
  5652. IF GetProcedure("String") THEN
  5653. NEW(string, LEN(s)); COPY(s, string^);
  5654. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5655. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5656. sv.SetType(type);
  5657. Designate(sv,res);
  5658. Emit(Push(position,res.tag));
  5659. Emit(Push(position,res.op));
  5660. ReleaseOperand(res);
  5661. CallProcedure;
  5662. END;
  5663. END String;
  5664. PROCEDURE Integer(op: IntermediateCode.Operand);
  5665. BEGIN
  5666. IF GetProcedure("Int") THEN
  5667. Emit(Push(position,op));
  5668. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5669. CallProcedure;
  5670. END;
  5671. END Integer;
  5672. PROCEDURE Float(op: IntermediateCode.Operand);
  5673. BEGIN
  5674. IF GetProcedure("HIntHex") THEN
  5675. Emit(Push(position,op));
  5676. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5677. CallProcedure;
  5678. END;
  5679. END Float;
  5680. PROCEDURE Set(op: IntermediateCode.Operand);
  5681. BEGIN
  5682. IF GetProcedure("Set") THEN
  5683. Emit(Push(position,op));
  5684. (*
  5685. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5686. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5687. *)
  5688. CallProcedure;
  5689. END;
  5690. END Set;
  5691. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5692. BEGIN
  5693. IF GetProcedure("Boolean") THEN
  5694. Emit(Push(position,op));
  5695. CallProcedure;
  5696. END;
  5697. END Boolean;
  5698. PROCEDURE Char(op: IntermediateCode.Operand);
  5699. BEGIN
  5700. IF GetProcedure("Char") THEN
  5701. Emit(Push(position,op));
  5702. CallProcedure;
  5703. END;
  5704. END Char;
  5705. PROCEDURE Address(op: IntermediateCode.Operand);
  5706. BEGIN
  5707. IF GetProcedure("Address") THEN
  5708. Emit(Push(position,op));
  5709. CallProcedure;
  5710. END;
  5711. END Address;
  5712. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5713. VAR len: IntermediateCode.Operand;
  5714. BEGIN
  5715. IF GetProcedure("String") THEN
  5716. len := GetArrayLength(type, op.tag);
  5717. Emit(Push(position,len));
  5718. ReleaseIntermediateOperand(len);
  5719. Emit(Push(position,op.op));
  5720. CallProcedure;
  5721. END;
  5722. END StringOperand;
  5723. PROCEDURE Ln;
  5724. BEGIN
  5725. IF GetProcedure("Ln") THEN
  5726. CallProcedure;
  5727. END;
  5728. END Ln;
  5729. BEGIN
  5730. IF backend.traceModuleName = "" THEN RETURN END;
  5731. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5732. IF GetProcedure("Enter") THEN
  5733. CallProcedure
  5734. END;
  5735. NEW(stringWriter,LEN(s));
  5736. FOR i := 0 TO x.Length()-1 DO
  5737. msg := "";
  5738. expression := x.GetExpression(i);
  5739. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5740. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5741. ELSE
  5742. Global.GetModuleName(module.module, s);
  5743. END;
  5744. IF i = 0 THEN
  5745. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5746. stringWriter.String(":");
  5747. END;
  5748. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5749. IF ~(expression IS SyntaxTree.StringValue) THEN
  5750. printout.Expression(expression);
  5751. stringWriter.Get(s);
  5752. Strings.Append(msg,s);
  5753. Strings.Append(msg,"= ");
  5754. ELSE stringWriter.Get(s); (* remove from string writer *)
  5755. Strings.Append(msg, s);
  5756. END;
  5757. String(msg);
  5758. IF SemanticChecker.IsStringType(expression.type) THEN
  5759. Designate(expression,res);
  5760. StringOperand(res, expression.type);
  5761. ELSE
  5762. Evaluate(expression,res);
  5763. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5764. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5765. Convert(res.op,int64);
  5766. END;
  5767. Integer(res.op);
  5768. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5769. Boolean(res.op);
  5770. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5771. Set(res.op);
  5772. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5773. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5774. Convert(res.op,float64);
  5775. END;
  5776. Float(res.op);
  5777. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5778. Char(res.op);
  5779. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5780. Address(res.op);String("H");
  5781. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5782. Address(res.op);String("H");
  5783. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5784. Address(res.op);String("H");
  5785. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5786. Address(res.op);String("H");
  5787. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5788. String("NIL");
  5789. ELSE HALT(200);
  5790. END;
  5791. END;
  5792. ReleaseOperand(res);
  5793. String("; ");
  5794. END;
  5795. IF GetProcedure("Exit") THEN
  5796. CallProcedure
  5797. ELSE
  5798. Ln;
  5799. END;
  5800. END;
  5801. END SystemTrace;
  5802. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5803. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5804. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5805. BEGIN
  5806. type := type.resolved;
  5807. IF type IS SyntaxTree.RecordType THEN
  5808. WITH type: SyntaxTree.RecordType DO
  5809. baseType := type.baseType;
  5810. IF baseType # NIL THEN
  5811. baseType := baseType.resolved;
  5812. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5813. InitFields(baseType,adr,offset);
  5814. END;
  5815. variable := type.recordScope.firstVariable;
  5816. WHILE variable # NIL DO
  5817. IF variable.initializer # NIL THEN
  5818. Evaluate(variable.initializer,initializerOp);
  5819. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5820. Emit(Mov(position,mem,initializerOp.op));
  5821. ReleaseOperand(initializerOp);
  5822. ReleaseIntermediateOperand(mem);
  5823. END;
  5824. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5825. variable := variable.nextVariable
  5826. END;
  5827. END;
  5828. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5829. WITH type: SyntaxTree.ArrayType DO
  5830. IF type.form = SyntaxTree.Static THEN
  5831. baseType := type.arrayBase;
  5832. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5833. FOR i := 0 TO type.staticLength-1 DO
  5834. InitFields(baseType,adr,offset+i*size);
  5835. END;
  5836. END;
  5837. END;
  5838. ELSIF type IS SyntaxTree.MathArrayType THEN
  5839. WITH type: SyntaxTree.MathArrayType DO
  5840. IF type.form = SyntaxTree.Open THEN
  5841. dim := DynamicDim(type);
  5842. imm := IntermediateCode.Immediate(addressType,dim);
  5843. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5844. baseType := SemanticChecker.ArrayBase(type,dim);
  5845. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5846. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5847. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5848. ReleaseIntermediateOperand(imm);
  5849. (* flags remain empty (=0) for open array *)
  5850. ELSIF type.form = SyntaxTree.Static THEN
  5851. baseType := type.arrayBase;
  5852. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5853. ASSERT(type.staticLength < 1024*1024*1024);
  5854. FOR i := 0 TO type.staticLength-1 DO
  5855. InitFields(baseType,adr,offset+i*size);
  5856. END;
  5857. END;
  5858. END;
  5859. END;
  5860. END InitFields;
  5861. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5862. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5863. BEGIN
  5864. type := variable.type.resolved;
  5865. IF (type IS SyntaxTree.MathArrayType) THEN
  5866. WITH type: SyntaxTree.MathArrayType DO
  5867. IF type.form = SyntaxTree.Open THEN
  5868. Symbol(variable,operand);
  5869. InitFields(type, operand.tag,0);
  5870. ELSIF type.form = SyntaxTree.Tensor THEN
  5871. Symbol(variable, operand);
  5872. MakeMemory(tmp,operand.op,addressType,0);
  5873. ReleaseOperand(operand);
  5874. Emit(Mov(position,tmp, nil ) );
  5875. ReleaseIntermediateOperand(tmp);
  5876. END;
  5877. END;
  5878. ELSE
  5879. Symbol(variable,operand);
  5880. IF variable.initializer # NIL THEN
  5881. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5882. reference.SetType(variable.type.resolved);
  5883. reference.SetAssignable(TRUE);
  5884. Assign(reference,variable.initializer);
  5885. END;
  5886. InitFields(type, operand.op,0);
  5887. ReleaseOperand(operand);
  5888. END;
  5889. END InitVariable;
  5890. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5891. VAR end: Label;
  5892. BEGIN
  5893. IF type.form = SyntaxTree.Tensor THEN
  5894. InitOperand(result,ModeValue);
  5895. ReuseCopy(result.op,base);
  5896. end := NewLabel();
  5897. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5898. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5899. SetLabel(end);
  5900. Convert(result.op,int32);
  5901. ELSE
  5902. InitOperand(result,ModeValue);
  5903. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5904. END
  5905. END MathArrayDim;
  5906. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5907. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5908. BEGIN
  5909. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5910. MakeMemory(mem,base,addressType,offset);
  5911. Emit(Mov(position,mem,value));
  5912. ReleaseIntermediateOperand(mem);
  5913. END PutMathArrayField;
  5914. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5915. VAR mem: IntermediateCode.Operand;
  5916. BEGIN
  5917. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5918. MakeMemory(mem,base,addressType,offset);
  5919. Emit(Mov(position,mem,value));
  5920. ReleaseIntermediateOperand(mem);
  5921. END PutMathArrayFieldOffset;
  5922. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5923. BEGIN
  5924. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5925. MakeMemory(value,base,addressType,offset);
  5926. END GetMathArrayField;
  5927. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5928. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5929. BEGIN
  5930. IF incr THEN
  5931. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5932. ELSE
  5933. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5934. END;
  5935. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5936. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5937. ELSE
  5938. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5939. Emit(Mov(position,reg,dim));
  5940. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5941. Emit(Add(position,reg,reg,base));
  5942. MakeMemory(mem, reg, addressType, offset);
  5943. ReleaseIntermediateOperand(reg);
  5944. Emit(Mov(position,mem,value));
  5945. ReleaseIntermediateOperand(mem);
  5946. END;
  5947. END PutMathArrayLenOrIncr;
  5948. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5949. BEGIN
  5950. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5951. END PutMathArrayLength;
  5952. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5953. BEGIN
  5954. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5955. END PutMathArrayIncrement;
  5956. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5957. BEGIN
  5958. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5959. END GetMathArrayIncrement;
  5960. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5961. BEGIN
  5962. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5963. END GetMathArrayLength;
  5964. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5965. VAR dimOp: IntermediateCode.Operand;
  5966. BEGIN
  5967. dimOp := IntermediateCode.Immediate(int32, dim);
  5968. GetMathArrayLength(type, operand, dimOp, check, result);
  5969. END GetMathArrayLengthAt;
  5970. PROCEDURE GetMathArrayIncrementAt(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. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5975. END GetMathArrayIncrementAt;
  5976. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5977. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5978. offset: LONGINT;
  5979. BEGIN
  5980. IF increment THEN
  5981. offset := MathIncrOffset;
  5982. ELSE
  5983. offset := MathLenOffset;
  5984. END;
  5985. INC(offset,operand.dimOffset*2);
  5986. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5987. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5988. END;
  5989. (* static dimension *)
  5990. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5991. IF check & (type.form = SyntaxTree.Tensor) THEN
  5992. DimensionCheck(operand.tag,dim,BrltL);
  5993. END;
  5994. val := SHORT(dim.intValue);
  5995. IF type.form # SyntaxTree.Tensor THEN
  5996. t := SemanticChecker.ArrayBase(type,val);
  5997. type := t.resolved(SyntaxTree.MathArrayType);
  5998. IF type.form = SyntaxTree.Static THEN
  5999. IF increment THEN
  6000. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6001. ELSE
  6002. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6003. END;
  6004. InitOperand(result,ModeValue);
  6005. result.op := res;
  6006. RETURN;
  6007. END;
  6008. END;
  6009. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6010. MakeMemory(res,operand.tag,addressType,offset);
  6011. (*
  6012. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6013. *)
  6014. InitOperand(result,ModeValue);
  6015. result.op := res;
  6016. ELSE
  6017. Convert(dim,addressType);
  6018. IF check THEN
  6019. IF type.form = SyntaxTree.Tensor THEN
  6020. DimensionCheck(operand.tag,dim,BrltL);
  6021. ELSIF isUnchecked THEN (* do nothing *)
  6022. ELSE
  6023. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6024. END;
  6025. END;
  6026. end := NewLabel(); next := NIL;
  6027. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6028. Emit(Mov(position,res,dim));
  6029. Convert(res,int32);
  6030. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6031. WHILE t IS SyntaxTree.MathArrayType DO
  6032. type := t(SyntaxTree.MathArrayType);
  6033. IF type.form = SyntaxTree.Static THEN
  6034. imm := IntermediateCode.Immediate(int32,val);
  6035. next := NewLabel();
  6036. BrneL(next,imm,res);
  6037. IF increment THEN
  6038. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6039. ELSE
  6040. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6041. END;
  6042. Emit(MovReplace(position,res,imm));
  6043. BrL(end);
  6044. ELSE hasDynamicPart := TRUE;
  6045. END;
  6046. t := type.arrayBase.resolved;
  6047. val := val + 1;
  6048. IF next # NIL THEN SetLabel(next) END;
  6049. END;
  6050. IF hasDynamicPart THEN
  6051. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6052. Emit(Mov(position,res2,dim));
  6053. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6054. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6055. Emit(Add(position,res2,res2,imm));
  6056. Emit(Add(position,res2,res2,operand.tag));
  6057. IntermediateCode.MakeMemory(res2,int32);
  6058. Emit(MovReplace(position,res,res2));
  6059. ReleaseIntermediateOperand(res2);
  6060. END;
  6061. SetLabel(end);
  6062. Convert(res,int32);
  6063. InitOperand(result,ModeValue);
  6064. result.op := res;
  6065. END;
  6066. END MathArrayLenOrIncr;
  6067. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6068. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6069. offset: LONGINT;
  6070. BEGIN
  6071. offset := operand.dimOffset+DynamicDim(type)-1;
  6072. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6073. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6074. val := SHORT(dim.intValue);
  6075. t := SemanticChecker.ArrayBase(type,val);
  6076. type := t.resolved(SyntaxTree.ArrayType);
  6077. IF type.form = SyntaxTree.Static THEN
  6078. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6079. ELSE
  6080. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6081. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6082. END;
  6083. UseIntermediateOperand(res);
  6084. InitOperand(result,ModeValue);
  6085. result.op := res;
  6086. ELSE
  6087. Convert(dim,addressType);
  6088. IF ~isUnchecked THEN
  6089. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6090. END;
  6091. end := NewLabel(); next := NIL;
  6092. (* ReuseCopy(dim,res); *)
  6093. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6094. Emit(Mov(position,res,dim));
  6095. Convert(res,int32);
  6096. Convert(res,int32);
  6097. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6098. WHILE t IS SyntaxTree.ArrayType DO
  6099. type := t(SyntaxTree.ArrayType);
  6100. IF type.form = SyntaxTree.Static THEN
  6101. imm := IntermediateCode.Immediate(int32,val);
  6102. next := NewLabel();
  6103. BrneL(next,imm,res);
  6104. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6105. Emit(MovReplace(position,res,imm));
  6106. BrL(end);
  6107. ELSE hasDynamicPart := TRUE;
  6108. END;
  6109. t := type.arrayBase.resolved;
  6110. val := val + 1;
  6111. IF next # NIL THEN SetLabel(next) END;
  6112. END;
  6113. IF hasDynamicPart THEN
  6114. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6115. Convert(res2,addressType);
  6116. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6117. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6118. Emit(Sub(position,res2,imm,res2));
  6119. Emit(Add(position,res2,res2,operand.tag));
  6120. IntermediateCode.MakeMemory(res2,int32);
  6121. Emit(MovReplace(position,res,res2));
  6122. ReleaseIntermediateOperand(res2);
  6123. END;
  6124. SetLabel(end);
  6125. Convert(res,int32);
  6126. InitOperand(result,ModeValue);
  6127. result.op := res;
  6128. END;
  6129. END ArrayLen;
  6130. (**
  6131. create a temporary variable in current scope
  6132. **)
  6133. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6134. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6135. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6136. BEGIN
  6137. IF ~register THEN
  6138. variable := temporaries.GetFreeVariable(type, index);
  6139. ELSE
  6140. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6141. END;
  6142. scope := currentScope;
  6143. IF variable = NIL THEN
  6144. COPY("@hiddenIRVar",string);
  6145. Basic.AppendNumber(string,index);
  6146. name := SyntaxTree.NewIdentifier(string);
  6147. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6148. variable.SetType(type);
  6149. variable.SetAccess(SyntaxTree.Hidden);
  6150. IF ~register THEN
  6151. temporaries.AddVariable(variable);
  6152. IF scope.lastVariable # NIL THEN
  6153. offset := scope.lastVariable.offsetInBits;
  6154. ELSE
  6155. offset := 0;
  6156. END;
  6157. DEC(offset,system.SizeOf(variable.type));
  6158. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6159. variable(SyntaxTree.Variable).SetOffset(offset);
  6160. scope.AddVariable(variable(SyntaxTree.Variable));
  6161. scope.EnterSymbol(variable, duplicate);
  6162. ASSERT(~duplicate);
  6163. InitVariable(variable(SyntaxTree.Variable));
  6164. ELSE
  6165. variable.SetUseRegister(TRUE);
  6166. variable(SyntaxTree.Variable).SetOffset(0);
  6167. END;
  6168. ELSE
  6169. InitVariable(variable(SyntaxTree.Variable));
  6170. (*
  6171. ASSERT(variable.type.resolved = type.resolved)
  6172. *)
  6173. END;
  6174. RETURN variable(SyntaxTree.Variable)
  6175. END GetTemporaryVariable;
  6176. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6177. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6178. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6179. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6180. i: LONGINT; duplicate: BOOLEAN;
  6181. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6182. VAR variable: SyntaxTree.Variable;
  6183. BEGIN
  6184. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6185. variable.SetType(type);
  6186. recordScope.AddVariable(variable);
  6187. END AddVariable;
  6188. BEGIN
  6189. name := "@ArrayDescriptor";
  6190. Basic.AppendNumber(name,dimensions);
  6191. identifier := SyntaxTree.NewIdentifier(name);
  6192. parentScope := module.module.moduleScope;
  6193. symbol := parentScope.FindSymbol(identifier);
  6194. IF symbol # NIL THEN
  6195. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6196. type := typeDeclaration.declaredType;
  6197. ELSE
  6198. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6199. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6200. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6201. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6202. recordType.SetTypeDeclaration(typeDeclaration);
  6203. recordType.SetState(SyntaxTree.Resolved);
  6204. typeDeclaration.SetDeclaredType(recordType);
  6205. AddVariable("@ptr",system.anyType);
  6206. AddVariable("@adr",system.addressType);
  6207. AddVariable("@flags",system.addressType);
  6208. AddVariable("@dim",system.addressType);
  6209. AddVariable("@elementSize",system.addressType);
  6210. FOR i := 0 TO dimensions-1 DO
  6211. name := "@len";
  6212. Basic.AppendNumber(name,i);
  6213. AddVariable(name,system.addressType);
  6214. name := "@incr";
  6215. Basic.AppendNumber(name,i);
  6216. AddVariable(name,system.addressType);
  6217. END;
  6218. parentScope.AddTypeDeclaration(typeDeclaration);
  6219. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6220. ASSERT(~duplicate);
  6221. type := recordType;
  6222. END;
  6223. RETURN type
  6224. END GetMathArrayDescriptorType;
  6225. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6226. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6227. BEGIN
  6228. type := GetMathArrayDescriptorType(dimensions);
  6229. Emit(Push(position,op.op));
  6230. (* push type descriptor *)
  6231. reg := TypeDescriptorAdr(type);
  6232. IF ~newObjectFile THEN
  6233. IntermediateCode.MakeMemory(reg,addressType);
  6234. END;
  6235. Emit(Push(position,reg));
  6236. ReleaseIntermediateOperand(reg);
  6237. (* push realtime flag: false by default *)
  6238. Emit(Push(position,false));
  6239. CallThis(position,"Heaps","NewRec",3);
  6240. END NewMathArrayDescriptor;
  6241. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6242. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6243. BEGIN
  6244. NEW(string, LEN(s)); COPY(s, string^);
  6245. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6246. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6247. sv.SetType(type);
  6248. Designate(sv,res);
  6249. Emit(Push(position,res.tag));
  6250. Emit(Push(position,res.op));
  6251. ReleaseOperand(res);
  6252. END PushConstString;
  6253. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6254. BEGIN
  6255. IF b THEN
  6256. Emit(Push(Basic.invalidPosition, true));
  6257. ELSE
  6258. Emit(Push(Basic.invalidPosition, false));
  6259. END;
  6260. END PushConstBoolean;
  6261. PROCEDURE PushConstSet(v: SET);
  6262. VAR value: SyntaxTree.Value; op: Operand;
  6263. BEGIN
  6264. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6265. value.SetType(system.setType);
  6266. Evaluate(value, op);
  6267. Emit(Push(Basic.invalidPosition, op.op));
  6268. ReleaseOperand(op);
  6269. END PushConstSet;
  6270. PROCEDURE PushConstInteger(v: LONGINT);
  6271. VAR value: SyntaxTree.Value; op: Operand;
  6272. BEGIN
  6273. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6274. value.SetType(system.longintType);
  6275. Evaluate(value, op);
  6276. Emit(Push(Basic.invalidPosition, op.op));
  6277. ReleaseOperand(op);
  6278. END PushConstInteger;
  6279. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6280. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6281. section: IntermediateCode.Section;
  6282. BEGIN
  6283. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6284. Global.GetSymbolSegmentedName(symbol, name);
  6285. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6286. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6287. procedure.SetScope(moduleScope);
  6288. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6289. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6290. procedure.SetAccess(SyntaxTree.Hidden);
  6291. currentScope := procedureScope;
  6292. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6293. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6294. RETURN section;
  6295. END OpenInitializer;
  6296. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6297. BEGIN
  6298. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6299. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6300. section := prev;
  6301. END CloseInitializer;
  6302. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6303. VAR name: SyntaxTree.IdentifierString;
  6304. variable: SyntaxTree.Variable;
  6305. parameter: SyntaxTree.Parameter;
  6306. type: SyntaxTree.Type;
  6307. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6308. BEGIN
  6309. InitOperand(op,ModeReference);
  6310. op.op := fp;
  6311. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6312. Dereference(op, x, FALSE);
  6313. result := op;
  6314. Symbol(symbol, op);
  6315. END Field;
  6316. PROCEDURE Direction(direction: LONGINT): SET;
  6317. BEGIN
  6318. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6319. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6320. ELSE HALT(100);
  6321. END;
  6322. END Direction;
  6323. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6324. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6325. BEGIN
  6326. Field(port, op);
  6327. ToMemory(op.op,addressType,0);
  6328. Emit(Push(Basic.invalidPosition, op.op));
  6329. ReleaseOperand(op);
  6330. Basic.GetString(modifier.identifier, name);
  6331. PushConstString(name);
  6332. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6333. ASSERT(SemanticChecker.IsStringType(value.type));
  6334. Designate(value, op);
  6335. Emit(Push(modifier.position, op.tag));
  6336. Emit(Push(modifier.position, op.op));
  6337. ReleaseOperand(op);
  6338. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6339. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6340. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6341. Evaluate(value, op);
  6342. Emit(Push(modifier.position, op.op));
  6343. ReleaseOperand(op);
  6344. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6345. ELSE
  6346. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6347. END;
  6348. END AddPortProperty;
  6349. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6350. VAR modifier: SyntaxTree.Modifier;
  6351. BEGIN
  6352. modifier := parameter.modifiers;
  6353. WHILE modifier # NIL DO
  6354. AddPortProperty(parameter,modifier, modifier.expression);
  6355. modifier := modifier.nextModifier;
  6356. END;
  6357. END AddPortProperties;
  6358. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6359. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6360. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6361. PROCEDURE PushLens(type: SyntaxTree.Type);
  6362. BEGIN
  6363. IF IsSemiDynamicArray(type) THEN
  6364. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6365. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6366. Emit(Push(Basic.invalidPosition, op.op));
  6367. ReleaseOperand(op);
  6368. INC(dim);
  6369. ELSIF IsStaticArray(type) THEN
  6370. len := len * type(SyntaxTree.ArrayType).staticLength;
  6371. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6372. INC(dim);
  6373. ELSE
  6374. baseType := type;
  6375. END;
  6376. END PushLens;
  6377. BEGIN
  6378. (* cell *)
  6379. IF parameter.type IS SyntaxTree.ArrayType THEN
  6380. type := parameter.type;
  6381. dim := 0;
  6382. len := 1;
  6383. PushLens(type);
  6384. portType := baseType.resolved(SyntaxTree.PortType);
  6385. ELSE
  6386. portType := parameter.type(SyntaxTree.PortType);
  6387. END;
  6388. PushSelfPointer();
  6389. (* port / array of ports *)
  6390. IF IsStaticArray(type) THEN
  6391. PushConstInteger(len);
  6392. END;
  6393. Field(parameter, op);
  6394. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6395. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6396. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6397. Designate(d, op);*)
  6398. Emit(Push(Basic.invalidPosition, op.op));
  6399. ReleaseOperand(op);
  6400. (* name *)
  6401. PushConstString(name);
  6402. (* inout *)
  6403. PushConstSet(Direction(portType.direction));
  6404. (* width *)
  6405. PushConstInteger(portType.sizeInBits);
  6406. IF parameter.type IS SyntaxTree.PortType THEN
  6407. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6408. AddPortProperties(parameter);
  6409. ELSIF IsStaticArray(type)THEN
  6410. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6411. ELSIF IsSemiDynamicArray(type) THEN
  6412. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6413. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6414. Emit(Add(position,reg, sp, size));
  6415. (* dim *)
  6416. PushConstInteger(dim);
  6417. (* len array *)
  6418. Emit(Push(position, reg));
  6419. ReleaseIntermediateOperand(reg);
  6420. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6421. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6422. Emit(Add(position, sp,sp, size));
  6423. ELSE
  6424. HALT(100);
  6425. END;
  6426. END Parameter;
  6427. BEGIN
  6428. IF backend.cellsAreObjects THEN
  6429. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6430. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6431. END;
  6432. parameter := x.firstParameter;
  6433. WHILE (parameter # NIL) DO
  6434. type := parameter.type.resolved;
  6435. WHILE (type IS SyntaxTree.ArrayType) DO
  6436. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6437. END;
  6438. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6439. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6440. Parameter(name,parameter);
  6441. END;
  6442. parameter := parameter.nextParameter;
  6443. END;
  6444. ELSE HALT(200)
  6445. END;
  6446. END AddPorts;
  6447. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6448. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6449. BEGIN
  6450. Symbol(cell,op);
  6451. ToMemory(op.op,addressType,0);
  6452. Emit(Push(position,op.op));
  6453. ReleaseOperand(op);
  6454. property.GetName(name);
  6455. (* does not work when inheritance is used:
  6456. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6457. *)
  6458. PushConstString(name);
  6459. IF (value # NIL) THEN
  6460. ASSERT(
  6461. SemanticChecker.IsStringType(property.type)
  6462. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6463. OR (property.type.resolved IS SyntaxTree.FloatType)
  6464. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6465. OR (property.type.resolved IS SyntaxTree.SetType)
  6466. );
  6467. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6468. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6469. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6470. Designate(d, op);
  6471. IF SemanticChecker.IsStringType(property.type) THEN
  6472. Emit(Push(Basic.invalidPosition, op.tag))
  6473. END;
  6474. Emit(Push(Basic.invalidPosition, op.op));
  6475. ReleaseOperand(op);
  6476. END;
  6477. IF value = NIL THEN
  6478. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6479. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6480. ASSERT(SemanticChecker.IsStringType(value.type));
  6481. Designate(value, op);
  6482. PushString(op, value.type);
  6483. ReleaseOperand(op);
  6484. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6485. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6486. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6487. Evaluate(value, op);
  6488. Emit(Push(property.position, op.op));
  6489. ReleaseOperand(op);
  6490. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6491. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6492. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6493. Evaluate(value, op);
  6494. Emit(Push(property.position, op.op));
  6495. ReleaseOperand(op);
  6496. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6497. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6498. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6499. Evaluate(value, op);
  6500. Emit(Push(property.position, op.op));
  6501. ReleaseOperand(op);
  6502. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6503. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6504. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6505. Evaluate(value, op);
  6506. Emit(Push(property.position, op.op));
  6507. ReleaseOperand(op);
  6508. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6509. ELSE
  6510. HALT(200);
  6511. END;
  6512. END AddProperty;
  6513. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6514. VAR symbol: SyntaxTree.Symbol;
  6515. BEGIN
  6516. WHILE modifier # NIL DO
  6517. symbol := cellType.FindProperty(modifier.identifier);
  6518. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6519. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6520. modifier := modifier.nextModifier;
  6521. END;
  6522. END AddModifiers;
  6523. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6524. VAR last: SyntaxTree.Modifier;
  6525. BEGIN
  6526. IF to = NIL THEN
  6527. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6528. ELSE
  6529. last := to;
  6530. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6531. last := last.nextModifier;
  6532. END;
  6533. IF last.identifier # this.identifier THEN
  6534. ASSERT(last.nextModifier = NIL);
  6535. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6536. END;
  6537. END;
  6538. END AppendModifier;
  6539. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6540. BEGIN
  6541. WHILE this # NIL DO
  6542. AppendModifier(to, this);
  6543. this := this.nextModifier;
  6544. END;
  6545. END AppendModifiers;
  6546. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6547. VAR base: SyntaxTree.Type;
  6548. BEGIN
  6549. AppendModifiers(to, c.modifiers);
  6550. base := c.GetBaseValueType();
  6551. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6552. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6553. END;
  6554. END AppendCellTypeModifiers;
  6555. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6556. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6557. BEGIN
  6558. Basic.GetString(modifier.identifier, name);
  6559. PushConstString(name);
  6560. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6561. ASSERT(SemanticChecker.IsStringType(value.type));
  6562. Designate(value, op);
  6563. PushString(op, value.type);
  6564. ReleaseOperand(op);
  6565. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6566. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6567. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6568. Evaluate(value, op);
  6569. Emit(Push(modifier.position, op.op));
  6570. ReleaseOperand(op);
  6571. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6572. ELSE
  6573. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6574. END;
  6575. END AddPortProperty;
  6576. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6577. BEGIN
  6578. WHILE modifier # NIL DO
  6579. AddPortProperty(modifier, modifier.expression);
  6580. modifier := modifier.nextModifier;
  6581. END;
  6582. END AddPortProperties;
  6583. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6584. VAR op: Operand;
  6585. BEGIN
  6586. Evaluate(p, op);
  6587. Emit(Push(p.position, op.op));
  6588. ReleaseOperand(op);
  6589. IF p IS SyntaxTree.Designator THEN
  6590. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6591. END;
  6592. END PushPort;
  6593. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6594. VAR tmp: IntermediateCode.Operand;
  6595. BEGIN
  6596. actualType := actualType.resolved;
  6597. IF actualType IS SyntaxTree.StringType THEN
  6598. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6599. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6600. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6601. ELSE
  6602. tmp := op.tag;
  6603. IntermediateCode.MakeMemory(tmp,addressType);
  6604. Emit(Push(position,tmp));
  6605. END;
  6606. Emit(Push(position,op.op))
  6607. END PushString;
  6608. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6609. VAR
  6610. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6611. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6612. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6613. tmp:IntermediateCode.Operand;
  6614. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6615. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6616. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6617. dest: IntermediateCode.Operand;
  6618. staticLength: LONGINT; itype: IntermediateCode.Type;
  6619. convert,isTensor: BOOLEAN;
  6620. recordType: SyntaxTree.RecordType;
  6621. baseType: SyntaxTree.Type;
  6622. flags: SET;
  6623. left: SyntaxTree.Expression;
  6624. call: SyntaxTree.Designator;
  6625. procedure: SyntaxTree.Procedure;
  6626. temporaryVariable: SyntaxTree.Variable;
  6627. dummy: IntermediateCode.Operand;
  6628. customBuiltin: SyntaxTree.CustomBuiltin;
  6629. isVarPar: ARRAY 3 OF BOOLEAN;
  6630. callsection: Sections.Section;
  6631. segmentedName: Basic.SegmentedName;
  6632. needsTrace: BOOLEAN;
  6633. n: ARRAY 256 OF CHAR;
  6634. modifier: SyntaxTree.Modifier;
  6635. previous, init: IntermediateCode.Section;
  6636. prevScope: SyntaxTree.Scope;
  6637. firstPar: LONGINT;
  6638. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6639. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6640. priority: IntermediateCode.Operand;
  6641. op,callop: Operand;
  6642. BEGIN
  6643. IF type = NIL THEN RETURN END;
  6644. type := type.resolved;
  6645. IF type IS SyntaxTree.PointerType THEN
  6646. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6647. END;
  6648. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6649. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6650. recordScope := type(SyntaxTree.RecordType).recordScope;
  6651. IF recordScope.bodyProcedure # NIL THEN
  6652. procedure := recordScope.bodyProcedure;
  6653. body := procedure.procedureScope.body;
  6654. Emit(Push(position,self));
  6655. IF body.isActive THEN
  6656. StaticCallOperand(callop,procedure);
  6657. Emit(Push(position,callop.op));
  6658. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6659. Convert(priority,sizeType);
  6660. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6661. END;
  6662. Emit(Push(position,priority));
  6663. ReleaseIntermediateOperand(priority);
  6664. IF backend.cooperative THEN
  6665. Emit(Push(position,self));
  6666. CallThis(position,"Activities","Create",3)
  6667. ELSE
  6668. flags := 0;
  6669. IF body.isSafe THEN
  6670. flags := 1;
  6671. END;
  6672. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6673. Emit(Push(position,self));
  6674. CallThis(position,"Objects","CreateProcess",4)
  6675. END;
  6676. ELSE
  6677. Emit(Push(position,self));
  6678. StaticCallOperand(callop,procedure);
  6679. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6680. END;
  6681. Emit(Pop(position,self));
  6682. END;
  6683. END CallBodies;
  6684. PROCEDURE PushTD(type: SyntaxTree.Type);
  6685. VAR op: IntermediateCode.Operand;
  6686. BEGIN
  6687. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6688. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6689. ELSE
  6690. IF type.resolved IS SyntaxTree.PointerType THEN
  6691. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6692. END;
  6693. op := TypeDescriptorAdr(type.resolved);
  6694. IF ~newObjectFile THEN
  6695. IntermediateCode.MakeMemory(op,addressType);
  6696. END;
  6697. Emit(Push(position,op));
  6698. END
  6699. END PushTD;
  6700. BEGIN
  6701. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6702. dest := destination; destination := emptyOperand;
  6703. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6704. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6705. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6706. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6707. CASE x.id OF
  6708. (* ---- COPY ----- *)
  6709. |Global.Copy:
  6710. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6711. (* ---- EXCL, INCL----- *)
  6712. |Global.Excl,Global.Incl:
  6713. Evaluate(p0,s0);
  6714. Evaluate(p1,s1);
  6715. Convert(s1.op,setType);
  6716. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6717. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6718. END;
  6719. ReuseCopy(res,s0.op);
  6720. ReleaseOperand(s0);
  6721. Reuse1(tmp,s1.op);
  6722. ReleaseOperand(s1);
  6723. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6724. IF x.id = Global.Excl THEN
  6725. Emit(Not(position,tmp,tmp));
  6726. Emit(And(position,res,res,tmp));
  6727. ELSE
  6728. Emit(Or(position,res,res,tmp));
  6729. END;
  6730. ReleaseIntermediateOperand(tmp);
  6731. Designate(p0,s0);
  6732. ToMemory(s0.op,setType,0);
  6733. Emit(Mov(position,s0.op,res));
  6734. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6735. (* ---- DISPOSE ----- *)
  6736. |Global.Dispose:
  6737. Designate(p0,s0);
  6738. Emit(Push(position,s0.op));
  6739. ReleaseOperand(s0);
  6740. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6741. (* ---- GETPROCEDURE ----- *)
  6742. |Global.GetProcedure:
  6743. Designate(p0,s0);
  6744. PushString(s0,p0.type);
  6745. Designate(p1,s1);
  6746. PushString(s1,p1.type);
  6747. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6748. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6749. ELSE PushTD(procedureType.firstParameter.type)
  6750. END;
  6751. PushTD(procedureType.returnType);
  6752. Designate(p2,s2);
  6753. Emit(Push(position,s2.op));
  6754. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6755. CallThis(position,"Modules","GetProcedure", 7);
  6756. (* ---- ASH, LSH, ROT ----- *)
  6757. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6758. Evaluate(p0,s0);
  6759. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6760. (* make unsigned arguments in order to produced a logical shift *)
  6761. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6762. convert:= TRUE;
  6763. itype := s0.op.type;
  6764. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6765. Convert(s0.op,itype);
  6766. ELSE
  6767. convert := FALSE;
  6768. END;
  6769. END;
  6770. Evaluate(p1,s1);
  6771. IF IsIntegerConstant(p1,hint) THEN
  6772. ReuseCopy(reg,s0.op);
  6773. IF hint > 0 THEN
  6774. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6775. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6776. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6777. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6778. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6779. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6780. END;
  6781. ELSIF hint < 0 THEN
  6782. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6783. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6784. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6785. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6786. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6787. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6788. END;
  6789. END;
  6790. ReleaseOperand(s0); ReleaseOperand(s1);
  6791. ELSE
  6792. exit := NewLabel();
  6793. end := NewLabel();
  6794. ReuseCopy(reg,s0.op);
  6795. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6796. Reuse1(tmp,s1.op);
  6797. Emit(Neg(position,tmp,s1.op));
  6798. Convert(tmp,s1.op.type);
  6799. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6800. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6801. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6802. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6803. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6804. END;
  6805. ReleaseIntermediateOperand(tmp);
  6806. BrL(end);
  6807. SetLabel(exit);
  6808. ReuseCopy(tmp,s1.op);
  6809. Convert(tmp,s1.op.type);
  6810. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6811. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6812. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6813. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6814. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6815. END;
  6816. ReleaseIntermediateOperand(tmp);
  6817. SetLabel(end);
  6818. ReleaseOperand(s0); ReleaseOperand(s1);
  6819. END;
  6820. InitOperand(result,ModeValue);
  6821. IF convert THEN
  6822. itype := reg.type;
  6823. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6824. Convert(reg,itype);
  6825. END;
  6826. result.op := reg;
  6827. (* ---- CAP ----- *)
  6828. |Global.Cap:
  6829. Evaluate(p0,result);
  6830. ReuseCopy(reg,result.op);
  6831. ReleaseIntermediateOperand(result.op);
  6832. ignore := NewLabel();
  6833. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6834. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6835. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6836. SetLabel(ignore);
  6837. result.op := reg;
  6838. (* ---- CHR ----- *)
  6839. |Global.Chr, Global.Chr32:
  6840. Evaluate(p0,result);
  6841. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6842. |Global.Entier, Global.EntierH:
  6843. Evaluate(p0,result);
  6844. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6845. (* ---- MIN and MAX ----- *)
  6846. |Global.Max,Global.Min:
  6847. Evaluate(p0,s0);
  6848. Evaluate(p1,s1);
  6849. Reuse2(res,s0.op,s1.op);
  6850. else := NewLabel();
  6851. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6852. ELSE BrltL(else,s1.op,s0.op) END;
  6853. Emit(Mov(position,res,s0.op));
  6854. ReleaseOperand(s0);
  6855. end := NewLabel();
  6856. BrL(end);
  6857. SetLabel(else);
  6858. Emit(MovReplace(position,res,s1.op));
  6859. SetLabel(end);
  6860. ReleaseOperand(s1);
  6861. InitOperand(result,ModeValue);
  6862. result.op := res;
  6863. (* ---- ODD ----- *)
  6864. |Global.Odd:
  6865. IF ~conditional THEN
  6866. ConditionToValue(x)
  6867. ELSE
  6868. Evaluate(p0,result);
  6869. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6870. Reuse1(res,result.op);
  6871. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6872. ReleaseIntermediateOperand(result.op);
  6873. result.op := res;
  6874. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6875. ReleaseOperand(result);
  6876. BrL(falseLabel);
  6877. END;
  6878. (* ---- ORD ----- *)
  6879. |Global.Ord, Global.Ord32:
  6880. Evaluate(p0,result);
  6881. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6882. (* ---- SHORT, LONG ----- *)
  6883. |Global.Short, Global.Long:
  6884. Evaluate(p0,result);
  6885. IF x.type IS SyntaxTree.ComplexType THEN
  6886. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6887. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6888. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6889. ELSE
  6890. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6891. END
  6892. (* ---- HALT, SYSTEM.HALT----- *)
  6893. |Global.Halt, Global.systemHalt:
  6894. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6895. EmitTrap (position, val);
  6896. (* ---- ASSERT ----- *)
  6897. |Global.Assert:
  6898. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6899. trueL := NewLabel();
  6900. falseL := NewLabel();
  6901. Condition(p0,trueL,falseL);
  6902. IF p1 = NIL THEN val := AssertTrap
  6903. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6904. END;
  6905. SetLabel(falseL);
  6906. EmitTrap(position,val);
  6907. SetLabel(trueL);
  6908. END;
  6909. (*
  6910. Emit(TrapC(result.op,val);
  6911. *)
  6912. (* ---- INC, DEC----- *)
  6913. |Global.Inc,Global.Dec:
  6914. Expression(p0); adr := result.op;
  6915. LoadValue(result,p0.type); l := result;
  6916. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6917. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6918. END;
  6919. IF x.id = Global.Inc THEN
  6920. Emit(Add(position,l.op,l.op,r.op));
  6921. ELSE
  6922. Emit(Sub(position,l.op,l.op,r.op));
  6923. END;
  6924. ReleaseOperand(l); ReleaseOperand(r);
  6925. (* ---- LEN ----- *)
  6926. |Global.Len: (* dynamic length, static length done by checker *)
  6927. Designate(p0,operand);
  6928. IF p1 = NIL THEN
  6929. InitOperand(l,ModeValue);
  6930. l.op := IntermediateCode.Immediate(int32,0);
  6931. ELSE
  6932. Evaluate(p1,l);
  6933. END;
  6934. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6935. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6936. Dereference(operand, p0.type.resolved, FALSE);
  6937. END;
  6938. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6939. ReleaseOperand(operand); ReleaseOperand(l);
  6940. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6941. ASSERT(p1 # NIL);
  6942. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6943. Dereference(operand,p0.type.resolved,FALSE);
  6944. END;
  6945. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6946. ReleaseOperand(operand); ReleaseOperand(l);
  6947. ELSE HALT(100);
  6948. END;
  6949. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6950. (* ---- FIRST ---- *)
  6951. |Global.First:
  6952. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6953. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6954. ELSE
  6955. Designate(p0, result)
  6956. END
  6957. (* ---- LAST ---- *)
  6958. |Global.Last:
  6959. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6960. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6961. ELSE
  6962. Designate(p0, result);
  6963. (* make sure result.op is a register *)
  6964. tmp := result.op;
  6965. ReuseCopy(result.op, result.op);
  6966. ReleaseIntermediateOperand(tmp);
  6967. (* add offset to result.op *)
  6968. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6969. END
  6970. (* ---- STEP ---- *)
  6971. |Global.Step:
  6972. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6973. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6974. ELSE
  6975. Designate(p0, result);
  6976. (* make sure result.op is a register *)
  6977. tmp := result.op;
  6978. ReuseCopy(result.op, result.op);
  6979. ReleaseIntermediateOperand(tmp);
  6980. (* add offset to result.op *)
  6981. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6982. END
  6983. (* ---- RE ---- *)
  6984. |Global.Re:
  6985. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6986. Designate(p0, result)
  6987. ELSE
  6988. Evaluate(p0, result)
  6989. END
  6990. (* ---- IM ---- *)
  6991. |Global.Im:
  6992. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6993. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6994. Designate(p0, result);
  6995. (* make sure result.op is a register *)
  6996. tmp := result.op;
  6997. ReuseCopy(result.op, result.op);
  6998. ReleaseIntermediateOperand(tmp);
  6999. (* add offset to result.op *)
  7000. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7001. (* ---- ABS ----- *)
  7002. |Global.Abs:
  7003. Evaluate(p0,operand);
  7004. type := p0.type.resolved;
  7005. InitOperand(result,ModeValue);
  7006. Reuse1a(result.op,operand.op,dest);
  7007. Emit(Abs(position,result.op,operand.op));
  7008. ReleaseOperand(operand);
  7009. (* ---- WAIT ----- *)
  7010. |Global.Wait:
  7011. Evaluate(p0,operand);
  7012. Emit(Push(position,operand.op));
  7013. ReleaseOperand(operand);
  7014. CallThis(position,"Activities","Wait", 1);
  7015. (* ---- NEW ----- *)
  7016. |Global.New:
  7017. (*! the following code is only correct for "standard" Oberon calling convention *)
  7018. IF x.type # NIL THEN
  7019. type := x.type.resolved;
  7020. firstPar := 0;
  7021. ELSE
  7022. type := p0.type.resolved;
  7023. firstPar := 1;
  7024. END;
  7025. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7026. THEN
  7027. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7028. IF backend.cooperative THEN
  7029. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7030. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7031. IF recordType.isObject THEN
  7032. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7033. IF recordType.IsActive() THEN
  7034. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7035. END;
  7036. IF recordType.IsProtected() THEN
  7037. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7038. END;
  7039. ELSE
  7040. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7041. END;
  7042. END;
  7043. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7044. CallThis(position,"Runtime","New", 1);
  7045. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7046. Emit(Result(position, pointer));
  7047. exit := NewLabel();
  7048. BreqL(exit,pointer,nil);
  7049. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7050. GetRecordTypeName (recordType,name);
  7051. IF ~recordType.isObject THEN
  7052. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7053. END;
  7054. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7055. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7056. IF recordType.isObject THEN
  7057. IF recordType.IsProtected() THEN
  7058. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7059. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7060. END;
  7061. IF recordType.IsActive() THEN
  7062. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7063. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7064. END;
  7065. END;
  7066. END;
  7067. (* initialize fields *)
  7068. IF type(SyntaxTree.PointerType).isPlain THEN
  7069. size := 0;
  7070. ELSIF recordType.isObject THEN
  7071. size := BaseObjectTypeSize;
  7072. ELSE
  7073. size := BaseRecordTypeSize;
  7074. END;
  7075. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7076. (* call initializer *)
  7077. constructor := GetConstructor(recordType);
  7078. IF constructor # NIL THEN
  7079. (*! should be unified with ProcedureCallDesignator *)
  7080. Emit(Push(position,pointer));
  7081. ReleaseIntermediateOperand(pointer);
  7082. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7083. FOR i := firstPar TO x.parameters.Length()-1 DO
  7084. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7085. formalParameter := formalParameter.nextParameter;
  7086. END;
  7087. (* static call of the constructor *)
  7088. GetCodeSectionNameForSymbol(constructor,name);
  7089. ASSERT(~constructor.isInline);
  7090. IF constructor.scope.ownerModule # module.module THEN
  7091. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7092. ELSE
  7093. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7094. END;
  7095. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7096. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7097. Emit(Pop(position,pointer));
  7098. END;
  7099. (* call bodies *)
  7100. CallBodies(pointer,type);
  7101. SetLabel(exit);
  7102. needsTrace := p0.NeedsTrace();
  7103. IF needsTrace THEN ModifyAssignments(true) END;
  7104. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7105. Emit(Push(position, pointer));
  7106. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7107. Emit(Pop(position, pointer));
  7108. END;
  7109. Designate(p0,l);
  7110. IF needsTrace THEN
  7111. CallAssignPointer(l.op, pointer);
  7112. ELSE
  7113. ToMemory(l.op,addressType,0);
  7114. Emit(Mov(position,l.op,pointer));
  7115. END;
  7116. ReleaseIntermediateOperand(pointer);
  7117. ReleaseOperand(l);
  7118. IF needsTrace THEN ModifyAssignments(false) END;
  7119. ELSE (* not cooperative backend *)
  7120. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7121. IF temporaryVariable # NIL THEN
  7122. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7123. ELSE
  7124. Designate(p0,l);
  7125. END;
  7126. (* l.op contains address of pointer to record *)
  7127. Emit(Push(position,l.op)); (* address for use after syscall *)
  7128. Emit(Push(position,l.op));
  7129. ReleaseOperand(l);
  7130. (* push type descriptor *)
  7131. reg := TypeDescriptorAdr(recordType);
  7132. IF ~newObjectFile THEN
  7133. IntermediateCode.MakeMemory(reg,addressType);
  7134. END;
  7135. Emit(Push(position,reg));
  7136. ReleaseIntermediateOperand(reg);
  7137. (* push realtime flag *)
  7138. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7139. ELSE Emit(Push(position,false));
  7140. END;
  7141. CallThis(position,"Heaps","NewRec", 3);
  7142. (* check allocation success, if not successful then do not call initializers and bodies *)
  7143. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7144. Emit(Pop(position,pointer));
  7145. MakeMemory(reg,pointer,addressType,0);
  7146. ReleaseIntermediateOperand(pointer);
  7147. pointer := reg;
  7148. exit := NewLabel();
  7149. BreqL(exit,pointer,nil);
  7150. Emit(Push(position,pointer));
  7151. (* initialize fields *)
  7152. InitFields(recordType, pointer,0);
  7153. (* call initializer *)
  7154. constructor := GetConstructor(recordType);
  7155. IF constructor # NIL THEN
  7156. (*! should be unified with ProcedureCallDesignator *)
  7157. Emit(Push(position,pointer));
  7158. ReleaseIntermediateOperand(pointer);
  7159. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7160. FOR i := firstPar TO x.parameters.Length()-1 DO
  7161. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7162. formalParameter := formalParameter.nextParameter;
  7163. END;
  7164. (* static call of the constructor *)
  7165. GetCodeSectionNameForSymbol(constructor,name);
  7166. ASSERT(~constructor.isInline);
  7167. IF constructor.scope.ownerModule # module.module THEN
  7168. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7169. ELSE
  7170. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7171. END;
  7172. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7173. ELSE
  7174. ReleaseIntermediateOperand(pointer);
  7175. END;
  7176. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7177. Emit(Pop(position,pointer));
  7178. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7179. Designate(p0,l);
  7180. ToMemory(l.op,addressType,0);
  7181. Emit(Mov(position,l.op,pointer));
  7182. ReleaseOperand(l);
  7183. result.tag := emptyOperand;
  7184. ELSIF (x.type # NIL) THEN
  7185. result := l; (* temporary variable is the result of NEW Type() *)
  7186. END;
  7187. (* call bodies *)
  7188. CallBodies(pointer,type);
  7189. ReleaseIntermediateOperand(pointer);
  7190. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7191. end := NewLabel();
  7192. BrL(end);
  7193. SetLabel(exit);
  7194. Designate(p0,l);
  7195. ToMemory(l.op,addressType,0);
  7196. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7197. ReleaseOperand(l);
  7198. SetLabel(end);
  7199. ELSE
  7200. SetLabel(exit);
  7201. END;
  7202. END;
  7203. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7204. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7205. dim := 0;
  7206. IntermediateCode.InitOperand(reg);
  7207. IF p1 # NIL THEN
  7208. FOR i := firstPar TO x.parameters.Length()-1 DO
  7209. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7210. parameter := x.parameters.GetExpression(i);
  7211. Evaluate(parameter,r);
  7212. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7213. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7214. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7215. END;
  7216. Emit(Push(position,r.op));
  7217. IF i=1 THEN
  7218. CopyInt(reg,r.op);
  7219. ELSE
  7220. MulInt(reg, reg, r.op);
  7221. END;
  7222. ReleaseOperand(r);
  7223. INC(dim);
  7224. END;
  7225. Convert(reg,addressType);
  7226. ELSE
  7227. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7228. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7229. END;
  7230. openDim := dim;
  7231. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7232. IF backend.cooperative THEN
  7233. size := ToMemoryUnits(system,system.SizeOf(type));
  7234. WHILE type IS SyntaxTree.ArrayType DO
  7235. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7236. END;
  7237. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7238. IF (size # 1) THEN
  7239. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7240. END;
  7241. Emit(Push(position,reg));
  7242. size := ToMemoryUnits(system,system.SizeOf(type));
  7243. IF (size # 1) THEN
  7244. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7245. END;
  7246. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  7247. Emit(Push(position,reg));
  7248. ReleaseIntermediateOperand(reg);
  7249. CallThis(position,"Runtime","New", 1);
  7250. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7251. Emit(Result(position, pointer));
  7252. exit := NewLabel();
  7253. else := NewLabel();
  7254. BreqL(else,pointer,nil);
  7255. IF ~type.hasPointers THEN
  7256. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7257. ELSIF type IS SyntaxTree.RecordType THEN
  7258. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7259. ELSIF type IS SyntaxTree.ProcedureType THEN
  7260. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7261. ELSE
  7262. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7263. END;
  7264. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7265. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7266. 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))));
  7267. IF type IS SyntaxTree.RecordType THEN
  7268. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7269. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7270. ELSE
  7271. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7272. END;
  7273. i := openDim;
  7274. WHILE i > 0 DO
  7275. DEC (i);
  7276. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7277. END;
  7278. needsTrace := p0.NeedsTrace();
  7279. IF needsTrace THEN ModifyAssignments(true) END;
  7280. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7281. Emit(Push(position, pointer));
  7282. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7283. Emit(Pop(position, pointer));
  7284. END;
  7285. Designate(p0,l);
  7286. IF needsTrace THEN
  7287. CallAssignPointer(l.op, pointer);
  7288. ModifyAssignments(false);
  7289. ELSE
  7290. ToMemory(l.op,addressType,0);
  7291. Emit(Mov(position,l.op,pointer));
  7292. END;
  7293. ReleaseIntermediateOperand(pointer);
  7294. ReleaseOperand(l);
  7295. BrL(exit);
  7296. SetLabel(else);
  7297. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7298. Designate(p0,l);
  7299. IF needsTrace THEN
  7300. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7301. ELSE
  7302. ToMemory(l.op,addressType,0);
  7303. Emit(Mov(position,l.op,pointer));
  7304. END;
  7305. ReleaseOperand(l);
  7306. SetLabel(exit);
  7307. ELSE
  7308. (*! the following code is only correct for "standard" Oberon calling convention *)
  7309. IF SemanticChecker.ContainsPointer(type) THEN
  7310. IF type IS SyntaxTree.ArrayType THEN
  7311. staticLength := 1;
  7312. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7313. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7314. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7315. END;
  7316. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7317. MulInt(reg,reg,tmp);
  7318. END;
  7319. Designate(p0,l);
  7320. IF openDim > 0 THEN
  7321. Emit(Push(position,l.op)); (* address for use after syscall *)
  7322. END;
  7323. Emit(Push(position,l.op)); (* address *)
  7324. ReleaseOperand(l);
  7325. tmp := TypeDescriptorAdr(type);
  7326. IF ~newObjectFile THEN
  7327. IntermediateCode.MakeMemory(tmp,addressType);
  7328. END;
  7329. Emit(Push(position,tmp)); (* type descriptor *)
  7330. ReleaseIntermediateOperand(tmp);
  7331. Emit(Push(position,reg)); (* number Elements *)
  7332. ReleaseIntermediateOperand(reg);
  7333. tmp := IntermediateCode.Immediate(addressType,dim);
  7334. Emit(Push(position,tmp)); (* dimensions *)
  7335. (* push realtime flag *)
  7336. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7337. ELSE Emit(Push(position,false));
  7338. END;
  7339. CallThis(position,"Heaps","NewArr",5)
  7340. ELSE
  7341. size := ToMemoryUnits(system,system.SizeOf(type));
  7342. IF (size # 1) THEN
  7343. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7344. (*
  7345. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7346. *)
  7347. END;
  7348. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7349. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7350. AddInt(reg, reg, tmp);
  7351. (*
  7352. Emit(Add(position,reg,reg,tmp));
  7353. *)
  7354. Designate(p0,l);
  7355. IF openDim >0 THEN
  7356. Emit(Push(position,l.op)); (* address for use after syscall *)
  7357. END;
  7358. Emit(Push(position,l.op)); (* address for syscall *)
  7359. ReleaseOperand(l); (* pointer address *)
  7360. Emit(Push(position,reg)); (* size *)
  7361. ReleaseIntermediateOperand(reg);
  7362. (* push realtime flag *)
  7363. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7364. ELSE Emit(Push(position,false));
  7365. END;
  7366. CallThis(position,"Heaps","NewSys", 3)
  7367. END;
  7368. IF openDim > 0 THEN
  7369. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7370. Emit(Pop(position,adr));
  7371. ToMemory(adr,addressType,0);
  7372. ReuseCopy(tmp,adr);
  7373. ReleaseIntermediateOperand(adr);
  7374. adr := tmp;
  7375. else := NewLabel();
  7376. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7377. i := openDim-1;
  7378. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7379. WHILE (i >= 0) DO
  7380. Emit(Pop(position,reg));
  7381. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7382. Emit(Mov(position,res,reg));
  7383. DEC(i);
  7384. END;
  7385. ReleaseIntermediateOperand(adr);
  7386. ReleaseIntermediateOperand(reg);
  7387. exit := NewLabel();
  7388. BrL(exit);
  7389. SetLabel(else);
  7390. (* else part: array could not be allocated *)
  7391. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7392. Emit(Add(position,sp,sp,tmp));
  7393. SetLabel(exit);
  7394. END;
  7395. END;
  7396. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7397. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7398. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7399. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7400. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7401. left.SetType(procedure.type);
  7402. formalParameter := procedureType.firstParameter;
  7403. (* push array to allocate *)
  7404. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7405. formalParameter :=formalParameter.nextParameter;
  7406. (* push length array *)
  7407. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7408. (* push size *)
  7409. type := t0;
  7410. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7411. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7412. END;
  7413. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7414. Emit(Push(position,tmp));
  7415. (* *)
  7416. IF SemanticChecker.ContainsPointer(type) THEN
  7417. tmp := TypeDescriptorAdr(type);
  7418. IF ~newObjectFile THEN
  7419. IntermediateCode.MakeMemory(tmp,addressType);
  7420. END;
  7421. ELSE
  7422. tmp := IntermediateCode.Immediate(addressType, 0);
  7423. END;
  7424. Emit(Push(position,tmp)); (* type descriptor *)
  7425. StaticCallOperand(result,procedure);
  7426. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7427. ReleaseOperand(result);
  7428. END;
  7429. (*
  7430. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7431. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7432. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7433. *)
  7434. ELSE
  7435. dim := 0;
  7436. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7437. (* generate geometry descriptor *)
  7438. Designate(p0,l);
  7439. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7440. ReleaseOperand(l);
  7441. isTensor := TRUE;
  7442. ELSE
  7443. isTensor := FALSE;
  7444. END;
  7445. FOR i := firstPar TO x.parameters.Length()-1 DO
  7446. IF ~isTensor THEN
  7447. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7448. END;
  7449. parameter := x.parameters.GetExpression(i);
  7450. Evaluate(parameter,r);
  7451. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7452. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7453. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7454. END;
  7455. Emit(Push(position,r.op));
  7456. IF i=1 THEN
  7457. CopyInt(reg, r.op);
  7458. ELSE
  7459. MulInt(reg, reg, r.op);
  7460. END;
  7461. ReleaseOperand(r);
  7462. INC(dim);
  7463. END;
  7464. Convert(reg,addressType);
  7465. openDim := dim;
  7466. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7467. (*! the following code is only correct for "standard" Oberon calling convention *)
  7468. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7469. t := type;
  7470. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7471. staticLength := 1;
  7472. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7473. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7474. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7475. END;
  7476. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7477. MulInt(reg,reg,tmp);
  7478. END;
  7479. Designate(p0,l);
  7480. IF isTensor THEN
  7481. Dereference(l,type,FALSE);
  7482. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7483. END;
  7484. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7485. Emit(Push(position,l.tag)); (* address *)
  7486. ReleaseOperand(l);
  7487. tmp := TypeDescriptorAdr(t);
  7488. IF ~newObjectFile THEN
  7489. IntermediateCode.MakeMemory(tmp,addressType);
  7490. END;
  7491. Emit(Push(position,tmp)); (* type descriptor *)
  7492. ReleaseIntermediateOperand(tmp);
  7493. Emit(Push(position,reg)); (* number Elements *)
  7494. ReleaseIntermediateOperand(reg);
  7495. tmp := IntermediateCode.Immediate(addressType,0);
  7496. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7497. (* push realtime flag: false by default *)
  7498. Emit(Push(position,false));
  7499. CallThis(position,"Heaps","NewArr",5);
  7500. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7501. Emit(Pop(position,adr));
  7502. GetMathArrayField(tmp,adr,MathPtrOffset);
  7503. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7504. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7505. PutMathArrayField(adr,reg,MathAdrOffset);
  7506. ReleaseIntermediateOperand(tmp);
  7507. ReleaseIntermediateOperand(reg);
  7508. ELSE
  7509. IF isTensor THEN
  7510. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7511. ELSE
  7512. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7513. END;
  7514. IF (size # 1) THEN
  7515. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7516. END;
  7517. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7518. AddInt(reg,reg,tmp);
  7519. Designate(p0,l);
  7520. IF isTensor THEN
  7521. Dereference(l,type,FALSE);
  7522. END;
  7523. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7524. Emit(Push(position,l.tag)); (* address for syscall *)
  7525. ReleaseOperand(l); (* pointer address *)
  7526. Emit(Push(position,reg)); (* size *)
  7527. ReleaseIntermediateOperand(reg);
  7528. (* push realtime flag: false by default *)
  7529. Emit(Push(position,false));
  7530. CallThis(position,"Heaps","NewSys",3);
  7531. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7532. Emit(Pop(position,adr));
  7533. GetMathArrayField(tmp,adr,MathPtrOffset);
  7534. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7535. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7536. PutMathArrayField(adr,reg,MathAdrOffset);
  7537. ReleaseIntermediateOperand(tmp);
  7538. ReleaseIntermediateOperand(reg);
  7539. END;
  7540. flags := {};
  7541. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7542. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7543. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7544. PutMathArrayField(adr,tmp,MathDimOffset);
  7545. else := NewLabel();
  7546. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7547. i := openDim-1;
  7548. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7549. IF isTensor THEN
  7550. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7551. ELSE
  7552. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7553. END;
  7554. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7555. WHILE (i >= 0) DO
  7556. Emit(Pop(position,reg));
  7557. PutMathArrayLength(adr,reg,i);
  7558. PutMathArrayIncrement(adr,tmp,i);
  7559. IF i > 0 THEN
  7560. IF i=openDim-1 THEN
  7561. CopyInt(tmp,tmp);
  7562. END;
  7563. MulInt(tmp,tmp,reg);
  7564. END;
  7565. DEC(i);
  7566. END;
  7567. ReleaseIntermediateOperand(adr);
  7568. ReleaseIntermediateOperand(reg);
  7569. ReleaseIntermediateOperand(tmp);
  7570. exit := NewLabel();
  7571. BrL(exit);
  7572. SetLabel(else);
  7573. (* else part: array could not be allocated *)
  7574. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7575. Emit(Add(position,sp,sp,tmp));
  7576. SetLabel(exit);
  7577. END;
  7578. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7579. THEN
  7580. IF ~backend.cellsAreObjects THEN RETURN END;
  7581. IF InCellScope(currentScope) THEN
  7582. PushSelfPointer()
  7583. ELSE
  7584. Emit(Push(position, nil));
  7585. END;
  7586. (* push temp address *)
  7587. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7588. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7589. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7590. (* l.op contains address of pointer to record *)
  7591. Emit(Push(position,l.op)); (* address for use after syscall *)
  7592. ReleaseOperand(l);
  7593. (* push type descriptor *)
  7594. reg := TypeDescriptorAdr(baseType);
  7595. IF ~newObjectFile THEN
  7596. IntermediateCode.MakeMemory(reg,addressType);
  7597. END;
  7598. Emit(Push(position,reg));
  7599. ReleaseIntermediateOperand(reg);
  7600. (* push name *)
  7601. (*Global.GetSymbolName(p0, n);*)
  7602. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7603. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7604. ELSE
  7605. Global.GetModuleName(module.module, n);
  7606. END;
  7607. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7608. (*type.typeDeclaration.GetName(n);*)
  7609. PushConstString(n);
  7610. (* push cellnet boolean *)
  7611. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7612. (* push engine boolean *)
  7613. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7614. (* allocate *)
  7615. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7616. (* add capabilities *)
  7617. modifier := p0(SyntaxTree.Designator).modifiers;
  7618. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7619. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7620. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7621. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7622. END;
  7623. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7624. (*
  7625. modifier := baseType(SyntaxTree.CellType).modifiers;
  7626. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7627. modifier := p0(SyntaxTree.Designator).modifiers;
  7628. *)
  7629. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7630. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7631. (* l.op contains address of pointer to record *)
  7632. ToMemory(l.op,addressType,0);
  7633. (* l.op contains value of pointer to record *)
  7634. Emit(Push(position,l.op)); (* address for use after syscall *)
  7635. ReleaseOperand(l);
  7636. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7637. prevScope := currentScope;
  7638. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7639. previous := section;
  7640. section := init;
  7641. (* add ports *)
  7642. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7643. CloseInitializer(previous);
  7644. currentScope := prevScope;
  7645. Symbol(temporaryVariable,l);
  7646. ToMemory(l.op,addressType,0);
  7647. Emit(Push(position,l.op));
  7648. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7649. (*
  7650. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7651. IF constructor # NIL THEN
  7652. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7653. FOR i := 1 TO x.parameters.Length()-1 DO
  7654. p := x.parameters.GetExpression(i);
  7655. Global.GetSymbolName(parameter,name);
  7656. Evaluate(p, value);
  7657. ASSERT(value.type # NIL);
  7658. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7659. par := instance.AddParameter(name);
  7660. par.SetInteger(value.integer);
  7661. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7662. par := instance.AddParameter(name);
  7663. par.SetBoolean(value.boolean);
  7664. ELSE Error(x.position,NotYetImplemented)
  7665. END;
  7666. parameter := parameter.nextParameter
  7667. END;
  7668. END;
  7669. *)
  7670. (* call initializer *)
  7671. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7672. IF constructor # NIL THEN
  7673. (*! should be unified with ProcedureCallDesignator *)
  7674. IF backend.cellsAreObjects THEN
  7675. Symbol(temporaryVariable,l);
  7676. ToMemory(l.op,addressType,0);
  7677. Emit(Push(position,l.op));
  7678. ReleaseOperand(l);
  7679. END;
  7680. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7681. FOR i := firstPar TO x.parameters.Length()-1 DO
  7682. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7683. formalParameter := formalParameter.nextParameter;
  7684. END;
  7685. (* static call of the constructor *)
  7686. Global.GetSymbolSegmentedName(constructor,name);
  7687. ASSERT(~constructor.isInline);
  7688. IF constructor.scope.ownerModule # module.module THEN
  7689. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7690. ELSE
  7691. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7692. END;
  7693. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7694. (*ELSE
  7695. ReleaseIntermediateOperand(pointer);*)
  7696. END;
  7697. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7698. ToMemory(l.op, addressType, 0);
  7699. Designate(p0,s0);
  7700. ToMemory(s0.op,addressType,0);
  7701. Emit(Mov(position,s0.op,l.op));
  7702. ReleaseOperand(l);
  7703. ReleaseOperand(s0);
  7704. result.tag := emptyOperand;
  7705. (* start *)
  7706. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7707. (* push cell *)
  7708. Symbol(temporaryVariable, l);
  7709. ToMemory(l.op,addressType,0);
  7710. Emit(Push(Basic.invalidPosition,l.op));
  7711. (* push delegate *)
  7712. Emit(Push(Basic.invalidPosition,l.op));
  7713. ReleaseOperand(l);
  7714. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7715. Emit(Push(position, s1.op));
  7716. ReleaseOperand(s1);
  7717. CallThis(position,"ActiveCellsRuntime","Start",3);
  7718. END;
  7719. (*IF temporaryVariable # NIL THEN
  7720. end := NewLabel();
  7721. BrL(end);
  7722. SetLabel(exit);
  7723. Designate(p0,l);
  7724. ToMemory(l.op,addressType,0);
  7725. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7726. ReleaseOperand(l);
  7727. SetLabel(end);
  7728. ELSE
  7729. SetLabel(exit);
  7730. END;
  7731. *)
  7732. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7733. ELSE (* no pointer to record, no pointer to array *)
  7734. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7735. (* ignore new statement *)
  7736. Warning(p0.position, "cannot run on final hardware");
  7737. ELSE
  7738. HALT(200);
  7739. END;
  7740. END;
  7741. (* ---- ADDRESSOF----- *)
  7742. |Global.systemAdr:
  7743. Designate(p0,s0);
  7744. s0.mode := ModeValue;
  7745. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7746. ReleaseIntermediateOperand(s0.op);
  7747. s0.op := s0.tag;
  7748. IntermediateCode.InitOperand(s0.tag);
  7749. END;
  7750. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7751. result := s0;
  7752. (* ---- BIT ----- *)
  7753. |Global.systemBit:
  7754. Evaluate(p0,s0);
  7755. ToMemory(s0.op,addressType,0);
  7756. ReuseCopy(res,s0.op);
  7757. ReleaseOperand(s0);
  7758. Evaluate(p1,s1);
  7759. Emit(Ror(position,res,res,s1.op));
  7760. ReleaseOperand(s1);
  7761. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7762. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7763. IF ~conditional THEN
  7764. InitOperand(result,ModeValue); result.op := res;
  7765. ELSE
  7766. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7767. BrL(falseLabel);
  7768. ReleaseIntermediateOperand(res);
  7769. END;
  7770. (* --- MSK ----*)
  7771. |Global.systemMsk:
  7772. Evaluate(p0,s0);
  7773. Evaluate(p1,s1);
  7774. ReuseCopy(res,s0.op);
  7775. ReleaseOperand(s0);
  7776. Emit(And(position,res,res,s1.op));
  7777. ReleaseOperand(s1);
  7778. InitOperand(result,ModeValue);
  7779. result.op := res;
  7780. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7781. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7782. Evaluate(p0,s0);
  7783. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7784. ReleaseOperand(s0);
  7785. InitOperand(result,ModeValue);
  7786. result.op := res;
  7787. (* ---- SYSTEM.GetStackPointer ----- *)
  7788. |Global.systemGetStackPointer:
  7789. InitOperand(result,ModeValue);
  7790. result.op := sp;
  7791. (* ---- SYSTEM.GetFramePointer ----- *)
  7792. |Global.systemGetFramePointer:
  7793. InitOperand(result,ModeValue);
  7794. result.op := fp;
  7795. (* ---- SYSTEM.GetActivity ----- *)
  7796. |Global.systemGetActivity:
  7797. ASSERT(backend.cooperative);
  7798. InitOperand(result,ModeValue);
  7799. result.op := ap;
  7800. (* ---- SYSTEM.SetStackPointer ----- *)
  7801. |Global.systemSetStackPointer:
  7802. Evaluate(p0,s0); (* *)
  7803. Emit(Mov(position,sp,s0.op));
  7804. ReleaseOperand(s0);
  7805. (* ---- SYSTEM.SetFramePointer ----- *)
  7806. |Global.systemSetFramePointer:
  7807. Evaluate(p0,s0); (* *)
  7808. Emit(Mov(position,fp,s0.op));
  7809. ReleaseOperand(s0);
  7810. (* ---- SYSTEM.Activity ----- *)
  7811. |Global.systemSetActivity:
  7812. ASSERT(backend.cooperative);
  7813. Evaluate(p0,s0); (* *)
  7814. Emit(Mov(position,ap,s0.op));
  7815. ReleaseOperand(s0);
  7816. (* ---- SYSTEM.VAL ----- *)
  7817. |Global.systemVal:
  7818. Expression(p1);
  7819. s1 := result;
  7820. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7821. IF s1.mode = ModeReference THEN
  7822. (* nothing to be done if not record type, just take over new type *)
  7823. IF (type IS SyntaxTree.RecordType) THEN
  7824. ReleaseIntermediateOperand(s1.tag);
  7825. s1.tag := TypeDescriptorAdr(type);
  7826. IF ~newObjectFile THEN
  7827. IntermediateCode.MakeMemory(s1.tag,addressType);
  7828. END;
  7829. UseIntermediateOperand(s1.tag);
  7830. END;
  7831. result := s1;
  7832. ELSE (* copy over result to different type, may not use convert *)
  7833. itype := IntermediateCode.GetType(system,type);
  7834. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7835. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7836. Emit(Mov(position,s0.op,s1.op));
  7837. ReleaseOperand(s1);
  7838. InitOperand(result,ModeValue);
  7839. result.op := s0.op;
  7840. ELSE (* different size, must convert *)
  7841. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7842. Convert(s1.op, IntermediateCode.GetType(system,type));
  7843. result := s1;
  7844. END;
  7845. END;
  7846. (* ---- SYSTEM.GET ----- *)
  7847. |Global.systemGet:
  7848. Evaluate(p0,s0); (* adr *)
  7849. Designate(p1,s1); (* variable *)
  7850. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7851. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7852. Emit(Mov(position,s1.op,s0.op));
  7853. ReleaseOperand(s1);
  7854. ReleaseOperand(s0);
  7855. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7856. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7857. Evaluate(p0,s0); (* *)
  7858. Evaluate(p1,s1); (* variable *)
  7859. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7860. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7861. (* real part *)
  7862. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7863. Emit(Mov(position,res, s1.op));
  7864. ReleaseIntermediateOperand(res);
  7865. (* imaginary part *)
  7866. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7867. Emit(Mov(position,res, s1.tag));
  7868. ReleaseIntermediateOperand(res);
  7869. ReleaseOperand(s1);
  7870. ReleaseOperand(s0);
  7871. ELSE
  7872. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7873. ReleaseOperand(s0);
  7874. Emit(Mov(position,res,s1.op));
  7875. ReleaseIntermediateOperand(res);
  7876. ReleaseOperand(s1);
  7877. END;
  7878. (* ---- SYSTEM.MOVE ----- *)
  7879. |Global.systemMove:
  7880. Evaluate(p0,s0);
  7881. Evaluate(p1,s1);
  7882. Evaluate(p2,s2);
  7883. Emit(Copy(position,s1.op,s0.op,s2.op));
  7884. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7885. (* ---- SYSTEM.NEW ----- *)
  7886. |Global.systemNew:
  7887. Designate(p0,s0);
  7888. Emit(Push(position,s0.op));
  7889. ReleaseOperand(s0);
  7890. Evaluate(p1,s1);
  7891. Emit(Push(position,s1.op));
  7892. ReleaseOperand(s1);
  7893. (* push realtime flag: false by default *)
  7894. Emit(Push(position,false));
  7895. CallThis(position,"Heaps","NewSys",3);
  7896. (* ---- SYSTEM.CALL ----- *)
  7897. |Global.systemRef:
  7898. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7899. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7900. s0.mode := ModeValue;
  7901. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7902. result := s0
  7903. (* ---- INCR ----- *)
  7904. |Global.Incr:
  7905. Designate(p0,operand);
  7906. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7907. Dereference(operand,p0.type.resolved,FALSE);
  7908. END;
  7909. ASSERT(p1 # NIL);
  7910. Evaluate(p1,l);
  7911. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7912. ReleaseOperand(operand); ReleaseOperand(l);
  7913. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7914. (* ---- SUM ----- *)
  7915. |Global.Sum: HALT(200);
  7916. (* ---- ALL ----- *)
  7917. |Global.All: HALT(200);
  7918. (* ---- CAS ----- *)
  7919. |Global.Cas:
  7920. needsTrace := p0.NeedsTrace();
  7921. IF needsTrace THEN ModifyAssignments(true) END;
  7922. Designate(p0,s0);
  7923. Evaluate(p1,s1);
  7924. Evaluate(p2,s2);
  7925. IF needsTrace THEN
  7926. Emit(Push(position, s0.op));
  7927. Emit(Push(position, s1.op));
  7928. Emit(Push(position, s2.op));
  7929. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7930. ELSE
  7931. Emit(Cas(position,s0.op,s1.op,s2.op));
  7932. END;
  7933. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7934. IF needsTrace THEN ModifyAssignments(false) END;
  7935. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7936. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7937. IF conditional THEN
  7938. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7939. BrL(falseLabel);
  7940. ReleaseIntermediateOperand(res);
  7941. END;
  7942. (* ---- DIM ----- *)
  7943. |Global.Dim:
  7944. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7945. Designate(p0,s0);
  7946. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7947. Dereference(s0,p0.type.resolved,FALSE);
  7948. END;
  7949. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7950. ReleaseOperand(s0);
  7951. (* ---- RESHAPE ----- *)
  7952. |Global.Reshape:
  7953. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7954. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7955. left.SetType(procedure.type);
  7956. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7957. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7958. END;
  7959. (* ---- SYSTEM.TYPECODE ----- *)
  7960. |Global.systemTypeCode:
  7961. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7962. IF type.resolved IS SyntaxTree.PointerType THEN
  7963. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7964. END;
  7965. result.op := TypeDescriptorAdr(type);
  7966. IF ~newObjectFile THEN
  7967. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7968. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7969. END;
  7970. result.mode := ModeValue;
  7971. (* ---- SYSTEM.TRACE ----- *)
  7972. |Global.systemTrace:
  7973. SystemTrace(x.parameters, x.position);
  7974. (* ----- CONNECT ------*)
  7975. |Global.Connect:
  7976. IF backend.cellsAreObjects THEN
  7977. PushPort(p0);
  7978. PushPort(p1);
  7979. IF p2 # NIL THEN
  7980. Evaluate(p2, s2);
  7981. Emit(Push(p2.position, s2.op));
  7982. ReleaseOperand(s2);
  7983. ELSE
  7984. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7985. END;
  7986. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7987. ELSE
  7988. Warning(x.position, "cannot run on final hardware");
  7989. END;
  7990. (* ----- DELEGATE ------*)
  7991. |Global.Delegate:
  7992. IF backend.cellsAreObjects THEN
  7993. PushPort(p0);
  7994. PushPort(p1);
  7995. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7996. ELSE
  7997. Warning(x.position, "cannot run on final hardware");
  7998. END;
  7999. (* ----- SEND ------*)
  8000. |Global.Send:
  8001. Evaluate(p0,s0);
  8002. Evaluate(p1,s1);
  8003. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8004. Emit(Push(position,s0.op));
  8005. Emit(Push(position,s1.op));
  8006. (*
  8007. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8008. *)
  8009. IF ~backend.cellsAreObjects THEN
  8010. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8011. END;
  8012. ReleaseOperand(s0);
  8013. ReleaseOperand(s1);
  8014. IF backend.cellsAreObjects THEN
  8015. CallThis(position,"ActiveCellsRuntime","Send",2);
  8016. ELSE
  8017. CallThis(position,ChannelModuleName,"Send",2);
  8018. END;
  8019. (* ----- RECEIVE ------*)
  8020. |Global.Receive:
  8021. Evaluate(p0,s0);
  8022. Emit(Push(position,s0.op));
  8023. Designate(p1,s1);
  8024. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8025. Emit(Push(position,s1.op));
  8026. IF p2 # NIL THEN
  8027. Designate(p2,s2);
  8028. Emit(Push(position,s2.op));
  8029. END;
  8030. (*
  8031. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8032. *)
  8033. IF ~backend.cellsAreObjects THEN
  8034. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8035. END;
  8036. ReleaseOperand(s0);
  8037. ReleaseOperand(s1);
  8038. ReleaseOperand(s2);
  8039. IF backend.cellsAreObjects THEN
  8040. IF p2 = NIL THEN
  8041. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8042. ELSE
  8043. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8044. END;
  8045. ELSE
  8046. IF p2 = NIL THEN
  8047. CallThis(position,ChannelModuleName,"Receive",2)
  8048. ELSE
  8049. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8050. END;
  8051. END;
  8052. | Global.systemSpecial:
  8053. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8054. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8055. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8056. (* determine if parameters are of the VAR kind *)
  8057. ASSERT(x.parameters.Length() <= 3);
  8058. formalParameter := procedureType.firstParameter;
  8059. FOR i := 0 TO x.parameters.Length() - 1 DO
  8060. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8061. formalParameter := formalParameter.nextParameter
  8062. END;
  8063. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8064. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8065. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8066. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8067. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8068. IF procedureType.returnType # NIL THEN
  8069. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8070. Emit(Result(position, res));
  8071. (*InitOperand(result,ModeValue);
  8072. result.op := res;
  8073. *)
  8074. IF ~conditional THEN
  8075. InitOperand(result,ModeValue); result.op := res;
  8076. ELSE
  8077. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8078. BrL(falseLabel);
  8079. ReleaseIntermediateOperand(res);
  8080. END;
  8081. END
  8082. ELSE (* function not yet implemented *)
  8083. Error(position,"not yet implemented");
  8084. END;
  8085. destination := dest;
  8086. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8087. END VisitBuiltinCallDesignator;
  8088. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8089. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8090. BEGIN
  8091. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8092. dest := destination; destination := emptyOperand;
  8093. Expression(x.left);
  8094. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8095. ELSIF isUnchecked THEN (* no check *)
  8096. ELSE
  8097. trueL := NewLabel();
  8098. falseL := NewLabel();
  8099. IF IsPointerToRecord(x.left.type,recordType) THEN
  8100. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8101. Emit(Mov(position,tag, result.op));
  8102. IF result.mode # ModeValue THEN
  8103. ptr := tag;
  8104. IntermediateCode.MakeMemory(ptr,addressType);
  8105. Emit(Mov(position,tag, ptr));
  8106. END;
  8107. IF ~backend.cooperative THEN
  8108. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8109. END;
  8110. IntermediateCode.MakeMemory(tag,addressType);
  8111. ELSE
  8112. tag := result.tag;
  8113. UseIntermediateOperand(tag);
  8114. END;
  8115. TypeTest(tag,x.type,trueL,falseL);
  8116. ReleaseIntermediateOperand(tag);
  8117. SetLabel(falseL);
  8118. EmitTrap(position,TypeCheckTrap);
  8119. SetLabel(trueL);
  8120. END;
  8121. destination := dest;
  8122. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8123. END VisitTypeGuardDesignator;
  8124. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8125. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8126. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8127. VAR label: Label; pc: LONGINT;
  8128. BEGIN
  8129. IF backend.cooperative & ~isUnchecked THEN
  8130. pc := section.pc;
  8131. label := NewLabel();
  8132. BrneL(label, operand.op, nil);
  8133. EmitTrap(position, NilPointerTrap);
  8134. SetLabel(label);
  8135. INC(statCoopNilCheck, section.pc - pc);
  8136. END;
  8137. END NilCheck;
  8138. BEGIN
  8139. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8140. ReuseCopy(dereferenced,operand.op);
  8141. ReleaseOperand(operand);
  8142. operand.mode := ModeReference;
  8143. operand.op := dereferenced;
  8144. operand.tag := dereferenced;
  8145. UseIntermediateOperand(operand.tag);
  8146. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8147. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8148. ReleaseIntermediateOperand(operand.tag);
  8149. operand.tag := TypeDescriptorAdr(type);
  8150. IF ~newObjectFile THEN
  8151. IntermediateCode.MakeMemory(operand.tag,addressType);
  8152. END;
  8153. ELSE
  8154. IF ~backend.cooperative THEN
  8155. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8156. END;
  8157. IntermediateCode.MakeMemory(operand.tag,addressType);
  8158. END;
  8159. NilCheck(operand.op);
  8160. ELSIF type IS SyntaxTree.ArrayType THEN
  8161. IF isUnsafe THEN
  8162. NilCheck(operand.op);
  8163. ReleaseIntermediateOperand(operand.tag);
  8164. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8165. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8166. ELSE
  8167. operand.tag := emptyOperand;
  8168. END;
  8169. ELSE
  8170. NilCheck(operand.op);
  8171. IF backend.cooperative THEN
  8172. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8173. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8174. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8175. ELSE
  8176. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8177. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8178. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8179. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8180. END;
  8181. END;
  8182. ELSIF type IS SyntaxTree.MathArrayType THEN
  8183. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8184. IntermediateCode.MakeMemory(operand.op,addressType);
  8185. ELSE HALT(100);
  8186. END;
  8187. END Dereference;
  8188. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8189. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8190. BEGIN
  8191. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8192. dest := destination; destination := emptyOperand;
  8193. Evaluate(x.left,d);
  8194. type := x.type.resolved;
  8195. prevIsUnchecked := isUnchecked;
  8196. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8197. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8198. END;
  8199. Dereference(d,type,IsUnsafePointer(x.left.type));
  8200. isUnchecked := prevIsUnchecked;
  8201. result := d;
  8202. 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
  8203. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8204. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8205. END;
  8206. END;
  8207. destination := dest;
  8208. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8209. END VisitDereferenceDesignator;
  8210. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8211. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8212. BEGIN
  8213. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8214. dest := destination; destination := emptyOperand;
  8215. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8216. tag := result.op;
  8217. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8218. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8219. StaticCallOperand(result,procedure.super);
  8220. ReleaseIntermediateOperand(result.tag);
  8221. UseIntermediateOperand(tag); (* necessary ? *)
  8222. result.tag := tag;
  8223. destination := dest;
  8224. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8225. END VisitSupercallDesignator;
  8226. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8227. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8228. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8229. name: Basic.SegmentedName;
  8230. procedure: SyntaxTree.Procedure;
  8231. procedureType: SyntaxTree.ProcedureType;
  8232. BEGIN
  8233. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8234. dest := destination; destination := emptyOperand;
  8235. scope := currentScope;
  8236. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8237. scope := scope.outerScope;
  8238. END;
  8239. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8240. IF newObjectFile THEN
  8241. moduleSection := meta.ModuleSection();
  8242. IF backend.cooperative THEN
  8243. moduleOffset := 0;
  8244. ELSE
  8245. moduleOffset := moduleSection.pc;
  8246. END;
  8247. result.mode := ModeValue;
  8248. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8249. ELSE
  8250. Symbol(moduleSelf,result);
  8251. IntermediateCode.MakeMemory(result.op,addressType);
  8252. END
  8253. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8254. result.mode := ModeValue;
  8255. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8256. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8257. ELSE
  8258. GetBaseRegister(basereg,currentScope,scope);
  8259. InitOperand(result,ModeReference);
  8260. result.op := basereg;
  8261. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8262. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8263. parametersSize := ProcedureParametersSize(system,procedure);
  8264. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8265. IF backend.cooperative THEN
  8266. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8267. END;
  8268. (* tag must be loaded when dereferencing SELF pointer *)
  8269. END;
  8270. destination := dest;
  8271. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8272. END VisitSelfDesignator;
  8273. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8274. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8275. BEGIN
  8276. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8277. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8278. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8279. parameter := procedureType.returnParameter;
  8280. VisitParameter(parameter);
  8281. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8282. END VisitResultDesignator;
  8283. (** values *)
  8284. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8285. BEGIN
  8286. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8287. IF conditional THEN
  8288. IF x.value THEN BrL(trueLabel)
  8289. ELSE BrL(falseLabel)
  8290. END;
  8291. ELSE
  8292. InitOperand(result,ModeValue);
  8293. IF x.value THEN result.op := true ELSE result.op := false END;
  8294. END;
  8295. END VisitBooleanValue;
  8296. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8297. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8298. BEGIN
  8299. Global.GetModuleSegmentedName(module.module, name);
  8300. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8301. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8302. RETURN section
  8303. END GetDataSection;
  8304. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8305. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8306. BEGIN
  8307. type := vop.type;
  8308. data := GetDataSection();
  8309. pc := EnterImmediate(data,vop);
  8310. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8311. IntermediateCode.MakeMemory(vop, type);
  8312. END GetImmediateMem;
  8313. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8314. BEGIN
  8315. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8316. InitOperand(result,ModeValue);
  8317. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8318. IF ~supportedImmediate(result.op) &~inData THEN
  8319. GetImmediateMem(result.op)
  8320. END;
  8321. END VisitIntegerValue;
  8322. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8323. BEGIN
  8324. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8325. InitOperand(result,ModeValue);
  8326. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8327. END VisitCharacterValue;
  8328. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8329. BEGIN
  8330. IF Trace THEN TraceEnter("VisitSetValue") END;
  8331. InitOperand(result,ModeValue);
  8332. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8333. END VisitSetValue;
  8334. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8335. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8336. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8337. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8338. BEGIN
  8339. numberElements := x.elements.Length();
  8340. FOR i := 0 TO numberElements-1 DO
  8341. expression := x.elements.GetExpression(i);
  8342. IF expression IS SyntaxTree.MathArrayExpression THEN
  8343. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8344. ELSE
  8345. inData := TRUE;
  8346. Evaluate(expression,op);
  8347. irv.Emit(Data(position,op.op));
  8348. inData := FALSE;
  8349. ReleaseOperand(op);
  8350. END;
  8351. END;
  8352. END RecursiveData;
  8353. BEGIN
  8354. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8355. IF ~TryConstantDeclaration() THEN
  8356. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8357. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8358. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8359. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8360. ELSE
  8361. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8362. END;
  8363. RecursiveData(x.array);
  8364. InitOperand(result,ModeReference);
  8365. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8366. END
  8367. END VisitMathArrayValue;
  8368. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8369. VAR constant: Sections.Section;
  8370. BEGIN
  8371. IF constantDeclaration = NIL THEN
  8372. RETURN FALSE
  8373. ELSE
  8374. (* Is a constant in this module: did we generate it already? *)
  8375. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8376. IF constant # NIL THEN
  8377. InitOperand(result,ModeReference);
  8378. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8379. RETURN TRUE;
  8380. END;
  8381. END;
  8382. RETURN FALSE
  8383. END TryConstantDeclaration;
  8384. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8385. BEGIN
  8386. constantDeclaration := x;
  8387. x.value.resolved.Accept(SELF);
  8388. END VisitConstant;
  8389. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8390. BEGIN
  8391. IF Trace THEN TraceEnter("VisitRealValue") END;
  8392. InitOperand(result,ModeValue);
  8393. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8394. END VisitRealValue;
  8395. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8396. VAR
  8397. componentType: SyntaxTree.Type;
  8398. BEGIN
  8399. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8400. ASSERT(x.type IS SyntaxTree.ComplexType);
  8401. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8402. InitOperand(result,ModeValue);
  8403. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8404. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8405. END VisitComplexValue;
  8406. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8407. VAR i: LONGINT; name: Basic.SegmentedName;
  8408. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8409. BEGIN
  8410. IF Trace THEN TraceEnter("VisitStringValue") END;
  8411. IF ~TryConstantDeclaration() THEN
  8412. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8413. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8414. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8415. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8416. ELSE
  8417. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8418. END;
  8419. FOR i := 0 TO x.length-1 DO
  8420. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8421. irv.Emit(Data(position,op));
  8422. END;
  8423. InitOperand(result,ModeReference);
  8424. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8425. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8426. END
  8427. END VisitStringValue;
  8428. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8429. BEGIN
  8430. IF Trace THEN TraceEnter("VisitNilValue") END;
  8431. InitOperand(result,ModeValue);
  8432. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8433. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8434. END VisitNilValue;
  8435. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8436. BEGIN
  8437. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8438. InitOperand(result,ModeValue);
  8439. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8440. END VisitEnumerationValue;
  8441. (** symbols *)
  8442. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8443. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8444. END VisitImport;
  8445. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8446. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8447. BEGIN
  8448. IF scope # baseScope THEN
  8449. (* left := [fp+8] *)
  8450. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8451. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8452. ReuseCopy(left,right);
  8453. ReleaseIntermediateOperand(right);
  8454. scope := scope.outerScope; DEC(level);
  8455. (* { left := [left+8] } *)
  8456. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8457. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8458. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8459. Emit(Mov(position,left,right));
  8460. scope := scope.outerScope; DEC(level);
  8461. END;
  8462. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8463. result := left;
  8464. ELSE
  8465. result := fp;
  8466. END;
  8467. END GetBaseRegister;
  8468. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8469. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8470. BEGIN
  8471. IF Trace THEN TraceEnter("VisitVariable"); END;
  8472. type := x.type.resolved;
  8473. IF (x.useRegister) THEN
  8474. InitOperand(result, ModeValue);
  8475. IF x.registerNumber < 0 THEN
  8476. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8477. reg := x.registerNumber;
  8478. ELSE
  8479. reg := registerUsageCount.Map(x.registerNumber);
  8480. UseRegister(reg);
  8481. END;
  8482. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8483. ELSIF x.externalName # NIL THEN
  8484. InitOperand(result,ModeReference);
  8485. Basic.ToSegmentedName(x.externalName^, name);
  8486. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8487. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8488. InitOperand(result,ModeReference);
  8489. GetBaseRegister(result.op,currentScope,x.scope);
  8490. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8491. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8492. InitOperand(result,ModeReference);
  8493. GetCodeSectionNameForSymbol(x,name);
  8494. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8495. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8496. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8497. InitOperand(result,ModeReference);
  8498. GetCodeSectionNameForSymbol(x,name);
  8499. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8500. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8501. ELSE (* field, left designator must have been emitted *)
  8502. ASSERT(result.mode = ModeReference);
  8503. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8504. ReuseCopy(temp,result.op);
  8505. ReleaseIntermediateOperand(result.op);
  8506. result.op := temp;
  8507. END;
  8508. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8509. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8510. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8511. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8512. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8513. END;
  8514. END;
  8515. END;
  8516. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8517. ValueToCondition(result);
  8518. ELSIF type IS SyntaxTree.ProcedureType THEN
  8519. ReleaseIntermediateOperand(result.tag);
  8520. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8521. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8522. UseIntermediateOperand(result.tag);
  8523. ELSE
  8524. result.tag := nil; (* nil *)
  8525. END;
  8526. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8527. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8528. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8529. ReleaseIntermediateOperand(result.tag);
  8530. Global.GetSymbolSegmentedName(x,name);
  8531. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8532. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8533. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8534. ELSE
  8535. END;
  8536. ELSE
  8537. ReleaseIntermediateOperand(result.tag);
  8538. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8539. END;
  8540. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8541. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8542. ReleaseIntermediateOperand(result.tag);
  8543. result.tag := result.op;
  8544. UseIntermediateOperand(result.tag);
  8545. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8546. IntermediateCode.MakeMemory(result.op,addressType);
  8547. END;
  8548. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8549. ReleaseIntermediateOperand(result.tag);
  8550. result.tag := TypeDescriptorAdr(type);
  8551. IF ~newObjectFile THEN
  8552. IntermediateCode.MakeMemory(result.tag,addressType);
  8553. END;
  8554. UseIntermediateOperand(result.tag);
  8555. (* tag for pointer type computed not here but during dereferencing *)
  8556. END;
  8557. IF Trace THEN TraceExit("VisitVariable") END;
  8558. END VisitVariable;
  8559. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8560. BEGIN
  8561. VisitVariable(property);
  8562. END VisitProperty;
  8563. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8564. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8565. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8566. BEGIN
  8567. type := x.type.resolved;
  8568. IF Trace THEN TraceEnter("VisitParameter") END;
  8569. IF x.ownerType IS SyntaxTree.CellType THEN
  8570. ptype := x.type.resolved;
  8571. IF backend.cellsAreObjects THEN
  8572. ASSERT(result.mode = ModeReference);
  8573. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8574. ReuseCopy(temp,result.op);
  8575. ReleaseIntermediateOperand(result.op);
  8576. result.op := temp;
  8577. END;
  8578. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8579. RETURN;
  8580. ELSE
  8581. InitOperand(result,ModeReference);
  8582. GetCodeSectionNameForSymbol(x,name);
  8583. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8584. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8585. RETURN;
  8586. END;
  8587. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8588. InitOperand(result,ModeReference);
  8589. GetCodeSectionNameForSymbol(x,name);
  8590. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8591. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8592. RETURN
  8593. ELSE
  8594. GetBaseRegister(basereg,currentScope,x.scope);
  8595. InitOperand(result,ModeReference);
  8596. result.op := basereg;
  8597. END;
  8598. IF IsOpenArray(type) THEN
  8599. result.tag := basereg;
  8600. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8601. IntermediateCode.MakeMemory(result.op,addressType);
  8602. IF Global.IsOberonProcedure(x.ownerType) THEN
  8603. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8604. UseIntermediateOperand(result.tag);
  8605. ELSE
  8606. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8607. END;
  8608. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8609. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8610. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8611. ELSIF IsStaticArray(type) THEN
  8612. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8613. IntermediateCode.MakeMemory(result.op,addressType);
  8614. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8615. ELSIF type IS SyntaxTree.MathArrayType THEN
  8616. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8617. WITH type: SyntaxTree.MathArrayType DO
  8618. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8619. IF type.form = SyntaxTree.Tensor THEN
  8620. ELSIF type.form = SyntaxTree.Open THEN
  8621. result.tag := result.op;
  8622. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8623. IntermediateCode.MakeMemory(result.op,addressType);
  8624. UseIntermediateOperand(result.tag);
  8625. ELSIF type.form = SyntaxTree.Static THEN
  8626. IF x.kind = SyntaxTree.ConstParameter THEN
  8627. IntermediateCode.MakeMemory(result.op,addressType);
  8628. END;
  8629. ELSE HALT(100)
  8630. END;
  8631. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8632. IF type.form = SyntaxTree.Tensor THEN
  8633. ToMemory(result.op,addressType,0);
  8634. ELSIF type.form = SyntaxTree.Open THEN
  8635. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8636. ReuseCopy(result.tag, mem);
  8637. ReleaseIntermediateOperand(mem);
  8638. ReleaseIntermediateOperand(result.op);
  8639. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8640. ELSIF type.form = SyntaxTree.Static THEN
  8641. IntermediateCode.MakeMemory(result.op,addressType);
  8642. ELSE HALT(100)
  8643. END;
  8644. ELSE HALT(100)
  8645. END;
  8646. END;
  8647. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8648. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8649. IntermediateCode.MakeMemory(result.op,addressType);
  8650. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8651. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8652. END;
  8653. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8654. ValueToCondition(result);
  8655. ELSIF type IS SyntaxTree.ProcedureType THEN
  8656. ReleaseIntermediateOperand(result.tag);
  8657. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8658. IF x.kind = SyntaxTree.VarParameter THEN
  8659. ReuseCopy(result.tag,result.op);
  8660. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8661. IntermediateCode.MakeMemory(result.tag,addressType);
  8662. ELSE
  8663. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8664. UseIntermediateOperand(result.tag);
  8665. END;
  8666. ELSE
  8667. result.tag := nil;
  8668. END;
  8669. (* tag for pointer type computed not here but during dereferencing *)
  8670. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8671. ReleaseIntermediateOperand(result.tag);
  8672. result.tag := basereg;
  8673. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8674. IntermediateCode.MakeMemory(result.tag,addressType);
  8675. UseIntermediateOperand(result.tag);
  8676. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8677. ReleaseIntermediateOperand(result.tag);
  8678. result.tag := TypeDescriptorAdr(type);
  8679. IF ~newObjectFile THEN
  8680. IntermediateCode.MakeMemory(result.tag,addressType);
  8681. END;
  8682. UseIntermediateOperand(result.tag);
  8683. END;
  8684. IF Trace THEN TraceExit("VisitParameter") END;
  8685. END VisitParameter;
  8686. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8687. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8688. BEGIN
  8689. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8690. (* left.p: left already emitted *)
  8691. tag := result.op; (* value of pointer to left *)
  8692. (* get type desc *)
  8693. tmp := result.tag;
  8694. IntermediateCode.MakeMemory(tmp,addressType);
  8695. (* get method adr *)
  8696. Reuse1(reg,tmp);
  8697. ReleaseIntermediateOperand(tmp);
  8698. IF backend.cooperative THEN
  8699. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8700. WHILE recordType.baseType # NIL DO
  8701. recordType := recordType.GetBaseRecord ();
  8702. END;
  8703. GetRecordTypeName (recordType,name);
  8704. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8705. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8706. offset := 0;
  8707. ELSE
  8708. offset := 2;
  8709. END;
  8710. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8711. ELSE
  8712. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8713. END;
  8714. InitOperand(operand,ModeReference);
  8715. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8716. operand.op := reg;
  8717. operand.tag := tag;
  8718. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8719. END DynamicCallOperand;
  8720. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8721. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8722. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8723. BEGIN
  8724. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8725. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8726. tag := operand.op;
  8727. ReleaseIntermediateOperand(operand.tag);
  8728. ELSE tag := nil
  8729. END;
  8730. IF x.isInline THEN
  8731. sectionType := Sections.InlineCodeSection;
  8732. ELSE
  8733. sectionType := Sections.CodeSection;
  8734. END;
  8735. IF x.externalName # NIL THEN
  8736. Basic.ToSegmentedName(x.externalName^, name);
  8737. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8738. ELSE
  8739. GetCodeSectionNameForSymbol(x, name);
  8740. IF (x.scope.ownerModule = module.module) THEN
  8741. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8742. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8743. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8744. IF source.pc = 0 THEN (* no code yet *)
  8745. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8746. END;
  8747. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8748. bits := x.procedureScope.body.code.inlineCode;
  8749. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8750. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8751. binary.CopyBits(bits, 0, bits.GetSize());
  8752. source.SetResolved(binary);
  8753. ELSE
  8754. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8755. END;
  8756. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8757. END;
  8758. InitOperand(operand,ModeValue);
  8759. operand.op := reg;
  8760. operand.tag := tag;
  8761. IF Trace THEN TraceExit("StaticCallOperand") END;
  8762. END StaticCallOperand;
  8763. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8764. (* handle expressions of the form designator.procedure or procedure *)
  8765. BEGIN
  8766. IF Trace THEN TraceEnter("VisitProcedure") END;
  8767. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8768. DynamicCallOperand(result,x);
  8769. ELSIF x.isInline THEN
  8770. StaticCallOperand(result,x);
  8771. ELSE
  8772. StaticCallOperand(result,x);
  8773. END;
  8774. IF Trace THEN TraceExit("VisitProcedure") END;
  8775. END VisitProcedure;
  8776. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8777. BEGIN
  8778. VisitProcedure(x);
  8779. END VisitOperator;
  8780. (** statements *)
  8781. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8782. BEGIN
  8783. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8784. Expression(x.call);
  8785. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8786. ReleaseOperand(result)
  8787. END;
  8788. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8789. END VisitProcedureCallStatement;
  8790. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8791. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8792. leftBase, rightBase: SyntaxTree.Type;
  8793. procedureName,s: SyntaxTree.IdentifierString;
  8794. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8795. size: LONGINT; rightKind: LONGINT;
  8796. dummy: IntermediateCode.Operand;
  8797. CONST moduleName = "FoxArrayBase";
  8798. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8799. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8800. BEGIN
  8801. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8802. IF base IS SyntaxTree.MathArrayType THEN
  8803. base := OpenArray(base(SyntaxTree.MathArrayType));
  8804. END;
  8805. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8806. result.SetArrayBase(base);
  8807. RETURN result
  8808. END OpenArray;
  8809. BEGIN
  8810. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8811. SaveRegisters();ReleaseUsedRegisters(saved);
  8812. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8813. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8814. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8815. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8816. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8817. IF leftType.form = SyntaxTree.Tensor THEN
  8818. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8819. ELSIF leftType.form = SyntaxTree.Open THEN
  8820. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8821. ELSIF leftType.form = SyntaxTree.Static THEN
  8822. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8823. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8824. END;
  8825. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8826. IF procedure = NIL THEN
  8827. s := "Instruction not supported on target, emulation procedure ";
  8828. Strings.Append(s,moduleName);
  8829. Strings.Append(s,".");
  8830. Strings.Append(s,procedureName);
  8831. Strings.Append(s," not present");
  8832. Error(position,s);
  8833. ELSE
  8834. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8835. parameter.SetType(leftType);
  8836. parameter.SetAccess(SyntaxTree.Internal);
  8837. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8838. parameter.SetKind(rightKind);
  8839. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8840. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8841. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8842. StaticCallOperand(result,procedure);
  8843. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8844. ReleaseOperand(result);
  8845. END;
  8846. RestoreRegisters(saved);
  8847. END;
  8848. END AssignMathArray;
  8849. VAR modifyAssignmentCounter := 0: LONGINT;
  8850. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8851. VAR processor,mem,dst: IntermediateCode.Operand;
  8852. BEGIN
  8853. IF value.intValue = true.intValue THEN
  8854. INC(modifyAssignmentCounter);
  8855. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8856. modifyAssignmentsPC := section.pc;
  8857. ELSE
  8858. DEC(modifyAssignmentCounter);
  8859. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8860. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8861. END;
  8862. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8863. dst := NewRegisterOperand (addressType);
  8864. Emit(Mov(position,dst, processor));
  8865. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8866. Emit(Mov(position,mem, value));
  8867. ReleaseIntermediateOperand(dst);
  8868. END ModifyAssignments;
  8869. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8870. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8871. BEGIN
  8872. type := left.type.resolved;
  8873. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8874. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8875. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8876. IF procedureType.returnParameter = parameter THEN
  8877. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8878. END;
  8879. END;
  8880. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8881. END CopySize;
  8882. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8883. VAR
  8884. leftO, rightO: Operand;
  8885. mem, sizeOp: IntermediateCode.Operand;
  8886. leftType, rightType, componentType: SyntaxTree.Type;
  8887. size: LONGINT;
  8888. parameters: SyntaxTree.ExpressionList;
  8889. procedure: SyntaxTree.Procedure;
  8890. call: SyntaxTree.ProcedureCallDesignator;
  8891. designator: SyntaxTree.Designator;
  8892. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8893. VAR procedureType: SyntaxTree.ProcedureType;
  8894. BEGIN
  8895. IF ReturnedAsParameter(right.type) THEN
  8896. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8897. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8898. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8899. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8900. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8901. IF right.id = Global.Reshape THEN RETURN TRUE
  8902. END;
  8903. END;
  8904. END;
  8905. END;
  8906. RETURN FALSE
  8907. END CanPassAsResultParameter;
  8908. BEGIN
  8909. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8910. leftType := left.type.resolved; rightType:= right.type.resolved;
  8911. IF backend.cooperative & left.NeedsTrace() THEN
  8912. ModifyAssignments(true);
  8913. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8914. Designate(right, rightO);
  8915. Designate(left, leftO);
  8916. ASSERT(leftO.mode = ModeReference);
  8917. TransferToRegister(leftO.op, leftO.op);
  8918. TransferToRegister(rightO.op, rightO.op);
  8919. Emit(Push(position, leftO.op));
  8920. Emit(Push(position, rightO.op));
  8921. CallAssignMethod(leftO.op, rightO.op, left.type);
  8922. Emit(Pop(position, rightO.op));
  8923. Emit(Pop(position, leftO.op));
  8924. sizeOp := CopySize(left);
  8925. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8926. ReleaseOperand(leftO);
  8927. ReleaseOperand(rightO);
  8928. ELSE
  8929. Evaluate(right,rightO);
  8930. Designate(left,leftO);
  8931. ASSERT(leftO.mode = ModeReference);
  8932. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8933. (* copy procedure address first *)
  8934. MakeMemory(mem,leftO.op,addressType,0);
  8935. Emit(Mov(position,mem,rightO.op));
  8936. ReleaseIntermediateOperand(mem);
  8937. (* copy pointer address *)
  8938. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8939. CallAssignPointer(leftO.tag, rightO.tag);
  8940. ELSE
  8941. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8942. CallAssignPointer(leftO.op, rightO.op);
  8943. END;
  8944. ReleaseOperand(leftO);
  8945. ReleaseOperand(rightO);
  8946. END;
  8947. ModifyAssignments(false);
  8948. RETURN;
  8949. END;
  8950. IF CanPassAsResultParameter(right) THEN
  8951. procedureResultDesignator := left(SyntaxTree.Designator);
  8952. Expression(right);
  8953. procedureResultDesignator := NIL;
  8954. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8955. (* left <-- ALIAS OF right: zerocopy *)
  8956. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8957. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8958. designator.SetType(procedure.type);
  8959. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8960. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8961. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8962. END;
  8963. ELSIF leftType IS SyntaxTree.RangeType THEN
  8964. (* LHS is of array range type *)
  8965. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8966. Evaluate(right, rightO);
  8967. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8968. (* first *)
  8969. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8970. Emit(Mov(position,mem, rightO.op));
  8971. ReleaseIntermediateOperand(mem);
  8972. (* last *)
  8973. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8974. Emit(Mov(position,mem, rightO.tag));
  8975. ReleaseIntermediateOperand(mem);
  8976. (* step *)
  8977. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8978. Emit(Mov(position,mem, rightO.extra));
  8979. ReleaseIntermediateOperand(mem);
  8980. ReleaseOperand(rightO);
  8981. ReleaseOperand(leftO)
  8982. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8983. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8984. Evaluate(right, rightO);
  8985. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8986. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8987. (* real part *)
  8988. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8989. Emit(Mov(position,mem, rightO.op));
  8990. ReleaseIntermediateOperand(mem);
  8991. (* imaginary part *)
  8992. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8993. Emit(Mov(position,mem, rightO.tag));
  8994. ReleaseIntermediateOperand(mem);
  8995. ReleaseOperand(rightO);
  8996. ReleaseOperand(leftO)
  8997. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8998. OR (leftType IS SyntaxTree.PortType) THEN
  8999. (* rightO := leftO;*)
  9000. Evaluate(right,rightO);
  9001. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9002. Designate(left,leftO);
  9003. IF leftO.mode = ModeReference THEN
  9004. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9005. destination := mem;
  9006. ELSE
  9007. destination := leftO.op;
  9008. END;
  9009. ReleaseOperand(leftO);
  9010. IF destination.mode # IntermediateCode.Undefined THEN
  9011. Emit(Mov(position,destination,rightO.op));
  9012. END;
  9013. ReleaseOperand(rightO);
  9014. ReleaseIntermediateOperand(mem);
  9015. IntermediateCode.InitOperand(destination);
  9016. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9017. Evaluate(right,rightO);
  9018. Designate(left,leftO);
  9019. MakeMemory(mem,leftO.op,addressType,0);
  9020. Emit(Mov(position,mem,rightO.op));
  9021. ReleaseIntermediateOperand(mem);
  9022. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9023. (* delegate *)
  9024. (*
  9025. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9026. *)
  9027. Emit(Mov(position,leftO.tag,rightO.tag));
  9028. END;
  9029. ReleaseOperand(leftO);
  9030. ReleaseOperand(rightO);
  9031. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9032. Designate(right,rightO);
  9033. Designate(left,leftO);
  9034. sizeOp := CopySize(left);
  9035. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9036. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9037. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9038. IF (rightType IS SyntaxTree.StringType) THEN
  9039. CopyString(left,right);
  9040. 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
  9041. Designate(right,rightO);
  9042. Designate(left,leftO);
  9043. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9044. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9045. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9046. ELSE
  9047. HALT(201)
  9048. END;
  9049. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9050. AssignMathArray(left,right);
  9051. ELSE
  9052. HALT(200);
  9053. END;
  9054. END Assign;
  9055. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9056. BEGIN
  9057. IF Trace THEN TraceEnter("VisitAssignment") END;
  9058. Assign(x.left,x.right);
  9059. IF Trace THEN TraceExit("VisitAssignment") END;
  9060. END VisitAssignment;
  9061. PROCEDURE EmitCooperativeSwitch;
  9062. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9063. BEGIN
  9064. ASSERT (cooperativeSwitches);
  9065. pc := section.pc;
  9066. IF lastSwitchPC = section.pc THEN RETURN END;
  9067. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9068. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9069. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9070. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9071. INC(statCoopSwitch, section.pc - pc);
  9072. END EmitCooperativeSwitch;
  9073. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9074. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9075. BEGIN
  9076. p0 := communication.left; p1 := communication.right;
  9077. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9078. Evaluate(p0,s0);
  9079. Evaluate(p1,s1);
  9080. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9081. Emit(Push(position,s0.op));
  9082. Emit(Push(position,s1.op));
  9083. (*
  9084. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9085. *)
  9086. IF ~backend.cellsAreObjects THEN
  9087. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9088. END;
  9089. ReleaseOperand(s0);
  9090. ReleaseOperand(s1);
  9091. IF backend.cellsAreObjects THEN
  9092. CallThis(position,"ActiveCellsRuntime","Send",2);
  9093. ELSE
  9094. CallThis(position,ChannelModuleName,"Send",2);
  9095. END;
  9096. (* ----- RECEIVE ------*)
  9097. ELSE
  9098. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9099. tmp := p0; p0 := p1; p1 := tmp;
  9100. END;
  9101. Evaluate(p0,s0);
  9102. Emit(Push(position,s0.op));
  9103. Designate(p1,s1);
  9104. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9105. Emit(Push(position,s1.op));
  9106. (*
  9107. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9108. *)
  9109. IF ~backend.cellsAreObjects THEN
  9110. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9111. END;
  9112. ReleaseOperand(s0);
  9113. ReleaseOperand(s1);
  9114. IF backend.cellsAreObjects THEN
  9115. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9116. ELSE
  9117. CallThis(position,ChannelModuleName,"Receive",2)
  9118. END;
  9119. END;
  9120. END VisitCommunicationStatement;
  9121. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9122. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9123. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9124. VAR true, false: Label; condition, value: BOOLEAN;
  9125. BEGIN
  9126. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9127. IF condition THEN
  9128. true := NewLabel();
  9129. false := NewLabel();
  9130. Condition(if.condition,true,false);
  9131. SetLabel(true);
  9132. StatementSequence(if.statements);
  9133. BrL(end);
  9134. SetLabel(false);
  9135. ELSE
  9136. IF value THEN (* always true *)
  9137. escape := TRUE;
  9138. StatementSequence(if.statements);
  9139. (* no branch necessary -- rest skipped *)
  9140. END;
  9141. END;
  9142. END IfPart;
  9143. BEGIN
  9144. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9145. end := NewLabel();
  9146. elsifs := x.ElsifParts();
  9147. IfPart(x.ifPart);
  9148. FOR i := 0 TO elsifs-1 DO
  9149. IF ~escape THEN
  9150. elsif := x.GetElsifPart(i);
  9151. IfPart(elsif);
  9152. END;
  9153. END;
  9154. IF (x.elsePart # NIL) & ~escape THEN
  9155. StatementSequence(x.elsePart);
  9156. END;
  9157. SetLabel(end);
  9158. IF Trace THEN TraceExit("VisitIfStatement") END;
  9159. END VisitIfStatement;
  9160. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9161. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9162. BEGIN
  9163. (*IF x.variable.type.resolved = x.type.resolved THEN
  9164. (* always true, do nothing *)
  9165. ELSE*)
  9166. Designate(x.variable,res);
  9167. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9168. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9169. END;
  9170. trueL := NewLabel();
  9171. TypeTest(res.tag,x.type,trueL,falseL);
  9172. ReleaseOperand(res);
  9173. SetLabel(trueL);
  9174. StatementSequence(x.statements);
  9175. BrL(endL);
  9176. END WithPart;
  9177. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9178. VAR endL,falseL: Label;i: LONGINT;
  9179. BEGIN
  9180. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9181. endL := NewLabel();
  9182. FOR i := 0 TO x.WithParts()-1 DO
  9183. falseL := NewLabel();
  9184. WithPart(x.GetWithPart(i),falseL,endL);
  9185. SetLabel(falseL);
  9186. END;
  9187. IF x.elsePart = NIL THEN
  9188. IF ~isUnchecked THEN
  9189. EmitTrap(position,WithTrap);
  9190. END;
  9191. ELSE
  9192. StatementSequence(x.elsePart)
  9193. END;
  9194. SetLabel(endL);
  9195. IF Trace THEN TraceExit("VisitWithStatement") END;
  9196. END VisitWithStatement;
  9197. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9198. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9199. out,else: Label; label: Label;
  9200. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9201. symbol: SyntaxTree.Symbol;
  9202. BEGIN
  9203. (*! split case statement into if-elsif statements for large case label lists *)
  9204. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9205. size := x.max-x.min+1;
  9206. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9207. END;
  9208. Evaluate(x.variable,var);
  9209. ReuseCopy(tmp,var.op);
  9210. ReleaseIntermediateOperand(var.op);
  9211. var.op := tmp;
  9212. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9213. Convert(var.op,addressType);
  9214. size := x.max-x.min+1;
  9215. else := NewLabel();
  9216. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9217. (*
  9218. UniqueId(name,module.module,"case",caseId);
  9219. *)
  9220. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9221. Global.GetModuleSegmentedName(module.module, name);
  9222. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9223. symbol := SyntaxTree.NewSymbol(name[1]);
  9224. symbol.SetScope(moduleScope);
  9225. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9226. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9227. section.isCaseTable := TRUE;
  9228. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9229. ReuseCopy(res,var.op);
  9230. ReleaseOperand(var);
  9231. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9232. Emit(Add(position,res,res,jmp));
  9233. IntermediateCode.MakeMemory(res,addressType);
  9234. Emit(Br(position,res));
  9235. ReleaseIntermediateOperand(res);
  9236. out := NewLabel();
  9237. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9238. part := x.GetCasePart(i);
  9239. constant := part.firstConstant;
  9240. label := NewLabel();
  9241. SetLabel(label);
  9242. WHILE(constant # NIL) DO (* case labels for this case part *)
  9243. FOR j := constant.min TO constant.max DO
  9244. fixups[j-x.min] := label;
  9245. END;
  9246. constant := constant.next;
  9247. END;
  9248. StatementSequence(part.statements);
  9249. BrL(out);
  9250. END;
  9251. SetLabel(else);
  9252. FOR i := 0 TO size-1 DO
  9253. IF fixups[i] = NIL THEN
  9254. fixups[i] := else;
  9255. END;
  9256. END;
  9257. IF x.elsePart # NIL THEN
  9258. StatementSequence(x.elsePart);
  9259. ELSIF ~isUnchecked THEN
  9260. EmitTrap(position,CaseTrap);
  9261. END;
  9262. SetLabel(out);
  9263. FOR i := 0 TO size-1 DO
  9264. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9265. section.Emit(Data(position,op));
  9266. END;
  9267. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9268. END VisitCaseStatement;
  9269. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9270. VAR start: Label; true,false: Label;
  9271. BEGIN
  9272. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9273. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9274. start := NewLabel();
  9275. true := NewLabel();
  9276. false := NewLabel();
  9277. SetLabel(start);
  9278. Condition(x.condition,true,false);
  9279. SetLabel(true);
  9280. StatementSequence(x.statements);
  9281. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9282. BrL(start);
  9283. SetLabel(false);
  9284. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9285. END VisitWhileStatement;
  9286. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9287. VAR false,true: Label;
  9288. BEGIN
  9289. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9290. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9291. true := NewLabel();
  9292. false := NewLabel();
  9293. SetLabel(false);
  9294. StatementSequence(x.statements);
  9295. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9296. Condition(x.condition,true,false);
  9297. SetLabel(true);
  9298. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9299. END VisitRepeatStatement;
  9300. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9301. VAR
  9302. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9303. temporaryVariable: SyntaxTree.Variable;
  9304. temporaryVariableDesignator : SyntaxTree.Designator;
  9305. BEGIN
  9306. IF Trace THEN TraceEnter("VisitForStatement") END;
  9307. true := NewLabel();
  9308. false := NewLabel();
  9309. start := NewLabel();
  9310. Assign(x.variable,x.from);
  9311. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9312. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9313. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9314. Assign(temporaryVariableDesignator,x.to);
  9315. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9316. IF by > 0 THEN
  9317. cmp := Scanner.LessEqual
  9318. ELSE
  9319. cmp := Scanner.GreaterEqual
  9320. END;
  9321. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9322. binary.SetType(system.booleanType);
  9323. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9324. SetLabel(start);
  9325. Condition(binary,true,false);
  9326. SetLabel(true);
  9327. StatementSequence(x.statements);
  9328. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9329. binary.SetType(x.variable.type);
  9330. Assign(x.variable,binary);
  9331. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9332. BrL(start);
  9333. SetLabel(false);
  9334. IF Trace THEN TraceExit("VisitForStatement") END;
  9335. END VisitForStatement;
  9336. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9337. VAR prevLoop: Label;
  9338. BEGIN
  9339. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9340. prevLoop := currentLoop;
  9341. currentLoop := NewLabel();
  9342. StatementSequence(x.statements);
  9343. SetLabel(currentLoop);
  9344. currentLoop := prevLoop;
  9345. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9346. END VisitExitableBlock;
  9347. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9348. VAR prevLoop,start: Label;
  9349. BEGIN
  9350. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9351. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9352. start := NewLabel();
  9353. prevLoop := currentLoop;
  9354. SetLabel(start);
  9355. currentLoop := NewLabel();
  9356. StatementSequence(x.statements);
  9357. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9358. BrL(start);
  9359. SetLabel(currentLoop);
  9360. currentLoop := prevLoop;
  9361. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9362. END VisitLoopStatement;
  9363. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9364. VAR outer: SyntaxTree.Statement;
  9365. BEGIN
  9366. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9367. IF locked THEN (* r if we jump out of an exclusive block *)
  9368. outer := x.outer;
  9369. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9370. outer := outer.outer;
  9371. END;
  9372. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9373. Lock(FALSE);
  9374. END;
  9375. END;
  9376. BrL(currentLoop);
  9377. IF Trace THEN TraceExit("VisitExitStatement") END;
  9378. END VisitExitStatement;
  9379. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9380. VAR
  9381. expression, parameterDesignator: SyntaxTree.Expression;
  9382. type, componentType: SyntaxTree.Type;
  9383. res, right: Operand;
  9384. left, mem, reg: IntermediateCode.Operand;
  9385. parameter: SyntaxTree.Parameter;
  9386. procedure: SyntaxTree.Procedure;
  9387. procedureType: SyntaxTree.ProcedureType;
  9388. returnTypeOffset: LONGINT;
  9389. delegate: BOOLEAN;
  9390. map: SymbolMap;
  9391. cc, parametersSize: LONGINT;
  9392. BEGIN
  9393. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9394. expression := x.returnValue;
  9395. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9396. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9397. IF currentIsInline THEN
  9398. IF expression # NIL THEN
  9399. map := currentMapper.Get(NIL);
  9400. IF map # NIL THEN
  9401. Assign(map.to, expression);
  9402. END;
  9403. END;
  9404. BrL(currentInlineExit);
  9405. RETURN;
  9406. END;
  9407. IF expression # NIL THEN
  9408. type := expression.type.resolved;
  9409. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9410. IF locked THEN Lock(FALSE) END;
  9411. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9412. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9413. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9414. (* return without structured return parameter *)
  9415. Evaluate(expression,res);
  9416. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9417. IF locked OR profile THEN
  9418. Emit(Push(position,res.op));
  9419. IF delegate THEN HALT(200) END;
  9420. ReleaseOperand(res);
  9421. IF locked THEN Lock(FALSE) END;
  9422. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9423. reg := NewRegisterOperand(res.op.type);
  9424. Emit(Pop(position,reg));
  9425. Emit(Return(position,reg));
  9426. ReleaseIntermediateOperand(reg);
  9427. ELSE
  9428. Emit(Return(position,res.op));
  9429. ReleaseOperand(res);
  9430. END;
  9431. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9432. THEN
  9433. (* return using structured return parameter *)
  9434. 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)
  9435. OR SemanticChecker.IsPointerType(type));
  9436. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9437. parameter :=procedureType.returnParameter;
  9438. ASSERT(parameter # NIL);
  9439. returnTypeOffset := parameter.offsetInBits;
  9440. (*
  9441. IF parameter# NIL THEN
  9442. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9443. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9444. ELSE
  9445. returnTypeOffset := system.offsetFirstParameter
  9446. END;
  9447. *)
  9448. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9449. IF type IS SyntaxTree.RangeType THEN
  9450. (* array range type *)
  9451. Evaluate(expression, right);
  9452. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9453. Emit(Mov(position,mem, right.op)); (* first *)
  9454. ReleaseIntermediateOperand(mem);
  9455. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9456. Emit(Mov(position,mem, right.tag)); (* last *)
  9457. ReleaseIntermediateOperand(mem);
  9458. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9459. Emit(Mov(position,mem, right.extra)); (* step *)
  9460. ReleaseIntermediateOperand(mem);
  9461. ReleaseOperand(right);
  9462. ELSIF type IS SyntaxTree.ComplexType THEN
  9463. Evaluate(expression, right);
  9464. componentType := type(SyntaxTree.ComplexType).componentType;
  9465. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9466. Emit(Mov(position,mem, right.op)); (* real part *)
  9467. ReleaseIntermediateOperand(mem);
  9468. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9469. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9470. ReleaseIntermediateOperand(mem);
  9471. ReleaseOperand(right);
  9472. ELSE (* covers cases: pointer / record / array *)
  9473. parameter := procedureType.returnParameter;
  9474. checker.SetCurrentScope(currentScope);
  9475. ASSERT(parameter # NIL);
  9476. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9477. Assign(parameterDesignator,expression);
  9478. END;
  9479. ReleaseIntermediateOperand(left);
  9480. IF locked THEN Lock(FALSE) END;
  9481. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9482. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9483. parameter := procedureType.returnParameter;
  9484. checker.SetCurrentScope(currentScope);
  9485. IF parameter = NIL THEN
  9486. Error(procedure.position, "structured return of parameter of procedure not found");
  9487. ELSE
  9488. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9489. Assign(parameterDesignator,expression);
  9490. END;
  9491. IF locked THEN Lock(FALSE) END;
  9492. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9493. ELSE
  9494. HALT(200);
  9495. END;
  9496. ELSE
  9497. IF locked THEN Lock(FALSE) END;
  9498. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9499. END;
  9500. IF backend.cooperative THEN
  9501. BrL(exitLabel);
  9502. ELSE
  9503. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9504. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9505. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9506. ELSE
  9507. parametersSize := 0;
  9508. END;
  9509. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9510. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9511. END;
  9512. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9513. END VisitReturnStatement;
  9514. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9515. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9516. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9517. statements: SyntaxTree.StatementSequence;
  9518. name, suffix: SyntaxTree.IdentifierString;
  9519. BEGIN
  9520. Strings.IntToStr(awaitProcCounter,suffix);
  9521. Strings.Concat("@AwaitProcedure",suffix,name);
  9522. identifier := SyntaxTree.NewIdentifier(name);
  9523. INC(awaitProcCounter);
  9524. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9525. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9526. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9527. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9528. procedure.SetAccess(SyntaxTree.Hidden);
  9529. procedure.SetScope(currentScope);
  9530. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9531. procedureType.SetReturnType(system.booleanType);
  9532. procedure.SetType(procedureType);
  9533. body := SyntaxTree.NewBody(x.position,procedureScope);
  9534. procedureScope.SetBody(body);
  9535. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9536. returnStatement.SetReturnValue(x.condition);
  9537. statements := SyntaxTree.NewStatementSequence();
  9538. statements.AddStatement(returnStatement);
  9539. body.SetStatementSequence(statements);
  9540. currentScope.AddProcedure(procedure);
  9541. RETURN procedure
  9542. END MakeAwaitProcedure;
  9543. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9544. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9545. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9546. BEGIN
  9547. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9548. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9549. IF backend.cooperative THEN
  9550. start := NewLabel();
  9551. true := NewLabel();
  9552. false := NewLabel();
  9553. SetLabel(start);
  9554. Condition(x.condition,true,false);
  9555. SetLabel(false);
  9556. PushSelfPointer();
  9557. CallThis(position,"ExclusiveBlocks","Await",1);
  9558. BrL(start);
  9559. SetLabel(true);
  9560. PushSelfPointer();
  9561. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9562. ELSE
  9563. proc := MakeAwaitProcedure(x);
  9564. Emit(Push(position,fp));
  9565. GetCodeSectionNameForSymbol(proc,name);
  9566. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9567. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9568. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9569. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9570. Emit(Result(position,res));
  9571. (*
  9572. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9573. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9574. *)
  9575. InitOperand(result,ModeValue);
  9576. result.op := res;
  9577. label := NewLabel();
  9578. BreqL(label, result.op, SELF.true);
  9579. ReleaseOperand(result);
  9580. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9581. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9582. Emit(Push(position,res));
  9583. Emit(Push(position,fp));
  9584. PushSelfPointer();
  9585. Emit(Push(position,nil));
  9586. CallThis(position,"Objects","Await",4);
  9587. SetLabel(label);
  9588. END;
  9589. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9590. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9591. END VisitAwaitStatement;
  9592. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9593. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9594. BEGIN
  9595. FOR i := 0 TO x.Length() - 1 DO
  9596. statement := x.GetStatement( i );
  9597. Statement(statement);
  9598. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9599. END;
  9600. END StatementSequence;
  9601. PROCEDURE PushSelfPointer;
  9602. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9603. procedureType: SyntaxTree.ProcedureType;
  9604. BEGIN
  9605. scope := currentScope;
  9606. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9607. scope := scope.outerScope;
  9608. END;
  9609. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9610. IF ~newObjectFile THEN
  9611. Symbol(moduleSelf,op);
  9612. IntermediateCode.MakeMemory(op.op,addressType);
  9613. ELSE
  9614. moduleSection := meta.ModuleSection();
  9615. IF backend.cooperative THEN
  9616. moduleOffset := 0;
  9617. ELSE
  9618. moduleOffset := moduleSection.pc;
  9619. END;
  9620. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9621. END;
  9622. ELSE
  9623. GetBaseRegister(op.op,currentScope,scope);
  9624. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9625. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9626. parametersSize := ProcedureParametersSize(system,procedure);
  9627. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9628. IF backend.cooperative THEN
  9629. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9630. END;
  9631. IntermediateCode.MakeMemory(op.op,addressType);
  9632. END;
  9633. Emit(Push(position,op.op));
  9634. ReleaseOperand(op);
  9635. END PushSelfPointer;
  9636. PROCEDURE Lock(lock: BOOLEAN);
  9637. BEGIN
  9638. IF Trace THEN TraceEnter("Lock") END;
  9639. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9640. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9641. ASSERT(modifyAssignmentCounter = 0);
  9642. IF dump # NIL THEN
  9643. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9644. dump.Ln;dump.Update;
  9645. END;
  9646. PushSelfPointer;
  9647. IF backend.cooperative THEN
  9648. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9649. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9650. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9651. END;
  9652. ELSE
  9653. Emit(Push(position,true));
  9654. IF lock THEN CallThis(position,"Objects","Lock",2)
  9655. ELSE CallThis(position,"Objects","Unlock",2);
  9656. END;
  9657. END;
  9658. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9659. IF Trace THEN TraceExit("Lock") END;
  9660. END Lock;
  9661. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9662. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9663. BEGIN
  9664. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9665. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9666. previouslyUnchecked := isUnchecked;
  9667. isUnchecked := isUnchecked OR x.isUnchecked;
  9668. previouslyCooperativeSwitches := cooperativeSwitches;
  9669. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9670. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9671. IF x.statements # NIL THEN
  9672. StatementSequence(x.statements);
  9673. END;
  9674. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9675. isUnchecked := previouslyUnchecked;
  9676. cooperativeSwitches := previouslyCooperativeSwitches;
  9677. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9678. END VisitStatementBlock;
  9679. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9680. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9681. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9682. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9683. procedure: SyntaxTree.Procedure;
  9684. map: SymbolMap;
  9685. cc, parametersSize: LONGINT;
  9686. BEGIN
  9687. scope := currentScope;
  9688. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9689. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9690. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9691. return := emptyOperand;
  9692. IF Trace THEN TraceEnter("VisitCode") END;
  9693. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9694. NEW(in, x.inRules.Length());
  9695. FOR i := 0 TO LEN(in)-1 DO
  9696. statement := x.inRules.GetStatement(i);
  9697. WITH statement: SyntaxTree.Assignment DO
  9698. Evaluate(statement.right, operand);
  9699. result := operand.op;
  9700. NEW(str, 64);
  9701. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9702. in[i] := result; IntermediateCode.SetString(in[i], str);
  9703. ReleaseIntermediateOperand(operand.tag);
  9704. END;
  9705. END;
  9706. ELSE in := NIL
  9707. END;
  9708. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9709. NEW(out, x.outRules.Length());
  9710. FOR i := 0 TO LEN(out)-1 DO
  9711. statement := x.outRules.GetStatement(i);
  9712. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9713. WITH statement: SyntaxTree.Assignment DO
  9714. Designate(statement.left, operand);
  9715. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9716. NEW(str, 64);
  9717. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9718. out[i] := result; IntermediateCode.SetString(out[i], str);
  9719. ReleaseIntermediateOperand(operand.tag);
  9720. |statement: SyntaxTree.ReturnStatement DO
  9721. NEW(str, 64);
  9722. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9723. IF currentIsInline THEN
  9724. map := currentMapper.Get(NIL);
  9725. Designate(map.to, operand);
  9726. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9727. (*Evaluate(map.to, operand);*)
  9728. out[i] := result;
  9729. ELSE
  9730. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9731. END;
  9732. IntermediateCode.SetString(out[i], str);
  9733. ReleaseIntermediateOperand(operand.tag);
  9734. return := out[i];
  9735. ELSE
  9736. END;
  9737. END;
  9738. ELSE out := NIL
  9739. END;
  9740. Emit(Asm(x.position,x.sourceCode, in, out));
  9741. IF in # NIL THEN
  9742. FOR i := 0 TO LEN(in)-1 DO
  9743. ReleaseIntermediateOperand(in[i]);
  9744. END;
  9745. END;
  9746. IF out # NIL THEN
  9747. FOR i := 0 TO LEN(out)-1 DO
  9748. WITH statement: SyntaxTree.Assignment DO
  9749. ReleaseIntermediateOperand(out[i]);
  9750. |statement: SyntaxTree.ReturnStatement DO
  9751. IF currentIsInline THEN
  9752. ReleaseIntermediateOperand(out[i]);
  9753. END;
  9754. ELSE
  9755. END;
  9756. statement := x.outRules.GetStatement(i);
  9757. END;
  9758. END;
  9759. IF return.mode # IntermediateCode.Undefined THEN
  9760. IF currentIsInline THEN
  9761. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9762. Symbol(procedureType.returnParameter, par);
  9763. MakeMemory(mem, par.op, return.type, 0);
  9764. ReleaseOperand(par);
  9765. Emit(Mov(position, mem, return));
  9766. ReleaseIntermediateOperand(mem);
  9767. ELSE
  9768. Emit(Return(position,return));
  9769. END;
  9770. IF currentIsInline THEN RETURN END;
  9771. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9772. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9773. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9774. ELSE
  9775. parametersSize := 0;
  9776. END;
  9777. EmitLeave(section, position,NIL, cc);
  9778. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9779. ReleaseIntermediateOperand(return);
  9780. END;
  9781. IF Trace THEN TraceExit("VisitCode") END;
  9782. END VisitCode;
  9783. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9784. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9785. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9786. const, call: IntermediateCode.Operand;
  9787. parameterDesignator: SyntaxTree.Expression;
  9788. saved: RegisterEntry;
  9789. name: Basic.SegmentedName;
  9790. BEGIN
  9791. IF Trace THEN TraceEnter("ParameterCopies") END;
  9792. parameter := x.firstParameter;
  9793. WHILE parameter # NIL DO
  9794. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9795. type := parameter.type.resolved;
  9796. IF IsOpenArray(type) THEN
  9797. VisitParameter(parameter);
  9798. op := result;
  9799. IF backend.cooperative & parameter.NeedsTrace() THEN
  9800. length := GetArrayLength(type, op.tag);
  9801. size := NewRegisterOperand(addressType);
  9802. base := ArrayBaseType(type);
  9803. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9804. Emit(Mul(position, size, length, const));
  9805. dst := NewRegisterOperand (addressType);
  9806. Emit(Mov(position, dst, size));
  9807. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9808. Emit(Sub(position,dst,sp,dst));
  9809. Emit(And(position,dst,dst,const));
  9810. Emit(Mov(position,sp,dst));
  9811. par := fp;
  9812. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9813. IntermediateCode.InitImmediate(null, byteType, 0);
  9814. Emit(Fill(position, dst, size, null));
  9815. ReleaseIntermediateOperand(dst);
  9816. ReleaseIntermediateOperand(length);
  9817. SaveRegisters();ReleaseUsedRegisters(saved);
  9818. (* register dst has been freed before SaveRegisters already *)
  9819. base := ArrayBaseType(type);
  9820. (* assign method of open array *)
  9821. IF base.IsRecordType() THEN
  9822. Emit (Push(position, length));
  9823. Emit (Push(position, dst));
  9824. Emit (Push(position, op.op));
  9825. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9826. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9827. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9828. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9829. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9830. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9831. Emit (Push(position,length));
  9832. Emit (Push(position, dst));
  9833. Emit (Push(position, length));
  9834. Emit (Push(position, op.op));
  9835. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9836. ELSE
  9837. Emit (Push(position, length));
  9838. Emit (Push(position, dst));
  9839. Emit (Push(position, length));
  9840. Emit (Push(position, op.op));
  9841. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9842. ASSERT(ArrayBaseType(type).IsPointer());
  9843. END;
  9844. RestoreRegisters(saved);
  9845. ELSE
  9846. temp := GetDynamicSize(type,op.tag);
  9847. ReuseCopy(size,temp);
  9848. ReleaseIntermediateOperand(temp);
  9849. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9850. Emit(Sub(position,size,sp,size));
  9851. Emit(And(position,size,size,const));
  9852. Emit(Mov(position,sp,size));
  9853. par := fp;
  9854. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9855. ReleaseIntermediateOperand(size);
  9856. size := GetDynamicSize(type,op.tag);
  9857. END;
  9858. Emit(Copy(position,sp,op.op,size));
  9859. ReleaseIntermediateOperand(size);
  9860. ReleaseOperand(op);
  9861. IntermediateCode.MakeMemory(par,addressType);
  9862. Emit(Mov(position,par,sp));
  9863. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9864. checker.SetCurrentScope(currentScope);
  9865. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9866. Assign(parameterDesignator,parameterDesignator);
  9867. END;
  9868. END;
  9869. parameter := parameter.nextParameter;
  9870. END;
  9871. IF Trace THEN TraceExit("ParameterCopies") END;
  9872. END ParameterCopies;
  9873. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9874. VAR x: SyntaxTree.Variable;
  9875. BEGIN
  9876. x := scope.firstVariable;
  9877. WHILE x # NIL DO
  9878. InitVariable(x);
  9879. x := x.nextVariable;
  9880. END;
  9881. END InitVariables;
  9882. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9883. BEGIN
  9884. IF (symbol # NIL) THEN
  9885. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9886. ELSE
  9887. RETURN 0
  9888. END;
  9889. END GetFingerprint;
  9890. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9891. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9892. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9893. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9894. name: Basic.SegmentedName;
  9895. offset: LONGINT;
  9896. BEGIN
  9897. IF Trace THEN TraceEnter("Body") END;
  9898. ReleaseUsedRegisters(saved); (* just in case ... *)
  9899. section := ir;
  9900. exitLabel := NewLabel ();
  9901. IF moduleBody THEN moduleBodySection := section END;
  9902. IF ir.comments # NIL THEN
  9903. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9904. commentPrintout.SingleStatement(TRUE);
  9905. dump := ir.comments;
  9906. ELSE
  9907. commentPrintout := NIL;
  9908. dump := NIL;
  9909. END;
  9910. prevScope := currentScope;
  9911. currentScope := scope;
  9912. lastSwitchPC := 0;
  9913. cooperativeSwitches := backend.cooperative;
  9914. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9915. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9916. IF x # NIL THEN
  9917. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9918. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9919. IF moduleBody THEN
  9920. ProfilerInit();
  9921. ELSE
  9922. Basic.SegmentedNameToString(ir.name, string);
  9923. ProfilerAddProcedure(numberProcedures,string);
  9924. ProfilerEnterExit(numberProcedures,TRUE);
  9925. END;
  9926. END;
  9927. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9928. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9929. END;
  9930. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9931. IF moduleBody & ~newObjectFile THEN
  9932. InitVariables(moduleScope)
  9933. END;
  9934. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9935. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9936. IF procedure = cellScope.bodyProcedure THEN
  9937. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9938. StaticCallOperand(op, cellScope.constructor);
  9939. Emit(Call(position,op.op,0));
  9940. END;
  9941. END;
  9942. END;
  9943. ParameterCopies(procedureType);
  9944. InitVariables(scope);
  9945. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9946. GetCodeSectionNameForSymbol(procedure, name);
  9947. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9948. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9949. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9950. IntermediateCode.SetOffset(right,offset); (* tag *)
  9951. IntermediateCode.InitMemory(left,addressType,fp,0);
  9952. Emit(Mov(position, left, right));
  9953. END;
  9954. IF x.code = NIL THEN
  9955. VisitStatementBlock(x);
  9956. ELSE
  9957. VisitCode(x.code)
  9958. END;
  9959. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9960. ir.SetFinally(ir.pc);
  9961. StatementSequence(x.finally)
  9962. END;
  9963. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9964. IF ~backend.cooperative THEN
  9965. ProfilerEnterExit(numberProcedures,FALSE);
  9966. END;
  9967. INC(numberProcedures);
  9968. END;
  9969. END;
  9970. IF backend.cooperative THEN
  9971. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9972. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9973. END;
  9974. IF x # NIL THEN
  9975. SELF.position := x.position;
  9976. END;
  9977. CheckRegistersFree();
  9978. ASSERT(modifyAssignmentCounter = 0);
  9979. currentScope := prevScope;
  9980. IF Trace THEN TraceExit("Body") END;
  9981. END Body;
  9982. END ImplementationVisitor;
  9983. MetaDataGenerator=OBJECT
  9984. VAR
  9985. implementationVisitor: ImplementationVisitor;
  9986. declarationVisitor: DeclarationVisitor;
  9987. module: Sections.Module;
  9988. moduleName: ARRAY 128 OF CHAR;
  9989. moduleNamePool: Basic.HashTableInt;
  9990. moduleNamePoolSection: IntermediateCode.Section;
  9991. modulePointerSection: IntermediateCode.Section;
  9992. modulePointerSizePC: LONGINT;
  9993. modulePointerSectionOffset: LONGINT;
  9994. modulePointers: LONGINT;
  9995. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9996. RecordBaseOffset: LONGINT;
  9997. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9998. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9999. TypeTags: LONGINT; (* type extension level support *)
  10000. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10001. patchInfoPC: LONGINT;
  10002. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10003. BEGIN
  10004. IF implementationVisitor.backend.cooperative THEN
  10005. TypeTags := MAX(LONGINT);
  10006. BaseTypesTableOffset := 0;
  10007. MethodTableOffset := 2;
  10008. TypeRecordBaseOffset := 0;
  10009. RecordBaseOffset := 0;
  10010. ELSIF simple THEN
  10011. TypeTags := 3; (* only 3 extensions allowed *)
  10012. BaseTypesTableOffset := 1;
  10013. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10014. TypeRecordBaseOffset := 0;
  10015. RecordBaseOffset := 1;
  10016. ELSE
  10017. TypeTags := 16;
  10018. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10019. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10020. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10021. RecordBaseOffset := 8;
  10022. END;
  10023. SELF.simple := simple;
  10024. SELF.implementationVisitor := implementationVisitor;
  10025. SELF.declarationVisitor := declarationVisitor;
  10026. implementationVisitor.meta := SELF;
  10027. declarationVisitor.meta := SELF;
  10028. END InitMetaDataGenerator;
  10029. PROCEDURE SetModule(module: Sections.Module);
  10030. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10031. BEGIN
  10032. SELF.module := module;
  10033. Global.GetModuleName(module.module,moduleName);
  10034. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10035. NEW(moduleNamePool, 32);
  10036. (*! require GC protection *)
  10037. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10038. name := "Heaps.AnyPtr";
  10039. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10040. (* set base pointer *)
  10041. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10042. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10043. modulePointers := 0;
  10044. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10045. AddPointer(moduleNamePoolSection, namePoolOffset);
  10046. END;
  10047. END SetModule;
  10048. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10049. BEGIN
  10050. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10051. INC(modulePointers);
  10052. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10053. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10054. END AddPointer;
  10055. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10056. BEGIN
  10057. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10058. END GetTypeRecordBaseOffset;
  10059. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10060. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10061. BEGIN
  10062. INC(dataAdrOffset,6);
  10063. Info(section,"headerAdr");
  10064. Address(section,0);
  10065. Info(section,"typeDesc");
  10066. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10067. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10068. NamedSymbol(section, name, symbol, 0, offset);
  10069. Info(section,"mark: LONGINT;");
  10070. Longint(section,-1);
  10071. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10072. Info(section,"dataAdr-: ADDRESS");
  10073. Symbol(section,section, dataAdrOffset,0);
  10074. Info(section,"size-: SIZE");
  10075. Address(section,0);
  10076. Info(section,"nextRealtime: HeapBlock;");
  10077. Address(section,0);
  10078. END HeapBlock;
  10079. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10080. VAR i: LONGINT;
  10081. BEGIN
  10082. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10083. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10084. Info(section,"count*: LONGINT");
  10085. Longint(section,0);
  10086. Info(section,"locked*: BOOLEAN");
  10087. Longint(section,0);
  10088. Info(section,"awaitingLock*: ProcessQueue");
  10089. Address(section,0);
  10090. Address(section,0);
  10091. Info(section,"awaitingCond*: ProcessQueue");
  10092. Address(section,0);
  10093. Address(section,0);
  10094. Info(section,"lockedBy*: ANY");
  10095. Address(section,0);
  10096. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10097. Longint(section,1);
  10098. FOR i := 2 TO 6 DO
  10099. Longint(section,0);
  10100. END;
  10101. Info(section,"lock*: ANY");
  10102. Address(section,0);
  10103. END ProtectedHeapBlock;
  10104. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10105. BEGIN
  10106. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10107. END Info;
  10108. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10109. VAR op: IntermediateCode.Operand;
  10110. BEGIN
  10111. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10112. section.Emit(Data(Basic.invalidPosition,op));
  10113. END Address;
  10114. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10115. VAR op: IntermediateCode.Operand;
  10116. BEGIN
  10117. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10118. section.Emit(Data(Basic.invalidPosition,op));
  10119. END Size;
  10120. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10121. VAR op: IntermediateCode.Operand;
  10122. BEGIN
  10123. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10124. section.Emit(Data(Basic.invalidPosition,op));
  10125. END Set;
  10126. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10127. VAR op: IntermediateCode.Operand;
  10128. BEGIN
  10129. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10130. section.Emit(Data(Basic.invalidPosition,op));
  10131. END Longint;
  10132. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10133. VAR op,noOperand: IntermediateCode.Operand;
  10134. BEGIN
  10135. IntermediateCode.InitOperand(noOperand);
  10136. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10137. section.PatchOperands(pc,op,noOperand,noOperand);
  10138. END PatchAddress;
  10139. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10140. VAR op,noOperand: IntermediateCode.Operand;
  10141. BEGIN
  10142. IntermediateCode.InitOperand(noOperand);
  10143. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10144. section.PatchOperands(pc,op,noOperand,noOperand);
  10145. END PatchSize;
  10146. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10147. VAR op,noOperand: IntermediateCode.Operand;
  10148. BEGIN
  10149. IntermediateCode.InitOperand(noOperand);
  10150. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10151. section.PatchOperands(pc,op,noOperand,noOperand);
  10152. END PatchLongint;
  10153. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10154. VAR op, noOperand: IntermediateCode.Operand;
  10155. BEGIN
  10156. IntermediateCode.InitOperand(noOperand);
  10157. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10158. section.PatchOperands(pc,op,noOperand,noOperand);
  10159. END PatchSymbol;
  10160. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10161. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10162. BEGIN
  10163. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10164. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10165. section.Emit(Data(Basic.invalidPosition,op));
  10166. END Boolean;
  10167. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10168. VAR op: IntermediateCode.Operand;
  10169. BEGIN
  10170. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10171. section.Emit(Data(Basic.invalidPosition,op));
  10172. END Char;
  10173. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10174. VAR op: IntermediateCode.Operand;
  10175. BEGIN
  10176. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10177. section.Emit(Data(Basic.invalidPosition,op));
  10178. END Integer;
  10179. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10180. VAR i: LONGINT;
  10181. BEGIN
  10182. Info(section,str);
  10183. i := 0;
  10184. WHILE(str[i] # 0X) DO
  10185. Char(section,str[i]);
  10186. INC(i);
  10187. END;
  10188. Char(section,0X);
  10189. END String;
  10190. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10191. VAR s: Basic.SectionName;
  10192. BEGIN
  10193. StringPool.GetString(str, s);
  10194. String(section, s);
  10195. END String0;
  10196. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10197. VAR op: IntermediateCode.Operand;
  10198. BEGIN
  10199. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10200. IntermediateCode.SetOffset(op,realOffset);
  10201. section.Emit(Data(Basic.invalidPosition,op));
  10202. END NamedSymbol;
  10203. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10204. VAR op: IntermediateCode.Operand;
  10205. BEGIN
  10206. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10207. IntermediateCode.SetOffset(op,realOffset);
  10208. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10209. END NamedSymbolAt;
  10210. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10211. BEGIN
  10212. IF symbol= NIL THEN
  10213. Address( section, realOffset);
  10214. ASSERT(virtualOffset = 0);
  10215. ELSE
  10216. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10217. END;
  10218. END Symbol;
  10219. (* OutPointers delivers
  10220. {pointerOffset}
  10221. *)
  10222. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10223. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10224. BEGIN
  10225. type := type.resolved;
  10226. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10227. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10228. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10229. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10230. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10231. ELSIF type IS SyntaxTree.PointerType THEN
  10232. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10233. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10234. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10235. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10236. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10237. ELSIF (type IS SyntaxTree.RecordType) THEN
  10238. (* never treat a record like a pointer, even if the pointer field is set! *)
  10239. WITH type: SyntaxTree.RecordType DO
  10240. base := type.GetBaseRecord();
  10241. IF base # NIL THEN
  10242. Pointers(offset,symbol,section, base,numberPointers);
  10243. END;
  10244. variable := type.recordScope.firstVariable;
  10245. WHILE(variable # NIL) DO
  10246. IF ~(variable.untraced) THEN
  10247. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10248. END;
  10249. variable := variable.nextVariable;
  10250. END;
  10251. END;
  10252. ELSIF (type IS SyntaxTree.CellType) THEN
  10253. WITH type: SyntaxTree.CellType DO
  10254. base := type.GetBaseRecord();
  10255. IF base # NIL THEN
  10256. Pointers(offset,symbol,section, base,numberPointers);
  10257. END;
  10258. variable := type.cellScope.firstVariable;
  10259. WHILE(variable # NIL) DO
  10260. IF ~(variable.untraced) THEN
  10261. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10262. END;
  10263. variable := variable.nextVariable;
  10264. END;
  10265. property := type.firstProperty;
  10266. WHILE(property # NIL) DO
  10267. IF ~(property.untraced) THEN
  10268. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10269. END;
  10270. property := property.nextProperty;
  10271. END;
  10272. parameter := type.firstParameter;
  10273. WHILE(parameter # NIL) DO
  10274. IF ~(parameter.untraced) THEN
  10275. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10276. END;
  10277. parameter := parameter.nextParameter;
  10278. END;
  10279. END;
  10280. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10281. WITH type: SyntaxTree.ArrayType DO
  10282. IF type.form= SyntaxTree.Static THEN
  10283. n := type.staticLength;
  10284. base := type.arrayBase.resolved;
  10285. WHILE(base IS SyntaxTree.ArrayType) DO
  10286. type := base(SyntaxTree.ArrayType);
  10287. n := n* type.staticLength;
  10288. base := type.arrayBase.resolved;
  10289. END;
  10290. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10291. IF SemanticChecker.ContainsPointer(base) THEN
  10292. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10293. FOR i := 0 TO n-1 DO
  10294. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10295. END;
  10296. END;
  10297. ELSE
  10298. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10299. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10300. END;
  10301. END;
  10302. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10303. WITH type: SyntaxTree.MathArrayType DO
  10304. IF type.form = SyntaxTree.Static THEN
  10305. n := type.staticLength;
  10306. base := type.arrayBase.resolved;
  10307. WHILE(base IS SyntaxTree.MathArrayType) DO
  10308. type := base(SyntaxTree.MathArrayType);
  10309. n := n* type.staticLength;
  10310. base := type.arrayBase.resolved;
  10311. END;
  10312. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10313. IF SemanticChecker.ContainsPointer(base) THEN
  10314. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10315. FOR i := 0 TO n-1 DO
  10316. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10317. END;
  10318. END;
  10319. ELSE
  10320. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10321. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10322. END
  10323. END;
  10324. (* ELSE no pointers in type *)
  10325. END;
  10326. END Pointers;
  10327. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10328. VAR position,i: LONGINT; ch: CHAR;
  10329. BEGIN
  10330. IF pool.Has(index) THEN
  10331. RETURN pool.GetInt(index)
  10332. ELSE
  10333. position := source.pc;
  10334. pool.PutInt(index, position);
  10335. Info(source, name);
  10336. i := 0;
  10337. REPEAT
  10338. ch := name[i]; INC(i);
  10339. Char( source, ch);
  10340. UNTIL ch = 0X;
  10341. END;
  10342. RETURN position;
  10343. END EnterDynamicName;
  10344. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10345. VAR name: Basic.SectionName; position: LONGINT;
  10346. BEGIN
  10347. IF pool.Has(index) THEN
  10348. RETURN pool.GetInt(index)
  10349. ELSE
  10350. StringPool.GetString(index, name);
  10351. position := EnterDynamicName(source,name,index, pool);
  10352. END;
  10353. RETURN position;
  10354. END DynamicName;
  10355. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10356. VAR section: IntermediateCode.Section;
  10357. BEGIN
  10358. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10359. IF implementationVisitor.backend.cooperative THEN
  10360. Info(section, "TypeDescriptor");
  10361. Basic.ToSegmentedName("BaseTypes.Array", name);
  10362. NamedSymbol(section, name,NIL, 0, 0);
  10363. BasePointer(section);
  10364. offset := 0;
  10365. ELSE
  10366. HeapBlock(mName,typeName,section,2);
  10367. Info(section, "HeapBlock");
  10368. (*
  10369. Symbol(section,section,2,0);
  10370. *)
  10371. Address(section,0); (* empty such that GC does not go on traversing *)
  10372. Info(section, "TypeDescriptor");
  10373. Address(section,0);
  10374. offset := section.pc;
  10375. END;
  10376. RETURN section
  10377. END NamedBlock;
  10378. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10379. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10380. BEGIN
  10381. COPY(moduleName,name);
  10382. Strings.Append(name,suffix);
  10383. Basic.ToSegmentedName(name, pooledName);
  10384. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10385. END Block;
  10386. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10387. VAR name: Basic.SegmentedName;
  10388. BEGIN
  10389. Info(source,"ArrayHeader");
  10390. IF implementationVisitor.backend.cooperative THEN
  10391. sizePC := source.pc;
  10392. Address(source,0);
  10393. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10394. IF baseType # "" THEN
  10395. Basic.ToSegmentedName(baseType, name);
  10396. NamedSymbol(source, name,NIL, 0, 0);
  10397. ELSE
  10398. Address(source,0);
  10399. END;
  10400. Address(source,0);
  10401. ELSE
  10402. Address(source,0);
  10403. Address(source,0);
  10404. (* first pointer for GC *)
  10405. IF hasPointer THEN
  10406. (* points to first element in the array, this is NOT the base type descriptor *)
  10407. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10408. ELSE
  10409. Address(source,0);
  10410. END;
  10411. sizePC := source.pc;
  10412. Address(source,0);
  10413. Info(source,"array data");
  10414. END;
  10415. END ArrayBlock;
  10416. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10417. BEGIN
  10418. IF implementationVisitor.backend.cooperative THEN
  10419. PatchSize(section, pc, size);
  10420. PatchSize(section, pc + 3, size);
  10421. ELSE
  10422. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10423. PatchSize(section, pc, size);
  10424. END;
  10425. END PatchArray;
  10426. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10427. VAR
  10428. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10429. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10430. poolMap: Basic.HashTableInt;
  10431. namePool: IntermediateCode.Section;
  10432. namePoolOffset: LONGINT;
  10433. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10434. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10435. BEGIN
  10436. Basic.SegmentedNameToString(s1.name,n1);
  10437. Basic.SegmentedNameToString(s2.name,n2);
  10438. index := 0;
  10439. ch1 := n1[index];
  10440. ch2 := n2[index];
  10441. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10442. INC(index);
  10443. ch1 := n1[index];
  10444. ch2 := n2[index];
  10445. END;
  10446. RETURN ch1 < ch2;
  10447. END Compare;
  10448. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10449. VAR
  10450. i, j: LONGINT;
  10451. x, t: Sections.Section;
  10452. BEGIN
  10453. IF lo < hi THEN
  10454. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10455. WHILE i <= j DO
  10456. WHILE Compare(list[i], x) DO INC(i) END;
  10457. WHILE Compare(x, list[j]) DO DEC(j) END;
  10458. IF i <= j THEN
  10459. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10460. INC(i); DEC(j)
  10461. END
  10462. END;
  10463. IF lo < j THEN QuickSort(list, lo, j) END;
  10464. IF i < hi THEN QuickSort(list, i, hi) END
  10465. END;
  10466. END QuickSort;
  10467. (*
  10468. ExportDesc* = RECORD
  10469. fp*: ADDRESS;
  10470. name* {UNTRACED}: DynamicName;
  10471. adr*: ADDRESS;
  10472. exports*: LONGINT;
  10473. dsc* {UNTRACED}: ExportArray
  10474. END;
  10475. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10476. *)
  10477. PROCEDURE ExportDesc2(
  10478. source: IntermediateCode.Section;
  10479. namePool: IntermediateCode.Section;
  10480. fingerPrinter: FingerPrinter.FingerPrinter;
  10481. symbol: Sections.Section;
  10482. name: StringPool.Index;
  10483. VAR patchAdr: LONGINT
  10484. ): BOOLEAN;
  10485. VAR fingerPrint: SyntaxTree.FingerPrint;
  10486. BEGIN
  10487. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10488. & (symbol.type # Sections.InlineCodeSection)
  10489. THEN
  10490. *)
  10491. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10492. & (symbol.type # Sections.InlineCodeSection))
  10493. THEN
  10494. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10495. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10496. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10497. Longint(source,fingerPrint.shallow);
  10498. ELSE
  10499. Longint(source, 0);
  10500. END;
  10501. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10502. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10503. Symbol(source, symbol,0,0);
  10504. patchAdr := source.pc;
  10505. Longint(source, 0);
  10506. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10507. Address(source,0);
  10508. END;
  10509. RETURN TRUE
  10510. ELSE
  10511. RETURN FALSE
  10512. END;
  10513. END ExportDesc2;
  10514. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10515. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10516. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10517. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10518. nextPatch: LONGINT;
  10519. TYPE
  10520. Scope = RECORD
  10521. elements: LONGINT;
  10522. gelements: LONGINT;
  10523. section: IntermediateCode.Section;
  10524. patchAdr: LONGINT;
  10525. arraySizePC: LONGINT;
  10526. beginPC: LONGINT; (* current scope start pc *)
  10527. END;
  10528. BEGIN
  10529. Basic.InitSegmentedName(prev);
  10530. olevel := -1;
  10531. scopes[0].section := source;
  10532. scopes[0].arraySizePC := MIN(LONGINT);
  10533. FOR s := 0 TO LEN(sections)-1 DO
  10534. symbol := sections[s];
  10535. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10536. this := sections[s].name;
  10537. level := 0;
  10538. WHILE (this[level] > 0) DO
  10539. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10540. INC(level);
  10541. END;
  10542. WHILE level < olevel DO
  10543. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10544. IF olevel > 0 THEN
  10545. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10546. nextPatch := scopes[olevel-1].patchAdr+1;
  10547. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10548. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10549. END;
  10550. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10551. DEC(olevel);
  10552. END;
  10553. IF (this[level] > 0) THEN
  10554. IF level > olevel THEN
  10555. (*TRACE("opening",level); *)
  10556. IF scopes[level].section = NIL THEN
  10557. arrayName := ".@ExportArray";
  10558. Strings.AppendInt(arrayName, level);
  10559. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10560. AddPointer(scopes[level].section,offset);
  10561. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10562. END;
  10563. scopes[level].beginPC := scopes[level].section.pc;
  10564. olevel := level;
  10565. scopes[olevel].elements := 0;
  10566. END;
  10567. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10568. sym := sections[s];
  10569. ELSE
  10570. sym := NIL;
  10571. END;
  10572. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10573. THEN
  10574. INC(scopes[olevel].elements);
  10575. END;
  10576. (* enter string in scope *)
  10577. INC(level);
  10578. END;
  10579. END;
  10580. Basic.SegmentedNameToString(this, name);
  10581. prev := this;
  10582. END;
  10583. END;
  10584. WHILE 0 <= olevel DO
  10585. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10586. IF olevel > 0 THEN
  10587. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10588. nextPatch := scopes[olevel-1].patchAdr+1;
  10589. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10590. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10591. END;
  10592. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10593. DEC(olevel);
  10594. END;
  10595. level := 0;
  10596. WHILE (level < LEN(scopes)) DO
  10597. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10598. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10599. END;
  10600. INC(level);
  10601. END;
  10602. END Export;
  10603. BEGIN
  10604. NEW(fingerPrinter, module.system);
  10605. NEW(poolMap, 64);
  10606. (* 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 *)
  10607. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10608. NEW(sectionArray, module.allSections.Length());
  10609. FOR i := 0 TO module.allSections.Length() - 1 DO
  10610. section := module.allSections.GetSection(i);
  10611. sectionArray[i] := section;
  10612. END;
  10613. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10614. Export(sectionArray^);
  10615. END ExportDesc;
  10616. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10617. VAR
  10618. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10619. BEGIN
  10620. Info(source, "exception table offsets array descriptor");
  10621. size := 0;
  10622. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10623. Info(source, "exception table content");
  10624. FOR i := 0 TO module.allSections.Length() - 1 DO
  10625. p := module.allSections.GetSection(i);
  10626. IF p.type = Sections.CodeSection THEN
  10627. finallyPC := p(IntermediateCode.Section).finally;
  10628. IF finallyPC>=0 THEN
  10629. Symbol( source, p, 0,0);
  10630. Symbol( source, p, finallyPC, 0);
  10631. Symbol( source, p, finallyPC,0);
  10632. INC(size);
  10633. END;
  10634. END
  10635. END;
  10636. PatchArray(source,sizePC,size);
  10637. END ExceptionArray;
  10638. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10639. VAR i: LONGINT; ch: CHAR;
  10640. BEGIN
  10641. i := 0;
  10642. REPEAT
  10643. ch := name[i]; INC(i);
  10644. Char( section, ch);
  10645. UNTIL ch = 0X;
  10646. WHILE i < 32 DO
  10647. Char( section, 0X); INC(i);
  10648. END;
  10649. END Name;
  10650. PROCEDURE References(section: IntermediateCode.Section);
  10651. CONST
  10652. sfTypeNone = 0X;
  10653. sfTypeCHAR = 01X;
  10654. sfTypeCHAR8 = 02X;
  10655. sfTypeCHAR16 = 03X;
  10656. sfTypeCHAR32 = 04X;
  10657. sfTypeRANGE = 05X;
  10658. sfTypeSHORTINT = 06X;
  10659. sfTypeINTEGER = 07X;
  10660. sfTypeLONGINT = 08X;
  10661. sfTypeHUGEINT = 09X;
  10662. sfTypeWORD = 0AX;
  10663. sfTypeLONGWORD = 0BX;
  10664. sfTypeSIGNED8 = 0CX;
  10665. sfTypeSIGNED16 = 0DX;
  10666. sfTypeSIGNED32 = 0EX;
  10667. sfTypeSIGNED64 = 0FX;
  10668. sfTypeUNSIGNED8 = 10X;
  10669. sfTypeUNSIGNED16 = 11X;
  10670. sfTypeUNSIGNED32 = 12X;
  10671. sfTypeUNSIGNED64 = 13X;
  10672. sfTypeREAL = 14X;
  10673. sfTypeLONGREAL = 15X;
  10674. sfTypeCOMPLEX = 16X;
  10675. sfTypeLONGCOMPLEX = 17X;
  10676. sfTypeBOOLEAN = 18X;
  10677. sfTypeSET = 19X;
  10678. sfTypeANY = 1AX;
  10679. sfTypeOBJECT = 1BX;
  10680. sfTypeBYTE = 1CX;
  10681. sfTypeADDRESS = 1DX;
  10682. sfTypeSIZE = 1EX;
  10683. sfTypeIndirect = 1FX;
  10684. sfTypeRecord = 20X;
  10685. sfTypePointerToRecord = 21X;
  10686. sfTypePointerToArray = 22X;
  10687. sfTypeOpenArray = 23X;
  10688. sfTypeStaticArray = 24X;
  10689. sfTypeDynamicArray = 25X;
  10690. sfTypeMathStaticArray = 26X;
  10691. sfTypeMathOpenArray = 27X;
  10692. sfTypeMathTensor = 28X;
  10693. sfTypeDelegate = 29X;
  10694. sfTypeENUM = 2AX;
  10695. sfTypeCELL = 2BX;
  10696. sfTypePORT = 2CX;
  10697. sfIN = 0X;
  10698. sfOUT = 1X;
  10699. flagDelegate = 0;
  10700. flagConstructor = 1;
  10701. (* variable / parameter addressing modes *)
  10702. sfAbsolute = 0X; (* global vars *)
  10703. sfRelative = 1X; (* variables, value parameters *)
  10704. sfIndirect = 2X; (* var parameters *)
  10705. sfScopeBegin = 0F0X;
  10706. sfScopeEnd = 0F1X;
  10707. sfProcedure = 0F2X;
  10708. sfVariable = 0F3X;
  10709. sfTypeDeclaration = 0F4X;
  10710. sfModule = 0FFX;
  10711. RefInfo = TRUE;
  10712. VAR
  10713. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10714. indirectTypes: Basic.HashTable;
  10715. PROCEDURE CurrentIndex(): SIZE;
  10716. VAR i: LONGINT;
  10717. BEGIN
  10718. FOR i := startPC TO section.pc -1 DO
  10719. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10720. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10721. END;
  10722. startPC := section.pc;
  10723. RETURN lastOffset;
  10724. END CurrentIndex;
  10725. (*
  10726. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10727. Module = sfModule prevSymbol:SIZE name:String Scope.
  10728. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10729. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10730. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10731. Type =
  10732. sfTypePointerToRecord
  10733. | sfTypePointerToArray Type
  10734. | sfTypeOpenArray Type
  10735. | sfTypeDynamicArray Type
  10736. | sfTypeStaticArray length:SIZE Type
  10737. | sfTypeMathOpenArray Type
  10738. | sfTypeMathStaticArray length:SIZE Type
  10739. | sfTypeMathTensor Type
  10740. | sfTypeRecord tdAdr:ADDRESS
  10741. | sfTypeDelegate {Parameter} return:Type
  10742. | sfTypePort (sfIN | sfOUT)
  10743. | sfTypeBOOLEAN
  10744. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10745. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10746. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10747. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10748. | sfTypeWORD | sfTypeLONGWORD
  10749. | sfTypeREAL | sfTypeLONGREAL
  10750. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10751. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10752. | sfTypeIndirect offset:SIZE.
  10753. *)
  10754. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10755. VAR offset: SIZE;
  10756. BEGIN
  10757. IF indirectTypes.Has(type) THEN
  10758. offset := indirectTypes.GetInt(type);
  10759. Char(section, sfTypeIndirect);
  10760. Size(section, offset);
  10761. RETURN TRUE;
  10762. ELSE
  10763. indirectTypes.PutInt(type, CurrentIndex());
  10764. RETURN FALSE;
  10765. END;
  10766. END Indirect;
  10767. PROCEDURE NType(type: SyntaxTree.Type);
  10768. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10769. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10770. BEGIN
  10771. IF type = NIL THEN
  10772. Char(section, sfTypeNone)
  10773. ELSE
  10774. type := type.resolved;
  10775. size := type.sizeInBits;
  10776. WITH type:SyntaxTree.PointerType DO
  10777. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10778. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10779. Char(section, sfTypePointerToRecord);
  10780. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10781. ELSE
  10782. IF RefInfo THEN Info(section,"PointerToArray") END;
  10783. Char(section, sfTypePointerToArray);
  10784. NType(type.pointerBase);
  10785. END;
  10786. | type: SyntaxTree.ArrayType DO
  10787. IF ~Indirect(type) THEN
  10788. IF type.form = SyntaxTree.Open THEN
  10789. IF RefInfo THEN Info(section,"OpenArray") END;
  10790. Char(section, sfTypeOpenArray);
  10791. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10792. IF RefInfo THEN Info(section,"DynamicArray") END;
  10793. Char(section, sfTypeDynamicArray);
  10794. ELSIF type.form = SyntaxTree.Static THEN
  10795. IF RefInfo THEN Info(section,"StaticArray") END;
  10796. Char(section, sfTypeStaticArray);
  10797. Size(section, type.staticLength);
  10798. ELSE
  10799. HALT(100);
  10800. END;
  10801. NType(type.arrayBase);
  10802. END;
  10803. | type: SyntaxTree.MathArrayType DO
  10804. IF ~Indirect(type) THEN
  10805. IF type.form = SyntaxTree.Open THEN
  10806. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10807. Char(section, sfTypeMathOpenArray);
  10808. ELSIF type.form = SyntaxTree.Static THEN
  10809. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10810. Char(section, sfTypeMathStaticArray);
  10811. Size(section, type.staticLength);
  10812. ELSIF type.form = SyntaxTree.Tensor THEN
  10813. IF RefInfo THEN Info(section,"MathTensor") END;
  10814. Char(section, sfTypeMathTensor);
  10815. ELSE
  10816. HALT(100);
  10817. END;
  10818. NType(type.arrayBase);
  10819. END;
  10820. | type: SyntaxTree.RecordType DO
  10821. IF ~Indirect(type) THEN
  10822. IF type.pointerType # NIL (* OBJECT *) THEN
  10823. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10824. Char(section, sfTypePointerToRecord)
  10825. ELSE
  10826. IF RefInfo THEN Info(section,"Record") END;
  10827. Char(section, sfTypeRecord);
  10828. td := type.typeDeclaration;
  10829. IF RefInfo THEN Info(section,"TD") END;
  10830. IF (td # NIL) THEN
  10831. Global.GetSymbolSegmentedName(td,segmentedName);
  10832. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10833. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10834. ELSE
  10835. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10836. END;
  10837. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10838. Symbol(section, tir, 0, offset);
  10839. ELSE
  10840. Address(section, 0);
  10841. END;
  10842. END;
  10843. END;
  10844. | type: SyntaxTree.CellType DO
  10845. IF ~Indirect(type) THEN
  10846. IF RefInfo THEN Info(section,"Record") END;
  10847. Char(section, sfTypeRecord);
  10848. td := type.typeDeclaration;
  10849. IF RefInfo THEN Info(section,"TD") END;
  10850. IF (td # NIL) THEN
  10851. Global.GetSymbolSegmentedName(td,segmentedName);
  10852. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10853. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10854. ELSE
  10855. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10856. END;
  10857. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10858. Symbol(section, tir, 0, offset);
  10859. ELSE
  10860. Address(section, 0);
  10861. END;
  10862. END;
  10863. | type: SyntaxTree.PortType DO
  10864. Char(section, sfTypePORT);
  10865. IF type.direction = SyntaxTree.OutPort THEN
  10866. Char(section, sfOUT)
  10867. ELSE
  10868. Char(section, sfIN)
  10869. END;
  10870. | type: SyntaxTree.ProcedureType DO
  10871. IF ~Indirect(type) THEN
  10872. Char(section, sfTypeDelegate);
  10873. parameter := type.firstParameter;
  10874. WHILE(parameter # NIL) DO
  10875. NParameter(parameter, -1);
  10876. parameter := parameter.nextParameter;
  10877. END;
  10878. NType(type.returnType);
  10879. END;
  10880. | type:SyntaxTree.EnumerationType DO
  10881. Char(section, sfTypeENUM);
  10882. | type: SyntaxTree.BasicType DO
  10883. WITH type: SyntaxTree.BooleanType DO
  10884. IF RefInfo THEN Info(section,"Boolean") END;
  10885. Char(section, sfTypeBOOLEAN);
  10886. | type: SyntaxTree.CharacterType DO
  10887. IF type = module.system.characterType THEN
  10888. IF RefInfo THEN Info(section,"CHAR") END;
  10889. Char(section, sfTypeCHAR);
  10890. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10891. IF RefInfo THEN Info(section,"CHAR8") END;
  10892. Char(section, sfTypeCHAR8)
  10893. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10894. IF RefInfo THEN Info(section,"CHAR16") END;
  10895. Char(section, sfTypeCHAR16);
  10896. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10897. IF RefInfo THEN Info(section,"CHAR32") END;
  10898. Char(section, sfTypeCHAR32);
  10899. ELSE
  10900. HALT(100);
  10901. END;
  10902. |type: SyntaxTree.IntegerType DO
  10903. IF type(SyntaxTree.IntegerType).signed THEN
  10904. IF (type = module.system.shortintType) THEN
  10905. IF RefInfo THEN Info(section,"SHORTINT") END;
  10906. Char(section, sfTypeSHORTINT)
  10907. ELSIF (type = module.system.integerType) THEN
  10908. IF RefInfo THEN Info(section,"INTEGER") END;
  10909. Char(section, sfTypeINTEGER)
  10910. ELSIF (type = module.system.longintType) THEN
  10911. IF RefInfo THEN Info(section,"LONGINT") END;
  10912. Char(section, sfTypeLONGINT)
  10913. ELSIF (type = module.system.hugeintType) THEN
  10914. IF RefInfo THEN Info(section,"HUGEINT") END;
  10915. Char(section, sfTypeHUGEINT)
  10916. ELSIF (type = module.system.wordType) THEN
  10917. IF RefInfo THEN Info(section,"WORD") END;
  10918. Char(section, sfTypeWORD)
  10919. ELSIF (type = module.system.longWordType) THEN
  10920. IF RefInfo THEN Info(section,"LONGWORD") END;
  10921. Char(section, sfTypeLONGWORD);
  10922. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10923. IF RefInfo THEN Info(section,"SIGNED8") END;
  10924. Char(section, sfTypeSIGNED8)
  10925. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10926. IF RefInfo THEN Info(section,"SIGNED16") END;
  10927. Char(section, sfTypeSIGNED16)
  10928. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10929. IF RefInfo THEN Info(section,"SIGNED32") END;
  10930. Char(section, sfTypeSIGNED32)
  10931. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10932. IF RefInfo THEN Info(section,"SIGNED64") END;
  10933. Char(section, sfTypeSIGNED64)
  10934. ELSE
  10935. HALT(100);
  10936. END
  10937. ELSE (* unsigned *)
  10938. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10939. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10940. Char(section, sfTypeUNSIGNED8)
  10941. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10942. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10943. Char(section, sfTypeUNSIGNED16)
  10944. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10945. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10946. Char(section, sfTypeUNSIGNED32)
  10947. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10948. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10949. Char(section, sfTypeUNSIGNED64)
  10950. ELSE
  10951. HALT(100)
  10952. END
  10953. END;
  10954. | type: SyntaxTree.FloatType DO
  10955. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10956. IF RefInfo THEN Info(section,"REAL") END;
  10957. Char(section, sfTypeREAL);
  10958. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  10959. IF RefInfo THEN Info(section,"LONGREAL") END;
  10960. Char(section, sfTypeLONGREAL);
  10961. ELSE
  10962. HALT(100);
  10963. END;
  10964. |type: SyntaxTree.ComplexType DO
  10965. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  10966. IF RefInfo THEN Info(section,"COMPLEX") END;
  10967. Char(section, sfTypeCOMPLEX);
  10968. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  10969. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  10970. Char(section, sfTypeLONGCOMPLEX);
  10971. ELSE
  10972. HALT(100);
  10973. END;
  10974. |type:SyntaxTree.SetType DO
  10975. IF RefInfo THEN Info(section,"SET") END;
  10976. Char(section, sfTypeSET);
  10977. |type:SyntaxTree.AnyType DO
  10978. IF RefInfo THEN Info(section,"ANY") END;
  10979. Char(section, sfTypeANY);
  10980. |type:SyntaxTree.ObjectType DO
  10981. IF RefInfo THEN Info(section,"OBJECT") END;
  10982. Char(section, sfTypeOBJECT);
  10983. |type:SyntaxTree.ByteType DO
  10984. IF RefInfo THEN Info(section,"BYTE") END;
  10985. Char(section, sfTypeBYTE);
  10986. |type:SyntaxTree.RangeType DO
  10987. IF RefInfo THEN Info(section,"RANGE") END;
  10988. Char(section, sfTypeRANGE)
  10989. |type:SyntaxTree.AddressType DO
  10990. IF RefInfo THEN Info(section,"ADDRESS") END;
  10991. Char(section, sfTypeADDRESS)
  10992. |type:SyntaxTree.SizeType DO
  10993. IF RefInfo THEN Info(section,"SIZE") END;
  10994. Char(section, sfTypeSIZE)
  10995. ELSE
  10996. HALT(100)
  10997. END;
  10998. ELSE HALT(101);
  10999. END;
  11000. END;
  11001. END NType;
  11002. (*
  11003. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11004. *)
  11005. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11006. VAR pos: LONGINT; type: SyntaxTree.Type;
  11007. BEGIN
  11008. IF RefInfo THEN Info(section, "Parameter") END;
  11009. Char(section, sfVariable);
  11010. Size(section, procOffset);
  11011. String0(section, parameter.name);
  11012. type := parameter.type.resolved;
  11013. IF parameter.kind = SyntaxTree.VarParameter THEN
  11014. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11015. ELSE Char(section, sfIndirect)
  11016. END;
  11017. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11018. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11019. Char(section, sfIndirect);
  11020. ELSE
  11021. Char(section, sfRelative);
  11022. END;
  11023. ELSE
  11024. Char(section, sfRelative);
  11025. END;
  11026. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11027. NType(parameter.type);
  11028. END NParameter;
  11029. (*
  11030. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11031. *)
  11032. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11033. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11034. name: Basic.SegmentedName; flags: SET;
  11035. BEGIN
  11036. IF RefInfo THEN Info(section, "Procedure") END;
  11037. pos := CurrentIndex();
  11038. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11039. Char(section, sfProcedure);
  11040. Size(section, scopeOffset);
  11041. String0(section,procedure.name);
  11042. s := module.allSections.FindBySymbol(procedure);
  11043. Symbol(section,s,0,0); (* start *)
  11044. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11045. flags := {};
  11046. IF procedureType.isDelegate THEN
  11047. INCL(flags, flagDelegate);
  11048. END;
  11049. IF procedure.isConstructor THEN
  11050. INCL(flags, flagConstructor);
  11051. END;
  11052. Set(section, flags);
  11053. Global.GetSymbolSegmentedName(procedure,name);
  11054. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11055. IF RefInfo THEN Info(section, "Parameters") END;
  11056. parameter := procedureType.firstParameter;
  11057. WHILE(parameter # NIL) DO
  11058. NParameter(parameter, pos);
  11059. parameter := parameter.nextParameter;
  11060. END;
  11061. IF RefInfo THEN Info(section, "ReturnType") END;
  11062. NType(procedureType.returnType);
  11063. NScope(procedure.procedureScope, pos);
  11064. END NProcedure;
  11065. (*
  11066. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11067. *)
  11068. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11069. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11070. BEGIN
  11071. IF RefInfo THEN Info(section, "Variable") END;
  11072. pos := CurrentIndex();
  11073. Char(section, sfVariable);
  11074. Size(section, scopeOffset);
  11075. String0(section, variable.name);
  11076. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11077. Char(section, sfAbsolute);
  11078. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11079. NamedSymbol(section, sn,variable, 0,0);
  11080. ELSE
  11081. Char(section, sfRelative);
  11082. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11083. END;
  11084. NType(variable.type);
  11085. s := module.allSections.FindBySymbol(variable);
  11086. END NVariable;
  11087. (*
  11088. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11089. *)
  11090. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11091. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11092. BEGIN
  11093. IF typeDeclaration = NIL THEN RETURN END;
  11094. pos := CurrentIndex();
  11095. s := module.allSections.FindBySymbol(typeDeclaration);
  11096. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11097. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11098. Char(section, sfTypeDeclaration);
  11099. Size(section, scopeOffset);
  11100. String0(section, typeDeclaration.name);
  11101. declared := typeDeclaration.declaredType.resolved;
  11102. IF (declared IS SyntaxTree.PointerType) THEN
  11103. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11104. END;
  11105. WITH declared: SyntaxTree.RecordType DO
  11106. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11107. Symbol(section, s, 0, offset);
  11108. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11109. Basic.AppendToSegmentedName(name,".@Info");
  11110. s := module.allSections.FindByName(name);
  11111. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11112. NScope(declared.recordScope, pos);
  11113. |declared: SyntaxTree.CellType DO
  11114. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11115. Symbol(section, s, 0, offset);
  11116. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11117. Basic.AppendToSegmentedName(name,".@Info");
  11118. s := module.allSections.FindByName(name);
  11119. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11120. NScope(declared.cellScope, pos);
  11121. ELSE
  11122. Address(section, 0);
  11123. END;
  11124. END NTypeDeclaration;
  11125. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11126. VAR pos: LONGINT;
  11127. BEGIN
  11128. pos := CurrentIndex();
  11129. Char(section,sfModule);
  11130. Size(section, prevSymbol);
  11131. String0(section, module.name);
  11132. NScope(module.moduleScope, pos);
  11133. END NModule;
  11134. (*
  11135. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11136. *)
  11137. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11138. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11139. BEGIN
  11140. IF scope = NIL THEN RETURN END;
  11141. IF RefInfo THEN Info(section, "Scope") END;
  11142. Char(section, sfScopeBegin);
  11143. variable := scope.firstVariable;
  11144. WHILE (variable # NIL) DO
  11145. NVariable(variable, prevSymbol);
  11146. variable := variable.nextVariable;
  11147. END;
  11148. WITH scope: SyntaxTree.ModuleScope DO
  11149. bodyProcedure := scope.bodyProcedure;
  11150. |scope: SyntaxTree.RecordScope DO
  11151. bodyProcedure := scope.bodyProcedure;
  11152. ELSE
  11153. bodyProcedure := NIL;
  11154. END;
  11155. IF bodyProcedure # NIL THEN
  11156. NProcedure(bodyProcedure, prevSymbol)
  11157. END;
  11158. procedure := scope.firstProcedure;
  11159. WHILE procedure # NIL DO
  11160. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11161. procedure := procedure.nextProcedure;
  11162. END;
  11163. typeDeclaration := scope.firstTypeDeclaration;
  11164. WHILE typeDeclaration # NIL DO
  11165. NTypeDeclaration(typeDeclaration, prevSymbol);
  11166. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11167. END;
  11168. Char(section, sfScopeEnd); (* scope ends *)
  11169. END NScope;
  11170. BEGIN
  11171. NEW(indirectTypes, 32);
  11172. ArrayBlock(section,sizePC,"", FALSE);
  11173. startPC := section.pc;
  11174. NModule(module.module, -1);
  11175. PatchArray(section,sizePC,CurrentIndex());
  11176. END References;
  11177. (*
  11178. Command* = RECORD
  11179. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11180. name*: Name; (* name of the procedure *)
  11181. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11182. entryAdr* : ADDRESS; (* entry address of procedure *)
  11183. END;
  11184. *)
  11185. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11186. VAR
  11187. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11188. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11189. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11190. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11191. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11192. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11193. BEGIN
  11194. RETURN
  11195. (type = NIL) OR
  11196. (type.resolved IS SyntaxTree.RecordType) OR
  11197. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11198. (type.resolved IS SyntaxTree.AnyType);
  11199. END TypeAllowed;
  11200. BEGIN
  11201. numberParameters := procedureType.numberParameters;
  11202. RETURN
  11203. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11204. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11205. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11206. END GetProcedureAllowed;
  11207. PROCEDURE WriteType(type : SyntaxTree.Type);
  11208. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11209. name: Basic.SegmentedName; offset: LONGINT;
  11210. BEGIN
  11211. IF type = NIL THEN
  11212. Address(source,0);
  11213. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11214. Address(source,1);
  11215. ELSE
  11216. type := type.resolved;
  11217. IF type IS SyntaxTree.PointerType THEN
  11218. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11219. END;
  11220. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11221. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11222. name[0] := typeDeclaration.name; name[1] := -1;
  11223. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11224. ASSERT(section # NIL);
  11225. ELSE
  11226. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11227. (* TODO *)
  11228. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11229. END;
  11230. IF implementationVisitor.backend.cooperative THEN
  11231. offset := 0;
  11232. ELSE
  11233. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11234. END;
  11235. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11236. END;
  11237. END WriteType;
  11238. BEGIN
  11239. Info(source, "command array descriptor");
  11240. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11241. numberCommands := 0;
  11242. Info(source, "command array content");
  11243. FOR i := 0 TO module.allSections.Length() - 1 DO
  11244. p := module.allSections.GetSection(i);
  11245. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11246. procedure := p.symbol(SyntaxTree.Procedure);
  11247. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11248. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11249. procedure.GetName(name);
  11250. Name(source,name);
  11251. numberParameters := procedureType.numberParameters;
  11252. (* offset of type of first parameter *)
  11253. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11254. ELSE WriteType(procedureType.firstParameter.type)
  11255. END;
  11256. (* offset of type of return parameter *)
  11257. WriteType(procedureType.returnType);
  11258. (* command name *)
  11259. (* command code offset *)
  11260. Symbol(source,p,0,0);
  11261. INC(numberCommands);
  11262. IF Trace THEN
  11263. D.Ln;
  11264. END;
  11265. END;
  11266. END
  11267. END;
  11268. PatchArray(source,sizePC,numberCommands);
  11269. END CommandArray;
  11270. (* to prevent from double import of different module aliases *)
  11271. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11272. VAR i: SyntaxTree.Import;
  11273. BEGIN
  11274. i := module.module.moduleScope.firstImport;
  11275. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11276. i := i.nextImport;
  11277. END;
  11278. RETURN i = import
  11279. END IsFirstDirectOccurence;
  11280. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11281. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11282. BEGIN
  11283. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11284. ArrayBlock(source,pc,"", FALSE);
  11285. Info(source, "import module array data");
  11286. IF implementationVisitor.backend.cooperative THEN
  11287. offset := 0;
  11288. ELSE
  11289. IF module.system.addressType.sizeInBits = 64 THEN
  11290. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11291. ELSE
  11292. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11293. END;
  11294. END;
  11295. import := module.module.moduleScope.firstImport;
  11296. numberImports := 0;
  11297. WHILE import # NIL DO
  11298. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11299. Global.GetModuleSegmentedName(import.module,name);
  11300. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11301. NamedSymbol(source, name, NIL, 0, offset);
  11302. INC(numberImports);
  11303. END;
  11304. import := import.nextImport
  11305. END;
  11306. PatchArray(source,pc,numberImports);
  11307. END ImportsArray;
  11308. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11309. VAR
  11310. p: Sections.Section; sizePC, size, i: LONGINT;
  11311. BEGIN
  11312. Info(source, "Type info section");
  11313. size := 0;
  11314. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11315. FOR i := 0 TO module.allSections.Length() - 1 DO
  11316. p := module.allSections.GetSection(i);
  11317. WITH p: IntermediateCode.Section DO
  11318. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11319. Symbol(source,p,0,0);
  11320. INC(size);
  11321. END;
  11322. END
  11323. END;
  11324. PatchArray(source,sizePC,size);
  11325. END TypeInfoSection;
  11326. (*
  11327. ProcTableEntry* = RECORD
  11328. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11329. noPtr*: LONGINT;
  11330. END;
  11331. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11332. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11333. *)
  11334. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11335. VAR
  11336. numberPointers: SIZE;
  11337. procedure: SyntaxTree.Procedure;
  11338. BEGIN
  11339. Info(section,"pcFrom");
  11340. Symbol(section,procedureSection,0,0);
  11341. Info(section,"pcTo");
  11342. Symbol(section, procedureSection, procedureSection.pc, 0);
  11343. Info(section,"pointer to offsets array");
  11344. Symbol(section, section,section.pc+1,0);
  11345. Info(section,"offsets array");
  11346. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11347. PointerArray(section, procedure.procedureScope, numberPointers);
  11348. END ProcedureDescriptor;
  11349. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11350. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11351. BEGIN
  11352. name := procedureSection.name;
  11353. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11354. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11355. ProcedureDescriptor(dest, procedureSection);
  11356. Symbol(section, dest, offset, 0);
  11357. END ProcedureDescriptorPointer;
  11358. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11359. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11360. BEGIN
  11361. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11362. numberProcs := 0;
  11363. FOR i := 0 TO module.allSections.Length() - 1 DO
  11364. destination := module.allSections.GetSection(i);
  11365. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11366. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11367. INC(numberProcs);
  11368. END
  11369. END;
  11370. PatchArray(section, sizePC, numberProcs);
  11371. END ProcedureDescriptorArray;
  11372. (*
  11373. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11374. VAR
  11375. next*: Module; (** once a module is published, all fields are read-only *)
  11376. name*: Name;
  11377. init, published: BOOLEAN;
  11378. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11379. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11380. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11381. command*: POINTER TO ARRAY OF Command;
  11382. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11383. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11384. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11385. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11386. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11387. data*, code*: Bytes;
  11388. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11389. export*: ExportDesc;
  11390. term*: TerminationHandler;
  11391. exTable*: ExceptionTable;
  11392. noProcs*: LONGINT;
  11393. firstProc*: ADDRESS; <- done by loader
  11394. maxPtrs*: LONGINT;
  11395. crc*: LONGINT;
  11396. *)
  11397. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11398. BEGIN
  11399. Info(section, "cycle");
  11400. Size(section,0);
  11401. Info(section, "references");
  11402. Size(section,0);
  11403. Info(section, "nextMarked");
  11404. Address(section,0);
  11405. Info(section, "nextWatched");
  11406. Address(section,0);
  11407. END BasePointer;
  11408. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11409. BEGIN
  11410. BasePointer(section);
  11411. Info(section, "action");
  11412. Address(section,0);
  11413. Info(section, "monitor");
  11414. Address(section,0);
  11415. END BaseObject;
  11416. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11417. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11418. pooledName: Basic.SegmentedName;
  11419. symbol: SyntaxTree.Symbol;
  11420. BEGIN
  11421. Global.GetModuleName(module.module,name);
  11422. Strings.Append(name,".@Module.@Descriptor");
  11423. Basic.ToSegmentedName(name, pooledName);
  11424. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11425. Symbol(section,descriptorSection,0,0);
  11426. Info(descriptorSection, "descriptor");
  11427. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11428. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11429. Address(descriptorSection,0);
  11430. Global.GetModuleName(module.module,name);
  11431. Strings.Append(name,".@Trace");
  11432. Basic.ToSegmentedName(name, pooledName);
  11433. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11434. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11435. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11436. END ModuleDescriptor;
  11437. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11438. VAR name: ARRAY 128 OF CHAR;
  11439. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11440. symbol: SyntaxTree.Symbol;
  11441. BEGIN
  11442. Global.GetModuleName(module.module,name);
  11443. Strings.Append(name,".@Module");
  11444. Basic.ToSegmentedName(name, pooledName);
  11445. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11446. moduleSection.SetExported(TRUE);
  11447. IF moduleSection.pc = 0 THEN
  11448. IF implementationVisitor.backend.cooperative THEN
  11449. Info(moduleSection, "descriptor");
  11450. ModuleDescriptor(moduleSection);
  11451. BaseObject(moduleSection);
  11452. implementationVisitor.CreateTraceModuleMethod(module.module);
  11453. ELSE
  11454. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11455. Info(moduleSection, "HeapBlock");
  11456. Symbol(moduleSection,moduleSection,2,0);
  11457. Info(moduleSection, "TypeDescriptor");
  11458. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11459. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11460. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11461. END;
  11462. END;
  11463. RETURN moduleSection;
  11464. END ModuleSection;
  11465. PROCEDURE NewModuleInfo();
  11466. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11467. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11468. sectionName: Basic.SectionName;
  11469. CONST MPO=-40000000H;
  11470. BEGIN
  11471. (*
  11472. TypeDesc* = POINTER TO RECORD
  11473. descSize: LONGINT;
  11474. sentinel: LONGINT; (* = MPO-4 *)
  11475. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11476. flags*: SET;
  11477. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11478. name*: Name;
  11479. END;
  11480. *)
  11481. Global.GetSymbolSegmentedName(module.module,name);
  11482. Basic.AppendToSegmentedName(name,".@Info");
  11483. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11484. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11485. Address(source,MPO-4);
  11486. Info(source, "type tag pointer");
  11487. Address( source,0);
  11488. Info(source, "type flags");
  11489. flags := {};
  11490. Set( source, flags);
  11491. Info(source, "pointer to module");
  11492. moduleSection := ModuleSection();
  11493. Symbol( source, moduleSection, moduleSection.pc,0);
  11494. Info(source, "type name");
  11495. i := 0;
  11496. sectionName := "@Self";
  11497. (*
  11498. Global.GetSymbolSegmentedName(td,name);
  11499. Basic.SegmentedNameToString(name, sectionName);
  11500. *)
  11501. Name(source,sectionName);
  11502. source.SetReferenced(FALSE);
  11503. patchInfoPC := source.pc;
  11504. Size(source, 0);
  11505. END NewModuleInfo;
  11506. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11507. VAR
  11508. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11509. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11510. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11511. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11512. referenceSectionOffset : LONGINT;
  11513. name: Basic.SegmentedName; offset: LONGINT;
  11514. BEGIN
  11515. NewModuleInfo();
  11516. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11517. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11518. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11519. ImportsArray(importSection);
  11520. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11521. CommandArray(commandsSection);
  11522. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11523. ExceptionArray(exceptionSection);
  11524. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11525. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11526. TypeInfoSection(typeInfoSection);
  11527. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11528. References(referenceSection);
  11529. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11530. ProcedureDescriptorArray(procTableSection, numberProcs);
  11531. name := "Heaps.AnyPtr";
  11532. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11533. (* set base pointer *)
  11534. NamedSymbolAt(procTableSection, procTableSectionOffset -1, name, NIL, 0, offset);
  11535. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11536. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11537. moduleSection := ModuleSection();
  11538. Info(moduleSection, "nextRoot*: RootObject");
  11539. Address(moduleSection,0);
  11540. Info(moduleSection, "next*: Module");
  11541. Address(moduleSection,0);
  11542. Info(moduleSection, "name*: Name");
  11543. Name(moduleSection,moduleName);
  11544. Info(moduleSection, "init, published: BOOLEAN");
  11545. Boolean(moduleSection,FALSE);
  11546. Boolean(moduleSection,FALSE);
  11547. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11548. Boolean(moduleSection,FALSE);
  11549. Boolean(moduleSection,FALSE);
  11550. Info(moduleSection, "refcnt*: LONGINT");
  11551. Longint(moduleSection,0);
  11552. Info(moduleSection, "sb*: ADDRESS");
  11553. Address(moduleSection,0);
  11554. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11555. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11556. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11557. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11558. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11559. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11560. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11561. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11562. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11563. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11564. Info(moduleSection, "procTable*: ProcTable");
  11565. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11566. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11567. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11568. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11569. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11570. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11571. Info(moduleSection, "export*: ExportDesc");
  11572. ExportDesc(moduleSection);
  11573. Info(moduleSection, "term*: TerminationHandler");
  11574. Address(moduleSection,0);
  11575. Info(moduleSection, "exTable*: ExceptionTable");
  11576. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11577. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11578. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11579. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11580. Info(moduleSection, "crc*: LONGINT");
  11581. Longint(moduleSection, 0); (*! must be implemented *)
  11582. Info(moduleSection, "body*: ADDRESS");
  11583. Symbol(moduleSection, bodyProc, 0,0);
  11584. IF implementationVisitor.backend.cooperative THEN
  11585. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11586. Info(moduleSection, "monitor.owner");
  11587. Address(moduleSection,0);
  11588. Info(moduleSection, "monitor.nestingLevel");
  11589. Address(moduleSection,0);
  11590. Info(moduleSection, "monitor.blockedQueue");
  11591. Address(moduleSection,0); Address(moduleSection,0);
  11592. Info(moduleSection, "monitor.waitingQueue");
  11593. Address(moduleSection,0); Address(moduleSection,0);
  11594. Info(moduleSection, "monitor.waitingSentinel");
  11595. Address(moduleSection,0);
  11596. END;
  11597. END Module;
  11598. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11599. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11600. BEGIN
  11601. ArrayBlock(source,pc,"",FALSE);
  11602. Info(source, "pointer offsets array data");
  11603. IF scope IS SyntaxTree.RecordScope THEN
  11604. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11605. ELSIF scope IS SyntaxTree.CellScope THEN
  11606. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11607. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11608. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11609. WHILE variable # NIL DO
  11610. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11611. symbol := module.allSections.FindBySymbol(variable);
  11612. ASSERT(symbol # NIL);
  11613. Pointers(0,symbol, source,variable.type,numberPointers);
  11614. END;
  11615. variable := variable.nextVariable;
  11616. END;
  11617. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11618. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11619. WHILE parameter # NIL DO
  11620. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11621. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11622. END;
  11623. parameter := parameter.nextParameter;
  11624. END;
  11625. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11626. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11627. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11628. IF implementationVisitor.backend.preciseGC THEN
  11629. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11630. END;
  11631. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11632. END;
  11633. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11634. WHILE(variable # NIL) DO
  11635. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11636. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11637. END;
  11638. variable := variable.nextVariable
  11639. END;
  11640. END;
  11641. PatchArray(source,pc,numberPointers);
  11642. END PointerArray;
  11643. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11644. VAR
  11645. name: Basic.SegmentedName;
  11646. section: IntermediateCode.Section;
  11647. BEGIN
  11648. ASSERT(implementationVisitor.newObjectFile);
  11649. Global.GetSymbolSegmentedName(symbol,name);
  11650. Basic.AppendToSegmentedName(name,suffix);
  11651. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11652. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11653. Info(section, "HeapBlock");
  11654. Address(section,0); (* empty such that GC does not go on traversing *)
  11655. Info(section, suffix);
  11656. Address(section,0);
  11657. pc := section.pc;
  11658. RETURN section;
  11659. END SymbolSection;
  11660. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11661. VAR recordType: SyntaxTree.RecordType;
  11662. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11663. section: Sections.Section; cellType: SyntaxTree.CellType;
  11664. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11665. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11666. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11667. sectionName: Basic.SectionName;
  11668. CONST MPO=-40000000H;
  11669. BEGIN
  11670. (*
  11671. TypeDesc* = POINTER TO RECORD
  11672. descSize: LONGINT;
  11673. sentinel: LONGINT; (* = MPO-4 *)
  11674. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11675. flags*: SET;
  11676. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11677. name*: Name;
  11678. END;
  11679. *)
  11680. (* source := module.sections.FindByName(...) *)
  11681. Global.GetSymbolSegmentedName(td,name);
  11682. Basic.AppendToSegmentedName(name,".@Info");
  11683. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11684. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11685. Address(source,MPO-4);
  11686. Info(source, "type tag pointer");
  11687. Symbol( source, tag, offset, 0);
  11688. Info(source, "type flags");
  11689. flags := {};
  11690. IF isProtected THEN INCL(flags,31) END;
  11691. Set( source, flags);
  11692. Info(source, "pointer to module");
  11693. moduleSection := ModuleSection();
  11694. Symbol( source, moduleSection, moduleSection.pc,0);
  11695. Info(source, "type name");
  11696. i := 0;
  11697. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11698. (*
  11699. Global.GetSymbolSegmentedName(td,name);
  11700. Basic.SegmentedNameToString(name, sectionName);
  11701. *)
  11702. Name(source,sectionName);
  11703. source.SetReferenced(FALSE);
  11704. Size(source, 0);
  11705. RETURN source;
  11706. END NewTypeDescriptorInfo;
  11707. PROCEDURE NewTypeDescriptor;
  11708. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11709. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11710. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11711. numberPointers: LONGINT; padding, i: LONGINT;
  11712. CONST MPO=-40000000H;
  11713. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11714. VAR i: LONGINT;
  11715. PROCEDURE Td(record: SyntaxTree.RecordType);
  11716. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11717. BEGIN
  11718. IF record # NIL THEN
  11719. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11720. baseTD := record.typeDeclaration;
  11721. Global.GetSymbolSegmentedName(baseTD,name);
  11722. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11723. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11724. ELSE
  11725. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11726. END;
  11727. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11728. Symbol(source, tir, 0, offset);
  11729. IF reverse THEN Td(record.GetBaseRecord()) END;
  11730. END;
  11731. END Td;
  11732. BEGIN
  11733. Info(source, "tag table");
  11734. baseRecord := recordType;
  11735. i := 0;
  11736. WHILE baseRecord # NIL DO
  11737. INC(i);
  11738. baseRecord := baseRecord.GetBaseRecord();
  11739. END;
  11740. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11741. IF ~reverse THEN Td(recordType) END;
  11742. WHILE i < size DO
  11743. Address(source,0);
  11744. INC(i);
  11745. END;
  11746. IF reverse THEN Td(recordType) END;
  11747. END TdTable;
  11748. PROCEDURE MethodTable(reverse: BOOLEAN);
  11749. VAR i,methods: LONGINT;
  11750. BEGIN
  11751. Info(source, "method table");
  11752. IF recordType # NIL THEN
  11753. methods := recordType.recordScope.numberMethods;
  11754. IF reverse THEN
  11755. FOR i := methods-1 TO 0 BY -1 DO
  11756. procedure := recordType.recordScope.FindMethod(i);
  11757. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11758. NamedSymbol(source, name,procedure, 0,0);
  11759. END;
  11760. ELSE
  11761. FOR i := 0 TO methods-1 DO
  11762. procedure := recordType.recordScope.FindMethod(i);
  11763. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11764. NamedSymbol(source, name,procedure, 0,0);
  11765. END;
  11766. END;
  11767. END;
  11768. END MethodTable;
  11769. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11770. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11771. BEGIN
  11772. Info(source, "method table");
  11773. baseRecord := recordType;
  11774. WHILE baseRecord.baseType # NIL DO
  11775. baseRecord := baseRecord.GetBaseRecord ();
  11776. END;
  11777. GetRecordTypeName (baseRecord, name);
  11778. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11779. IF name = stackFrame THEN
  11780. start := 0;
  11781. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11782. baseRecord := recordType;
  11783. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11784. baseRecord := baseRecord.GetBaseRecord ();
  11785. END;
  11786. IF baseRecord # NIL THEN
  11787. GetRecordTypeName (baseRecord, name);
  11788. IF pointer & ~baseRecord.isObject THEN
  11789. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11790. END;
  11791. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11792. ELSIF recordType.isObject THEN
  11793. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11794. ELSIF pointer THEN
  11795. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11796. ELSE
  11797. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11798. END;
  11799. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11800. Symbol(source, tir, 0, 0);
  11801. start := 0;
  11802. baseRecord := recordType;
  11803. WHILE (baseRecord # NIL) DO
  11804. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11805. baseRecord := baseRecord.GetBaseRecord ();
  11806. END;
  11807. ELSE
  11808. (* explicit trace method: *)
  11809. procedure := recordType.recordScope.FindMethod(0);
  11810. IF ~procedure.isFinalizer THEN
  11811. Global.GetSymbolSegmentedName(procedure, name);
  11812. NamedSymbol(source, name,procedure, 0,0);
  11813. END;
  11814. start := 1;
  11815. END;
  11816. IF (name # stackFrame) & recordType.isObject THEN
  11817. baseRecord := recordType;
  11818. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11819. baseRecord := baseRecord.GetBaseRecord ();
  11820. END;
  11821. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11822. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11823. ELSE
  11824. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11825. END;
  11826. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11827. Symbol(source, tir, 0, 0);
  11828. END;
  11829. methods := recordType.recordScope.numberMethods;
  11830. FOR i := start TO methods-1 DO
  11831. procedure := recordType.recordScope.FindMethod(i);
  11832. IF ~procedure.isFinalizer THEN
  11833. Global.GetSymbolSegmentedName(procedure, name);
  11834. NamedSymbol(source, name,procedure, 0,0);
  11835. END;
  11836. END;
  11837. END CooperativeMethodTable;
  11838. BEGIN
  11839. Global.GetSymbolSegmentedName(td,name);
  11840. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11841. source.SetExported(IsExported(td));
  11842. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11843. IF implementationVisitor.backend.cooperative THEN
  11844. base := NIL;
  11845. baseRecord := recordType.GetBaseRecord();
  11846. IF baseRecord # NIL THEN
  11847. baseTD := baseRecord.typeDeclaration;
  11848. END;
  11849. IF ~recordType.isObject THEN
  11850. Info(source, "parent");
  11851. IF baseRecord # NIL THEN
  11852. Global.GetSymbolSegmentedName(baseTD,name);
  11853. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11854. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11855. ELSE
  11856. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11857. END;
  11858. Symbol(source, tir, 0, 0);
  11859. ELSE
  11860. Address(source,0);
  11861. END;
  11862. Info(source, "record size");
  11863. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11864. source.SetReferenced(FALSE);
  11865. CooperativeMethodTable(FALSE);
  11866. base := source;
  11867. Global.GetSymbolSegmentedName(td,name);
  11868. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11869. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11870. source.SetExported(IsExported(td));
  11871. source.SetReferenced(FALSE);
  11872. END;
  11873. Info(source, "parent");
  11874. IF baseRecord # NIL THEN
  11875. Global.GetSymbolSegmentedName(baseTD,name);
  11876. sym := baseTD;
  11877. IF ~recordType.isObject THEN
  11878. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11879. sym := NIL;
  11880. END;
  11881. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11882. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11883. ELSE
  11884. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11885. END;
  11886. Symbol(source, tir, 0, 0);
  11887. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11888. Address(source,0);
  11889. ELSE
  11890. IF recordType.isObject THEN
  11891. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11892. ELSE
  11893. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11894. END;
  11895. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11896. Symbol(source, tir, 0, 0);
  11897. END;
  11898. Info(source, "base record descriptor");
  11899. Symbol(source, base, 0, 0);
  11900. CooperativeMethodTable(TRUE);
  11901. source.SetReferenced(FALSE);
  11902. IF recordType.hasPointers THEN
  11903. IF ~HasExplicitTraceMethod (recordType) THEN
  11904. implementationVisitor.CreateTraceMethod(recordType);
  11905. END;
  11906. implementationVisitor.CreateResetProcedure(recordType);
  11907. implementationVisitor.CreateAssignProcedure(recordType);
  11908. END;
  11909. ELSIF ~simple THEN
  11910. (*
  11911. MethodEnd = MPO
  11912. ---
  11913. methods (# methods)
  11914. ---
  11915. tags (16)
  11916. ---
  11917. TypeDesc = TypeInfoAdr
  11918. ---
  11919. td adr ---> rec size
  11920. ----
  11921. pointer offsets
  11922. ----
  11923. (padding)
  11924. -----
  11925. empty [2 addresses aligned]
  11926. empty
  11927. empty
  11928. numPtrs
  11929. ---
  11930. pointer offsets
  11931. ---
  11932. *)
  11933. Info(source, "MethodEnd = MPO");
  11934. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11935. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11936. MethodTable(TRUE);
  11937. TdTable(TypeTags, TRUE);
  11938. Info(source, "type descriptor info pointer");
  11939. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11940. IF (cellType # NIL) THEN
  11941. IF cellType.sizeInBits < 0 THEN
  11942. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11943. END;
  11944. Info(source, "cell size");
  11945. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11946. ELSE
  11947. Info(source, "record size");
  11948. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11949. END;
  11950. Info(source, "pointer offsets pointer");
  11951. padding := 1- source.pc MOD 2;
  11952. Symbol(source, source, source.pc+1+padding,0);
  11953. IF padding >0 THEN
  11954. Info(source, "padding");
  11955. FOR i := 1 TO padding DO Address(source,0) END;
  11956. END;
  11957. IF cellType # NIL THEN
  11958. PointerArray(source, cellType.cellScope, numberPointers);
  11959. ELSE
  11960. PointerArray(source, recordType.recordScope, numberPointers);
  11961. END;
  11962. ELSE
  11963. (*
  11964. simple:
  11965. td adr --> size
  11966. tag(1)
  11967. tag(2)
  11968. tag(3)
  11969. methods ->
  11970. *)
  11971. Info(source, "record size");
  11972. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11973. TdTable(TypeTags, FALSE);
  11974. MethodTable(FALSE);
  11975. source.SetReferenced(FALSE);
  11976. END;
  11977. END NewTypeDescriptor;
  11978. BEGIN
  11979. x := x.resolved;
  11980. IF (x IS SyntaxTree.PointerType) THEN
  11981. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11982. END;
  11983. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11984. recordType := x(SyntaxTree.RecordType);
  11985. td := x.typeDeclaration;
  11986. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11987. ASSERT(td # NIL);
  11988. section := module.allSections.FindBySymbol(td); (* TODO *)
  11989. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11990. IF implementationVisitor.newObjectFile THEN
  11991. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11992. NewTypeDescriptor
  11993. END;
  11994. ELSE
  11995. (* data section in intermediate code *)
  11996. Global.GetSymbolSegmentedName(td,name);
  11997. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  11998. Basic.SuffixSegmentedName (name, module.module.name);
  11999. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12000. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12001. tir.Emit(Data(Basic.invalidPosition,op));
  12002. END;
  12003. END;
  12004. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12005. cellType := x(SyntaxTree.CellType);
  12006. td := x.typeDeclaration;
  12007. section := module.allSections.FindBySymbol(td); (* TODO *)
  12008. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12009. IF implementationVisitor.newObjectFile THEN
  12010. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12011. NewTypeDescriptor
  12012. END;
  12013. END;
  12014. END;
  12015. END
  12016. END CheckTypeDeclaration
  12017. END MetaDataGenerator;
  12018. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12019. VAR
  12020. trace-: BOOLEAN;
  12021. traceString-: SyntaxTree.IdentifierString;
  12022. traceModuleName-: SyntaxTree.IdentifierString;
  12023. newObjectFile-: BOOLEAN;
  12024. profile-: BOOLEAN;
  12025. noRuntimeChecks: BOOLEAN;
  12026. simpleMetaData-: BOOLEAN;
  12027. noAsserts: BOOLEAN;
  12028. optimize-: BOOLEAN;
  12029. cooperative-: BOOLEAN;
  12030. preregisterStatic-: BOOLEAN;
  12031. dump-: Basic.Writer;
  12032. cellsAreObjects: BOOLEAN;
  12033. preciseGC: BOOLEAN;
  12034. PROCEDURE &InitIntermediateBackend*;
  12035. BEGIN
  12036. simpleMetaData := FALSE;
  12037. newObjectFile := FALSE;
  12038. InitBackend;
  12039. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12040. SetTraceModuleName(DefaultTraceModuleName);
  12041. END InitIntermediateBackend;
  12042. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12043. VAR
  12044. declarationVisitor: DeclarationVisitor;
  12045. implementationVisitor: ImplementationVisitor;
  12046. module: Sections.Module;
  12047. name, platformName: SyntaxTree.IdentifierString;
  12048. meta: MetaDataGenerator;
  12049. BEGIN
  12050. ResetError;
  12051. Global.GetSymbolName(x,name);
  12052. NEW(module,x,system); (* backend structures *)
  12053. Global.GetModuleName(x, name);
  12054. module.SetModuleName(name);
  12055. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12056. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12057. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12058. declarationVisitor.Module(x,module);
  12059. IF newObjectFile & ~meta.simple THEN
  12060. meta.Module(implementationVisitor.moduleBodySection);
  12061. END;
  12062. GetDescription(platformName);
  12063. module.SetPlatformName(platformName);
  12064. RETURN module
  12065. END GenerateIntermediate;
  12066. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12067. BEGIN RETURN TRUE
  12068. END SupportedImmediate;
  12069. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12070. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12071. END ProcessSyntaxTreeModule;
  12072. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12073. VAR
  12074. result: Sections.Module;
  12075. traceName: Basic.MessageString;
  12076. BEGIN
  12077. ASSERT(intermediateCodeModule IS Sections.Module);
  12078. result := intermediateCodeModule(Sections.Module);
  12079. IF trace THEN
  12080. traceName := "intermediate code trace: ";
  12081. Strings.Append(traceName,traceString);
  12082. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12083. IF (traceString="") OR (traceString="*") THEN
  12084. result.Dump(dump);
  12085. dump.Update
  12086. ELSE
  12087. Sections.DumpFiltered(dump, result, traceString);
  12088. END
  12089. END;
  12090. RETURN result
  12091. END ProcessIntermediateCodeModule;
  12092. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12093. BEGIN instructionSet := "Intermediate";
  12094. END GetDescription;
  12095. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12096. BEGIN
  12097. SELF.newObjectFile := newObjectFile;
  12098. SELF.simpleMetaData := simpleMetaData;
  12099. END SetNewObjectFile;
  12100. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12101. BEGIN COPY(name, traceModuleName)
  12102. END SetTraceModuleName;
  12103. PROCEDURE DefineOptions(options: Options.Options);
  12104. BEGIN
  12105. DefineOptions^(options);
  12106. options.Add(0X,"trace",Options.String);
  12107. options.Add(0X,"runtime",Options.String);
  12108. options.Add(0X,"newObjectFile",Options.Flag);
  12109. options.Add(0X,"traceModule",Options.String);
  12110. options.Add(0X,"profile",Options.Flag);
  12111. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12112. options.Add(0X,"noAsserts",Options.Flag);
  12113. options.Add(0X,"metaData",Options.String);
  12114. options.Add('o',"optimize", Options.Flag);
  12115. options.Add(0X,"preregisterStatic", Options.Flag);
  12116. options.Add(0X,"cellsAreObjects", Options.Flag);
  12117. options.Add(0X,"preciseGC", Options.Flag);
  12118. END DefineOptions;
  12119. PROCEDURE GetOptions(options: Options.Options);
  12120. VAR name,string: SyntaxTree.IdentifierString;
  12121. BEGIN
  12122. GetOptions^(options);
  12123. trace := options.GetString("trace",traceString);
  12124. profile := options.GetFlag("profile");
  12125. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12126. noAsserts := options.GetFlag("noAsserts");
  12127. cooperative := options.GetFlag("cooperative");
  12128. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12129. IF cooperative THEN
  12130. SetRuntimeModuleName("CPU") END
  12131. END;
  12132. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12133. simpleMetaData := TRUE
  12134. END;
  12135. IF options.GetString("metaData",string) THEN
  12136. IF string = "simple" THEN simpleMetaData := TRUE
  12137. ELSIF string ="full" THEN simpleMetaData := FALSE
  12138. END;
  12139. END;
  12140. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12141. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12142. optimize := options.GetFlag("optimize");
  12143. preregisterStatic := options.GetFlag("preregisterStatic");
  12144. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12145. preciseGC := options.GetFlag("preciseGC");
  12146. END GetOptions;
  12147. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12148. BEGIN RETURN SymbolFileFormat.Get()
  12149. END DefaultSymbolFileFormat;
  12150. END IntermediateBackend;
  12151. (* ----------------------------------- register allocation ------------------------------------- *)
  12152. (* register mapping scheme
  12153. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12154. spill offset
  12155. part(n) --> ticket <--> physical register
  12156. spill offset
  12157. *)
  12158. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12159. emptyOperand: IntermediateCode.Operand;
  12160. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12161. statCoopResetVariables: LONGINT;
  12162. statCoopModifyAssignments: LONGINT;
  12163. modifyAssignmentsPC : LONGINT;
  12164. statCoopNilCheck: LONGINT;
  12165. statCoopSwitch: LONGINT;
  12166. statCoopAssignProcedure: LONGINT;
  12167. statCoopTraceMethod: LONGINT;
  12168. statCoopResetProcedure: LONGINT;
  12169. statCoopTraceModule: LONGINT;
  12170. PROCEDURE ResetStatistics*;
  12171. BEGIN
  12172. statCoopResetVariables := 0;
  12173. statCoopModifyAssignments := 0;
  12174. statCoopNilCheck:= 0;
  12175. statCoopSwitch:= 0;
  12176. statCoopAssignProcedure:= 0;
  12177. statCoopTraceMethod:= 0;
  12178. statCoopResetProcedure:= 0;
  12179. statCoopTraceModule:= 0;
  12180. END ResetStatistics;
  12181. PROCEDURE Statistics*;
  12182. BEGIN
  12183. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12184. TRACE(statCoopNilCheck, statCoopSwitch);
  12185. TRACE(statCoopAssignProcedure,
  12186. statCoopTraceMethod,
  12187. statCoopResetProcedure,
  12188. statCoopTraceModule)
  12189. END Statistics;
  12190. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12191. VAR h: LONGINT;
  12192. BEGIN
  12193. WHILE b # 0 DO
  12194. h := a MOD b;
  12195. a := b;
  12196. b := h;
  12197. END;
  12198. RETURN a
  12199. END GCD;
  12200. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12201. BEGIN
  12202. RETURN a*b DIV GCD(a,b)
  12203. END SCM;
  12204. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12205. BEGIN
  12206. (*TRACE(a,b);*)
  12207. IF a = 0 THEN RETURN b
  12208. ELSIF b = 0 THEN RETURN a
  12209. ELSE RETURN SCM(a,b)
  12210. END;
  12211. END CommonAlignment;
  12212. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12213. BEGIN
  12214. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12215. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12216. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12217. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12218. 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)
  12219. END
  12220. END PassBySingleReference;
  12221. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12222. BEGIN
  12223. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12224. END PassInRegister;
  12225. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12226. VAR new: RegisterEntry;
  12227. BEGIN
  12228. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12229. IF queue = NIL THEN
  12230. queue := new
  12231. ELSE
  12232. new.next := queue;
  12233. IF queue#NIL THEN queue.prev := new END;
  12234. queue := new
  12235. END;
  12236. END AddRegisterEntry;
  12237. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12238. VAR this: RegisterEntry;
  12239. BEGIN
  12240. this := queue;
  12241. WHILE (this # NIL) & (this.register # register) DO
  12242. this := this.next;
  12243. END;
  12244. IF this = NIL THEN
  12245. RETURN FALSE
  12246. END;
  12247. ASSERT(this # NIL);
  12248. IF this = queue THEN queue := queue.next END;
  12249. IF this.prev # NIL THEN this.prev.next := this.next END;
  12250. IF this.next # NIL THEN this.next.prev := this.prev END;
  12251. RETURN TRUE
  12252. END RemoveRegisterEntry;
  12253. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12254. BEGIN ASSERT(cond);
  12255. END Assert;
  12256. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12257. BEGIN
  12258. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12259. END ReusableRegister;
  12260. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12261. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12262. BEGIN
  12263. procedure := moduleScope.bodyProcedure;
  12264. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12265. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12266. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12267. procedure.SetScope(moduleScope);
  12268. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12269. procedure.SetAccess(SyntaxTree.Hidden);
  12270. moduleScope.SetBodyProcedure(procedure);
  12271. moduleScope.AddProcedure(procedure);
  12272. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12273. END;
  12274. END EnsureBodyProcedure;
  12275. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12276. VAR import: SyntaxTree.Import;
  12277. selfName: SyntaxTree.IdentifierString;
  12278. module: SyntaxTree.Module;
  12279. BEGIN
  12280. scope.ownerModule.GetName(selfName);
  12281. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12282. module := scope.ownerModule
  12283. ELSE
  12284. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12285. IF import = NIL THEN
  12286. RETURN NIL
  12287. ELSIF import.module = NIL THEN
  12288. RETURN NIL
  12289. ELSE module := import.module
  12290. END;
  12291. END;
  12292. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12293. END GetSymbol;
  12294. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12295. BEGIN
  12296. op.mode := mode;
  12297. IntermediateCode.InitOperand(op.op);
  12298. IntermediateCode.InitOperand(op.tag);
  12299. IntermediateCode.InitOperand(op.extra);
  12300. op.dimOffset := 0;
  12301. END InitOperand;
  12302. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12303. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12304. BEGIN RETURN IntermediateCode.GetType(system, type)
  12305. END GetType;
  12306. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12307. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12308. BEGIN
  12309. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12310. (*
  12311. UniqueId(name,module,name,adr);
  12312. *)
  12313. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12314. constant.SetValue(value);
  12315. constant.SetAccess(SyntaxTree.Hidden);
  12316. module.moduleScope.AddConstant(constant);
  12317. constant.SetScope(module.moduleScope);
  12318. RETURN constant
  12319. END BuildConstant;
  12320. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12321. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12322. BEGIN
  12323. variable := scope.firstVariable;
  12324. WHILE variable # NIL DO
  12325. IF variable.NeedsTrace() THEN
  12326. RETURN TRUE;
  12327. END;
  12328. variable := variable.nextVariable;
  12329. END;
  12330. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12331. WHILE parameter # NIL DO
  12332. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12333. RETURN TRUE;
  12334. END;
  12335. parameter := parameter.nextParameter;
  12336. END;
  12337. RETURN FALSE;
  12338. END HasPointers;
  12339. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12340. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12341. END IsVariableParameter;
  12342. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12343. VAR parameter: SyntaxTree.Parameter;
  12344. BEGIN
  12345. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12346. WHILE parameter # NIL DO
  12347. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12348. IF parameter.movable THEN RETURN TRUE END;
  12349. parameter := parameter.nextParameter;
  12350. END;
  12351. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12352. END HasVariableParameters;
  12353. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12354. BEGIN
  12355. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12356. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12357. END HasExplicitTraceMethod;
  12358. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12359. BEGIN
  12360. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12361. IF (expression IS SyntaxTree.IntegerValue) THEN
  12362. val := expression(SyntaxTree.IntegerValue).value;
  12363. RETURN TRUE
  12364. ELSE
  12365. RETURN FALSE
  12366. END;
  12367. END IsIntegerConstant;
  12368. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12369. BEGIN
  12370. IF val <= 0 THEN RETURN FALSE END;
  12371. exp := 0;
  12372. WHILE ~ODD(val) DO
  12373. val := val DIV 2;
  12374. INC(exp)
  12375. END;
  12376. RETURN val = 1
  12377. END PowerOf2;
  12378. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12379. VAR procedure: SyntaxTree.Procedure;
  12380. BEGIN
  12381. procedure := record.recordScope.constructor;
  12382. IF procedure = NIL THEN
  12383. record := record.GetBaseRecord();
  12384. IF record # NIL THEN
  12385. procedure := GetConstructor(record)
  12386. END;
  12387. END;
  12388. RETURN procedure;
  12389. END GetConstructor;
  12390. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12391. BEGIN
  12392. value := SHORT(op.intValue);
  12393. RETURN op.mode = IntermediateCode.ModeImmediate;
  12394. END IsIntegerImmediate;
  12395. (** whether a type strictily is a pointer to record or object type
  12396. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12397. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12398. BEGIN
  12399. IF type = NIL THEN
  12400. RETURN FALSE
  12401. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12402. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12403. ELSE
  12404. RETURN FALSE
  12405. END
  12406. END IsStrictlyPointerToRecord;
  12407. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12408. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12409. END IsUnsafePointer;
  12410. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12411. BEGIN type := type.resolved;
  12412. IF type IS SyntaxTree.PointerType THEN
  12413. type := type(SyntaxTree.PointerType).pointerBase;
  12414. type := type.resolved;
  12415. IF type IS SyntaxTree.RecordType THEN
  12416. recordType := type(SyntaxTree.RecordType);
  12417. RETURN TRUE
  12418. ELSE
  12419. RETURN FALSE
  12420. END
  12421. ELSIF type IS SyntaxTree.RecordType THEN
  12422. recordType := type(SyntaxTree.RecordType);
  12423. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12424. ELSIF type IS SyntaxTree.ObjectType THEN
  12425. RETURN TRUE
  12426. ELSIF type IS SyntaxTree.AnyType THEN
  12427. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12428. ELSE
  12429. RETURN FALSE
  12430. END;
  12431. END IsPointerToRecord;
  12432. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12433. BEGIN
  12434. type := type.resolved;
  12435. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12436. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12437. END IsArrayOfSystemByte;
  12438. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12439. BEGIN
  12440. IF type = NIL THEN RETURN FALSE END;
  12441. type := type.resolved;
  12442. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12443. END IsOpenArray;
  12444. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12445. BEGIN
  12446. IF type = NIL THEN RETURN FALSE END;
  12447. type := type.resolved;
  12448. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12449. END IsSemiDynamicArray;
  12450. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12451. BEGIN
  12452. IF type = NIL THEN RETURN FALSE END;
  12453. type := type.resolved;
  12454. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12455. END IsStaticArray;
  12456. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12457. VAR size: LONGINT;
  12458. BEGIN
  12459. size := 1;
  12460. WHILE (IsStaticArray(type)) DO
  12461. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12462. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12463. END;
  12464. RETURN size;
  12465. END StaticArrayNumElements;
  12466. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12467. BEGIN
  12468. WHILE (IsStaticArray(type)) DO
  12469. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12470. END;
  12471. RETURN type;
  12472. END StaticArrayBaseType;
  12473. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12474. BEGIN
  12475. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12476. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12477. END;
  12478. RETURN type;
  12479. END ArrayBaseType;
  12480. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12481. BEGIN
  12482. IF type = NIL THEN RETURN FALSE END;
  12483. type := type.resolved;
  12484. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12485. END IsDelegate;
  12486. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12487. VAR i: LONGINT;
  12488. BEGIN
  12489. i := 0; type := type.resolved;
  12490. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12491. INC(i);
  12492. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12493. END;
  12494. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12495. INC(i);
  12496. type := type(SyntaxTree.MathArrayType).arrayBase;
  12497. IF type # NIL THEN type := type.resolved END;
  12498. END;
  12499. RETURN i
  12500. END DynamicDim;
  12501. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12502. BEGIN
  12503. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12504. type := type(SyntaxTree.ArrayType).arrayBase;
  12505. END;
  12506. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12507. type := type(SyntaxTree.MathArrayType).arrayBase;
  12508. END;
  12509. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12510. END StaticSize;
  12511. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12512. BEGIN
  12513. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12514. END IsImmediate;
  12515. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12516. BEGIN
  12517. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12518. END IsAddress;
  12519. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12520. BEGIN
  12521. RETURN (x.mode = IntermediateCode.ModeRegister);
  12522. END IsRegister;
  12523. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12524. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12525. BEGIN
  12526. typeDeclaration := recordType.typeDeclaration;
  12527. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12528. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12529. END GetRecordTypeName;
  12530. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12531. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12532. BEGIN
  12533. parSize := 0;
  12534. IF StructuredReturnType(procedureType) THEN
  12535. parameter := procedureType.returnParameter;
  12536. INC(parSize,system.SizeOfParameter(parameter));
  12537. parSize := parSize + (-parSize) MOD system.addressSize;
  12538. END;
  12539. parameter :=procedureType.lastParameter;
  12540. WHILE (parameter # NIL) DO
  12541. INC(parSize,system.SizeOfParameter(parameter));
  12542. parSize := parSize + (-parSize) MOD system.addressSize;
  12543. parameter := parameter.prevParameter;
  12544. END;
  12545. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  12546. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12547. RETURN ToMemoryUnits(system,parSize)
  12548. END ParametersSize;
  12549. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  12550. BEGIN
  12551. IF type = NIL THEN RETURN FALSE
  12552. ELSE
  12553. type := type.resolved;
  12554. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  12555. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  12556. END
  12557. END ReturnedAsParameter;
  12558. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  12559. BEGIN
  12560. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  12561. END StructuredReturnType;
  12562. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12563. BEGIN
  12564. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12565. END IsNested;
  12566. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12567. BEGIN
  12568. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12569. scope := scope.outerScope;
  12570. END;
  12571. RETURN scope # NIL;
  12572. END InCellScope;
  12573. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12574. BEGIN
  12575. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12576. RETURN 0
  12577. ELSE
  12578. *)
  12579. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12580. (*END;*)
  12581. END ProcedureParametersSize;
  12582. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12583. VAR dataUnit: LONGINT;
  12584. BEGIN dataUnit := system.dataUnit;
  12585. ASSERT(size MOD system.dataUnit = 0);
  12586. RETURN size DIV system.dataUnit
  12587. END ToMemoryUnits;
  12588. PROCEDURE Get*(): Backend.Backend;
  12589. VAR backend: IntermediateBackend;
  12590. BEGIN NEW(backend); RETURN backend
  12591. END Get;
  12592. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12593. VAR instruction: IntermediateCode.Instruction;
  12594. BEGIN
  12595. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12596. RETURN instruction
  12597. END Nop;
  12598. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12599. VAR instruction: IntermediateCode.Instruction;
  12600. BEGIN
  12601. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12602. RETURN instruction
  12603. END Mov;
  12604. (* like Mov but ensures that no new register will be allocated for dest *)
  12605. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12606. VAR instruction: IntermediateCode.Instruction;
  12607. BEGIN
  12608. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12609. RETURN instruction
  12610. END MovReplace;
  12611. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12612. VAR instruction: IntermediateCode.Instruction;
  12613. BEGIN
  12614. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12615. RETURN instruction
  12616. END Conv;
  12617. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12618. BEGIN
  12619. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12620. END SysvABI;
  12621. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12622. BEGIN
  12623. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12624. END SysvABIorWINAPI;
  12625. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12626. VAR instruction: IntermediateCode.Instruction;
  12627. BEGIN
  12628. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12629. RETURN instruction
  12630. END Call;
  12631. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12632. VAR op1, op2, op3: IntermediateCode.Operand;
  12633. VAR instruction: IntermediateCode.Instruction;
  12634. BEGIN
  12635. IntermediateCode.InitNumber(op1,pcOffset);
  12636. IntermediateCode.InitNumber(op2,callingConvention);
  12637. IntermediateCode.InitNumber(op3,unwind);
  12638. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12639. RETURN instruction
  12640. END Exit;
  12641. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12642. VAR instruction: IntermediateCode.Instruction;
  12643. BEGIN
  12644. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12645. RETURN instruction
  12646. END Return;
  12647. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12648. VAR instruction: IntermediateCode.Instruction;
  12649. BEGIN
  12650. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12651. RETURN instruction
  12652. END Result;
  12653. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12654. VAR op1: IntermediateCode.Operand;
  12655. VAR instruction: IntermediateCode.Instruction;
  12656. BEGIN
  12657. IntermediateCode.InitNumber(op1,nr);
  12658. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12659. RETURN instruction
  12660. END Trap;
  12661. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12662. VAR instruction: IntermediateCode.Instruction;
  12663. BEGIN
  12664. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12665. RETURN instruction
  12666. END Br;
  12667. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12668. VAR instruction: IntermediateCode.Instruction;
  12669. BEGIN
  12670. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12671. RETURN instruction
  12672. END Breq;
  12673. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12674. VAR instruction: IntermediateCode.Instruction;
  12675. BEGIN
  12676. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12677. RETURN instruction
  12678. END Brne;
  12679. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12680. VAR instruction: IntermediateCode.Instruction;
  12681. BEGIN
  12682. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12683. RETURN instruction
  12684. END Brge;
  12685. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12686. VAR instruction: IntermediateCode.Instruction;
  12687. BEGIN
  12688. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12689. RETURN instruction
  12690. END Brlt;
  12691. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12692. VAR instruction: IntermediateCode.Instruction;
  12693. BEGIN
  12694. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12695. RETURN instruction
  12696. END Pop;
  12697. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12698. VAR instruction: IntermediateCode.Instruction;
  12699. BEGIN
  12700. ASSERT(op.mode # IntermediateCode.Undefined);
  12701. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12702. RETURN instruction
  12703. END Push;
  12704. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12705. VAR instruction: IntermediateCode.Instruction;
  12706. BEGIN
  12707. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12708. RETURN instruction
  12709. END Neg;
  12710. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12711. VAR instruction: IntermediateCode.Instruction;
  12712. BEGIN
  12713. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12714. RETURN instruction
  12715. END Not;
  12716. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12717. VAR instruction: IntermediateCode.Instruction;
  12718. BEGIN
  12719. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12720. RETURN instruction
  12721. END Abs;
  12722. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12723. VAR instruction: IntermediateCode.Instruction;
  12724. BEGIN
  12725. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12726. ASSERT(~IsImmediate(instruction.op1));
  12727. RETURN instruction
  12728. END Mul;
  12729. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12730. VAR instruction: IntermediateCode.Instruction;
  12731. BEGIN
  12732. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12733. RETURN instruction
  12734. END Div;
  12735. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12736. VAR instruction: IntermediateCode.Instruction;
  12737. BEGIN
  12738. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12739. RETURN instruction
  12740. END Mod;
  12741. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12742. VAR instruction: IntermediateCode.Instruction;
  12743. BEGIN
  12744. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12745. RETURN instruction
  12746. END Sub;
  12747. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12748. VAR instruction: IntermediateCode.Instruction;
  12749. BEGIN
  12750. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12751. RETURN instruction
  12752. END Add;
  12753. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12754. VAR instruction: IntermediateCode.Instruction;
  12755. BEGIN
  12756. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12757. RETURN instruction
  12758. END And;
  12759. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12760. VAR instruction: IntermediateCode.Instruction;
  12761. BEGIN
  12762. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12763. RETURN instruction
  12764. END Or;
  12765. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12766. VAR instruction: IntermediateCode.Instruction;
  12767. BEGIN
  12768. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12769. RETURN instruction
  12770. END Xor;
  12771. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12772. VAR instruction: IntermediateCode.Instruction;
  12773. BEGIN
  12774. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12775. RETURN instruction
  12776. END Shl;
  12777. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12778. VAR instruction: IntermediateCode.Instruction;
  12779. BEGIN
  12780. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12781. RETURN instruction
  12782. END Shr;
  12783. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12784. VAR instruction: IntermediateCode.Instruction;
  12785. BEGIN
  12786. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12787. RETURN instruction
  12788. END Rol;
  12789. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12790. VAR instruction: IntermediateCode.Instruction;
  12791. BEGIN
  12792. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12793. RETURN instruction
  12794. END Ror;
  12795. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12796. VAR instruction: IntermediateCode.Instruction;
  12797. BEGIN
  12798. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12799. RETURN instruction
  12800. END Cas;
  12801. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12802. VAR instruction: IntermediateCode.Instruction;
  12803. BEGIN
  12804. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12805. RETURN instruction
  12806. END Copy;
  12807. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12808. VAR instruction: IntermediateCode.Instruction;
  12809. BEGIN
  12810. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12811. RETURN instruction
  12812. END Fill;
  12813. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12814. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12815. BEGIN
  12816. string := IntermediateCode.String(s);
  12817. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12818. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12819. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12820. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12821. RETURN instruction
  12822. END Asm;
  12823. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12824. VAR instruction: IntermediateCode.Instruction;
  12825. BEGIN
  12826. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12827. RETURN instruction
  12828. END Data;
  12829. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12830. VAR instruction: IntermediateCode.Instruction;
  12831. BEGIN
  12832. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12833. IntermediateCode.SetSubType(instruction, subtype);
  12834. RETURN instruction
  12835. END SpecialInstruction;
  12836. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12837. VAR op1: IntermediateCode.Operand;
  12838. VAR instruction: IntermediateCode.Instruction;
  12839. BEGIN
  12840. (*! generate a warning if size exceeds a certain limit *)
  12841. (*
  12842. ASSERT(bytes < 1000000); (* sanity check *)
  12843. *)
  12844. ASSERT(0 <= units); (* sanity check *)
  12845. IntermediateCode.InitNumber(op1,units);
  12846. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12847. RETURN instruction
  12848. END Reserve;
  12849. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12850. VAR op1: IntermediateCode.Operand;
  12851. VAR instruction: IntermediateCode.Instruction;
  12852. BEGIN
  12853. IntermediateCode.InitNumber(op1,position.start);
  12854. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12855. RETURN instruction
  12856. END LabelInstruction;
  12857. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12858. VAR pc: LONGINT;
  12859. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12860. BEGIN
  12861. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12862. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12863. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12864. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12865. IF instr.op1.type.form = IntermediateCode.Float THEN
  12866. RETURN instr.op1.floatValue = op.floatValue
  12867. ELSE
  12868. RETURN instr.op1.intValue = op.intValue
  12869. END;
  12870. END ProvidesValue;
  12871. BEGIN
  12872. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12873. pc := 0;
  12874. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12875. INC(pc);
  12876. END;
  12877. IF pc = data.pc THEN
  12878. data.Emit(Data(Basic.invalidPosition,vop));
  12879. END;
  12880. RETURN pc
  12881. END EnterImmediate;
  12882. PROCEDURE Init;
  12883. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12884. BEGIN
  12885. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12886. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12887. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12888. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12889. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12890. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12891. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12892. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12893. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12894. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12895. IntermediateCode.InitOperand(emptyOperand);
  12896. FOR i := 0 TO NumberSystemCalls-1 DO
  12897. name := "@SystemCall";
  12898. Basic.AppendNumber(name,i);
  12899. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12900. END;
  12901. END Init;
  12902. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12903. BEGIN
  12904. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12905. END IsExported;
  12906. BEGIN
  12907. Init;
  12908. END FoxIntermediateBackend.
  12909. Compiler.Compile FoxIntermediateBackend.Mod ~
  12910. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12911. # 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 ~
  12912. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12913. # 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 ~
  12914. FSTools.CloseFiles A2Z.exe ~
  12915. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12916. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12917. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12918. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12919. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12920. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12921. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12922. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12923. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12924. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12925. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod