FoxIntermediateBackend.Mod 559 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235
  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. WarningDynamicLoading = FALSE;
  91. TYPE
  92. Position=SyntaxTree.Position;
  93. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  94. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  95. Operand = RECORD
  96. mode: SHORTINT;
  97. op: IntermediateCode.Operand;
  98. tag: IntermediateCode.Operand;
  99. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  100. dimOffset: LONGINT;
  101. END;
  102. Fixup= POINTER TO RECORD
  103. pc: LONGINT;
  104. nextFixup: Fixup;
  105. END;
  106. WriteBackCall = POINTER TO RECORD
  107. call: SyntaxTree.ProcedureCallDesignator;
  108. next: WriteBackCall;
  109. END;
  110. Label= OBJECT
  111. VAR
  112. fixups: Fixup;
  113. section: IntermediateCode.Section;
  114. pc: LONGINT;
  115. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  116. BEGIN
  117. SELF.section := section; pc := -1;
  118. END InitLabel;
  119. PROCEDURE Resolve(pc: LONGINT);
  120. VAR at: LONGINT;
  121. BEGIN
  122. SELF.pc := pc;
  123. WHILE(fixups # NIL) DO
  124. at := fixups.pc;
  125. section.PatchAddress(at,pc);
  126. fixups := fixups.nextFixup;
  127. END;
  128. END Resolve;
  129. PROCEDURE AddFixup(at: LONGINT);
  130. VAR fixup: Fixup;
  131. BEGIN
  132. ASSERT(pc=-1);
  133. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  134. END AddFixup;
  135. END Label;
  136. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  137. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  138. VAR
  139. backend: IntermediateBackend;
  140. implementationVisitor: ImplementationVisitor;
  141. meta: MetaDataGenerator;
  142. system: Global.System;
  143. currentScope: SyntaxTree.Scope;
  144. module: Sections.Module;
  145. moduleSelf: SyntaxTree.Variable;
  146. dump: BOOLEAN;
  147. forceModuleBody: BOOLEAN;
  148. addressType: IntermediateCode.Type;
  149. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  150. BEGIN
  151. currentScope := NIL; module := NIL; moduleSelf := NIL;
  152. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  153. SELF.dump := dump;
  154. SELF.backend := backend;
  155. SELF.forceModuleBody := forceModuleBody;
  156. addressType := IntermediateCode.GetType(system,system.addressType)
  157. END Init;
  158. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  159. BEGIN
  160. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  161. END Error;
  162. PROCEDURE Type(x: SyntaxTree.Type);
  163. BEGIN
  164. x.Accept(SELF);
  165. END Type;
  166. (** types **)
  167. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  168. BEGIN (* no code emission *) END VisitBasicType;
  169. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  170. BEGIN (* no code emission *) END VisitCharacterType;
  171. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  172. BEGIN (* no code emission *) END VisitIntegerType;
  173. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  174. BEGIN (* no code emission *) END VisitFloatType;
  175. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  176. BEGIN (* no code emission *) END VisitComplexType;
  177. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  178. VAR type: SyntaxTree.Type;
  179. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  180. type := x.resolved;
  181. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  182. meta.CheckTypeDeclaration(type);
  183. END;
  184. END VisitQualifiedType;
  185. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  186. BEGIN (* no code emission *) END VisitStringType;
  187. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  188. BEGIN (* no code emission *)
  189. END VisitArrayRangeType;
  190. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  191. BEGIN (* no code emission *) END VisitArrayType;
  192. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  193. BEGIN (* no code emission *) END VisitPortType;
  194. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  195. BEGIN
  196. meta.CheckTypeDeclaration(x);
  197. END VisitMathArrayType;
  198. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  199. BEGIN
  200. meta.CheckTypeDeclaration(x);
  201. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  202. END VisitPointerType;
  203. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  204. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  205. BEGIN (* no code emission *)
  206. meta.CheckTypeDeclaration(x);
  207. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  208. IF x.pointerType.typeDeclaration # NIL THEN
  209. td := x.pointerType.typeDeclaration
  210. ELSE
  211. td := x.typeDeclaration
  212. END;
  213. Global.GetSymbolName(td,name);
  214. (* code section for object *)
  215. END;
  216. Scope(x.recordScope);
  217. END VisitRecordType;
  218. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  219. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  220. BEGIN
  221. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  222. WHILE (this # NIL) & (this.identifier# id) DO
  223. this := this.nextModifier;
  224. END;
  225. RETURN this # NIL
  226. END HasFlag;
  227. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  228. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  229. BEGIN
  230. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  231. capabilities := {};
  232. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  233. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  234. backend.SetCapabilities(capabilities);
  235. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  236. Scope(x.cellScope);
  237. END;
  238. END VisitCellType;
  239. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  240. BEGIN (* no code emission *) END VisitProcedureType;
  241. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  242. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  243. END VisitEnumerationType;
  244. (* symbols *)
  245. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  246. BEGIN
  247. Procedure(x);
  248. END VisitProcedure;
  249. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  250. BEGIN
  251. Procedure(x);
  252. END VisitOperator;
  253. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  254. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  255. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  256. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  257. BEGIN
  258. type := type.resolved;
  259. IF type IS SyntaxTree.RecordType THEN
  260. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  261. ELSIF (type IS SyntaxTree.ArrayType) THEN
  262. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  263. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  264. END;
  265. ELSIF type IS SyntaxTree.MathArrayType THEN
  266. WITH type: SyntaxTree.MathArrayType DO
  267. IF type.form = SyntaxTree.Open THEN
  268. RETURN TRUE
  269. ELSIF type.form = SyntaxTree.Static THEN
  270. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  271. END;
  272. END;
  273. END;
  274. RETURN FALSE
  275. END TypeNeedsInitialization;
  276. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  277. VAR variable: SyntaxTree.Variable;
  278. BEGIN
  279. variable := scope.firstVariable;
  280. WHILE variable # NIL DO
  281. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  282. IF variable.initializer # NIL THEN RETURN TRUE END;
  283. variable := variable.nextVariable;
  284. END;
  285. RETURN FALSE
  286. END ScopeNeedsInitialization;
  287. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  288. BEGIN
  289. size := offset - lastUpdated;
  290. IF size > 0 THEN
  291. irv.Emit(Reserve(x.position,size));
  292. END;
  293. irv.Emit(Data(x.position, op));
  294. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  295. END SingleInitialize;
  296. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  297. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  298. BEGIN
  299. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  300. WITH type: SyntaxTree.RecordType DO
  301. baseType := type.baseType;
  302. IF baseType # NIL THEN
  303. baseType := baseType.resolved;
  304. IF baseType IS SyntaxTree.PointerType THEN
  305. baseType := baseType(SyntaxTree.PointerType).pointerBase
  306. END;
  307. Initialize(baseType,NIL, offset);
  308. END;
  309. variable := type.recordScope.firstVariable;
  310. WHILE variable # NIL DO
  311. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  312. variable := variable.nextVariable
  313. END;
  314. | type: SyntaxTree.ArrayType DO
  315. IF type.form = SyntaxTree.Static THEN
  316. baseType := type.arrayBase;
  317. IF TypeNeedsInitialization(baseType) THEN
  318. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  319. FOR i := 0 TO type.staticLength-1 DO
  320. Initialize(baseType,NIL,offset+i*size);
  321. END;
  322. END;
  323. END;
  324. | type: SyntaxTree.MathArrayType DO
  325. IF type.form = SyntaxTree.Open THEN
  326. dim := DynamicDim(type);
  327. baseType := SemanticChecker.ArrayBase(type,dim);
  328. imm := IntermediateCode.Immediate(addressType,dim);
  329. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  330. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  331. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  332. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  333. (* flags remain empty (=0) for open array *)
  334. ELSIF type.form = SyntaxTree.Static THEN
  335. baseType := type.arrayBase;
  336. IF TypeNeedsInitialization(baseType) THEN
  337. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  338. ASSERT(type.staticLength < 1024*1024*1024);
  339. FOR i := 0 TO type.staticLength-1 DO
  340. Initialize(baseType,NIL,offset+i*size);
  341. END;
  342. END;
  343. END;
  344. ELSE
  345. IF initializer # NIL THEN
  346. implementationVisitor.Evaluate(initializer, op);
  347. SingleInitialize(op.op, offset);
  348. END;
  349. END;
  350. END Initialize;
  351. BEGIN
  352. IF x.externalName # NIL THEN RETURN END;
  353. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  354. (* code section for variable *)
  355. Global.GetSymbolSegmentedName(x,name);
  356. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  357. irv.SetExported(IsExported(x));
  358. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  359. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  360. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  361. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  362. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  363. FOR i := 0 TO DynamicDim(x.type)-1 DO
  364. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  365. END;
  366. ELSE
  367. lastUpdated:= 0;
  368. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  369. Initialize(x.type, x.initializer, 0);
  370. END;
  371. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  372. IF size > 0 THEN
  373. irv.Emit(Reserve(x.position,size));
  374. END;
  375. IF ~x.fixed THEN
  376. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  377. ELSE
  378. align := x.alignment;
  379. END;
  380. irv.SetPositionOrAlignment(x.fixed, align);
  381. meta.CheckTypeDeclaration(x.type);
  382. END;
  383. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  384. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  385. END;
  386. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  387. END VisitVariable;
  388. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  389. BEGIN
  390. VisitVariable(x)
  391. END VisitProperty;
  392. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  393. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  394. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  395. BEGIN
  396. ASSERT(currentScope IS SyntaxTree.CellScope);
  397. Global.GetSymbolSegmentedName(x,name);
  398. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  399. irv.SetExported(IsExported(x));
  400. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  401. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  402. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  403. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  404. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  405. FOR i := 0 TO DynamicDim(x.type)-1 DO
  406. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  407. END;
  408. ELSE
  409. lastUpdated:= 0;
  410. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  411. IF size > 0 THEN
  412. irv.Emit(Reserve(x.position,size));
  413. END;
  414. IF ~x.fixed THEN
  415. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  416. ELSE
  417. align := x.alignment;
  418. END;
  419. irv.SetPositionOrAlignment(x.fixed, align);
  420. meta.CheckTypeDeclaration(x.type);
  421. END;
  422. END VisitParameter;
  423. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  424. BEGIN
  425. Type(x.declaredType); (* => code in objects *)
  426. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  427. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  428. END;
  429. END VisitTypeDeclaration;
  430. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  431. BEGIN
  432. IF (SyntaxTree.Public * x.access # {}) THEN
  433. implementationVisitor.VisitConstant(x);
  434. END;
  435. END VisitConstant;
  436. PROCEDURE Scope(x: SyntaxTree.Scope);
  437. VAR procedure: SyntaxTree.Procedure;
  438. constant: SyntaxTree.Constant;
  439. variable: SyntaxTree.Variable;
  440. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  441. cell: SyntaxTree.CellType;
  442. parameter: SyntaxTree.Parameter;
  443. property: SyntaxTree.Property;
  444. BEGIN
  445. prevScope := currentScope;
  446. currentScope := x;
  447. (* constants treated in implementation visitor *)
  448. WITH x: SyntaxTree.CellScope DO
  449. cell := x.ownerCell;
  450. parameter := cell.firstParameter;
  451. WHILE parameter # NIL DO
  452. VisitParameter(parameter);
  453. parameter := parameter.nextParameter;
  454. END;
  455. property := cell.firstProperty;
  456. WHILE property # NIL DO
  457. VisitProperty(property);
  458. property := property.nextProperty;
  459. END;
  460. ELSE
  461. END;
  462. typeDeclaration := x.firstTypeDeclaration;
  463. WHILE typeDeclaration # NIL DO
  464. VisitTypeDeclaration(typeDeclaration);
  465. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  466. END;
  467. variable := x.firstVariable;
  468. WHILE variable # NIL DO
  469. VisitVariable(variable);
  470. variable := variable.nextVariable;
  471. END;
  472. procedure := x.firstProcedure;
  473. WHILE procedure # NIL DO
  474. VisitProcedure(procedure);
  475. procedure := procedure.nextProcedure;
  476. END;
  477. constant := x.firstConstant;
  478. WHILE constant # NIL DO
  479. VisitConstant(constant);
  480. constant := constant.nextConstant;
  481. END;
  482. currentScope := prevScope;
  483. END Scope;
  484. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  485. VAR parameter: SyntaxTree.Parameter;
  486. BEGIN
  487. parameter := first;
  488. WHILE parameter # NIL DO
  489. VisitParameter(parameter);
  490. parameter := parameter.nextParameter;
  491. END;
  492. END Parameters;
  493. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  494. VAR scope: SyntaxTree.ProcedureScope;
  495. prevScope: SyntaxTree.Scope;
  496. inline, finalizer: BOOLEAN;
  497. procedureType: SyntaxTree.ProcedureType;
  498. pc: LONGINT;
  499. stackSize: LONGINT;
  500. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  501. null,size,src,dest,fp,res: IntermediateCode.Operand;
  502. cc: LONGINT;
  503. cellType: SyntaxTree.CellType;
  504. registerNumber: LONGINT;
  505. registerParameter: Backend.Registers;
  506. registerParameters: LONGINT;
  507. registerClass: IntermediateCode.RegisterClass;
  508. type: IntermediateCode.Type;
  509. formalParameter: SyntaxTree.Parameter;
  510. recordType: SyntaxTree.RecordType;
  511. isModuleBody: BOOLEAN;
  512. parametersSize: LONGINT;
  513. PROCEDURE Signature;
  514. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  515. BEGIN
  516. procedureType := x.type(SyntaxTree.ProcedureType);
  517. returnType := procedureType.returnType;
  518. IF returnType # NIL THEN
  519. meta.CheckTypeDeclaration(returnType)
  520. END;
  521. parameter := procedureType.firstParameter;
  522. WHILE parameter # NIL DO
  523. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  524. parameter := parameter.nextParameter;
  525. END;
  526. END Signature;
  527. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  528. VAR result: BOOLEAN;
  529. BEGIN
  530. result := FALSE;
  531. IF x = SyntaxTree.invalidExpression THEN
  532. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  533. result := TRUE;
  534. value := x.resolved(SyntaxTree.IntegerValue).value;
  535. ELSE
  536. Error(x.position,"expression is not an integer constant");
  537. END;
  538. RETURN result;
  539. END CheckIntegerValue;
  540. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  541. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  542. BEGIN
  543. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  544. WHILE (this # NIL) & (this.identifier # id) DO
  545. this := this.nextModifier;
  546. END;
  547. IF this # NIL THEN
  548. IF this.expression = NIL THEN
  549. Error(this.position,"expected expression value");
  550. ELSIF CheckIntegerValue(this.expression,value) THEN
  551. END;
  552. RETURN TRUE
  553. ELSE RETURN FALSE
  554. END;
  555. END HasValue;
  556. CONST DefaultDataMemorySize=512;
  557. BEGIN
  558. IF x.externalName # NIL THEN RETURN END;
  559. (*
  560. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  561. *)
  562. (* code section for this procedure *)
  563. scope := x.procedureScope;
  564. prevScope := currentScope;
  565. currentScope := scope;
  566. procedureType := x.type(SyntaxTree.ProcedureType);
  567. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  568. implementationVisitor.temporaries.Init;
  569. implementationVisitor.usedRegisters := NIL;
  570. implementationVisitor.registerUsageCount.Init;
  571. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  572. IF (scope.body # NIL) & (x.isInline) THEN
  573. inline := TRUE;
  574. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  575. ir.SetExported(IsExported(x));
  576. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  577. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  578. IF backend.cellsAreObjects THEN
  579. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  580. ir.SetExported(IsExported(x));
  581. ELSE
  582. RETURN; (* cellnet cannot be compiled for final static hardware *)
  583. END;
  584. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  585. inline := FALSE;
  586. AddBodyCallStub(x);
  587. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  588. ir.SetExported(IsExported(x));
  589. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  590. inline := FALSE;
  591. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  592. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  593. AddBodyCallStub(x);
  594. AddStackAllocation(x,stackSize);
  595. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  596. ir.SetExported(IsExported(x));
  597. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  598. inline := FALSE;
  599. Parameters(procedureType.firstParameter);
  600. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  601. ir.SetExported(IsExported(x));
  602. ELSE
  603. inline := FALSE;
  604. IF x.isEntry OR x.isExit THEN
  605. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  606. ir.SetExported(TRUE);
  607. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  608. ELSE
  609. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  610. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  611. END;
  612. END;
  613. cc := procedureType.callingConvention;
  614. IF cc = SyntaxTree.WinAPICallingConvention THEN
  615. parametersSize := ProcedureParametersSize(backend.system,x);
  616. ELSE
  617. parametersSize := 0;
  618. END;
  619. IF scope.body # NIL THEN
  620. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  621. registerNumber := 0;
  622. IF ~inline THEN
  623. IF scope.lastVariable = NIL THEN
  624. stackSize := 0
  625. ELSE
  626. stackSize := scope.lastVariable.offsetInBits;
  627. IF stackSize <0 THEN stackSize := -stackSize END;
  628. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  629. END;
  630. (*
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  632. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  633. *)
  634. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  635. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  636. END;
  637. pc := ir.pc-1;
  638. (*
  639. ir.Emit(Nop(position)); (* placeholder for fill *)
  640. *)
  641. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  642. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  643. IF registerParameter = NIL THEN registerParameters := 0
  644. ELSE registerParameters := LEN(registerParameter)
  645. END;
  646. formalParameter := procedureType.lastParameter;
  647. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  648. IF ~PassInRegister(formalParameter) THEN
  649. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  650. ELSE
  651. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  652. type := GetType(system, formalParameter.type);
  653. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  654. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  655. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  656. implementationVisitor.ReleaseIntermediateOperand(src);
  657. INC(registerNumber);
  658. formalParameter := formalParameter.prevParameter;
  659. END;
  660. END;
  661. END;
  662. END;
  663. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  664. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  665. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  666. IF scope.lastVariable # NIL THEN
  667. stackSize := scope.lastVariable.offsetInBits;
  668. IF stackSize <0 THEN stackSize := -stackSize END;
  669. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  670. END;
  671. END;
  672. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  673. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  674. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  675. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  676. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  677. ir.EmitAt(pc,Push(size));
  678. implementationVisitor.StaticCallOperand(result,procedure);
  679. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  680. END;
  681. *)
  682. END;
  683. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  684. IF stackSize > 0 THEN
  685. IF (stackSize MOD system.addressSize = 0) THEN
  686. null := IntermediateCode.Immediate(addressType,0);
  687. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  688. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  689. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  690. ELSE
  691. null := IntermediateCode.Immediate(int8,0);
  692. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  693. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  694. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  695. END;
  696. (*! should potentially be called by backend -- enter might initialize
  697. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  698. *)
  699. END;
  700. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  701. parametersSize := ProcedureParametersSize(backend.system,x);
  702. ELSE
  703. parametersSize := 0;
  704. END;
  705. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  706. finalizer := FALSE;
  707. IF backend.cooperative & x.isFinalizer THEN
  708. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  709. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  710. GetRecordTypeName(recordType,name);
  711. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  712. END;
  713. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  714. IF backend.cooperative THEN
  715. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  716. IF implementationVisitor.profile & ~isModuleBody THEN
  717. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  718. END;
  719. END;
  720. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  721. IF finalizer THEN
  722. IF backend.hasLinkRegister THEN
  723. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  724. END;
  725. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  726. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  727. ir.Emit(Br(x.position,dest));
  728. ELSE
  729. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  730. END;
  731. ELSE
  732. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  733. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  734. END;
  735. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  736. IF backend.cooperative THEN
  737. IF HasPointers (scope) THEN
  738. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  739. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  740. ir.Emit(Result(x.position, res));
  741. ir.Emit(Push(x.position, res));
  742. implementationVisitor.ResetVariables(scope);
  743. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  744. ir.Emit(Pop(x.position, res));
  745. ir.Emit(Return(x.position, res));
  746. ELSE
  747. implementationVisitor.ResetVariables(scope);
  748. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  749. END;
  750. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  751. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  752. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  753. ir.Emit(Result(x.position, res));
  754. ir.Emit(Push(x.position, res));
  755. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  756. ir.Emit(Pop(x.position, res));
  757. ir.Emit(Return(x.position, res));
  758. ELSE
  759. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  760. END;
  761. END;
  762. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  763. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  764. ELSE
  765. ir.Emit(Nop(x.position));
  766. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  767. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  768. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  769. END;
  770. END;
  771. END
  772. END;
  773. ELSE (* force body for procedures *)
  774. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  775. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  776. (*IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;*)
  777. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  778. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  779. END;
  780. Scope(scope);
  781. Signature;
  782. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  783. currentScope := prevScope;
  784. END Procedure;
  785. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  786. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  787. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  788. BEGIN
  789. ASSERT (bodyProcedure # NIL);
  790. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  791. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  792. procedure.SetScope(bodyProcedure.scope);
  793. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  794. procedure.SetAccess(SyntaxTree.Hidden);
  795. Global.GetSymbolSegmentedName (procedure,name);
  796. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  797. ir.SetExported(TRUE);
  798. ir.SetPriority(InitPriority);
  799. Global.GetSymbolSegmentedName (bodyProcedure,name);
  800. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  801. implementationVisitor.currentScope := module.module.moduleScope;
  802. implementationVisitor.section := ir;
  803. implementationVisitor.PushSelfPointer();
  804. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  805. ELSIF backend.preregisterStatic THEN
  806. implementationVisitor.currentScope := module.module.moduleScope;
  807. implementationVisitor.section := ir;
  808. implementationVisitor.PushSelfPointer();
  809. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  810. ELSE
  811. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  812. ir.Emit(Call(bodyProcedure.position,op, 0));
  813. END;
  814. END AddBodyCallStub;
  815. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  816. VAR name: Basic.SegmentedName;
  817. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  818. BEGIN
  819. Global.GetSymbolSegmentedName (symbol,name);
  820. Basic.RemoveSuffix(name);
  821. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  822. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  823. ir.SetExported(TRUE);
  824. ir.SetPriority(FirstPriority);
  825. IntermediateCode.InitImmediate(op,addressType,initStack);
  826. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  827. END AddStackAllocation;
  828. (** entry function to visit a complete module *)
  829. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  830. VAR
  831. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  832. hasDynamicOperatorDeclarations: BOOLEAN;
  833. operator: SyntaxTree.Operator;
  834. import: SyntaxTree.Import;
  835. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  836. BEGIN
  837. type := type.resolved;
  838. IF type IS SyntaxTree.RecordType THEN
  839. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  840. ELSIF (type IS SyntaxTree.ArrayType) THEN
  841. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  842. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  843. END;
  844. ELSIF type IS SyntaxTree.MathArrayType THEN
  845. WITH type: SyntaxTree.MathArrayType DO
  846. IF type.form = SyntaxTree.Open THEN
  847. RETURN TRUE
  848. ELSIF type.form = SyntaxTree.Static THEN
  849. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  850. END;
  851. END;
  852. END;
  853. RETURN FALSE
  854. END TypeNeedsInitialization;
  855. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  856. VAR variable: SyntaxTree.Variable;
  857. BEGIN
  858. variable := scope.firstVariable;
  859. WHILE variable # NIL DO
  860. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  861. IF variable.initializer # NIL THEN RETURN TRUE END;
  862. variable := variable.nextVariable;
  863. END;
  864. RETURN FALSE
  865. END ScopeNeedsInitialization;
  866. BEGIN
  867. ASSERT(x # NIL); ASSERT(module # NIL);
  868. SELF.module := module;
  869. (* add import names to the generated Sections.Module *)
  870. import := x.moduleScope.firstImport;
  871. WHILE import # NIL DO
  872. import.module.GetName(idstr);
  873. module.imports.AddName(idstr);
  874. import := import.nextImport
  875. END;
  876. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  877. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  878. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  879. moduleSelf.SetType(system.anyType);
  880. moduleSelf.SetScope(x.moduleScope);
  881. moduleSelf.SetUntraced(TRUE);
  882. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  883. ir.SetExported(TRUE);
  884. IntermediateCode.InitImmediate(op,addressType,0);
  885. ir.Emit(Data(Basic.invalidPosition,op));
  886. END;
  887. implementationVisitor.module := module;
  888. implementationVisitor.moduleScope := x.moduleScope;
  889. implementationVisitor.moduleSelf := moduleSelf;
  890. implementationVisitor.canBeLoaded := TRUE;
  891. meta.SetModule(module);
  892. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  893. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  894. END;
  895. IF backend.profile THEN
  896. EnsureBodyProcedure(x.moduleScope);
  897. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  898. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  899. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  900. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  901. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  902. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  903. Global.GetModuleName(module.module,idstr);
  904. implementationVisitor.ProfilerAddModule(idstr);
  905. implementationVisitor.numberProcedures := 0;
  906. END;
  907. implementationVisitor.profile := backend.profile;
  908. (* check if there is at least one dynamic operator locally defined *)
  909. hasDynamicOperatorDeclarations := FALSE;
  910. operator := x.moduleScope.firstOperator;
  911. WHILE operator # NIL DO
  912. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  913. operator := operator.nextOperator
  914. END;
  915. (* add operator initialization code section *)
  916. IF hasDynamicOperatorDeclarations THEN
  917. EnsureBodyProcedure(x.moduleScope);
  918. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  919. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  920. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  921. END;
  922. Scope(x.moduleScope);
  923. IF hasDynamicOperatorDeclarations THEN
  924. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  925. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  926. END;
  927. IF backend.profile THEN
  928. implementationVisitor.ProfilerPatchInit;
  929. END;
  930. END Module;
  931. END DeclarationVisitor;
  932. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  933. RegisterUsageCount*=OBJECT
  934. VAR used: UsedArray; count: LONGINT;
  935. PROCEDURE &Init;
  936. VAR i: LONGINT;
  937. BEGIN
  938. count := 0;
  939. IF used = NIL THEN NEW(used,64); END;
  940. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  941. END Init;
  942. PROCEDURE Grow;
  943. VAR new: UsedArray; size,i: LONGINT;
  944. BEGIN
  945. size := LEN(used)*2;
  946. NEW(new,size);
  947. FOR i := 0 TO LEN(used)-1 DO
  948. new[i].count := used[i].count;
  949. new[i].type := used[i].type;
  950. new[i].map := used[i].map
  951. END;
  952. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  953. used := new
  954. END Grow;
  955. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  956. BEGIN
  957. INC(count);
  958. IF count = LEN(used) THEN Grow END;
  959. used[count].type := type;
  960. used[count].class := class;
  961. used[count].map := count;
  962. RETURN count;
  963. END Next;
  964. PROCEDURE IncUse(register: LONGINT);
  965. BEGIN
  966. INC(used[register].count);
  967. (*
  968. IF (register = 1) & (count > 30) THEN
  969. D.TraceBack;
  970. END;
  971. *)
  972. END IncUse;
  973. PROCEDURE DecUse(register: LONGINT);
  974. BEGIN
  975. DEC(used[register].count);
  976. END DecUse;
  977. PROCEDURE Map(register: LONGINT): LONGINT;
  978. VAR map : LONGINT;
  979. BEGIN
  980. IF register > 0 THEN
  981. map := used[register].map;
  982. WHILE register # map DO register := map; map := used[register].map END;
  983. END;
  984. RETURN register
  985. END Map;
  986. PROCEDURE Use(register: LONGINT): LONGINT;
  987. BEGIN
  988. IF register< LEN(used) THEN
  989. RETURN used[register].count
  990. ELSE
  991. RETURN 0
  992. END
  993. END Use;
  994. END RegisterUsageCount;
  995. RegisterEntry = POINTER TO RECORD
  996. prev,next: RegisterEntry;
  997. register: LONGINT;
  998. registerClass: IntermediateCode.RegisterClass;
  999. type: IntermediateCode.Type;
  1000. END;
  1001. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1002. Variables = OBJECT (Basic.List)
  1003. VAR
  1004. inUse: VariableUse;
  1005. registerIndex: LONGINT;
  1006. PROCEDURE & Init;
  1007. VAR i: LONGINT;
  1008. BEGIN
  1009. InitList(16);
  1010. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1011. registerIndex := 1024;
  1012. END Init;
  1013. PROCEDURE GetUsage(VAR use: VariableUse);
  1014. BEGIN
  1015. use := inUse;
  1016. END GetUsage;
  1017. PROCEDURE SetUsage(CONST use: VariableUse);
  1018. BEGIN
  1019. inUse := use;
  1020. END SetUsage;
  1021. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1022. VAR any: ANY;
  1023. BEGIN
  1024. any := Get(i);;
  1025. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1026. END GetVariable;
  1027. PROCEDURE Occupy(pos: LONGINT);
  1028. BEGIN
  1029. INCL(inUse[pos DIV 32], pos MOD 32);
  1030. END Occupy;
  1031. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1032. BEGIN
  1033. Occupy(Length());
  1034. Add(v);
  1035. END AddVariable;
  1036. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1037. VAR var : SyntaxTree.Variable;
  1038. BEGIN
  1039. FOR pos := 0 TO Length()-1 DO
  1040. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1041. var := GetVariable(pos);
  1042. IF type.SameType(var.type) THEN
  1043. Occupy(pos); RETURN var
  1044. END;
  1045. END;
  1046. END;
  1047. pos := Length();
  1048. RETURN NIL
  1049. END GetFreeVariable;
  1050. END Variables;
  1051. SymbolMap = POINTER TO RECORD
  1052. this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap;
  1053. isAddress: BOOLEAN;
  1054. END;
  1055. SymbolMapper = OBJECT
  1056. VAR
  1057. first: SymbolMap;
  1058. PROCEDURE & Init;
  1059. BEGIN
  1060. first := NIL;
  1061. END Init;
  1062. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; isAddress: BOOLEAN);
  1063. VAR new: SymbolMap;
  1064. BEGIN
  1065. NEW(new); new.this := this; new.to := to; new.tag := tag; new.isAddress := isAddress;
  1066. new.next := first; first := new;
  1067. END Add;
  1068. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1069. VAR s: SymbolMap;
  1070. BEGIN
  1071. s := first;
  1072. WHILE (s # NIL) & (s.this # this) DO
  1073. s := s.next
  1074. END;
  1075. RETURN s
  1076. END Get;
  1077. END SymbolMapper;
  1078. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1079. VAR
  1080. system: Global.System;
  1081. section: IntermediateCode.Section;
  1082. module: Sections.Module;
  1083. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1084. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1085. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1086. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1087. checker: SemanticChecker.Checker;
  1088. backend: IntermediateBackend;
  1089. meta: MetaDataGenerator;
  1090. position: Position;
  1091. moduleSelf: SyntaxTree.Variable;
  1092. (* variables for hand over of variables / temporary state *)
  1093. currentScope: SyntaxTree.Scope;
  1094. constantDeclaration : SyntaxTree.Symbol;
  1095. result: Operand; (* result of the most recent expression / statement *)
  1096. destination: IntermediateCode.Operand;
  1097. arrayDestinationTag: IntermediateCode.Operand;
  1098. arrayDestinationDimension:LONGINT;
  1099. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1100. conditional: BOOLEAN;
  1101. trueLabel, falseLabel, exitLabel: Label;
  1102. locked: BOOLEAN;
  1103. (*
  1104. usedRegisters: Registers;
  1105. *)
  1106. registerUsageCount: RegisterUsageCount;
  1107. usedRegisters: RegisterEntry;
  1108. (* useful operands and types *)
  1109. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1110. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1111. commentPrintout: Printout.Printer;
  1112. dump: Streams.Writer;
  1113. tagsAvailable : BOOLEAN;
  1114. supportedInstruction: SupportedInstructionProcedure;
  1115. supportedImmediate: SupportedImmediateProcedure;
  1116. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1117. emitLabels: BOOLEAN;
  1118. runtimeModuleName : SyntaxTree.IdentifierString;
  1119. newObjectFile: BOOLEAN;
  1120. indexCounter: LONGINT;
  1121. profile: BOOLEAN;
  1122. profileId, profileInit: IntermediateCode.Section;
  1123. profileInitPatchPosition: LONGINT;
  1124. numberProcedures: LONGINT;
  1125. procedureResultDesignator : SyntaxTree.Designator;
  1126. operatorInitializationCodeSection: IntermediateCode.Section;
  1127. fingerPrinter: FingerPrinter.FingerPrinter;
  1128. temporaries: Variables;
  1129. canBeLoaded : BOOLEAN;
  1130. currentIsInline: BOOLEAN;
  1131. currentMapper: SymbolMapper;
  1132. currentInlineExit: Label;
  1133. moduleBodySection: IntermediateCode.Section;
  1134. NeedDescriptor : BOOLEAN;
  1135. cooperativeSwitches: BOOLEAN;
  1136. lastSwitchPC: LONGINT;
  1137. isUnchecked: BOOLEAN;
  1138. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1139. newObjectFile: BOOLEAN);
  1140. BEGIN
  1141. SELF.system := system;
  1142. SELF.runtimeModuleName := runtime;
  1143. currentScope := NIL;
  1144. hiddenPointerType := NIL;
  1145. delegatePointerType := NIL;
  1146. awaitProcCounter := 0;
  1147. labelId := 0; constId := 0; labelId := 0;
  1148. SELF.checker := checker;
  1149. SELF.backend := backend;
  1150. position := Basic.invalidPosition;
  1151. conditional := FALSE;
  1152. locked := FALSE;
  1153. InitOperand(result,ModeUndefined);
  1154. addressType := IntermediateCode.GetType(system,system.addressType);
  1155. setType := IntermediateCode.GetType(system,system.setType);
  1156. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1157. byteType := IntermediateCode.GetType(system, system.byteType);
  1158. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1159. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1160. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1161. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1162. nil := IntermediateCode.Immediate(addressType,0);
  1163. one := IntermediateCode.Immediate(addressType,1);
  1164. IntermediateCode.InitOperand(destination);
  1165. tagsAvailable := TRUE;
  1166. supportedInstruction := supportedInstructionProcedure;
  1167. supportedImmediate := supportedImmediateProcedure;
  1168. inData := FALSE;
  1169. SELF.emitLabels := emitLabels;
  1170. IntermediateCode.InitOperand(arrayDestinationTag);
  1171. bool := IntermediateCode.GetType(system,system.booleanType);
  1172. IntermediateCode.InitImmediate(false,bool,0);
  1173. IntermediateCode.InitImmediate(true,bool,1);
  1174. SELF.newObjectFile := newObjectFile;
  1175. indexCounter := 0;
  1176. NEW(registerUsageCount);
  1177. usedRegisters := NIL;
  1178. procedureResultDesignator := NIL;
  1179. NEW(fingerPrinter);
  1180. NEW(temporaries);
  1181. currentIsInline := FALSE;
  1182. NeedDescriptor := FALSE;
  1183. isUnchecked := backend.noRuntimeChecks;
  1184. END Init;
  1185. TYPE Context = RECORD
  1186. section: IntermediateCode.Section;
  1187. registerUsageCount: RegisterUsageCount;
  1188. usedRegisters: RegisterEntry;
  1189. END;
  1190. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1191. VAR context: Context;
  1192. BEGIN
  1193. context.section := section;
  1194. context.registerUsageCount := registerUsageCount;
  1195. context.usedRegisters := usedRegisters;
  1196. section := new;
  1197. NEW(registerUsageCount);
  1198. usedRegisters := NIL;
  1199. RETURN context;
  1200. END SwitchContext;
  1201. PROCEDURE ReturnToContext(context: Context);
  1202. BEGIN
  1203. section := context.section;
  1204. registerUsageCount := context.registerUsageCount;
  1205. usedRegisters := context.usedRegisters;
  1206. END ReturnToContext;
  1207. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1208. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1209. BEGIN
  1210. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1211. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1212. END;
  1213. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1214. section.SetExported(IsExported(syntaxTreeSymbol));
  1215. RETURN section
  1216. END NewSection;
  1217. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1218. VAR new: LONGINT;
  1219. BEGIN
  1220. new := registerUsageCount.Next(type,class);
  1221. UseRegister(new);
  1222. RETURN new
  1223. END AcquireRegister;
  1224. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1225. VAR
  1226. fingerPrint: SyntaxTree.FingerPrint;
  1227. fingerPrintString: ARRAY 32 OF CHAR;
  1228. BEGIN
  1229. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1230. string := "[";
  1231. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1232. Strings.Append(string, fingerPrintString);
  1233. Strings.Append(string, "]");
  1234. END GetFingerprintString;
  1235. (** get the name for the code section that represens a certain symbol
  1236. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1237. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1238. VAR string: ARRAY 32 OF CHAR;
  1239. BEGIN
  1240. Global.GetSymbolSegmentedName(symbol, name);
  1241. (* if the symbol is an operator, then append the fingerprint to the name *)
  1242. IF symbol IS SyntaxTree.Operator THEN
  1243. GetFingerprintString(symbol, string);
  1244. Basic.AppendToSegmentedName(name,string);
  1245. END
  1246. END GetCodeSectionNameForSymbol;
  1247. (** get the name for the code section that represens a certain symbol
  1248. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1249. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1250. VAR string: ARRAY 32 OF CHAR;
  1251. BEGIN
  1252. Global.GetSymbolNameInScope(symbol, scope, name);
  1253. (* if the symbol is an operator, then append the fingerprint to the name *)
  1254. IF symbol IS SyntaxTree.Operator THEN
  1255. GetFingerprintString(symbol, string);
  1256. Strings.Append(name, string);
  1257. END
  1258. END GetCodeSectionNameForSymbolInScope;
  1259. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1260. BEGIN
  1261. IF dump # NIL THEN
  1262. dump.String("enter "); dump.String(s); dump.Ln;
  1263. END;
  1264. END TraceEnter;
  1265. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1266. BEGIN
  1267. IF dump # NIL THEN
  1268. dump.String("exit "); dump.String(s); dump.Ln;
  1269. END;
  1270. END TraceExit;
  1271. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1272. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1273. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1274. VAR i: LONGINT;
  1275. BEGIN
  1276. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1277. IF op.rule # NIL THEN
  1278. FOR i := 0 TO LEN(op.rule)-1 DO
  1279. CheckRegister(op.rule[i])
  1280. END;
  1281. END;
  1282. END CheckRegister;
  1283. BEGIN
  1284. CheckRegister(instruction.op1);
  1285. CheckRegister(instruction.op2);
  1286. CheckRegister(instruction.op3);
  1287. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1288. ELSE section.Emit(instruction);
  1289. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1290. END;
  1291. END Emit;
  1292. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1293. BEGIN
  1294. IF backend.cooperative THEN
  1295. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1296. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1297. ELSE
  1298. Emit(Trap(position,trapNo));
  1299. END;
  1300. END EmitTrap;
  1301. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1302. VAR name: Basic.SegmentedName;
  1303. VAR op1, op2, reg: IntermediateCode.Operand;
  1304. VAR call, nocall: Label;
  1305. VAR parametersSize: LONGINT;
  1306. VAR prevSection: IntermediateCode.Section;
  1307. VAR prevDump: Streams.Writer;
  1308. VAR body: SyntaxTree.Body;
  1309. VAR procedureType: SyntaxTree.ProcedureType;
  1310. BEGIN
  1311. IF procedure # NIL THEN
  1312. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1313. ELSE procedureType := NIL;
  1314. END;
  1315. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1316. prevSection := SELF.section;
  1317. SELF.section := section;
  1318. prevDump := dump;
  1319. dump := section.comments;
  1320. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1321. IF backend.hasLinkRegister THEN
  1322. Emit(Push(Basic.invalidPosition, lr));
  1323. END;
  1324. Emit(Push(Basic.invalidPosition,fp));
  1325. END;
  1326. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1327. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1328. GetCodeSectionNameForSymbol(procedure, name);
  1329. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1330. ELSE
  1331. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1332. END;
  1333. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1334. Emit(Push(Basic.invalidPosition,op1));
  1335. Emit(Mov(Basic.invalidPosition,fp, sp));
  1336. body := procedure.procedureScope.body;
  1337. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1338. NEW(call, section);
  1339. NEW(nocall, section);
  1340. reg := NewRegisterOperand(addressType);
  1341. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1342. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1343. BrltL(call, sp, reg);
  1344. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1345. BrgeL(nocall, sp, op2);
  1346. call.Resolve(section.pc);
  1347. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1348. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1349. Emit(Push(Basic.invalidPosition, op2));
  1350. Emit(Push(Basic.invalidPosition, reg));
  1351. ReleaseIntermediateOperand(reg);
  1352. CallThis(position, "Activities","ExpandStack",2);
  1353. Emit(Result(Basic.invalidPosition, sp));
  1354. nocall.Resolve(section.pc);
  1355. END;
  1356. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1357. IF procedure # NIL THEN
  1358. body := procedure.procedureScope.body;
  1359. ELSE
  1360. body := NIL;
  1361. END;
  1362. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1363. Emit(Push(Basic.invalidPosition, one)) ;
  1364. procedureType.SetParametersOffset(1);
  1365. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1366. END;
  1367. Emit(Mov(Basic.invalidPosition, fp, sp));
  1368. END;
  1369. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1370. SELF.section := prevSection;
  1371. dump := prevDump;
  1372. END EmitEnter;
  1373. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1374. VAR op1,op2: IntermediateCode.Operand;
  1375. VAR instruction: IntermediateCode.Instruction;
  1376. BEGIN
  1377. IntermediateCode.InitNumber(op1,callconv);
  1378. IntermediateCode.InitNumber(op2,varSize);
  1379. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1380. RETURN instruction
  1381. END Enter;
  1382. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1383. VAR op1: IntermediateCode.Operand;
  1384. VAR instruction: IntermediateCode.Instruction;
  1385. BEGIN
  1386. IntermediateCode.InitNumber(op1,callconv);
  1387. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1388. RETURN instruction
  1389. END Leave;
  1390. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1391. VAR prevSection: IntermediateCode.Section;
  1392. VAR op2, size: IntermediateCode.Operand;
  1393. VAR body: SyntaxTree.Body;
  1394. BEGIN
  1395. prevSection := SELF.section;
  1396. SELF.section := section;
  1397. Emit(Leave(position, callconv));
  1398. IF procedure # NIL THEN
  1399. body := procedure.procedureScope.body;
  1400. ELSE
  1401. body := NIL;
  1402. END;
  1403. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1404. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1405. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1406. Emit(Add(position, sp, fp, op2));
  1407. ELSE
  1408. Emit(Mov(position, sp, fp));
  1409. END;
  1410. Emit(Pop(position, fp));
  1411. END;
  1412. SELF.section := prevSection;
  1413. END EmitLeave;
  1414. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1415. VAR m: SymbolMap;
  1416. BEGIN
  1417. position := x.position;
  1418. IF currentIsInline THEN
  1419. m := currentMapper.Get(x);
  1420. IF m # NIL THEN
  1421. (*
  1422. Printout.Info("mapping from", x);
  1423. Printout.Info("mapping to ", m.to);
  1424. *)
  1425. m.to.Accept(SELF);
  1426. op := result;
  1427. IF m.tag # NIL THEN
  1428. ReleaseIntermediateOperand(result.tag);
  1429. m.tag.Accept(SELF);
  1430. op.tag := result.op;
  1431. ReleaseIntermediateOperand(result.tag);
  1432. END;
  1433. RETURN
  1434. END;
  1435. END;
  1436. x.Accept(SELF);
  1437. op := result;
  1438. END Symbol;
  1439. PROCEDURE Expression(x: SyntaxTree.Expression);
  1440. BEGIN
  1441. position := x.position;
  1442. constantDeclaration := NIL;
  1443. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1444. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1445. END;
  1446. IF x.resolved # NIL THEN
  1447. x.resolved.Accept(SELF)
  1448. ELSE
  1449. x.Accept(SELF)
  1450. END;
  1451. (* check this, was commented out in ActiveCells3 *)
  1452. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1453. Error(x.position, "unsupported modifier");
  1454. END;
  1455. END Expression;
  1456. (*
  1457. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1458. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1459. BEGIN
  1460. temporaries.GetUsage(current);
  1461. FOR i := 0 TO LEN(current)-1 DO
  1462. set := current[i] - previous[i];
  1463. IF set # {} THEN
  1464. FOR j := 0 TO MAX(SET)-1 DO
  1465. IF j IN set THEN
  1466. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1467. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1468. Symbol(variable, op);
  1469. MakeMemory(tmp,op.op,addressType,0);
  1470. ReleaseOperand(op);
  1471. Emit(Mov(position,tmp, nil ) );
  1472. ReleaseIntermediateOperand(tmp);
  1473. END;
  1474. END;
  1475. END;
  1476. END;
  1477. END;
  1478. END ResetUsedTemporaries;
  1479. *)
  1480. PROCEDURE Statement(x: SyntaxTree.Statement);
  1481. VAR use: VariableUse;
  1482. BEGIN
  1483. temporaries.GetUsage(use);
  1484. position := x.position;
  1485. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1486. IF commentPrintout # NIL THEN
  1487. commentPrintout.Statement(x);
  1488. dump.Ln;
  1489. (*dump.Update;*)
  1490. END;
  1491. x.Accept(SELF);
  1492. (*
  1493. CheckRegistersFree();
  1494. *)
  1495. (*ResetUsedTemporaries(use);*)
  1496. temporaries.SetUsage(use);
  1497. END Statement;
  1498. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1499. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1500. *)
  1501. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1502. BEGIN
  1503. ASSERT(op.mode # IntermediateCode.Undefined);
  1504. IF op.mode = IntermediateCode.ModeMemory THEN
  1505. ReuseCopy(res,op);
  1506. ELSE
  1507. res := op;
  1508. UseIntermediateOperand(res);
  1509. END;
  1510. IntermediateCode.AddOffset(res,offset);
  1511. IntermediateCode.MakeMemory(res,type);
  1512. END MakeMemory;
  1513. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1514. VAR mem: IntermediateCode.Operand;
  1515. BEGIN
  1516. MakeMemory(mem,res,type,offset);
  1517. ReleaseIntermediateOperand(res);
  1518. res := mem;
  1519. END ToMemory;
  1520. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1521. VAR mem: IntermediateCode.Operand;
  1522. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1523. componentType: SyntaxTree.Type;
  1524. BEGIN
  1525. type := type.resolved;
  1526. IF operand.mode = ModeReference THEN
  1527. IF type IS SyntaxTree.RangeType THEN
  1528. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1529. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1530. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1531. ReleaseOperand(operand);
  1532. operand.op := firstOp;
  1533. operand.tag := lastOp;
  1534. operand.extra := stepOp;
  1535. ELSIF type IS SyntaxTree.ComplexType THEN
  1536. componentType := type(SyntaxTree.ComplexType).componentType;
  1537. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1538. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1539. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1540. ReleaseOperand(operand);
  1541. operand.op := firstOp;
  1542. operand.tag := lastOp
  1543. ELSE
  1544. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1545. ReleaseIntermediateOperand(operand.op);
  1546. operand.op := mem;
  1547. END;
  1548. operand.mode := ModeValue;
  1549. END;
  1550. ASSERT(operand.mode = ModeValue);
  1551. END LoadValue;
  1552. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1553. VAR prevConditional: BOOLEAN;
  1554. BEGIN
  1555. prevConditional := conditional;
  1556. conditional := FALSE;
  1557. InitOperand(result, ModeUndefined);
  1558. Expression(x);
  1559. op := result;
  1560. LoadValue(op,x.type.resolved);
  1561. conditional := prevConditional;
  1562. END Evaluate;
  1563. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1564. VAR prevConditional: BOOLEAN;
  1565. BEGIN
  1566. prevConditional := conditional;
  1567. conditional := FALSE;
  1568. InitOperand(result,ModeUndefined);
  1569. Expression(x);
  1570. op := result;
  1571. (*
  1572. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1573. *)
  1574. conditional := prevConditional;
  1575. END Designate;
  1576. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1577. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1578. BEGIN
  1579. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1580. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1581. conditional := TRUE;
  1582. trueLabel := trueL; falseLabel := falseL;
  1583. Expression(x);
  1584. trueL := trueLabel; falseL := falseLabel;
  1585. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1586. END Condition;
  1587. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1588. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1589. BEGIN
  1590. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1591. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1592. RETURN op
  1593. END NewRegisterOperand;
  1594. PROCEDURE UnuseRegister(register: LONGINT);
  1595. BEGIN
  1596. IF (register > 0) THEN
  1597. register := registerUsageCount.Map(register);
  1598. registerUsageCount.DecUse(register);
  1599. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1600. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1601. END;
  1602. IF registerUsageCount.Use(register)=0 THEN
  1603. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1604. Warning(position, "register cannot be removed");
  1605. END;
  1606. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1607. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1608. END;
  1609. ELSIF registerUsageCount.Use(register)<0 THEN
  1610. Warning(position, "register removed too often");
  1611. IF dump # NIL THEN
  1612. dump.String("register removed too often"); dump.Ln; dump.Update;
  1613. END;
  1614. END;
  1615. END;
  1616. END UnuseRegister;
  1617. PROCEDURE UseRegister(register: LONGINT);
  1618. BEGIN
  1619. IF (register > 0) THEN
  1620. register := registerUsageCount.Map(register);
  1621. registerUsageCount.IncUse(register);
  1622. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1623. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1624. END;
  1625. IF registerUsageCount.Use(register)=1 THEN
  1626. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1627. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1628. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1629. END;
  1630. END;
  1631. END;
  1632. END UseRegister;
  1633. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1634. BEGIN
  1635. UnuseRegister(op.register)
  1636. END ReleaseIntermediateOperand;
  1637. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1638. BEGIN
  1639. UseRegister(op.register)
  1640. END UseIntermediateOperand;
  1641. PROCEDURE ReleaseOperand(CONST op: Operand);
  1642. BEGIN
  1643. UnuseRegister(op.op.register);
  1644. UnuseRegister(op.tag.register);
  1645. UnuseRegister(op.extra.register);
  1646. END ReleaseOperand;
  1647. (* save registers marked in array "markedRegisters" to the stack
  1648. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1649. *)
  1650. PROCEDURE SaveRegisters();
  1651. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1652. BEGIN
  1653. entry := usedRegisters;
  1654. WHILE entry # NIL DO
  1655. type := registerUsageCount.used[entry.register].type;
  1656. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1657. Emit(Push(position,op));
  1658. entry := entry.next;
  1659. END;
  1660. END SaveRegisters;
  1661. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1662. BEGIN
  1663. saved := usedRegisters;
  1664. usedRegisters := NIL;
  1665. END ReleaseUsedRegisters;
  1666. (** remove parameter registers from used queue *)
  1667. PROCEDURE ReleaseParameterRegisters;
  1668. VAR entry,prev,next: RegisterEntry;
  1669. BEGIN
  1670. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1671. WHILE entry # NIL DO
  1672. next := entry.next;
  1673. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1674. registerUsageCount.DecUse(entry.register);
  1675. ASSERT(registerUsageCount.Use(entry.register)=0);
  1676. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1677. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1678. END;
  1679. ELSIF prev = NIL THEN
  1680. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1681. ELSE
  1682. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1683. END;
  1684. entry := next;
  1685. END;
  1686. END ReleaseParameterRegisters;
  1687. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1688. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1689. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1690. BEGIN
  1691. entry := saved;
  1692. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1693. entry := prev;
  1694. WHILE entry # NIL DO
  1695. prev := entry.prev;
  1696. type := entry.type;
  1697. class := entry.registerClass;
  1698. IntermediateCode.InitRegister(op,type,class,entry.register);
  1699. (*
  1700. new := registerUsageCount.Next(type,class);
  1701. registerUsageCount.Remap(entry.register,new);
  1702. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1703. dump.String("remap register "); dump.Int(entry.register,1);
  1704. dump.String("to "); dump.Int(new,1);
  1705. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1706. END;
  1707. entry.register := new;
  1708. *)
  1709. Emit(Pop(position,op));
  1710. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1711. entry := prev;
  1712. END;
  1713. (*
  1714. usedRegisters := saved;
  1715. *)
  1716. END RestoreRegisters;
  1717. PROCEDURE CheckRegistersFree;
  1718. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1719. BEGIN
  1720. IF usedRegisters # NIL THEN
  1721. r := usedRegisters;
  1722. WHILE r # NIL DO
  1723. warning := "register ";
  1724. Strings.AppendInt(warning, r.register);
  1725. Strings.Append(warning, " not released.");
  1726. Warning(position,warning);
  1727. r := r .next;
  1728. END;
  1729. END;
  1730. FOR i := 0 TO registerUsageCount.count-1 DO
  1731. IF registerUsageCount.used[i].count < 0 THEN
  1732. warning := "register ";
  1733. Strings.AppendInt(warning, i);
  1734. Strings.Append(warning, " unused too often.");
  1735. Warning(position,warning);
  1736. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1737. warning := "register ";
  1738. Strings.AppendInt(warning, i);
  1739. Strings.Append(warning, " not unused often enough.");
  1740. Warning(position,warning);
  1741. END;
  1742. END;
  1743. END CheckRegistersFree;
  1744. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1745. Otherwise allocate a new register.
  1746. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1747. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1748. BEGIN
  1749. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1750. UseIntermediateOperand(result);
  1751. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1752. UseIntermediateOperand(result);
  1753. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1754. END;
  1755. END Reuse2;
  1756. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1757. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1758. If not then allocate a new register.
  1759. Does NOT necessarily keep the content of src1 or src2 in result!
  1760. *)
  1761. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1762. BEGIN
  1763. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1764. UseIntermediateOperand(result);
  1765. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1766. UseIntermediateOperand(result);
  1767. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1768. result := alternative; alternative := emptyOperand;
  1769. UseIntermediateOperand(result);
  1770. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1771. END;
  1772. END Reuse2a;
  1773. (* like reuse2 but only one source *)
  1774. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1775. BEGIN
  1776. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1777. UseIntermediateOperand(result);
  1778. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1779. END;
  1780. END Reuse1;
  1781. (* like reuse2a but only one source *)
  1782. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1783. BEGIN
  1784. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1785. UseIntermediateOperand(result);
  1786. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1787. UseIntermediateOperand(result);
  1788. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1789. END;
  1790. END Reuse1a;
  1791. (* like reuse1 but guarantees that content of src1 is in result *)
  1792. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1793. BEGIN
  1794. IF ReusableRegister(src1) THEN
  1795. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1796. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1797. UseIntermediateOperand(result);
  1798. ELSE
  1799. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1800. Emit(Mov(position,result,src1));
  1801. END
  1802. END ReuseCopy;
  1803. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1804. BEGIN
  1805. IF ReusableRegister(src) THEN
  1806. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1807. ELSE
  1808. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1809. Emit(Mov(position,result,src));
  1810. ReleaseIntermediateOperand(src);
  1811. END
  1812. END TransferToRegister;
  1813. (** labels and branches **)
  1814. PROCEDURE NewLabel(): Label;
  1815. VAR label: Label;
  1816. BEGIN
  1817. NEW(label,section); RETURN label;
  1818. END NewLabel;
  1819. PROCEDURE SetLabel(label: Label);
  1820. BEGIN label.Resolve(section.pc);
  1821. END SetLabel;
  1822. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1823. BEGIN
  1824. ASSERT(label # NIL);
  1825. IF label.pc < 0 THEN (* label not yet set *)
  1826. label.AddFixup(section.pc);
  1827. END;
  1828. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1829. END LabelOperand;
  1830. PROCEDURE BrL(label: Label);
  1831. BEGIN
  1832. Emit(Br(position,LabelOperand(label)));
  1833. END BrL;
  1834. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1835. BEGIN
  1836. Emit(Brge(position,LabelOperand(label),left,right));
  1837. END BrgeL;
  1838. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1839. BEGIN
  1840. Emit(Brlt(position,LabelOperand(label),left,right));
  1841. END BrltL;
  1842. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1843. BEGIN
  1844. Emit(Breq(position,LabelOperand(label),left,right));
  1845. END BreqL;
  1846. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1847. BEGIN
  1848. Emit(Brne(position,LabelOperand(label),left,right));
  1849. END BrneL;
  1850. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1851. VAR new: IntermediateCode.Operand;
  1852. BEGIN
  1853. IF Trace THEN TraceEnter("Convert") END;
  1854. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1855. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1856. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1857. & (operand.offset = 0)
  1858. THEN
  1859. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1860. Emit(Conv(position,new,operand));
  1861. ELSE
  1862. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1863. Emit(Conv(position,new,operand));
  1864. ReleaseIntermediateOperand(operand);
  1865. END;
  1866. operand := new;
  1867. 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
  1868. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1869. ELSE
  1870. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1871. Emit(Conv(position,new,operand));
  1872. ReleaseIntermediateOperand(operand);
  1873. operand := new;
  1874. END;
  1875. IF Trace THEN TraceExit("Convert") END;
  1876. END Convert;
  1877. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1878. VAR exit: Label;
  1879. BEGIN
  1880. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1881. exit := NewLabel();
  1882. br(exit,left,right);
  1883. EmitTrap(position,trapNo);
  1884. SetLabel(exit);
  1885. END TrapC;
  1886. (** expressions *)
  1887. (** emit necessary runtime check for set elements **)
  1888. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1889. VAR max: IntermediateCode.Operand;
  1890. BEGIN
  1891. IF isUnchecked THEN RETURN END;
  1892. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1893. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1894. TrapC(BrgeL, max, o, SetElementTrap);
  1895. END;
  1896. END CheckSetElement;
  1897. (** the set that a range represents **)
  1898. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1899. VAR
  1900. operand: Operand;
  1901. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1902. BEGIN
  1903. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1904. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1905. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1906. one := IntermediateCode.Immediate(setType, 1);
  1907. Evaluate(x, operand);
  1908. Convert(operand.op, setType);
  1909. Convert(operand.tag, setType);
  1910. CheckSetElement(operand.op);
  1911. CheckSetElement(operand.tag);
  1912. (* create mask for lower bound
  1913. i.e. shift 11111111 to the left by the value of the lower bound
  1914. *)
  1915. Reuse1(temp, operand.op);
  1916. Emit(Shl(position,temp, allBits, operand.op));
  1917. ReleaseIntermediateOperand(operand.op);
  1918. operand.op := temp;
  1919. (* create mask for upper bound
  1920. i.e. shift 11111111 to the right by the difference between the
  1921. upper bound and the maximum number of set elements
  1922. *)
  1923. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1924. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1925. Reuse1(temp, operand.tag);
  1926. ELSE
  1927. Reuse1(temp, operand.tag);
  1928. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1929. Emit(Sub(position,temp, size, operand.tag));
  1930. END;
  1931. Emit(Shr(position,temp, allBits, operand.tag));
  1932. ReleaseIntermediateOperand(operand.tag);
  1933. operand.tag := temp;
  1934. Reuse2(resultingSet, operand.op, operand.tag);
  1935. (* intersect the two masks *)
  1936. Emit(And(position,resultingSet, operand.op, operand.tag));
  1937. ReleaseOperand(operand);
  1938. RETURN resultingSet
  1939. END SetFromRange;
  1940. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1941. VAR
  1942. res, operand: Operand;
  1943. temp, one, noBits, dest: IntermediateCode.Operand;
  1944. expression: SyntaxTree.Expression;
  1945. i: LONGINT;
  1946. BEGIN
  1947. IF Trace THEN TraceEnter("VisitSet") END;
  1948. dest := destination;
  1949. destination := emptyOperand;
  1950. noBits := IntermediateCode.Immediate(setType, 0);
  1951. one := IntermediateCode.Immediate(setType, 1);
  1952. (* start off with the empty set *)
  1953. InitOperand(res, ModeValue);
  1954. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1955. Emit(Mov(position,res.op, noBits));
  1956. FOR i := 0 TO x.elements.Length() - 1 DO
  1957. expression := x.elements.GetExpression(i);
  1958. IF expression IS SyntaxTree.RangeExpression THEN
  1959. (* range of set elements *)
  1960. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1961. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1962. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1963. ReleaseIntermediateOperand(temp)
  1964. ELSE
  1965. (* singelton element *)
  1966. Evaluate(expression, operand);
  1967. Convert(operand.op, setType);
  1968. CheckSetElement(operand.op);
  1969. (* create subset containing single element *)
  1970. Reuse1(temp, operand.op);
  1971. Emit(Shl(position,temp, one, operand.op));
  1972. ReleaseOperand(operand);
  1973. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1974. ReleaseIntermediateOperand(temp);
  1975. END
  1976. END;
  1977. result := res;
  1978. destination := dest;
  1979. IF Trace THEN TraceExit("VisitSet") END;
  1980. END VisitSet;
  1981. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1982. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1983. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1984. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1985. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1986. element: SyntaxTree.IntegerValue;
  1987. BEGIN
  1988. numberElements := x.elements.Length();
  1989. expression := index.parameters.GetExpression(dim);
  1990. element := expression(SyntaxTree.IntegerValue);
  1991. FOR i := 0 TO numberElements-1 DO
  1992. expression := x.elements.GetExpression(i);
  1993. element.SetValue(i);
  1994. IF expression IS SyntaxTree.MathArrayExpression THEN
  1995. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1996. ELSE
  1997. Assign(index,expression);
  1998. END;
  1999. END;
  2000. END RecursiveAssignment;
  2001. BEGIN
  2002. variable := GetTemporaryVariable(x.type, FALSE);
  2003. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2004. designator.SetType(variable.type);
  2005. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2006. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2007. FOR i := 0 TO dim-1 DO
  2008. element := SyntaxTree.NewIntegerValue(x.position,0);
  2009. element.SetType(system.longintType);
  2010. index.parameters.AddExpression(element);
  2011. END;
  2012. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2013. RecursiveAssignment(x,0);
  2014. Expression(designator);
  2015. END VisitMathArrayExpression;
  2016. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2017. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2018. BEGIN
  2019. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2020. dest := destination; destination := emptyOperand;
  2021. IF x.operator = Scanner.Not THEN
  2022. IF conditional THEN
  2023. Condition(x.left,falseLabel,trueLabel)
  2024. ELSE
  2025. Evaluate(x.left,operand);
  2026. InitOperand(result,ModeValue);
  2027. Reuse1a(result.op,operand.op,dest);
  2028. Emit(Xor(position,result.op,operand.op,true));
  2029. ReleaseOperand(operand);
  2030. END;
  2031. ELSIF x.operator = Scanner.Minus THEN
  2032. Evaluate(x.left,operand);
  2033. InitOperand(result,ModeValue);
  2034. Reuse1a(result.op,operand.op,dest);
  2035. type := x.left.type.resolved;
  2036. IF type IS SyntaxTree.SetType THEN
  2037. Emit(Not(position,result.op,operand.op));
  2038. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2039. Reuse1(result.tag,operand.tag);
  2040. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2041. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2042. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2043. Emit(Neg(position,result.op,operand.op));
  2044. ELSE HALT(200)
  2045. END;
  2046. ReleaseOperand(operand);
  2047. ELSIF x.operator = Scanner.Address THEN
  2048. Designate(x.left,operand);
  2049. operand.mode := ModeValue;
  2050. t0 := x.left.type.resolved;
  2051. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2052. ReleaseIntermediateOperand(operand.op);
  2053. operand.op := operand.tag;
  2054. IntermediateCode.InitOperand(operand.tag);
  2055. END;
  2056. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2057. result := operand;
  2058. ELSE HALT(100)
  2059. END;
  2060. destination := dest;
  2061. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2062. END VisitUnaryExpression;
  2063. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2064. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2065. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2066. BEGIN
  2067. type := type.resolved;
  2068. originalType := type;
  2069. IF type IS SyntaxTree.PointerType THEN
  2070. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2071. END;
  2072. IF type IS SyntaxTree.ObjectType THEN
  2073. BrL(trueL);
  2074. ELSE
  2075. ASSERT(type IS SyntaxTree.RecordType);
  2076. (*
  2077. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2078. *)
  2079. ReuseCopy(left,tag);
  2080. right := TypeDescriptorAdr(type);
  2081. IF ~newObjectFile THEN
  2082. IntermediateCode.MakeMemory(right,addressType);
  2083. END;
  2084. IF backend.cooperative THEN
  2085. repeatL := NewLabel();
  2086. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2087. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2088. END;
  2089. SetLabel(repeatL);
  2090. BreqL(trueL,left,right);
  2091. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2092. BrneL(repeatL,left,nil);
  2093. ELSIF meta.simple THEN
  2094. level := type(SyntaxTree.RecordType).Level();
  2095. (* get type desc tag of level relative to base tag *)
  2096. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2097. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2098. IntermediateCode.MakeMemory(left,addressType);
  2099. BreqL(trueL,left,right);
  2100. ELSE
  2101. level := type(SyntaxTree.RecordType).Level();
  2102. (* get type desc tag of level relative to base tag *)
  2103. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2104. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2105. IntermediateCode.MakeMemory(left,addressType);
  2106. BreqL(trueL,left,right);
  2107. END;
  2108. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2109. BrL(falseL);
  2110. END;
  2111. END TypeTest;
  2112. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2113. BEGIN
  2114. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2115. IF dump # NIL THEN
  2116. dump.String(s); dump.Ln;
  2117. END;
  2118. END Error;
  2119. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2120. BEGIN
  2121. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2122. IF dump # NIL THEN
  2123. dump.String(s); dump.Ln; dump.Update;
  2124. END;
  2125. END Warning;
  2126. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2127. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2128. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2129. operand:Operand;
  2130. BEGIN
  2131. previousSection := section;
  2132. Global.GetModuleName(mod,name);
  2133. Strings.Append(name,".@Trace");
  2134. Basic.ToSegmentedName(name, pooledName);
  2135. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2136. IF dump # NIL THEN dump := section.comments END;
  2137. IF backend.hasLinkRegister THEN
  2138. Emit(Push(Basic.invalidPosition, lr));
  2139. END;
  2140. variable := mod.moduleScope.firstVariable;
  2141. WHILE variable # NIL DO
  2142. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2143. Symbol(variable, operand);
  2144. register := operand.op;
  2145. CallTraceMethod(register, variable.type);
  2146. ReleaseIntermediateOperand(register);
  2147. END;
  2148. variable := variable.nextVariable;
  2149. END;
  2150. IF backend.hasLinkRegister THEN
  2151. Emit(Pop(Basic.invalidPosition, lr));
  2152. END;
  2153. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2154. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2155. Emit(Br(position,op));
  2156. INC(statCoopTraceModule, section.pc);
  2157. section := previousSection;
  2158. IF dump # NIL THEN dump := section.comments END;
  2159. END CreateTraceModuleMethod;
  2160. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2161. BEGIN
  2162. Emit (Push(position, dst));
  2163. Emit (Push(position, src));
  2164. CallThis(position,"GarbageCollector","Assign", 2);
  2165. END CallAssignPointer;
  2166. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2167. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2168. BEGIN
  2169. IF SemanticChecker.IsPointerType (type) THEN
  2170. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2171. ELSIF type.IsRecordType() THEN
  2172. Emit (Push(position,dst));
  2173. Emit (Push(position,src));
  2174. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2175. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2176. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2177. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2178. ELSIF IsStaticArray(type) THEN
  2179. size := StaticArrayNumElements(type);
  2180. base := StaticArrayBaseType(type);
  2181. IF base.IsRecordType() THEN
  2182. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2183. Emit (Push(position, dst));
  2184. Emit (Push(position, src));
  2185. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2187. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2188. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2189. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2190. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2191. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2192. Emit (Push(position, dst));
  2193. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2194. Emit (Push(position, src));
  2195. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2196. ELSE
  2197. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2198. Emit (Push(position, dst));
  2199. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2200. Emit (Push(position, src));
  2201. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2202. ASSERT(StaticArrayBaseType(type).IsPointer());
  2203. END;
  2204. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2205. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2206. Emit (Push(position, dst));
  2207. Emit (Push(position, src));
  2208. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2209. ELSE HALT(100); (* missing ? *)
  2210. END;
  2211. END CallAssignMethod;
  2212. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2213. VAR name: Basic.SegmentedName;
  2214. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2215. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2216. context: Context;
  2217. BEGIN
  2218. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2219. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2220. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2221. GetRecordTypeName (recordType,name);
  2222. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2223. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2224. IF dump # NIL THEN dump := section.comments END;
  2225. IF backend.hasLinkRegister THEN
  2226. Emit(Push(Basic.invalidPosition, lr));
  2227. END;
  2228. variable := recordType.recordScope.firstVariable;
  2229. WHILE variable # NIL DO
  2230. IF variable.NeedsTrace() THEN
  2231. dst := NewRegisterOperand (addressType);
  2232. src := NewRegisterOperand (addressType);
  2233. Emit (Mov(position, dst, parameter1));
  2234. Emit (Mov(position, src, parameter2));
  2235. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2236. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2237. CallAssignMethod(dst, src, variable.type);
  2238. ReleaseIntermediateOperand(src);
  2239. ReleaseIntermediateOperand(dst);
  2240. END;
  2241. variable := variable.nextVariable;
  2242. END;
  2243. recordBase := recordType.GetBaseRecord();
  2244. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2245. IF backend.hasLinkRegister THEN
  2246. Emit(Pop(Basic.invalidPosition, lr));
  2247. END;
  2248. GetRecordTypeName (recordBase,name);
  2249. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2250. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2251. Emit(Br(position,op));
  2252. ELSE
  2253. Emit(Exit(position,0,0, 0));
  2254. END;
  2255. IF ~recordType.isObject THEN
  2256. GetRecordTypeName (recordType,name);
  2257. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2258. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2259. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2260. NEW(registerUsageCount);
  2261. usedRegisters := NIL;
  2262. dst := NewRegisterOperand (addressType);
  2263. src := NewRegisterOperand (addressType);
  2264. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2265. Emit(Mov(position, dst, parameter1));
  2266. Emit(Mov(position, src, parameter2));
  2267. label := NewLabel();
  2268. SetLabel(label);
  2269. Emit(Push(position, dst));
  2270. Emit(Push(position, src));
  2271. GetRecordTypeName (recordType,name);
  2272. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2273. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2274. Emit(Call(position, op, 0));
  2275. Emit(Pop(position, src));
  2276. Emit(Pop(position, dst));
  2277. Emit(Add(position, dst, dst, ofs));
  2278. Emit(Add(position, src, src, ofs));
  2279. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2280. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2281. Emit(Exit(position,0,0, 0));
  2282. END;
  2283. INC(statCoopAssignProcedure, section.pc);
  2284. ReturnToContext(context);
  2285. IF dump # NIL THEN dump := section.comments END;
  2286. END CreateAssignProcedure;
  2287. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2288. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2289. BEGIN
  2290. IF IsUnsafePointer (type) THEN
  2291. skip := NewLabel();
  2292. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2293. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2294. BreqL (skip, op, nil);
  2295. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2296. SetLabel (skip);
  2297. ELSIF SemanticChecker.IsPointerType (type) THEN
  2298. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2299. CallThis(position,"GarbageCollector","Mark", 1);
  2300. ELSIF type.IsRecordType() THEN
  2301. Emit (Push(position,register));
  2302. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2303. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2304. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2305. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2306. ELSIF IsStaticArray(type) THEN
  2307. size := StaticArrayNumElements(type);
  2308. base := StaticArrayBaseType(type);
  2309. IF base.IsRecordType() THEN
  2310. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2311. Emit (Push(position, register));
  2312. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2314. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2315. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2316. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2317. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2318. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2319. Emit (Push(position, register));
  2320. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2321. ELSE
  2322. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2323. Emit (Push(position, register));
  2324. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2325. ASSERT(base.IsPointer());
  2326. END;
  2327. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2328. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2329. CallThis(position,"GarbageCollector","Mark", 1);
  2330. ELSE HALT(100); (* missing ? *)
  2331. END;
  2332. END CallTraceMethod;
  2333. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2334. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2335. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2336. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2337. BEGIN
  2338. previousSection := section;
  2339. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2340. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2341. GetRecordTypeName (recordType,name);
  2342. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2343. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2344. IF dump # NIL THEN dump := section.comments END;
  2345. IF backend.hasLinkRegister THEN
  2346. Emit(Push(Basic.invalidPosition, lr));
  2347. END;
  2348. variable := recordType.recordScope.firstVariable;
  2349. WHILE variable # NIL DO
  2350. IF variable.NeedsTrace() THEN
  2351. register := NewRegisterOperand (addressType);
  2352. Emit (Mov(position,register,parameter1));
  2353. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2354. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2355. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2356. END;
  2357. CallTraceMethod(register, variable.type);
  2358. ReleaseIntermediateOperand(register);
  2359. END;
  2360. variable := variable.nextVariable;
  2361. END;
  2362. recordBase := recordType.GetBaseRecord();
  2363. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2364. recordBase := recordBase.GetBaseRecord();
  2365. END;
  2366. IF recordBase # NIL THEN
  2367. IF backend.hasLinkRegister THEN
  2368. Emit(Pop(Basic.invalidPosition, lr));
  2369. END;
  2370. GetRecordTypeName (recordBase,name);
  2371. IF HasExplicitTraceMethod (recordBase) THEN
  2372. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2373. ELSE
  2374. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2375. END;
  2376. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2377. Emit(Br(position,op));
  2378. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2379. Emit(Exit(position,0,0,0));
  2380. ELSE
  2381. IF backend.hasLinkRegister THEN
  2382. Emit(Pop(Basic.invalidPosition, lr));
  2383. END;
  2384. IF recordType.isObject THEN
  2385. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2386. ELSE
  2387. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2388. END;
  2389. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2390. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2391. Emit(Br(position,op));
  2392. END;
  2393. IF ~recordType.isObject THEN
  2394. GetRecordTypeName (recordType,name);
  2395. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2396. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2397. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2398. NEW(registerUsageCount);
  2399. usedRegisters := NIL;
  2400. IF dump # NIL THEN dump := section.comments END;
  2401. register := NewRegisterOperand (addressType);
  2402. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2403. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2404. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2405. END;
  2406. Emit (Push(position,register));
  2407. GetRecordTypeName (recordType,name);
  2408. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2409. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2410. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2411. ReleaseIntermediateOperand(register);
  2412. Emit(Exit(position,0,0,0));
  2413. GetRecordTypeName (recordType,name);
  2414. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2415. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2416. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2417. NEW(registerUsageCount);
  2418. usedRegisters := NIL;
  2419. register := NewRegisterOperand (addressType);
  2420. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2421. Emit(Mov(position, register, parameter1));
  2422. label := NewLabel();
  2423. SetLabel(label);
  2424. Emit(Push(position, register));
  2425. GetRecordTypeName (recordType,name);
  2426. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2427. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2428. Emit(Call(position, op, 0));
  2429. Emit(Pop(position, register));
  2430. Emit(Add(position, register, register, ofs));
  2431. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2432. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2433. Emit(Exit(position,0,0,0));
  2434. END;
  2435. INC(statCoopTraceMethod, section.pc);
  2436. ReturnToContext(context);
  2437. IF dump # NIL THEN dump := section.comments END;
  2438. END CreateTraceMethod;
  2439. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2440. VAR name: Basic.SegmentedName;
  2441. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2442. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2443. context: Context;
  2444. BEGIN
  2445. IF recordType.isObject THEN RETURN END;
  2446. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2447. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2448. GetRecordTypeName (recordType,name);
  2449. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2450. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2451. IF dump # NIL THEN dump := section.comments END;
  2452. IF backend.hasLinkRegister THEN
  2453. Emit(Push(Basic.invalidPosition, lr));
  2454. END;
  2455. variable := recordType.recordScope.firstVariable;
  2456. WHILE variable # NIL DO
  2457. IF variable.NeedsTrace() THEN
  2458. dst := NewRegisterOperand (addressType);
  2459. Emit (Mov(position, dst, parameter1));
  2460. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2461. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2462. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2463. END;
  2464. CallResetProcedure(dst, nil, variable.type);
  2465. ReleaseIntermediateOperand(dst);
  2466. END;
  2467. variable := variable.nextVariable;
  2468. END;
  2469. recordBase := recordType.GetBaseRecord();
  2470. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2471. IF backend.hasLinkRegister THEN
  2472. Emit(Pop(Basic.invalidPosition, lr));
  2473. END;
  2474. GetRecordTypeName (recordBase,name);
  2475. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2476. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2477. Emit(Br(position,op));
  2478. ELSE
  2479. Emit(Exit(position,0,0, 0));
  2480. END;
  2481. GetRecordTypeName (recordType,name);
  2482. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2483. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2484. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2485. NEW(registerUsageCount);
  2486. usedRegisters := NIL;
  2487. dst := NewRegisterOperand (addressType);
  2488. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2489. Emit(Mov(position, dst, parameter1));
  2490. label := NewLabel();
  2491. SetLabel(label);
  2492. Emit(Push(position, dst));
  2493. GetRecordTypeName (recordType,name);
  2494. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2495. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2496. Emit(Call(position, op, 0));
  2497. Emit(Pop(position, dst));
  2498. Emit(Add(position, dst, dst, ofs));
  2499. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2500. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2501. Emit(Exit(position,0,0, 0));
  2502. INC(statCoopResetProcedure, section.pc);
  2503. ReturnToContext(context);
  2504. IF dump # NIL THEN dump := section.comments END;
  2505. END CreateResetProcedure;
  2506. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2507. VAR name: Basic.SegmentedName; context: Context;
  2508. BEGIN
  2509. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2510. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2511. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2512. IF dump # NIL THEN dump := section.comments END;
  2513. IF backend.hasLinkRegister THEN
  2514. Emit(Push(Basic.invalidPosition, lr));
  2515. END;
  2516. Emit(Push(position,fp));
  2517. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2518. ResetVariables(scope);
  2519. Emit(Pop(position,fp));
  2520. Emit(Exit(position,0,0, 0));
  2521. ReturnToContext(context);
  2522. IF dump # NIL THEN dump := section.comments END;
  2523. END CreateResetMethod;
  2524. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2525. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2526. BEGIN
  2527. IF SemanticChecker.IsPointerType (type) THEN
  2528. Emit (Push(position, dest));
  2529. CallThis(position,"GarbageCollector","Reset", 1);
  2530. ELSIF type.IsRecordType() THEN
  2531. Emit (Push(position, dest));
  2532. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2533. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2534. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2535. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2536. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2537. size := GetArrayLength(type, tag);
  2538. base := ArrayBaseType(type);
  2539. IF base.IsRecordType() THEN
  2540. Emit (Push(position, size));
  2541. Emit (Push(position, dest));
  2542. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2543. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2544. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2545. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2546. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2547. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2548. Emit (Push(position, size));
  2549. Emit (Push(position, dest));
  2550. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2551. ELSE
  2552. Emit (Push(position, size));
  2553. Emit (Push(position, dest));
  2554. CallThis(position,"GarbageCollector","ResetArray", 2);
  2555. ASSERT(ArrayBaseType(type).IsPointer());
  2556. END;
  2557. ReleaseIntermediateOperand(size);
  2558. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2559. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2560. Emit (Push(position, dest));
  2561. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2562. ELSE HALT(100); (* missing ? *)
  2563. END;
  2564. END CallResetProcedure;
  2565. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2566. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2567. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2568. VAR operand: Operand;
  2569. BEGIN
  2570. Symbol (symbol, operand);
  2571. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2572. ReleaseOperand(operand);
  2573. END Reset;
  2574. BEGIN
  2575. previousScope := currentScope;
  2576. currentScope := scope;
  2577. pc := section.pc;
  2578. variable := scope.firstVariable;
  2579. WHILE variable # NIL DO
  2580. IF variable.NeedsTrace() THEN
  2581. Reset (variable);
  2582. END;
  2583. variable := variable.nextVariable;
  2584. END;
  2585. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2586. WHILE parameter # NIL DO
  2587. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2588. Reset (parameter);
  2589. END;
  2590. parameter := parameter.nextParameter;
  2591. END;
  2592. INC(statCoopResetVariables, section.pc - pc);
  2593. currentScope := previousScope;
  2594. END ResetVariables;
  2595. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2596. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2597. VAR op: IntermediateCode.Operand; context: Context;
  2598. BEGIN
  2599. previousSection := section;
  2600. GetCodeSectionNameForSymbol(procedure, name);
  2601. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2602. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2603. IF dump # NIL THEN dump := section.comments END;
  2604. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2605. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2606. Emit(Data(position,op));
  2607. Emit(Data(position,nil));
  2608. IF HasPointers (procedure.procedureScope) THEN
  2609. GetCodeSectionNameForSymbol(procedure, name);
  2610. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2611. ELSE
  2612. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2613. END;
  2614. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2615. Emit(Data(position,op));
  2616. ReturnToContext(context);
  2617. IF dump # NIL THEN dump := section.comments END;
  2618. END CreateProcedureDescriptor;
  2619. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2620. VAR import: SyntaxTree.Import;
  2621. s: Basic.MessageString;
  2622. selfName: SyntaxTree.IdentifierString;
  2623. BEGIN
  2624. moduleScope.ownerModule.GetName(selfName);
  2625. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2626. module := moduleScope.ownerModule
  2627. ELSE
  2628. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2629. IF import = NIL THEN
  2630. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2631. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2632. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2633. s := "Module ";
  2634. Strings.Append(s,moduleName);
  2635. Strings.Append(s," cannot be imported.");
  2636. IF force THEN
  2637. Error(position,s);
  2638. ELSIF canBeLoaded THEN
  2639. IF WarningDynamicLoading THEN
  2640. Strings.Append(s, "=> no dynamic linking.");
  2641. Warning(position, s);
  2642. END;
  2643. canBeLoaded := FALSE;
  2644. END;
  2645. RETURN FALSE
  2646. ELSE
  2647. SELF.module.imports.AddName(moduleName)
  2648. END;
  2649. ELSIF import.module = NIL THEN (* already tried *)
  2650. RETURN FALSE
  2651. END;
  2652. module := import.module;
  2653. END;
  2654. RETURN TRUE
  2655. END AddImport;
  2656. (* needed for old binary object file format*)
  2657. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2658. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2659. BEGIN
  2660. IF AddImport(moduleName,module,TRUE) THEN
  2661. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2662. IF procedure = NIL THEN
  2663. s := "Instruction not supported on target, emulation procedure ";
  2664. Strings.Append(s,moduleName);
  2665. Strings.Append(s,".");
  2666. Strings.Append(s,procedureName);
  2667. Strings.Append(s," not present");
  2668. Error(position,s);
  2669. ELSE
  2670. StaticCallOperand(r,procedure);
  2671. ReleaseOperand(r);
  2672. fp := GetFingerprint(procedure);
  2673. END;
  2674. END;
  2675. END EnsureSymbol;
  2676. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2677. VAR exit: Label; trueL,falseL: Label;
  2678. BEGIN
  2679. trueL := NewLabel();
  2680. falseL := NewLabel();
  2681. exit := NewLabel();
  2682. Condition(x,trueL,falseL);
  2683. InitOperand(result,ModeValue);
  2684. SetLabel(trueL);
  2685. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2686. Emit(Mov(position,result.op,true));
  2687. BrL(exit);
  2688. SetLabel(falseL);
  2689. Emit(MovReplace(position,result.op,false));
  2690. SetLabel(exit);
  2691. END ConditionToValue;
  2692. PROCEDURE ValueToCondition(VAR op: Operand);
  2693. BEGIN
  2694. LoadValue(op,system.booleanType);
  2695. BrneL(trueLabel,op.op, false);
  2696. ReleaseOperand(op);
  2697. BrL(falseLabel);
  2698. END ValueToCondition;
  2699. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2700. VAR size: LONGINT;
  2701. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2702. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2703. BEGIN
  2704. ASSERT(type.form = SyntaxTree.Open);
  2705. baseType := type.arrayBase.resolved;
  2706. IF IsOpenArray(baseType) THEN
  2707. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2708. ELSE
  2709. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2710. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2711. END;
  2712. len := tag;
  2713. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2714. IntermediateCode.MakeMemory(len,addressType);
  2715. UseIntermediateOperand(len);
  2716. Reuse2(res,sizeOperand,len);
  2717. Emit(Mul(position,res,sizeOperand,len));
  2718. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2719. RETURN res
  2720. END GetArraySize;
  2721. BEGIN
  2722. type := type.resolved;
  2723. IF IsOpenArray(type) THEN
  2724. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2725. RETURN tag
  2726. ELSE
  2727. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2728. END;
  2729. ELSE
  2730. size := ToMemoryUnits(system,system.SizeOf(type));
  2731. RETURN IntermediateCode.Immediate(addressType,size)
  2732. END;
  2733. END GetDynamicSize;
  2734. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2735. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2736. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2737. BEGIN
  2738. ASSERT(type.form = SyntaxTree.Open);
  2739. baseType := type.arrayBase.resolved;
  2740. IF IsOpenArray(baseType) THEN
  2741. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2742. ELSE
  2743. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2744. END;
  2745. len := tag;
  2746. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2747. IntermediateCode.MakeMemory(len,addressType);
  2748. UseIntermediateOperand(len);
  2749. Reuse2(res,sizeOperand,len);
  2750. Emit(Mul(position,res,sizeOperand,len));
  2751. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2752. RETURN res
  2753. END GetLength;
  2754. BEGIN
  2755. type := type.resolved;
  2756. IF IsOpenArray(type) THEN
  2757. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2758. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2759. ELSIF type IS SyntaxTree.StringType THEN
  2760. RETURN tag;
  2761. ELSE
  2762. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2763. END;
  2764. END GetArrayLength;
  2765. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2766. VAR result: IntermediateCode.Operand;
  2767. BEGIN
  2768. IF backend.cooperative THEN
  2769. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2770. ELSE
  2771. MakeMemory(result, tag, addressType, 0);
  2772. END;
  2773. RETURN result
  2774. END GetSizeFromTag;
  2775. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2776. VAR parameter: SyntaxTree.Parameter;
  2777. BEGIN
  2778. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2779. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2780. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2781. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2782. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2783. END;
  2784. RETURN GetDynamicSize(e.type, tag);
  2785. END GetArrayOfBytesSize;
  2786. (*
  2787. to find imported symbol. not needed ?
  2788. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2789. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2790. BEGIN
  2791. IF AddImport(moduleName,importedModule,FALSE) THEN
  2792. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2793. RETURN symbol
  2794. ELSE
  2795. RETURN NIL
  2796. END
  2797. END SymbolByName;
  2798. *)
  2799. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2800. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2801. BEGIN
  2802. IF AddImport(moduleName,runtimeModule,force) THEN
  2803. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2804. IF procedure = NIL THEN
  2805. s := "Procedure ";
  2806. Strings.Append(s,moduleName);
  2807. Strings.Append(s,".");
  2808. Strings.Append(s,procedureName);
  2809. Strings.Append(s," not present");
  2810. Error(position,s);
  2811. RETURN FALSE
  2812. ELSE
  2813. RETURN TRUE
  2814. END;
  2815. ELSE RETURN FALSE
  2816. END;
  2817. END GetRuntimeProcedure;
  2818. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2819. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2820. s: Basic.MessageString;
  2821. BEGIN
  2822. Basic.InitSegmentedName(name);
  2823. name[0] := Basic.MakeString(moduleName);
  2824. name[1] := Basic.MakeString(typeName);
  2825. name[2] := -1;
  2826. IF AddImport(moduleName,importedModule, FALSE) THEN
  2827. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2828. IF symbol = NIL THEN
  2829. s := "type ";
  2830. Strings.Append(s,moduleName);
  2831. Strings.Append(s,".");
  2832. Strings.Append(s,typeName);
  2833. Strings.Append(s," not present");
  2834. Error(position,s);
  2835. END;
  2836. ELSE symbol := NIL;
  2837. END;
  2838. IF importedModule = moduleScope.ownerModule THEN
  2839. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2840. ELSE
  2841. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2842. END;
  2843. RETURN symbol
  2844. END GetTypeDescriptor;
  2845. (* 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. *)
  2846. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2847. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2848. pooledName: Basic.SegmentedName; size: LONGINT;
  2849. BEGIN
  2850. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2851. StaticCallOperand(result,procedure);
  2852. IF numberParameters < 0 THEN
  2853. size := ProcedureParametersSize(system,procedure);
  2854. ELSE
  2855. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2856. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2857. Error(position,"runtime call parameter count mismatch");
  2858. END;
  2859. END;
  2860. Emit(Call(position, result.op, size));
  2861. ReleaseOperand(result);
  2862. ELSE (* only static linking possible *)
  2863. ASSERT(numberParameters >= 0);
  2864. Basic.InitSegmentedName(pooledName);
  2865. pooledName[0] := Basic.MakeString(moduleName);
  2866. pooledName[1] := Basic.MakeString(procedureName);
  2867. pooledName[2] := -1;
  2868. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2869. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2870. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2871. END;
  2872. END CallThisChecked;
  2873. (* 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. *)
  2874. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2875. BEGIN
  2876. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2877. END CallThis;
  2878. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2879. VAR
  2880. left,right: Operand;
  2881. leftSize, rightSize: IntermediateCode.Operand;
  2882. saved: RegisterEntry;
  2883. reg: IntermediateCode.Operand;
  2884. procedureName: SyntaxTree.IdentifierString;
  2885. BEGIN
  2886. procedureName := "CompareString";
  2887. SaveRegisters();ReleaseUsedRegisters(saved);
  2888. Designate(leftExpression,left);
  2889. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2890. Emit(Push(position,leftSize));
  2891. ReleaseIntermediateOperand(leftSize);
  2892. Emit(Push(position,left.op));
  2893. ReleaseOperand(left);
  2894. Designate(rightExpression,right);
  2895. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2896. Emit(Push(position,rightSize));
  2897. ReleaseIntermediateOperand(rightSize);
  2898. Emit(Push(position,right.op));
  2899. ReleaseOperand(right);
  2900. IF backend.cooperative THEN
  2901. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2902. ELSE
  2903. CallThis(position,runtimeModuleName,procedureName, 4);
  2904. END;
  2905. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2906. Emit(Result(position,reg));
  2907. (*
  2908. AcquireThisRegister(int8,IntermediateCode.Result);
  2909. *)
  2910. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2911. (*
  2912. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2913. *)
  2914. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2915. ReleaseIntermediateOperand(reg);
  2916. BrL(falseLabel);
  2917. END CompareString;
  2918. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2919. VAR
  2920. left,right: Operand;
  2921. leftSize, rightSize: IntermediateCode.Operand;
  2922. saved: RegisterEntry;
  2923. procedureName: SyntaxTree.IdentifierString;
  2924. BEGIN
  2925. procedureName := "CopyString";
  2926. SaveRegisters();ReleaseUsedRegisters(saved);
  2927. Designate(leftExpression,left);
  2928. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2929. Emit(Push(position,leftSize));
  2930. ReleaseIntermediateOperand(leftSize);
  2931. Emit(Push(position,left.op));
  2932. ReleaseOperand(left);
  2933. Designate(rightExpression,right);
  2934. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2935. Emit(Push(position,rightSize));
  2936. ReleaseIntermediateOperand(rightSize);
  2937. Emit(Push(position,right.op));
  2938. ReleaseOperand(right);
  2939. IF backend.cooperative THEN
  2940. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2941. ELSE
  2942. CallThis(position,runtimeModuleName,procedureName,4);
  2943. END;
  2944. RestoreRegisters(saved);
  2945. END CopyString;
  2946. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2947. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2948. leftType,rightType: SyntaxTree.Type;
  2949. leftExpression,rightExpression : SyntaxTree.Expression;
  2950. componentType: IntermediateCode.Type;
  2951. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2952. size: LONGINT;
  2953. BEGIN
  2954. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2955. dest := destination; destination := emptyOperand;
  2956. leftType := x.left.type.resolved;
  2957. rightType := x.right.type.resolved;
  2958. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2959. CASE x.operator OF
  2960. Scanner.Or:
  2961. (* shortcut evaluation of left OR right *)
  2962. IF ~conditional THEN ConditionToValue(x);
  2963. ELSE
  2964. next := NewLabel();
  2965. Condition(x.left,trueLabel,next);
  2966. SetLabel(next);
  2967. Condition(x.right,trueLabel,falseLabel);
  2968. END;
  2969. |Scanner.And:
  2970. (* shortcut evaluation of left & right *)
  2971. IF ~conditional THEN ConditionToValue(x);
  2972. ELSE
  2973. next := NewLabel();
  2974. Condition(x.left,next,falseLabel);
  2975. SetLabel(next);
  2976. Condition(x.right,trueLabel,falseLabel);
  2977. END;
  2978. |Scanner.Is:
  2979. IF ~conditional THEN ConditionToValue(x);
  2980. ELSE
  2981. (* get type desc tag *)
  2982. IF IsPointerToRecord(leftType,recordType) THEN
  2983. Evaluate(x.left,left);
  2984. Dereference(left,recordType,IsUnsafePointer(leftType))
  2985. ELSE
  2986. Designate(x.left,left);
  2987. END;
  2988. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2989. ReleaseOperand(left);
  2990. END;
  2991. |Scanner.Plus:
  2992. Evaluate(x.left,left);
  2993. Evaluate(x.right,right);
  2994. IF leftType IS SyntaxTree.SetType THEN
  2995. InitOperand(result,ModeValue);
  2996. Reuse2a(result.op,left.op,right.op,dest);
  2997. Emit(Or(position,result.op,left.op,right.op));
  2998. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2999. InitOperand(result,ModeValue);
  3000. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3001. Reuse2(result.tag,left.tag,right.tag);
  3002. Emit(Add(position,result.op,left.op,right.op));
  3003. Emit(Add(position,result.tag,left.tag,right.tag))
  3004. ELSE
  3005. InitOperand(result,ModeValue);
  3006. (*! IF SemanticChecker.IsIntegerType(leftType) THEN
  3007. AddInt(result.op, left.op, right.op) ;
  3008. ELSE
  3009. *)
  3010. Reuse2a(result.op,left.op,right.op,dest);
  3011. Emit(Add(position,result.op,left.op,right.op));
  3012. (*
  3013. END;
  3014. *)
  3015. END;
  3016. ReleaseOperand(left); ReleaseOperand(right);
  3017. |Scanner.Minus:
  3018. Evaluate(x.left,left);
  3019. Evaluate(x.right,right);
  3020. IF leftType IS SyntaxTree.SetType THEN
  3021. InitOperand(result,ModeValue);
  3022. Reuse1(result.op,right.op);
  3023. Emit(Not(position,result.op,right.op));
  3024. ReleaseOperand(right);
  3025. Emit(And(position,result.op,result.op,left.op));
  3026. ReleaseOperand(left);
  3027. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3028. InitOperand(result,ModeValue);
  3029. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3030. Reuse2(result.tag,left.tag,right.tag);
  3031. Emit(Sub(position,result.op,left.op,right.op));
  3032. Emit(Sub(position,result.tag,left.tag,right.tag));
  3033. ReleaseOperand(left); ReleaseOperand(right)
  3034. ELSE
  3035. InitOperand(result,ModeValue);
  3036. Reuse2a(result.op,left.op,right.op,dest);
  3037. Emit(Sub(position,result.op,left.op,right.op));
  3038. ReleaseOperand(left); ReleaseOperand(right);
  3039. END;
  3040. |Scanner.Times:
  3041. Evaluate(x.left,left);
  3042. Evaluate(x.right,right);
  3043. IF leftType IS SyntaxTree.SetType THEN
  3044. InitOperand(result,ModeValue);
  3045. Reuse2a(result.op,left.op,right.op,dest);
  3046. Emit(And(position,result.op,left.op,right.op));
  3047. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3048. InitOperand(result, ModeValue);
  3049. componentType := left.op.type;
  3050. (* TODO: review this *)
  3051. (*
  3052. result.op = left.op * right.op - left.tag * right.tag
  3053. result.tag = left.tag * right.op + left.op * right.tag
  3054. *)
  3055. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3056. Emit(Mul(position,result.op, left.op, right.op));
  3057. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3058. Emit(Mul(position,tempReg, left.tag, right.tag));
  3059. Emit(Sub(position,result.op, result.op, tempReg));
  3060. Reuse2(result.tag, left.tag, right.op);
  3061. Emit(Mul(position,result.tag, left.tag, right.op));
  3062. Emit(Mul(position,tempReg, left.op, right.tag));
  3063. Emit(Add(position,result.tag, result.tag, tempReg));
  3064. ReleaseIntermediateOperand(tempReg)
  3065. ELSE
  3066. InitOperand(result,ModeValue);
  3067. Reuse2a(result.op,left.op,right.op,dest);
  3068. Emit(Mul(position,result.op,left.op,right.op));
  3069. END;
  3070. ReleaseOperand(left); ReleaseOperand(right);
  3071. |Scanner.Div:
  3072. Evaluate(x.left,left);
  3073. Evaluate(x.right,right);
  3074. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3075. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3076. IF ~isUnchecked THEN
  3077. exit := NewLabel();
  3078. BrltL(exit,zero,right.op);
  3079. EmitTrap(position,NegativeDivisorTrap);
  3080. SetLabel(exit);
  3081. END;
  3082. END;
  3083. InitOperand(result,ModeValue);
  3084. Reuse2a(result.op,left.op,right.op,dest);
  3085. Emit(Div(position,result.op,left.op,right.op));
  3086. ReleaseOperand(left); ReleaseOperand(right);
  3087. |Scanner.Mod:
  3088. Evaluate(x.left,left);
  3089. Evaluate(x.right,right);
  3090. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3091. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3092. IF ~isUnchecked THEN
  3093. exit := NewLabel();
  3094. BrltL(exit,zero,right.op);
  3095. EmitTrap(position,NegativeDivisorTrap);
  3096. SetLabel(exit);
  3097. END;
  3098. END;
  3099. InitOperand(result,ModeValue);
  3100. Reuse2a(result.op,left.op,right.op,dest);
  3101. Emit(Mod(position,result.op,left.op,right.op));
  3102. ReleaseOperand(left); ReleaseOperand(right);
  3103. |Scanner.Slash:
  3104. Evaluate(x.left,left);
  3105. Evaluate(x.right,right);
  3106. IF leftType IS SyntaxTree.SetType THEN
  3107. InitOperand(result,ModeValue);
  3108. Reuse2a(result.op,left.op,right.op,dest);
  3109. Emit(Xor(position,result.op,left.op,right.op));
  3110. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3111. InitOperand(result,ModeValue);
  3112. componentType := left.op.type;
  3113. (* review this *)
  3114. (*
  3115. divisor = right.op * right.op + right.tag * right.tag
  3116. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3117. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3118. *)
  3119. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3120. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3121. Emit(Mul(position,tempReg, right.op, right.op));
  3122. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3123. Emit(Add(position,tempReg, tempReg, tempReg2));
  3124. result.op := tempReg2;
  3125. Emit(Mul(position,result.op, left.op, right.op));
  3126. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3127. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3128. Emit(Add(position,result.op, result.op, tempReg2));
  3129. Emit(Div(position,result.op, result.op, tempReg));
  3130. Reuse2(result.tag, left.tag, right.op);
  3131. Emit(Mul(position,result.tag, left.tag, right.op));
  3132. Emit(Mul(position,tempReg2, left.op, right.tag));
  3133. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3134. Emit(Div(position,result.tag, result.tag, tempReg));
  3135. ReleaseIntermediateOperand(tempReg);
  3136. ReleaseIntermediateOperand(tempReg2)
  3137. ELSE
  3138. InitOperand(result,ModeValue);
  3139. Reuse2a(result.op,left.op,right.op,dest);
  3140. Emit(Div(position,result.op,left.op,right.op));
  3141. END;
  3142. ReleaseOperand(left); ReleaseOperand(right);
  3143. |Scanner.Equal:
  3144. IF ~conditional THEN ConditionToValue(x);
  3145. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3146. CompareString(BreqL,x.left,x.right);
  3147. ELSE
  3148. Evaluate(x.left,left);
  3149. Evaluate(x.right,right);
  3150. IF leftType IS SyntaxTree.RangeType THEN
  3151. ASSERT(rightType IS SyntaxTree.RangeType);
  3152. BrneL(falseLabel, left.op, right.op); (* first *)
  3153. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3154. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3155. ReleaseOperand(left); ReleaseOperand(right);
  3156. BrL(trueLabel)
  3157. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3158. BrneL(falseLabel, left.op, right.op); (* first *)
  3159. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3160. ReleaseOperand(left); ReleaseOperand(right);
  3161. BrL(trueLabel)
  3162. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3163. (* TODO: review this *)
  3164. BrneL(falseLabel, left.op, right.op); (* real part *)
  3165. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3166. ReleaseOperand(left); ReleaseOperand(right);
  3167. BrL(trueLabel)
  3168. ELSE
  3169. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3170. ReleaseOperand(left); ReleaseOperand(right);
  3171. BrL(trueLabel);
  3172. END;
  3173. END;
  3174. |Scanner.LessEqual:
  3175. IF ~conditional THEN ConditionToValue(x);
  3176. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3177. CompareString(BrgeL,x.right,x.left);
  3178. ELSE
  3179. Evaluate(x.left,left);
  3180. Evaluate(x.right,right);
  3181. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3182. Reuse1(temp.op,right.op);
  3183. Emit(And(position,temp.op,left.op,right.op));
  3184. ReleaseOperand(right);
  3185. BreqL(trueLabel,temp.op,left.op);
  3186. BrL(falseLabel);
  3187. ReleaseOperand(temp);ReleaseOperand(left);
  3188. ELSE
  3189. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3190. ReleaseOperand(left); ReleaseOperand(right);
  3191. BrL(trueLabel);
  3192. END;
  3193. END;
  3194. |Scanner.Less:
  3195. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3196. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3197. leftExpression.SetType(system.booleanType);
  3198. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3199. rightExpression.SetType(system.booleanType);
  3200. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3201. leftExpression.SetType(system.booleanType);
  3202. Expression(leftExpression);
  3203. ELSIF ~conditional THEN ConditionToValue(x);
  3204. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3205. CompareString(BrltL,x.left,x.right);
  3206. ELSE
  3207. Evaluate(x.left,left);
  3208. Evaluate(x.right,right);
  3209. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3210. ReleaseOperand(left); ReleaseOperand(right);
  3211. BrL(trueLabel);
  3212. END;
  3213. |Scanner.Greater:
  3214. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3215. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3216. leftExpression.SetType(system.booleanType);
  3217. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3218. rightExpression.SetType(system.booleanType);
  3219. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3220. leftExpression.SetType(system.booleanType);
  3221. Expression(leftExpression);
  3222. ELSIF ~conditional THEN ConditionToValue(x);
  3223. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3224. CompareString(BrltL,x.right,x.left);
  3225. ELSE
  3226. Evaluate(x.left,left);
  3227. Evaluate(x.right,right);
  3228. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3229. ReleaseOperand(left); ReleaseOperand(right);
  3230. BrL(trueLabel);
  3231. END;
  3232. |Scanner.GreaterEqual:
  3233. IF ~conditional THEN ConditionToValue(x);
  3234. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3235. CompareString(BrgeL,x.left,x.right);
  3236. ELSE
  3237. Evaluate(x.left,left);
  3238. Evaluate(x.right,right);
  3239. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3240. Reuse1(temp.op,left.op);
  3241. Emit(And(position,temp.op,left.op,right.op));
  3242. ReleaseOperand(left);
  3243. BreqL(trueLabel, temp.op,right.op);
  3244. ReleaseOperand(temp); ReleaseOperand(right);
  3245. BrL(falseLabel);
  3246. ELSE
  3247. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3248. ReleaseOperand(left); ReleaseOperand(right);
  3249. BrL(trueLabel);
  3250. END;
  3251. END;
  3252. |Scanner.Unequal:
  3253. IF ~conditional THEN ConditionToValue(x);
  3254. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3255. CompareString(BrneL,x.left,x.right);
  3256. ELSE
  3257. Evaluate(x.left,left);
  3258. Evaluate(x.right,right);
  3259. IF leftType IS SyntaxTree.RangeType THEN
  3260. ASSERT(rightType IS SyntaxTree.RangeType);
  3261. BrneL(trueLabel, left.op, right.op); (* first *)
  3262. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3263. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3264. ReleaseOperand(left); ReleaseOperand(right);
  3265. BrL(falseLabel)
  3266. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3267. BrneL(trueLabel, left.op, right.op); (* first *)
  3268. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3269. ReleaseOperand(left); ReleaseOperand(right);
  3270. BrL(falseLabel)
  3271. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3272. (* TODO: review this *)
  3273. BrneL(trueLabel, left.op, right.op); (* real part *)
  3274. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3275. ReleaseOperand(left); ReleaseOperand(right);
  3276. BrL(falseLabel)
  3277. ELSE
  3278. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3279. ReleaseOperand(left); ReleaseOperand(right);
  3280. BrL(trueLabel);
  3281. END;
  3282. END;
  3283. |Scanner.In:
  3284. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3285. Evaluate(x.left,left);
  3286. Evaluate(x.right,right);
  3287. Convert(left.op,setType);
  3288. ReuseCopy(temp.op,right.op);
  3289. Emit(Shr(position,temp.op,temp.op,left.op));
  3290. ReleaseOperand(right); ReleaseOperand(left);
  3291. IntermediateCode.InitImmediate(one,setType,1);
  3292. Emit(And(position,temp.op,temp.op,one));
  3293. IF conditional THEN
  3294. IntermediateCode.InitImmediate(zero,setType,0);
  3295. BrneL(trueLabel,temp.op,zero);
  3296. ReleaseOperand(temp);
  3297. BrL(falseLabel);
  3298. ELSE
  3299. Convert(temp.op,bool);
  3300. result.mode := ModeValue;
  3301. result.op := temp.op;
  3302. result.tag := nil; (* may be left over from calls to evaluate *)
  3303. END;
  3304. ELSE
  3305. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3306. IF x.operator = Scanner.Questionmarks THEN
  3307. leftExpression := x.left;
  3308. rightExpression := x.right;
  3309. ELSE
  3310. leftExpression := x.right;
  3311. rightExpression := x.left;
  3312. END;
  3313. Evaluate(leftExpression, left);
  3314. Emit(Push(position,left.op));
  3315. ReleaseOperand(left);
  3316. Designate(rightExpression, right);
  3317. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3318. IF ~backend.cellsAreObjects THEN
  3319. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3320. END;
  3321. Emit(Push(position,right.op));
  3322. ReleaseOperand(right);
  3323. IF backend.cellsAreObjects THEN
  3324. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3325. ELSE
  3326. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3327. END;
  3328. InitOperand(result, ModeValue);
  3329. result.op := NewRegisterOperand(bool);
  3330. Emit(Result(position,result.op));
  3331. IF conditional THEN
  3332. IntermediateCode.InitImmediate(zero,setType,0);
  3333. BrneL(trueLabel,result.op,zero);
  3334. ReleaseOperand(result);
  3335. BrL(falseLabel);
  3336. END;
  3337. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3338. leftExpression := x.left;
  3339. rightExpression := x.right;
  3340. Evaluate(leftExpression, left);
  3341. Emit(Push(position,left.op));
  3342. ReleaseOperand(left);
  3343. Evaluate(rightExpression, right);
  3344. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3345. IF ~backend.cellsAreObjects THEN
  3346. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3347. END;
  3348. Emit(Push(position,right.op));
  3349. ReleaseOperand(right);
  3350. IF backend.cellsAreObjects THEN
  3351. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3352. ELSE
  3353. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3354. END;
  3355. InitOperand(result, ModeValue);
  3356. result.op := NewRegisterOperand(bool);
  3357. Emit(Result(position,result.op));
  3358. IF conditional THEN
  3359. IntermediateCode.InitImmediate(zero,setType,0);
  3360. BrneL(trueLabel,result.op,zero);
  3361. ReleaseOperand(result);
  3362. BrL(falseLabel);
  3363. END;
  3364. ELSE
  3365. HALT(100);
  3366. END;
  3367. END;
  3368. destination := dest;
  3369. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3370. END VisitBinaryExpression;
  3371. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3372. VAR localResult, operand: Operand;
  3373. BEGIN
  3374. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3375. InitOperand(localResult, ModeValue);
  3376. ASSERT(x.first # NIL);
  3377. Evaluate(x.first, operand);
  3378. localResult.op := operand.op;
  3379. ReleaseOperand(operand);
  3380. UseIntermediateOperand(localResult.op);
  3381. ASSERT(x.last # NIL);
  3382. Evaluate(x.last, operand);
  3383. localResult.tag := operand.op;
  3384. ReleaseOperand(operand);
  3385. UseIntermediateOperand(localResult.tag);
  3386. IF x.step # NIL THEN
  3387. Evaluate(x.step, operand);
  3388. localResult.extra := operand.op;
  3389. ReleaseOperand(operand);
  3390. UseIntermediateOperand(localResult.extra);
  3391. END;
  3392. result := localResult;
  3393. IF Trace THEN TraceExit("VisitRangeExpression") END
  3394. END VisitRangeExpression;
  3395. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3396. BEGIN
  3397. HALT(100); (* should never be evaluated *)
  3398. END VisitTensorRangeExpression;
  3399. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3400. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3401. BEGIN
  3402. IF Trace THEN TraceEnter("VisitConversion") END;
  3403. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3404. dest := destination; destination := emptyOperand;
  3405. Evaluate(x.expression,old);
  3406. InitOperand(result,ModeValue);
  3407. result.op := old.op;
  3408. ASSERT(result.op.mode # 0);
  3409. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3410. (* convert TO a complex number *)
  3411. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3412. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3413. ASSERT(result.op.mode # 0);
  3414. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3415. (* convert FROM a complex number TO a complex number*)
  3416. result.tag := old.tag;
  3417. ASSERT(result.tag.mode # 0);
  3418. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3419. ASSERT(result.tag.mode # 0)
  3420. ELSE
  3421. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3422. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3423. END
  3424. ELSE
  3425. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3426. ASSERT(result.op.mode # 0);
  3427. result.tag := old.tag; (*! probably never used *)
  3428. END;
  3429. destination := dest;
  3430. IF Trace THEN TraceExit("VisitConversion") END;
  3431. END VisitConversion;
  3432. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3433. BEGIN
  3434. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3435. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3436. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3437. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3438. END VisitTypeDeclaration;
  3439. (** designators (expressions) *)
  3440. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3441. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3442. BEGIN
  3443. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3444. IF x.left # NIL THEN Expression(x.left) END;
  3445. Symbol(x.symbol,result);
  3446. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3447. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3448. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3449. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3450. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3451. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3452. END;
  3453. END;
  3454. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3455. END VisitSymbolDesignator;
  3456. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3457. BEGIN
  3458. IF isUnchecked THEN RETURN END;
  3459. IF tagsAvailable THEN
  3460. TrapC(BrltL,index,length,IndexCheckTrap);
  3461. END;
  3462. END BoundCheck;
  3463. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3464. VAR d: IntermediateCode.Operand;
  3465. BEGIN
  3466. IF isUnchecked THEN RETURN END;
  3467. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3468. TrapC(op,dim,d,ArraySizeTrap);
  3469. ReleaseIntermediateOperand(d);
  3470. END DimensionCheck;
  3471. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3472. VAR
  3473. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3474. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3475. expression: SyntaxTree.Expression;
  3476. resultingType, leftType, baseType: SyntaxTree.Type;
  3477. skipLabel1: Label;
  3478. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3479. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3480. variableOp: Operand;
  3481. variable: SyntaxTree.Variable;
  3482. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3483. VAR expression: SyntaxTree.Expression;
  3484. BEGIN
  3485. (* count the number of indices, ranges and tensorRanges in the index list *)
  3486. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3487. FOR i := 0 TO parameters.Length() - 1 DO
  3488. expression := parameters.GetExpression(i);
  3489. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3490. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3491. ELSE INC(indexCount)
  3492. END
  3493. END;
  3494. END CountIndices;
  3495. BEGIN
  3496. ASSERT(tagsAvailable);
  3497. resultingType := x.type.resolved; (* resulting type *)
  3498. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3499. InitOperand(localResult, ModeReference);
  3500. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3501. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3502. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3503. (* a globally defined array destination tag is available -> use and invalidate it*)
  3504. localResult.tag := arrayDestinationTag;
  3505. IntermediateCode.InitOperand(arrayDestinationTag)
  3506. ELSE
  3507. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3508. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3509. Symbol(variable, variableOp);
  3510. ReuseCopy(localResult.tag, variableOp.op);
  3511. ReleaseOperand(variableOp);
  3512. END
  3513. END;
  3514. indexListSize := x.parameters.Length();
  3515. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3516. ASSERT(tensorRangeCount <= 1);
  3517. (* designate the array to be indexed over, perform tensor range check if known *)
  3518. Designate(x.left, array);
  3519. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3520. Dereference(array, leftType,FALSE);
  3521. IF tensorRangeCount=0 THEN
  3522. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3523. END
  3524. END;
  3525. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3526. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3527. srcDimOffset := -indexListSize;
  3528. destDimOffset := -rangeCount
  3529. ELSE
  3530. srcDimOffset := 0;
  3531. destDimOffset := 0
  3532. END;
  3533. indexDim := 0;
  3534. (* use address of source array as basis *)
  3535. (*
  3536. ReuseCopy(localResult.op, array.op);
  3537. *)
  3538. localResult.op := array.op;
  3539. UseIntermediateOperand(localResult.op);
  3540. (* go through the index list *)
  3541. FOR i := 0 TO indexListSize - 1 DO
  3542. expression := x.parameters.GetExpression(i);
  3543. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3544. (* nothing to do *)
  3545. ELSE
  3546. (* determine which dimension of source array is currently looked at *)
  3547. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3548. (* get the memory operand pointing to array descriptor dimension *)
  3549. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3550. (* make a reusable register from it *)
  3551. ReuseCopy(srcDim, tmp);
  3552. ReleaseIntermediateOperand(tmp);
  3553. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3554. ELSE
  3555. srcDim := IntermediateCode.Immediate(int32, i)
  3556. END;
  3557. (* get length and increment of source array for current dimension *)
  3558. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3559. Convert(sourceLength.op, sizeType);
  3560. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3561. Convert(sourceIncrement.op, sizeType);
  3562. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3563. ReleaseIntermediateOperand(srcDim);
  3564. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3565. (* SINGLE INDEX *)
  3566. Evaluate(expression, index);
  3567. ReleaseIntermediateOperand(index.tag);
  3568. index.tag := emptyOperand;
  3569. Convert(index.op, sizeType);
  3570. (* lower bound check *)
  3571. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3572. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3573. ELSIF isUnchecked THEN (* do nothing *)
  3574. ELSE
  3575. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3576. END;
  3577. (* upper bound check *)
  3578. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3579. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3580. ELSIF isUnchecked THEN (* do nothing *)
  3581. ELSE
  3582. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3583. END;
  3584. ReleaseOperand(sourceLength);
  3585. Convert(index.op, addressType);
  3586. summand := index.op;
  3587. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3588. (* RANGE OF INDICES *)
  3589. Evaluate(expression, range);
  3590. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3591. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3592. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3593. ReleaseOperand(range);
  3594. Convert(firstIndex, sizeType);
  3595. Convert(lastIndex, sizeType);
  3596. Convert(stepSize, sizeType);
  3597. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3598. if it is 'MAX(LONGINT)' *)
  3599. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3600. TransferToRegister(lastIndex, lastIndex);
  3601. skipLabel1 := NewLabel();
  3602. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3603. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3604. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3605. SetLabel(skipLabel1)
  3606. END;
  3607. (* check if step size is valid *)
  3608. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3609. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3610. ELSIF isUnchecked THEN (* do nothing *)
  3611. ELSE
  3612. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3613. END;
  3614. (* check lower bound check *)
  3615. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3616. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3617. ELSIF isUnchecked THEN (* do nothing *)
  3618. ELSE
  3619. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3620. END;
  3621. (* check upper bound check *)
  3622. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3623. (* statically open range: nothing to do *)
  3624. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3625. ASSERT(staticLastIndex < staticSourceLength)
  3626. ELSIF isUnchecked THEN (* do nothing *)
  3627. ELSE
  3628. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3629. END;
  3630. (* determine length of target array for current dimension *)
  3631. (* 1. incorporate last index: *)
  3632. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3633. (* last index is static *)
  3634. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3635. targetLength := sourceLength.op
  3636. ELSE
  3637. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3638. END;
  3639. UseIntermediateOperand(targetLength);
  3640. ELSE
  3641. (* targetLength := lastIndex + 1
  3642. Reuse1(targetLength, lastIndex);
  3643. *)
  3644. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3645. END;
  3646. ReleaseOperand(sourceLength);
  3647. ReleaseIntermediateOperand(lastIndex);
  3648. (* 2. incorporate first index: *)
  3649. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3650. (* first index and current target length are static *)
  3651. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3652. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3653. (* first index = 0: nothing to do *)
  3654. ELSE
  3655. (* targetLength := targetLength - firstIndex *)
  3656. TransferToRegister(targetLength, targetLength);
  3657. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3658. END;
  3659. (* clip negative lengths to 0 *)
  3660. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3661. IF staticTargetLength < 0 THEN
  3662. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3663. END
  3664. ELSE
  3665. skipLabel1 := NewLabel();
  3666. TransferToRegister(targetLength, targetLength);
  3667. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3668. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3669. SetLabel(skipLabel1)
  3670. END;
  3671. (* 3. incorporate index step size: *)
  3672. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3673. (*step size and current target length are static *)
  3674. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3675. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3676. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3677. (* step size = 1: nothing to do *)
  3678. ELSE
  3679. (* emit code for this:
  3680. targetLength := (targetLength-1) DIV stepSize +1;
  3681. *)
  3682. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3683. DivInt(targetLength, targetLength, stepSize);
  3684. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3685. END;
  3686. (* determine increment of target array for current dimension *)
  3687. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3688. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3689. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3690. (* step size = 1 *)
  3691. targetIncrement := sourceIncrement.op;
  3692. UseIntermediateOperand(targetIncrement)
  3693. ELSE
  3694. (* targetIncrement := sourceIncrement * stepSize *)
  3695. Reuse1(targetIncrement, stepSize);
  3696. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3697. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3698. END;
  3699. ReleaseIntermediateOperand(stepSize);
  3700. (* write length and increment of target array to descriptor *)
  3701. IF destDimOffset < 0 THEN
  3702. (* determine which dimension of target array is currently looked at *)
  3703. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3704. TransferToRegister(destDim, tmp);
  3705. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3706. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3707. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3708. ReleaseIntermediateOperand(destDim)
  3709. ELSE
  3710. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3711. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3712. END;
  3713. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3714. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3715. INC(indexDim);
  3716. Convert(firstIndex, addressType);
  3717. TransferToRegister(summand, firstIndex);
  3718. ELSE HALT(100);
  3719. END;
  3720. (*
  3721. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3722. *)
  3723. Convert(sourceIncrement.op, addressType);
  3724. Convert(summand, addressType);
  3725. MulInt(summand, summand, sourceIncrement.op);
  3726. ReleaseIntermediateOperand(sourceIncrement.op);
  3727. AddInt(localResult.op, localResult.op, summand);
  3728. ReleaseIntermediateOperand(summand);
  3729. END
  3730. END;
  3731. result := localResult;
  3732. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3733. ReleaseIntermediateOperand(result.tag);
  3734. result.tag := TypeDescriptorAdr(resultingType);
  3735. IF ~newObjectFile THEN
  3736. IntermediateCode.MakeMemory(result.tag,addressType);
  3737. END;
  3738. ELSIF IsDelegate(resultingType) THEN
  3739. ReleaseIntermediateOperand(result.tag);
  3740. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3741. UseIntermediateOperand(result.tag);
  3742. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3743. ReleaseIntermediateOperand(result.tag);
  3744. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3745. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3746. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3747. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3748. (* finalize target array descriptor *)
  3749. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3750. (* write lengths and increments of target array for remaining dimensions *)
  3751. i := indexListSize;
  3752. WHILE indexDim < targetArrayDimensionality DO
  3753. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3754. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3755. ReleaseOperand(sourceLength);
  3756. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3757. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3758. ReleaseOperand(sourceIncrement);
  3759. INC(i); INC(indexDim);
  3760. END;
  3761. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3762. tmp := nil;
  3763. ELSE
  3764. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3765. END;
  3766. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3767. ReleaseIntermediateOperand(tmp);
  3768. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3769. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3770. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3771. ELSE
  3772. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3773. END;
  3774. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3775. ReleaseIntermediateOperand(tmp);
  3776. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3777. (* write dimensionality *)
  3778. IF targetArrayDimensionality # 0 THEN
  3779. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3780. END;
  3781. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3782. END;
  3783. ReleaseOperand(array);
  3784. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3785. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3786. END;
  3787. END MathIndexDesignator;
  3788. (* get the length of an array , trying to make use of static information *)
  3789. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3790. VAR res: IntermediateCode.Operand; size: LONGINT;
  3791. BEGIN
  3792. type := type.resolved;
  3793. IF type IS SyntaxTree.ArrayType THEN
  3794. WITH type: SyntaxTree.ArrayType DO
  3795. IF type.form = SyntaxTree.Static THEN
  3796. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3797. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3798. Evaluate(type.length, op);
  3799. ReleaseIntermediateOperand(op.tag);
  3800. RETURN op.op;*)
  3801. ELSE
  3802. res := tag;
  3803. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3804. IntermediateCode.MakeMemory(res,addressType);
  3805. UseIntermediateOperand(res);
  3806. RETURN res
  3807. END
  3808. END;
  3809. ELSE
  3810. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3811. RETURN IntermediateCode.Immediate(addressType,size);
  3812. END;
  3813. END ArrayLength;
  3814. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3815. BEGIN
  3816. IF IsImmediate(x) THEN
  3817. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3818. ELSE
  3819. IF ~ReusableRegister(res) THEN
  3820. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3821. ELSE
  3822. UseIntermediateOperand(res);
  3823. END;
  3824. Emit(Mov(position,res,x))
  3825. END;
  3826. END CopyInt;
  3827. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3828. BEGIN
  3829. ReleaseIntermediateOperand(res);
  3830. IF IsImmediate(x) & IsImmediate(y) THEN
  3831. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3832. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3833. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3834. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3835. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3836. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3837. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3838. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3839. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3840. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3841. UseIntermediateOperand(res);
  3842. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3843. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3844. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3845. UseIntermediateOperand(res);
  3846. ELSE
  3847. IF ~ReusableRegister(res) THEN
  3848. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3849. ELSE
  3850. UseIntermediateOperand(res);
  3851. END;
  3852. IF IsImmediate(x) & (x.intValue = 0) THEN
  3853. Emit(Mov(position,res,y))
  3854. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3855. Emit(Mov(position,res,x))
  3856. ELSE
  3857. Emit(Add(position,res, x, y));
  3858. END;
  3859. END;
  3860. END AddInt;
  3861. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3862. BEGIN
  3863. ReleaseIntermediateOperand(res);
  3864. IF IsImmediate(x) & IsImmediate(y) THEN
  3865. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3866. ELSE
  3867. IF ~ReusableRegister(res) THEN
  3868. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3869. ELSE UseIntermediateOperand(res);
  3870. END;
  3871. IF IsImmediate(x) & (x.intValue = 1) THEN
  3872. Emit(Mov(position,res,y))
  3873. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3874. Emit(Mov(position,res,x))
  3875. ELSE
  3876. Emit(Mul(position,res, x, y));
  3877. END;
  3878. END;
  3879. END MulInt;
  3880. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3881. BEGIN
  3882. ReleaseIntermediateOperand(res);
  3883. IF IsImmediate(x) & IsImmediate(y) THEN
  3884. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3885. ELSE
  3886. IF ~ReusableRegister(res) THEN
  3887. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3888. ELSE UseIntermediateOperand(res);
  3889. END;
  3890. IF IsImmediate(x) & (x.intValue = 1) THEN
  3891. Emit(Mov(position,res,y))
  3892. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3893. Emit(Mov(position,res,x))
  3894. ELSE
  3895. Emit(Div(position,res, x, y));
  3896. END;
  3897. END;
  3898. END DivInt;
  3899. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3900. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3901. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3902. BEGIN
  3903. type := x.left.type.resolved;
  3904. IF type IS SyntaxTree.StringType THEN
  3905. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3906. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3907. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3908. type := atype;
  3909. x.left.SetType(type);
  3910. END;
  3911. IntermediateCode.InitImmediate(res,addressType,0);
  3912. maxDim := x.parameters.Length()-1;
  3913. (*
  3914. computation rule:
  3915. a: ARRAY X,Y,Z OF Element with size S
  3916. a[i,j,k] -->
  3917. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3918. *)
  3919. FOR i := 0 TO maxDim DO
  3920. e := x.parameters.GetExpression(i);
  3921. Evaluate(e,index);
  3922. Convert(index.op,addressType);
  3923. AddInt(res, res, index.op);
  3924. IF i = 0 THEN
  3925. (*
  3926. ReuseCopy(res, index.op);
  3927. *)
  3928. Designate(x.left,array);
  3929. type := x.left.type.resolved;
  3930. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3931. Dereference(array, type, FALSE);
  3932. END;
  3933. (*
  3934. ELSE AddInt(res, res, index.op);
  3935. *)
  3936. END;
  3937. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3938. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3939. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3940. BoundCheck(index.op, length);
  3941. END;
  3942. ReleaseIntermediateOperand(length);
  3943. END;
  3944. ReleaseOperand(index);
  3945. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3946. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3947. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3948. MulInt(res,res,length);
  3949. END;
  3950. ReleaseIntermediateOperand(length);
  3951. END;
  3952. (* remaining open dimensions -- compute address *)
  3953. i := maxDim+1;
  3954. IF type IS SyntaxTree.ArrayType THEN
  3955. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3956. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3957. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3958. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3959. MulInt(res,res,length);
  3960. END;
  3961. ReleaseIntermediateOperand(length);
  3962. INC(i);
  3963. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3964. END;
  3965. END;
  3966. IF (type IS SyntaxTree.ArrayType) THEN
  3967. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3968. size := StaticSize(system, type);
  3969. IF size # 1 THEN
  3970. length := IntermediateCode.Immediate(addressType,size);
  3971. MulInt(res,res,length);
  3972. END;
  3973. ELSE
  3974. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3975. IF size # 1 THEN
  3976. length := IntermediateCode.Immediate(addressType,size);
  3977. MulInt(res,res,length);
  3978. END;
  3979. END;
  3980. END;
  3981. AddInt(res,res,array.op);
  3982. InitOperand(result,ModeReference);
  3983. result.op := res;
  3984. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3985. ReleaseIntermediateOperand(result.tag);
  3986. result.tag := TypeDescriptorAdr(type);
  3987. IF ~newObjectFile THEN
  3988. IntermediateCode.MakeMemory(result.tag,addressType);
  3989. END
  3990. ELSIF IsDelegate(type) THEN
  3991. ReleaseIntermediateOperand(result.tag);
  3992. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3993. UseIntermediateOperand(result.tag);
  3994. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3995. ReleaseIntermediateOperand(result.tag);
  3996. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3997. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3998. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3999. END;
  4000. ReleaseOperand(array);
  4001. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  4002. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  4003. END;
  4004. END IndexDesignator;
  4005. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  4006. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  4007. BEGIN
  4008. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  4009. dest := destination; destination := emptyOperand;
  4010. type := x.left.type.resolved;
  4011. IF type IS SyntaxTree.MathArrayType THEN
  4012. MathIndexDesignator(x);
  4013. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4014. IndexDesignator(x);
  4015. END;
  4016. destination := dest;
  4017. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4018. END VisitIndexDesignator;
  4019. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4020. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4021. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4022. procedure: SyntaxTree.Procedure;
  4023. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4024. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4025. BEGIN
  4026. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4027. parameters := expression.parameters;
  4028. moduleName := "FoxArrayBase";
  4029. procedureName := "CopyDescriptor";
  4030. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4031. SaveRegisters();ReleaseUsedRegisters(saved);
  4032. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4033. IF procedure = NIL THEN
  4034. s := "procedure ";
  4035. Strings.Append(s,moduleName);
  4036. Strings.Append(s,".");
  4037. Strings.Append(s,procedureName);
  4038. Strings.Append(s," not present");
  4039. Error(position,s);
  4040. ELSE
  4041. (* push address of temporary variable *)
  4042. Symbol(variable,destOperand);
  4043. Emit(Push(position,destOperand.op));
  4044. ReleaseOperand(destOperand);
  4045. (* push src *)
  4046. Evaluate(expression.left,srcOperand);
  4047. (*
  4048. Dereference(srcOperand,expression.type.resolved);
  4049. Emit(Push(position,srcOperand.tag));
  4050. *)
  4051. Emit(Push(position,srcOperand.op));
  4052. ReleaseOperand(srcOperand);
  4053. tensorFound := FALSE;
  4054. FOR i := 0 TO parameters.Length()-1 DO
  4055. e := parameters.GetExpression(i);
  4056. IF e IS SyntaxTree.TensorRangeExpression THEN
  4057. tensorFound := TRUE;
  4058. ELSIF e IS SyntaxTree.RangeExpression THEN
  4059. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4060. ELSE
  4061. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4062. END;
  4063. END;
  4064. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4065. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4066. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4067. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4068. StaticCallOperand(procOp,procedure);
  4069. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4070. ReleaseOperand(procOp);
  4071. END;
  4072. RestoreRegisters(saved);
  4073. END;
  4074. RETURN variable
  4075. END PrepareTensorDescriptor;
  4076. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4077. VAR
  4078. type, descriptorType, baseType: SyntaxTree.Type;
  4079. operand, tmpOperand, variableOp, variable2Op: Operand;
  4080. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4081. variable, variable2: SyntaxTree.Variable;
  4082. dim, i, size: LONGINT;
  4083. (* TODO: needed? *)
  4084. oldArrayDestinationTag: IntermediateCode.Operand;
  4085. oldArrayDestinationDimension: LONGINT;
  4086. position: Position;
  4087. saved: RegisterEntry;
  4088. arrayFlags: SET;
  4089. m, n: LONGINT;
  4090. PROCEDURE Pass(op: IntermediateCode.Operand);
  4091. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4092. BEGIN
  4093. IF numberRegister >= 0 THEN
  4094. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4095. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4096. Emit(Mov(position,parameterRegister, op));
  4097. ELSE
  4098. Emit(Push(position,op))
  4099. END
  4100. END Pass;
  4101. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4102. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4103. BEGIN
  4104. formalType := formalType.resolved; actualType := actualType.resolved;
  4105. IF IsOpenArray(formalType)THEN
  4106. IF actualType IS SyntaxTree.StringType THEN
  4107. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4108. RETURN;
  4109. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4110. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4111. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4112. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4113. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4114. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4115. ELSE
  4116. tmp := baseReg;
  4117. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4118. IntermediateCode.MakeMemory(tmp,addressType);
  4119. Pass((tmp));
  4120. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4121. END;
  4122. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4123. END;
  4124. END PushArrayLens;
  4125. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4126. BEGIN
  4127. CASE size OF
  4128. |2: INCL(flags,Size2Flag);
  4129. |3: INCL(flags,Size3Flag);
  4130. |4: INCL(flags,Size4Flag);
  4131. |5: INCL(flags,Size5Flag);
  4132. |6: INCL(flags,Size6Flag);
  4133. |7: INCL(flags,Size7Flag);
  4134. |8: INCL(flags,Size8Flag);
  4135. END;
  4136. END SetSmallArraySizeFlag;
  4137. BEGIN
  4138. IF Trace THEN TraceEnter("PushParameter") END;
  4139. position := expression.position;
  4140. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4141. type := expression.type.resolved;
  4142. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4143. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4144. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4145. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4146. );
  4147. (* TODO: needed? *)
  4148. oldArrayDestinationTag := arrayDestinationTag;
  4149. oldArrayDestinationDimension := arrayDestinationDimension;
  4150. IF IsArrayOfSystemByte(parameter.type) THEN
  4151. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4152. Designate(expression,operand);
  4153. ELSE
  4154. Evaluate(expression, tmpOperand);
  4155. variable := GetTemporaryVariable(expression.type, FALSE);
  4156. Symbol(variable, operand);
  4157. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4158. Emit(Mov(position,tmp, tmpOperand.op));
  4159. ReleaseOperand(tmpOperand);
  4160. END;
  4161. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4162. ReleaseIntermediateOperand(operand.tag);
  4163. operand.tag := tmp;
  4164. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4165. Pass((operand.tag));
  4166. END;
  4167. Pass((operand.op));
  4168. ELSIF IsOpenArray(parameter.type) THEN
  4169. Designate(expression,operand);
  4170. baseReg := operand.tag;
  4171. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4172. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4173. END;
  4174. Pass((operand.op)); (* address of the array *)
  4175. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4176. (* case 1 *)
  4177. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4178. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4179. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4180. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4181. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4182. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4183. arrayDestinationTag := sp;
  4184. (* case 1b *)
  4185. IF expression IS SyntaxTree.IndexDesignator THEN
  4186. (*
  4187. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4188. descriptorType := GetMathArrayDescriptorType(dim);
  4189. variable := GetTemporaryVariable(descriptorType,expression.position);
  4190. Symbol(variable,variableOp);
  4191. arrayDestinationTag := variableOp.op;
  4192. *)
  4193. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4194. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4195. arrayDestinationDimension := dim;
  4196. Designate(expression,operand);
  4197. (* case 1a *)
  4198. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4199. Designate(expression,operand);
  4200. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4201. i := 0;
  4202. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4203. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4204. INC(i);
  4205. END;
  4206. type := expression.type.resolved;
  4207. WHILE (i<dim) DO (* remaining static dimensions *)
  4208. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4209. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4210. ReleaseOperand(tmpOperand);
  4211. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4212. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4213. ReleaseOperand(tmpOperand);
  4214. INC(i);
  4215. END;
  4216. dimOp := IntermediateCode.Immediate(addressType,dim);
  4217. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4218. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4219. (* case 1d *)
  4220. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4221. Designate(expression,operand);
  4222. Dereference(operand,type.resolved,FALSE);
  4223. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4224. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4225. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4226. (* case 1f *)
  4227. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4228. Designate(expression,operand);
  4229. FOR i := 0 TO dim-1 DO
  4230. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4231. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4232. ReleaseOperand(tmpOperand);
  4233. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4234. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4235. ReleaseOperand(tmpOperand);
  4236. END;
  4237. (*******
  4238. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4239. *)
  4240. arrayFlags := {StaticFlag};
  4241. IF dim = 1 THEN
  4242. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4243. ReleaseOperand(tmpOperand);
  4244. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4245. m := LONGINT(tmpOperand.op.intValue);
  4246. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4247. ELSIF dim = 2 THEN
  4248. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4249. ReleaseOperand(tmpOperand);
  4250. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4251. m := LONGINT(tmpOperand.op.intValue);
  4252. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4253. ReleaseOperand(tmpOperand);
  4254. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4255. n := LONGINT(tmpOperand.op.intValue);
  4256. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4257. INCL(arrayFlags,SmallMatrixFlag);
  4258. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4259. END;
  4260. END;
  4261. (*******)
  4262. dimOp := IntermediateCode.Immediate(addressType,dim);
  4263. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4264. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4265. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4266. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4267. baseType := SemanticChecker.ArrayBase(type,dim);
  4268. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4269. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4270. ELSE HALT(100);
  4271. END;
  4272. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4273. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4274. (* case 2b *)
  4275. IF expression IS SyntaxTree.IndexDesignator THEN
  4276. descriptorType := GetMathArrayDescriptorType(dim);
  4277. variable := GetTemporaryVariable(descriptorType, FALSE);
  4278. Symbol(variable,variableOp);
  4279. arrayDestinationTag := variableOp.op;
  4280. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4281. arrayDestinationDimension := dim;
  4282. NeedDescriptor := TRUE;
  4283. Designate(expression,operand);
  4284. Pass((operand.tag));
  4285. NeedDescriptor := FALSE;
  4286. (* case 2a *)
  4287. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4288. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4289. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4290. INC(i);
  4291. END;
  4292. IF i = dim THEN
  4293. Designate(expression,operand);
  4294. Pass((operand.tag));
  4295. ELSE (* open-static *)
  4296. type := expression.type.resolved;
  4297. descriptorType := GetMathArrayDescriptorType(dim);
  4298. variable := GetTemporaryVariable(descriptorType, FALSE);
  4299. Symbol(variable,variableOp);
  4300. arrayDestinationTag := variableOp.op;
  4301. Designate(expression,operand);
  4302. FOR i := 0 TO dim-1 DO
  4303. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4304. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4305. ReleaseOperand(tmpOperand);
  4306. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4307. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4308. ReleaseOperand(tmpOperand);
  4309. END;
  4310. dimOp := IntermediateCode.Immediate(addressType,dim);
  4311. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4312. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4313. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4314. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4315. baseType := SemanticChecker.ArrayBase(type,dim);
  4316. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4317. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4318. Pass((arrayDestinationTag));
  4319. END;
  4320. (* case 2d *)
  4321. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4322. Designate(expression,operand);
  4323. Dereference(operand,type.resolved,FALSE);
  4324. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4325. Pass((operand.tag));
  4326. (* case 2f *)
  4327. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4328. descriptorType := GetMathArrayDescriptorType(dim);
  4329. variable := GetTemporaryVariable(descriptorType, FALSE);
  4330. Symbol(variable,variableOp);
  4331. arrayDestinationTag := variableOp.op;
  4332. Designate(expression,operand);
  4333. FOR i := 0 TO dim-1 DO
  4334. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4335. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4336. ReleaseOperand(tmpOperand);
  4337. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4338. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4339. ReleaseOperand(tmpOperand);
  4340. END;
  4341. (*
  4342. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4343. *)
  4344. arrayFlags := {StaticFlag};
  4345. IF dim = 1 THEN
  4346. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4347. ReleaseOperand(tmpOperand);
  4348. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4349. m := LONGINT(tmpOperand.op.intValue);
  4350. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4351. ELSIF dim = 2 THEN
  4352. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4353. ReleaseOperand(tmpOperand);
  4354. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4355. m := LONGINT(tmpOperand.op.intValue);
  4356. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4357. ReleaseOperand(tmpOperand);
  4358. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4359. n := LONGINT(tmpOperand.op.intValue);
  4360. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4361. INCL(arrayFlags,SmallMatrixFlag);
  4362. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4363. END;
  4364. END;
  4365. (*******)
  4366. dimOp := IntermediateCode.Immediate(addressType,dim);
  4367. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4368. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4369. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4370. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4371. baseType := SemanticChecker.ArrayBase(type,dim);
  4372. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4373. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4374. Pass((arrayDestinationTag));
  4375. ELSE HALT(100);
  4376. END;
  4377. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4378. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4379. (* case 3b *)
  4380. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4381. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4382. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4383. Symbol(variable,variableOp);
  4384. LoadValue(variableOp,system.addressType);
  4385. ELSE
  4386. descriptorType := GetMathArrayDescriptorType(dim);
  4387. variable := GetTemporaryVariable(descriptorType, FALSE);
  4388. Symbol(variable,variableOp);
  4389. END;
  4390. arrayDestinationTag := variableOp.op;
  4391. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4392. arrayDestinationDimension := 0;
  4393. Designate(expression,operand);
  4394. Pass((operand.tag));
  4395. (* case 3a *)
  4396. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4397. i := 0;
  4398. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4399. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4400. INC(i);
  4401. END;
  4402. IF i = dim THEN
  4403. Designate(expression,operand);
  4404. Pass((operand.tag));
  4405. ELSE (* open-static *)
  4406. type := expression.type.resolved;
  4407. descriptorType := GetMathArrayDescriptorType(dim);
  4408. variable := GetTemporaryVariable(descriptorType, FALSE);
  4409. Symbol(variable,variableOp);
  4410. arrayDestinationTag := variableOp.op;
  4411. Designate(expression,operand);
  4412. FOR i := 0 TO dim-1 DO
  4413. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4414. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4415. ReleaseOperand(tmpOperand);
  4416. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4417. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4418. ReleaseOperand(tmpOperand);
  4419. END;
  4420. dimOp := IntermediateCode.Immediate(addressType,dim);
  4421. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4422. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4423. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4424. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4425. baseType := SemanticChecker.ArrayBase(type,dim);
  4426. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4427. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4428. Pass((arrayDestinationTag));
  4429. END;
  4430. (* case 3d *)
  4431. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4432. Designate(expression,operand);
  4433. Dereference(operand,type.resolved,FALSE);
  4434. (*
  4435. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4436. *)
  4437. Pass((operand.tag));
  4438. (* case 3f *)
  4439. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4440. descriptorType := GetMathArrayDescriptorType(dim);
  4441. variable := GetTemporaryVariable(descriptorType, FALSE);
  4442. Symbol(variable,variableOp);
  4443. arrayDestinationTag := variableOp.op;
  4444. Designate(expression,operand);
  4445. IF operand.op.type.length >1 THEN (* vector register *)
  4446. variable2 := GetTemporaryVariable(type, FALSE);
  4447. Symbol(variable2, variable2Op);
  4448. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4449. Emit(Mov(position,tmp, operand.op));
  4450. ReleaseOperand(operand);
  4451. Symbol(variable2, operand);
  4452. END;
  4453. FOR i := 0 TO dim-1 DO
  4454. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4455. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4456. ReleaseOperand(tmpOperand);
  4457. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4458. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4459. ReleaseOperand(tmpOperand);
  4460. END;
  4461. dimOp := IntermediateCode.Immediate(addressType,dim);
  4462. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4463. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4464. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4465. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4466. baseType := SemanticChecker.ArrayBase(type,dim);
  4467. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4468. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4469. Pass((arrayDestinationTag));
  4470. ELSE HALT(100);
  4471. END;
  4472. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4473. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4474. (* case 4b *)
  4475. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4476. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4477. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4478. Symbol(variable,variableOp);
  4479. LoadValue(variableOp,system.addressType);
  4480. ELSE
  4481. descriptorType := GetMathArrayDescriptorType(dim);
  4482. variable := GetTemporaryVariable(descriptorType, FALSE);
  4483. Symbol(variable,variableOp);
  4484. END;
  4485. arrayDestinationTag := variableOp.op;
  4486. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4487. arrayDestinationDimension := 0;
  4488. Designate(expression,operand);
  4489. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4490. Symbol(variable,variableOp);
  4491. ELSE
  4492. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4493. Symbol(variable,variableOp);
  4494. MakeMemory(tmp,variableOp.op,addressType,0);
  4495. Emit(Mov(position,tmp,operand.tag));
  4496. ReleaseIntermediateOperand(tmp);
  4497. END;
  4498. Pass((variableOp.op));
  4499. ReleaseOperand(variableOp);
  4500. (* case 4a *)
  4501. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4502. i := 0;
  4503. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4504. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4505. INC(i);
  4506. END;
  4507. IF i = dim THEN
  4508. Designate(expression,operand);
  4509. arrayDestinationTag := operand.tag;
  4510. ELSE (* open-static *)
  4511. type := expression.type.resolved;
  4512. descriptorType := GetMathArrayDescriptorType(dim);
  4513. variable := GetTemporaryVariable(descriptorType, FALSE);
  4514. Symbol(variable,variableOp);
  4515. arrayDestinationTag := variableOp.op;
  4516. Designate(expression,operand);
  4517. FOR i := 0 TO dim-1 DO
  4518. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4519. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4520. ReleaseOperand(tmpOperand);
  4521. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4522. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4523. ReleaseOperand(tmpOperand);
  4524. END;
  4525. dimOp := IntermediateCode.Immediate(addressType,dim);
  4526. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4527. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4528. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4529. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4530. baseType := SemanticChecker.ArrayBase(type,dim);
  4531. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4532. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4533. END;
  4534. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4535. Symbol(variable,variableOp);
  4536. MakeMemory(tmp,variableOp.op,addressType,0);
  4537. Emit(Mov(position,tmp,arrayDestinationTag));
  4538. ReleaseIntermediateOperand(tmp);
  4539. Pass((variableOp.op));
  4540. ReleaseOperand(variableOp);
  4541. (* case 4d *)
  4542. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4543. Designate(expression,operand);
  4544. (*
  4545. Dereference(operand,type.resolved,FALSE);
  4546. *)
  4547. (*
  4548. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4549. *)
  4550. Pass((operand.op));
  4551. (* case 4f *)
  4552. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4553. descriptorType := GetMathArrayDescriptorType(dim);
  4554. variable := GetTemporaryVariable(descriptorType, FALSE);
  4555. Symbol(variable,variableOp);
  4556. arrayDestinationTag := variableOp.op;
  4557. Designate(expression,operand);
  4558. FOR i := 0 TO dim-1 DO
  4559. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4560. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4561. ReleaseOperand(tmpOperand);
  4562. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4563. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4564. ReleaseOperand(tmpOperand);
  4565. END;
  4566. dimOp := IntermediateCode.Immediate(addressType,dim);
  4567. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4568. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4569. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4570. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4571. baseType := SemanticChecker.ArrayBase(type,dim);
  4572. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4573. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4574. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4575. Symbol(variable,variableOp);
  4576. MakeMemory(tmp,variableOp.op,addressType,0);
  4577. Emit(Mov(position,tmp,arrayDestinationTag));
  4578. ReleaseIntermediateOperand(tmp);
  4579. Pass((variableOp.op));
  4580. ReleaseOperand(variableOp);
  4581. ELSE HALT(100);
  4582. END;
  4583. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4584. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4585. Designate(expression,operand);
  4586. IF operand.op.type.length > 1 THEN
  4587. Emit(Push(position, operand.op));
  4588. ELSE
  4589. size := system.SizeOf(type);
  4590. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4591. size := ToMemoryUnits(system,size);
  4592. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4593. arrayDestinationTag := sp;
  4594. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4595. END;
  4596. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4597. Designate(expression,operand);
  4598. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4599. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4600. Symbol(variable,variableOp);
  4601. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4602. Emit(Mov(position,tmp,operand.op));
  4603. Emit(Push(position,variableOp.op));
  4604. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4605. Pass((operand.op));
  4606. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4607. END;
  4608. ELSE HALT(200)
  4609. END;
  4610. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4611. IF parameter.kind = SyntaxTree.VarParameter THEN
  4612. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  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.extra)); (* step *)
  4619. Pass((operand.tag)); (* last *)
  4620. Pass((operand.op)) (* first *)
  4621. END
  4622. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4623. IF parameter.kind = SyntaxTree.VarParameter THEN
  4624. Designate(expression, operand);
  4625. Pass((operand.op));
  4626. ELSE
  4627. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4628. Evaluate(expression, operand);
  4629. Pass((operand.tag)); (* real part *)
  4630. Pass((operand.op)) (* imaginary part *)
  4631. END
  4632. ELSE
  4633. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4634. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4635. Designate(expression,operand);
  4636. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4637. (* stack allocation *)
  4638. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4639. (*! parameter alignment to be discussed ... *)
  4640. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4641. size := type(SyntaxTree.StringType).length;
  4642. END;
  4643. IF backend.cooperative & parameter.NeedsTrace() THEN
  4644. dst := NewRegisterOperand (addressType);
  4645. Emit(Mov(position,dst, sp));
  4646. IntermediateCode.InitImmediate(null, byteType, 0);
  4647. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4648. ReleaseIntermediateOperand(dst);
  4649. SaveRegisters();ReleaseUsedRegisters(saved);
  4650. (* register dst has been freed before SaveRegisters already *)
  4651. CallAssignMethod(dst, operand.op, parameter.type);
  4652. RestoreRegisters(saved);
  4653. END;
  4654. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4655. ELSIF IsOpenArray(parameter.type) THEN
  4656. Designate(expression,operand);
  4657. baseReg := operand.tag;
  4658. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4659. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4660. END;
  4661. Pass((operand.op)); (* address of the array *)
  4662. ELSIF IsDelegate(parameter.type) THEN
  4663. Evaluate(expression,operand);
  4664. IF backend.cooperative & parameter.NeedsTrace() THEN
  4665. Emit(Push(position, nil));
  4666. dst := NewRegisterOperand (addressType);
  4667. Emit(Mov(position,dst, sp));
  4668. ReleaseIntermediateOperand(dst);
  4669. SaveRegisters();ReleaseUsedRegisters(saved);
  4670. Emit(Push(position, dst));
  4671. (* register dst has been freed before SaveRegisters already *)
  4672. Emit(Push(position, operand.tag));
  4673. CallThis(position,"GarbageCollector","Assign",2);
  4674. RestoreRegisters(saved);
  4675. ELSE
  4676. Pass((operand.tag));
  4677. END;
  4678. Pass((operand.op));
  4679. ELSE
  4680. Evaluate(expression,operand);
  4681. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4682. Emit(Push(position, nil));
  4683. dst := NewRegisterOperand (addressType);
  4684. Emit(Mov(position,dst, sp));
  4685. ReleaseIntermediateOperand(dst);
  4686. SaveRegisters();ReleaseUsedRegisters(saved);
  4687. Emit(Push(position, dst));
  4688. (* register dst has been freed before SaveRegisters already *)
  4689. Emit(Push(position, operand.op));
  4690. CallThis(position,"GarbageCollector","Assign",2);
  4691. RestoreRegisters(saved);
  4692. ELSE
  4693. Pass((operand.op));
  4694. END;
  4695. END;
  4696. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4697. Evaluate(expression,operand);
  4698. Pass((operand.op));
  4699. ELSE (* var parameter *)
  4700. Designate(expression,operand);
  4701. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4702. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4703. Pass((operand.tag));
  4704. END;
  4705. END;
  4706. Pass((operand.op));
  4707. END;
  4708. END;
  4709. (* TODO: needed? *)
  4710. arrayDestinationTag := oldArrayDestinationTag;
  4711. arrayDestinationDimension := oldArrayDestinationDimension;
  4712. IF needsParameterBackup THEN
  4713. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4714. ReuseCopy(parameterBackup, operand.op)
  4715. END;
  4716. ReleaseOperand(operand);
  4717. IF Trace THEN TraceExit("PushParameter") END;
  4718. END PushParameter;
  4719. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4720. VAR prevConditional: BOOLEAN;
  4721. BEGIN
  4722. prevConditional := conditional;
  4723. conditional := FALSE;
  4724. 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
  4725. Expression(x.result); (* use register *)
  4726. END;
  4727. Statement(x.statement);
  4728. conditional := prevConditional;
  4729. IF x.result # NIL THEN Expression(x.result) END;
  4730. 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
  4731. ReleaseIntermediateOperand(result.op);
  4732. END;
  4733. END VisitStatementDesignator;
  4734. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4735. VAR
  4736. procedure: SyntaxTree.Procedure;
  4737. procedureType: SyntaxTree.ProcedureType;
  4738. wasInline: BOOLEAN;
  4739. actualParameters: SyntaxTree.ExpressionList;
  4740. formalParameter: SyntaxTree.Parameter;
  4741. actualParameter: SyntaxTree.Expression;
  4742. i: LONGINT;
  4743. localVariable: SyntaxTree.Variable;
  4744. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4745. src, dest: Operand;
  4746. prevInlineExit : Label;
  4747. prevMapper: SymbolMapper;
  4748. tooComplex: BOOLEAN;
  4749. resultDesignator: SyntaxTree.Expression;
  4750. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4751. BEGIN
  4752. IF e = NIL THEN RETURN TRUE
  4753. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4754. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4755. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4756. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4757. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4758. ELSE RETURN FALSE
  4759. END;
  4760. END SimpleExpression;
  4761. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4762. BEGIN
  4763. RETURN checker.CanPassInRegister(type)
  4764. END FitsInRegister;
  4765. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4766. VAR
  4767. variable: SyntaxTree.Variable;
  4768. variableDesignator: SyntaxTree.Designator;
  4769. BEGIN
  4770. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4771. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4772. variableDesignator.SetType(type);
  4773. RETURN variableDesignator
  4774. END GetTemp;
  4775. BEGIN
  4776. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4777. wasInline := currentIsInline;
  4778. prevInlineExit := currentInlineExit;
  4779. prevMapper := currentMapper;
  4780. currentInlineExit := NewLabel();
  4781. tooComplex := FALSE;
  4782. NEW(currentMapper);
  4783. currentIsInline := TRUE;
  4784. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4785. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4786. formalParameter := procedureType.firstParameter;
  4787. actualParameters := x.parameters;
  4788. i := 0;
  4789. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4790. actualParameter := actualParameters.GetExpression(i);
  4791. IF actualParameter.resolved # NIL THEN
  4792. actualParameter := actualParameter.resolved
  4793. END;
  4794. (*
  4795. if expression is simple and can be passed immediately
  4796. or if type fits in register then we can proceed
  4797. otherwise we escape to ordinary procedure call.
  4798. *)
  4799. (* cases where the expression can be mapped identically *)
  4800. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4801. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4802. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4803. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4804. (*
  4805. Assign(variableDesignator, actualParameter);
  4806. *)
  4807. Evaluate(actualParameter, src);
  4808. Designate(variableDesignator, dest);
  4809. Emit(Mov(x.position, dest.op, src.op));
  4810. ReleaseOperand(dest);
  4811. ReleaseOperand(src);
  4812. (* the src operand should now have been completely released ! *)
  4813. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4814. ELSE tooComplex := TRUE
  4815. END;
  4816. (*
  4817. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4818. currentMapper.Add(formalParameter, actualParameter, NIL);
  4819. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4820. variableDesignator := GetTemp(system.addressType, FALSE);
  4821. Designate(actualParameter, src);
  4822. Designate(variableDesignator, dest);
  4823. IntermediateCode.MakeMemory(dest.op,addressType);
  4824. Emit(Mov(x.position, dest.op, src.op));
  4825. ReleaseOperand(dest);
  4826. IF src.tag.mode # IntermediateCode.Undefined THEN
  4827. tagDesignator := GetTemp(system.addressType, FALSE);
  4828. Designate(tagDesignator, dest);
  4829. IntermediateCode.MakeMemory(dest.op,addressType);
  4830. Emit(Mov(x.position, dest.op, src.op));
  4831. END;
  4832. ReleaseOperand(dest); ReleaseOperand(src);
  4833. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4834. END;
  4835. *)
  4836. formalParameter := formalParameter.nextParameter;
  4837. INC(i);
  4838. END;
  4839. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4840. IF resultDesignator # NIL THEN
  4841. returnDesignator := resultDesignator
  4842. ELSE
  4843. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4844. END;
  4845. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4846. END;
  4847. localVariable := procedure.procedureScope.firstVariable;
  4848. WHILE ~tooComplex & (localVariable # NIL) DO
  4849. variableDesignator := GetTemp(localVariable.type, FALSE);
  4850. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4851. localVariable := localVariable.nextVariable;
  4852. END;
  4853. IF ~tooComplex THEN
  4854. VisitStatementBlock(procedure.procedureScope.body);
  4855. SetLabel(currentInlineExit);
  4856. IF procedureType.returnType # NIL THEN
  4857. Designate(returnDesignator, result);
  4858. IF conditional THEN
  4859. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4860. ValueToCondition(result)
  4861. END;
  4862. END;
  4863. END;
  4864. currentMapper := prevMapper;
  4865. currentInlineExit := prevInlineExit;
  4866. currentIsInline := wasInline;
  4867. RETURN ~tooComplex
  4868. END InlineProcedureCall;
  4869. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4870. VAR
  4871. parameters: SyntaxTree.ExpressionList;
  4872. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4873. designator: SyntaxTree.Designator;
  4874. procedureType: SyntaxTree.ProcedureType;
  4875. formalParameter: SyntaxTree.Parameter;
  4876. operand, returnValue: Operand;
  4877. reg, size, mask, dest: IntermediateCode.Operand;
  4878. saved: RegisterEntry;
  4879. symbol: SyntaxTree.Symbol;
  4880. variable: SyntaxTree.Variable;
  4881. i, parametersSize, returnTypeSize : LONGINT;
  4882. structuredReturnType: BOOLEAN;
  4883. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4884. tempVariableDesignator: SyntaxTree.Designator;
  4885. gap, alignment: LONGINT; (*fld*)
  4886. (* TODO: remove unnecessary backup variables *)
  4887. oldResult: Operand;
  4888. oldCurrentScope: SyntaxTree.Scope;
  4889. oldArrayDestinationTag: IntermediateCode.Operand;
  4890. oldArrayDestinationDimension: LONGINT;
  4891. oldConstantDeclaration: SyntaxTree.Symbol;
  4892. oldDestination: IntermediateCode.Operand;
  4893. oldCurrentLoop: Label;
  4894. oldConditional: BOOLEAN;
  4895. oldTrueLabel, oldFalseLabel: Label;
  4896. oldLocked: BOOLEAN;
  4897. usedRegisters,oldUsedRegisters: RegisterEntry;
  4898. return: IntermediateCode.Operand;
  4899. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4900. arg: IntermediateCode.Operand;
  4901. dummy: IntermediateCode.Operand;
  4902. recordType: SyntaxTree.RecordType;
  4903. operatorSelectionProcedureOperand: Operand;
  4904. operatorSelectionProcedure: SyntaxTree.Procedure;
  4905. fingerPrint: SyntaxTree.FingerPrint;
  4906. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4907. identifierNumber: LONGINT;
  4908. parameterRegister: Backend.Registers;
  4909. parameterRegisters: LONGINT;
  4910. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4911. procedure: SyntaxTree.Procedure;
  4912. PROCEDURE BackupGlobalState;
  4913. BEGIN
  4914. oldResult := result;
  4915. oldCurrentScope := currentScope;
  4916. oldArrayDestinationTag := arrayDestinationTag;
  4917. oldArrayDestinationDimension := arrayDestinationDimension;
  4918. oldConstantDeclaration := constantDeclaration;
  4919. oldDestination := destination;
  4920. oldCurrentLoop := currentLoop;
  4921. oldConditional := conditional;
  4922. oldTrueLabel := trueLabel;
  4923. oldFalseLabel := falseLabel;
  4924. oldLocked := locked;
  4925. oldUsedRegisters := usedRegisters
  4926. END BackupGlobalState;
  4927. PROCEDURE RestoreGlobalState;
  4928. BEGIN
  4929. result := oldResult;
  4930. currentScope := oldCurrentScope;
  4931. arrayDestinationTag := oldArrayDestinationTag;
  4932. arrayDestinationDimension := oldArrayDestinationDimension;
  4933. constantDeclaration := oldConstantDeclaration;
  4934. destination := oldDestination;
  4935. currentLoop := oldCurrentLoop;
  4936. conditional := oldConditional;
  4937. trueLabel := oldTrueLabel;
  4938. falseLabel := oldFalseLabel;
  4939. locked := oldLocked;
  4940. usedRegisters := oldUsedRegisters
  4941. END RestoreGlobalState;
  4942. (** do preparations before parameter push for array-structured object types (ASOTs):
  4943. if ASOT is passed as VAR parameter:
  4944. - allocate temporary variable of math array type
  4945. - copy contents of ASOT to be passed to temporary variable
  4946. - use temporary variable as the actual parameter instead
  4947. - 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)
  4948. **)
  4949. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4950. VAR
  4951. expression: SyntaxTree.Expression;
  4952. BEGIN
  4953. IF actualParameter IS SyntaxTree.Designator THEN
  4954. designator := actualParameter(SyntaxTree.Designator);
  4955. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4956. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4957. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4958. ASSERT(checker # NIL);
  4959. checker.SetCurrentScope(currentScope);
  4960. (* allocate temporary variable *)
  4961. ASSERT(actualParameter.type # NIL);
  4962. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4963. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4964. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4965. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4966. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4967. ASSERT(tempVariableDesignator.type # NIL);
  4968. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4969. (* copy math array stored in actual parameter to temporary variable *)
  4970. BackupGlobalState;
  4971. AssignMathArray(tempVariableDesignator, actualParameter);
  4972. RestoreGlobalState;
  4973. (* use temporary variable as actual parameter instead of the original one *)
  4974. actualParameter := tempVariableDesignator;
  4975. (* create write-back call and store it in linked list *)
  4976. (* create new list entry *)
  4977. IF firstWriteBackCall = NIL THEN
  4978. NEW(firstWriteBackCall);
  4979. currentWriteBackCall := firstWriteBackCall
  4980. ELSE
  4981. ASSERT(currentWriteBackCall # NIL);
  4982. NEW(currentWriteBackCall.next);
  4983. currentWriteBackCall := currentWriteBackCall.next
  4984. END;
  4985. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4986. ASSERT(expression.type = NIL);
  4987. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4988. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4989. (* prepare writeback for any other "normal" indexer *)
  4990. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4991. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4992. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4993. Assign(tempVariableDesignator, actualParameter);
  4994. actualParameter := tempVariableDesignator;
  4995. IF firstWriteBackCall = NIL THEN
  4996. NEW(firstWriteBackCall);
  4997. currentWriteBackCall := firstWriteBackCall
  4998. ELSE
  4999. ASSERT(currentWriteBackCall # NIL);
  5000. NEW(currentWriteBackCall.next);
  5001. currentWriteBackCall := currentWriteBackCall.next
  5002. END;
  5003. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  5004. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  5005. END
  5006. END
  5007. END PrepareParameter;
  5008. BEGIN
  5009. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5010. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5011. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5012. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5013. IF InlineProcedureCall(x) THEN
  5014. RETURN
  5015. ELSE
  5016. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5017. END
  5018. END;
  5019. END;
  5020. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5021. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5022. dest := destination; destination := emptyOperand;
  5023. SaveRegisters();ReleaseUsedRegisters(saved);
  5024. parameters := x.parameters;
  5025. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5026. (* an operator is called *)
  5027. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5028. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5029. (* check if a dynamic operator call should be performed *)
  5030. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5031. ELSE
  5032. isCallOfDynamicOperator := FALSE
  5033. END;
  5034. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5035. Emit(Push(position, ap));
  5036. END;
  5037. alignment := procedureType.stackAlignment;
  5038. IF alignment > 1 THEN
  5039. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5040. Emit(Mov(position,reg, sp));
  5041. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5042. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5043. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5044. Emit(And(position,sp, sp, mask));
  5045. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5046. Emit(Push(position,reg));
  5047. (*
  5048. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5049. Emit(Mov(position,mem,reg));
  5050. *)
  5051. ReleaseIntermediateOperand(reg);
  5052. END;
  5053. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5054. (* align stack to 16-byte boundary *)
  5055. IntermediateCode.InitImmediate(mask,addressType,-16);
  5056. Emit(And(position,sp, sp, mask));
  5057. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5058. IF gap # 0 THEN
  5059. IntermediateCode.InitImmediate(size,addressType,gap);
  5060. Emit(Sub(position,sp,sp,size))
  5061. END;
  5062. END;
  5063. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5064. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5065. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5066. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5067. ELSE
  5068. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5069. END;
  5070. Evaluate(x.left, operand);
  5071. IF symbol IS SyntaxTree.Procedure THEN
  5072. IF (procedureType.selfParameter # NIL) THEN
  5073. Emit(Push(position,operand.tag));
  5074. ELSIF x.left IS SyntaxTree.SupercallDesignator THEN
  5075. Emit(Push(position,operand.tag));
  5076. ELSIF (procedureType.isDelegate) THEN
  5077. Emit(Push(position,operand.tag));
  5078. END;
  5079. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5080. IF (procedureType.selfParameter # NIL) THEN
  5081. Emit(Push(position,operand.tag));
  5082. ELSIF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5083. Emit(Push(position,operand.tag));
  5084. END;
  5085. ELSE HALT(200);
  5086. END;
  5087. ReleaseIntermediateOperand(operand.tag);
  5088. operand.tag := emptyOperand;
  5089. (* determine if a structured return type is needed *)
  5090. structuredReturnType := SemanticChecker.StructuredReturnType(procedureType);
  5091. IF structuredReturnType THEN
  5092. IF resultDesignator # NIL THEN
  5093. d := resultDesignator;
  5094. ELSE
  5095. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5096. variable.SetUntraced(procedureType.hasUntracedReturn);
  5097. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5098. d.SetType(variable.type);
  5099. END;
  5100. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5101. Designate(d,returnValue);
  5102. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5103. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5104. Emit(Push(position,size));
  5105. Emit(Push(position,returnValue.op));
  5106. ReleaseOperand(returnValue);
  5107. ELSE
  5108. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5109. END;
  5110. END;
  5111. firstWriteBackCall := NIL; (* reset write-back call list *)
  5112. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5113. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5114. IF parameterRegister = NIL THEN parameterRegisters := 0
  5115. ELSE parameterRegisters := LEN(parameterRegister)
  5116. END;
  5117. passByRegister := parameterRegisters > 0;
  5118. registerNumber := 0;
  5119. formalParameter := procedureType.lastParameter;
  5120. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5121. actualParameter := parameters.GetExpression(i);
  5122. PrepareParameter(actualParameter, formalParameter);
  5123. IF passByRegister & (i < parameterRegisters) THEN
  5124. IF ~PassInRegister(formalParameter) THEN
  5125. Error(actualParameter.position,"cannot be passed by register")
  5126. ELSE
  5127. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5128. END;
  5129. INC(registerNumber);
  5130. ELSE
  5131. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5132. END;
  5133. formalParameter := formalParameter.prevParameter;
  5134. END;
  5135. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5136. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5137. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5138. END;
  5139. ELSE
  5140. hasDynamicOperands := FALSE;
  5141. formalParameter := procedureType.firstParameter;
  5142. FOR i := 0 TO parameters.Length() - 1 DO
  5143. actualParameter := parameters.GetExpression(i);
  5144. IF formalParameter # NIL THEN (* TENTATIVE *)
  5145. PrepareParameter(actualParameter, formalParameter);
  5146. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5147. ASSERT(i < 2);
  5148. hasDynamicOperands := TRUE;
  5149. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5150. ELSE
  5151. IF passByRegister & (registerNumber > 0) THEN
  5152. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5153. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5154. END;
  5155. passByRegister := FALSE;
  5156. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5157. END;
  5158. formalParameter := formalParameter.nextParameter;
  5159. END;
  5160. END;
  5161. END;
  5162. IF symbol IS SyntaxTree.Procedure THEN
  5163. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5164. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5165. Emit(Push(position,reg));
  5166. ReleaseIntermediateOperand(reg);
  5167. END;
  5168. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5169. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5170. parametersSize := ParametersSize(system,procedureType,FALSE);
  5171. END;
  5172. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5173. (*
  5174. dynamic operator overloading:
  5175. first push parameters, regularly:
  5176. [self]
  5177. par1
  5178. par2
  5179. then push parameters for GetOperator
  5180. identifier
  5181. ptr1
  5182. tag
  5183. ptr2
  5184. tag
  5185. call GetOperatorRuntimeProc
  5186. call Operator
  5187. *)
  5188. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5189. (* push ID *)
  5190. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5191. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5192. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5193. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5194. formalParameter := procedureType.firstParameter;
  5195. FOR i := 0 TO parameters.Length() - 1 DO
  5196. IF formalParameter.access # SyntaxTree.Hidden THEN
  5197. ASSERT(i < 2);
  5198. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5199. (* push pointer *)
  5200. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5201. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5202. (* add dereference *)
  5203. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5204. (*! better: do refer to stack above than using parameter backups !!*)
  5205. ReleaseIntermediateOperand(parameterBackups[i]);
  5206. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5207. END;
  5208. Emit(Push(position,parameterBackups[i]));
  5209. ReleaseIntermediateOperand(parameterBackups[i]);
  5210. (* push typetag *)
  5211. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5212. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5213. arg := TypeDescriptorAdr(recordType);
  5214. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5215. Emit(Push(position,arg));
  5216. ELSE
  5217. (* push 'NonPointer' *)
  5218. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5219. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5220. (* push fingerprint *)
  5221. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5222. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5223. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5224. END
  5225. END;
  5226. formalParameter := formalParameter.nextParameter
  5227. END;
  5228. (* for unary operators: complete the information for the second parameter *)
  5229. IF procedureType.numberParameters < 2 THEN
  5230. (* push 'NonPointer' *)
  5231. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5232. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5233. (* push 'NoType' *)
  5234. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5235. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5236. END;
  5237. (* call operator selection procedure *)
  5238. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5239. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5240. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5241. ReleaseOperand(operatorSelectionProcedureOperand);
  5242. (* use the address that the operator selection procedure returned as the target address of the call *)
  5243. InitOperand(operand, ModeValue);
  5244. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5245. Emit(Result(position,operand.op))
  5246. END
  5247. END;
  5248. ReleaseParameterRegisters();
  5249. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5250. Emit(Call(position,operand.op,0));
  5251. ELSE
  5252. Emit(Call(position,operand.op,parametersSize));
  5253. END;
  5254. ReleaseOperand(operand);
  5255. IF procedureType.noReturn THEN
  5256. EmitTrap(position,NoReturnTrap);
  5257. END;
  5258. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5259. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5260. Emit(Result(position,return));
  5261. END;
  5262. (* === return parameter space === *)
  5263. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5264. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5265. IF parametersSize < 32 THEN
  5266. (* allocated space for all parameter registers *)
  5267. parametersSize := 32
  5268. END;
  5269. size := IntermediateCode.Immediate(addressType,parametersSize);
  5270. Emit(Add(position,sp,sp,size))
  5271. END;
  5272. IF SysvABI(procedureType.callingConvention) THEN
  5273. IF passByRegister THEN
  5274. IF parameters.Length() > parameterRegisters THEN
  5275. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5276. ELSE
  5277. parametersSize := 0
  5278. END;
  5279. ELSE
  5280. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5281. INC( parametersSize, (-parametersSize) MOD 16 )
  5282. END;
  5283. IF parametersSize > 0 THEN
  5284. size := IntermediateCode.Immediate(addressType,parametersSize);
  5285. Emit(Add(position,sp,sp,size))
  5286. END;
  5287. END;
  5288. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5289. IF structuredReturnType THEN
  5290. (* stack pointer rewinding done by callee
  5291. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5292. Emit(Add(position,sp,sp,size));
  5293. *)
  5294. RestoreRegisters(saved);
  5295. InitOperand(result,ModeReference);
  5296. Symbol(variable,result);
  5297. ELSE
  5298. RestoreRegisters(saved);
  5299. InitOperand(result,ModeValue);
  5300. result.op := return;
  5301. END;
  5302. END;
  5303. IF alignment > 1 THEN
  5304. Emit(Pop(position,sp));
  5305. END;
  5306. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5307. Emit(Pop(position, ap));
  5308. END;
  5309. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5310. ValueToCondition(result);
  5311. END;
  5312. destination := dest;
  5313. (* perform all write-back calls in the list *)
  5314. BackupGlobalState;
  5315. currentWriteBackCall := firstWriteBackCall;
  5316. WHILE currentWriteBackCall # NIL DO
  5317. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5318. currentWriteBackCall := currentWriteBackCall.next
  5319. END;
  5320. RestoreGlobalState;
  5321. (* TENATIVE *)
  5322. (*
  5323. IF isOperatorCall THEN
  5324. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5325. END;
  5326. *)
  5327. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5328. END VisitProcedureCallDesignator;
  5329. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5330. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5331. td: SyntaxTree.Symbol;
  5332. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5333. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5334. BEGIN
  5335. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5336. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5337. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5338. variable.SetType(system.anyType);
  5339. scope.AddVariable(variable);
  5340. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5341. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5342. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5343. typeDeclaration.SetScope(module.module.moduleScope);
  5344. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5345. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5346. END;
  5347. RETURN hiddenPointerType;
  5348. END GetHiddenPointerType;
  5349. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5350. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5351. BEGIN
  5352. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5353. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5354. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5355. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5356. scope.AddVariable(variable);
  5357. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5358. variable.SetType(system.anyType);
  5359. scope.AddVariable(variable);
  5360. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5361. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5362. typeDeclaration.SetDeclaredType(delegatePointerType);
  5363. typeDeclaration.SetScope(module.module.moduleScope);
  5364. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5365. delegatePointerType.SetState(SyntaxTree.Resolved);
  5366. END;
  5367. RETURN delegatePointerType
  5368. END GetDelegateType;
  5369. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5370. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5371. such as in VAR a: RECORD ... END;
  5372. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5373. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5374. *)
  5375. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5376. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5377. BEGIN (* no code emission *)
  5378. source := NIL;
  5379. x := x.resolved;
  5380. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5381. x := GetHiddenPointerType();
  5382. ELSIF IsDelegate(x) THEN
  5383. x := GetDelegateType();
  5384. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5385. ELSE HALT(200);
  5386. END;
  5387. td := x.typeDeclaration;
  5388. IF td = NIL THEN
  5389. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5390. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5391. ASSERT(td # NIL);
  5392. END;
  5393. IF newObjectFile THEN
  5394. GetCodeSectionNameForSymbol(td,name);
  5395. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5396. meta.CheckTypeDeclaration(x);
  5397. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5398. ELSE
  5399. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5400. END;
  5401. IF backend.cooperative OR meta.simple THEN
  5402. offset := 0;
  5403. ELSE
  5404. IF x IS SyntaxTree.CellType THEN
  5405. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5406. IF baseRecord = NIL THEN
  5407. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5408. ELSE
  5409. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5410. END;
  5411. ELSE
  5412. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5413. END;
  5414. END;
  5415. ELSE
  5416. offset := 0;
  5417. source := module.allSections.FindBySymbol(td); (*TODO*)
  5418. IF source = NIL THEN
  5419. null := 0;
  5420. GetCodeSectionNameForSymbol(td,name);
  5421. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5422. Basic.SuffixSegmentedName (name, module.module.name);
  5423. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5424. IntermediateCode.InitImmediate(op,addressType,0);
  5425. source(IntermediateCode.Section).Emit(Data(position,op));
  5426. source.SetReferenced(FALSE)
  5427. ELSE
  5428. name := source.name;
  5429. END;
  5430. END;
  5431. RETURN td
  5432. END GetBackendType;
  5433. BEGIN
  5434. (*td := t.typeDeclaration;*)
  5435. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5436. (*
  5437. IF t IS SyntaxTree.PointerType THEN
  5438. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5439. ELSE
  5440. source := GetBackendType(t);
  5441. END;
  5442. *)
  5443. IF newObjectFile THEN
  5444. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5445. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5446. IntermediateCode.SetOffset(res,offset);
  5447. ELSE
  5448. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5449. END;
  5450. (*
  5451. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5452. make memory should be used when tag is used, not earlier
  5453. *)
  5454. RETURN res
  5455. END TypeDescriptorAdr;
  5456. (*
  5457. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5458. VAR result: IntermediateCode.Operand;
  5459. BEGIN
  5460. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5461. operand.tag := TypeDescriptorAdr(operand.type);
  5462. IntermediateCode.MakeMemory(operand.tag,addressType);
  5463. UseIntermediateOperand(operand.tag);
  5464. END;
  5465. END MakeTypeTag;
  5466. *)
  5467. PROCEDURE ProfilerInit;
  5468. VAR reg: IntermediateCode.Operand;
  5469. BEGIN
  5470. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5471. Emit(Call(position,reg,0));
  5472. END ProfilerInit;
  5473. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5474. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5475. BEGIN
  5476. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5477. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5478. THEN
  5479. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5480. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5481. Emit(Push(position,reg));
  5482. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5483. Emit(Push(position,reg));
  5484. StaticCallOperand(result,procedure);
  5485. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5486. ReleaseOperand(result);
  5487. END;
  5488. END ProfilerEnterExit;
  5489. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5490. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5491. BEGIN
  5492. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5493. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5494. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5495. profileInit.Emit(Push(position,reg));
  5496. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5497. profileInit.Emit(Push(position,reg));
  5498. NEW(string, LEN(name)); COPY(name, string^);
  5499. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5500. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5501. sv.SetType(type);
  5502. Designate(sv,result);
  5503. profileInit.Emit(Push(position,result.tag));
  5504. profileInit.Emit(Push(position,result.op));
  5505. StaticCallOperand(result,procedure);
  5506. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5507. ReleaseOperand(result);
  5508. END;
  5509. END ProfilerAddProcedure;
  5510. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5511. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5512. BEGIN
  5513. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5514. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5515. profileInit.Emit(Push(position,reg));
  5516. profileInitPatchPosition := profileInit.pc;
  5517. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5518. NEW(string, LEN(name)); COPY(name, string^);
  5519. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5520. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5521. sv.SetType(type);
  5522. Designate(sv,result);
  5523. profileInit.Emit(Push(position,result.tag));
  5524. profileInit.Emit(Push(position,result.op));
  5525. StaticCallOperand(result,procedure);
  5526. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5527. ReleaseOperand(result);
  5528. END;
  5529. END ProfilerAddModule;
  5530. PROCEDURE ProfilerPatchInit;
  5531. VAR reg: IntermediateCode.Operand;
  5532. BEGIN
  5533. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5534. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5535. EmitLeave(profileInit,position,NIL,0);
  5536. profileInit.Emit(Exit(position,0,0,0));
  5537. END ProfilerPatchInit;
  5538. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5539. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5540. VAR
  5541. id: LONGINT;
  5542. leftType, rightType: SyntaxTree.Type;
  5543. procedureType: SyntaxTree.ProcedureType;
  5544. runtimeProcedure: SyntaxTree.Procedure;
  5545. runtimeProcedureOperand, operatorOperand: Operand;
  5546. kind: SET;
  5547. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5548. VAR
  5549. arg: IntermediateCode.Operand;
  5550. recordType: SyntaxTree.RecordType;
  5551. fingerPrint: SyntaxTree.FingerPrint;
  5552. BEGIN
  5553. IF type = NIL THEN
  5554. (* no type: push 'NoType' *)
  5555. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5556. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5557. ELSIF IsStrictlyPointerToRecord(type) THEN
  5558. (* pointer to record type: push typetag *)
  5559. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5560. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5561. arg := TypeDescriptorAdr(recordType);
  5562. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5563. ELSE
  5564. (* non-pointer to record type: push fingerprint *)
  5565. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5566. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5567. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5568. END;
  5569. operatorInitializationCodeSection.Emit(Push(position,arg))
  5570. END PushTypeInfo;
  5571. BEGIN
  5572. ASSERT(operatorInitializationCodeSection # NIL);
  5573. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5574. procedureType := operator.type(SyntaxTree.ProcedureType);
  5575. (* determine types *)
  5576. leftType := procedureType.firstParameter.type;
  5577. IF procedureType.numberParameters = 2 THEN
  5578. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5579. rightType := procedureType.firstParameter.nextParameter.type;
  5580. ELSE
  5581. rightType := NIL
  5582. END;
  5583. (* determine operator kind *)
  5584. IF IsStrictlyPointerToRecord(leftType) THEN
  5585. kind := {LhsIsPointer}
  5586. ELSE
  5587. kind := {}
  5588. END;
  5589. IF IsStrictlyPointerToRecord(rightType) THEN
  5590. kind := kind + {RhsIsPointer}
  5591. END;
  5592. IF kind # {} THEN (* TODO: to be removed later on *)
  5593. (* at least one of the types is a pointer to record *)
  5594. (* emit a code that registers this specific operator in the runtime *)
  5595. dump := operatorInitializationCodeSection.comments;
  5596. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5597. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5598. (* push ID *)
  5599. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5600. id := Global.GetSymbol(module.module.case, operator.name);
  5601. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5602. (* push kind *)
  5603. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5604. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5605. (* push type infos *)
  5606. PushTypeInfo(leftType);
  5607. PushTypeInfo(rightType);
  5608. (* push operator address *)
  5609. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5610. StaticCallOperand(operatorOperand, operator);
  5611. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5612. ReleaseOperand(operatorOperand);
  5613. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5614. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5615. ReleaseOperand(runtimeProcedureOperand)
  5616. END
  5617. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5618. END
  5619. END RegisterDynamicOperator;
  5620. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5621. VAR
  5622. traceModule: SyntaxTree.Module;
  5623. procedure: SyntaxTree.Procedure;
  5624. procedureVariable: SyntaxTree.Variable;
  5625. s,msg: Basic.MessageString;
  5626. res: Operand;
  5627. i: LONGINT;
  5628. sv: SyntaxTree.StringValue;
  5629. type: SyntaxTree.Type;
  5630. recordType: SyntaxTree.RecordType;
  5631. printout: Printout.Printer;
  5632. stringWriter: Streams.StringWriter;
  5633. expression: SyntaxTree.Expression;
  5634. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5635. BEGIN
  5636. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5637. IF procedure = NIL THEN
  5638. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5639. END;
  5640. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5641. s := "procedure ";
  5642. Strings.Append(s,backend.traceModuleName);
  5643. Strings.Append(s,".");
  5644. Strings.Append(s,procedureName);
  5645. Strings.Append(s," not present");
  5646. Error(position,s);
  5647. RETURN FALSE
  5648. ELSE
  5649. RETURN TRUE
  5650. END;
  5651. END GetProcedure;
  5652. PROCEDURE CallProcedure;
  5653. VAR size: LONGINT;
  5654. BEGIN
  5655. IF procedure # NIL THEN
  5656. StaticCallOperand(result,procedure);
  5657. size := ProcedureParametersSize(system,procedure);
  5658. ELSE
  5659. Symbol(procedureVariable, result);
  5660. LoadValue(result, procedureVariable.type.resolved);
  5661. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5662. END;
  5663. Emit(Call(position,result.op,size));
  5664. END CallProcedure;
  5665. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5666. VAR res: Operand; string: SyntaxTree.String;
  5667. BEGIN
  5668. IF GetProcedure("String") THEN
  5669. NEW(string, LEN(s)); COPY(s, string^);
  5670. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5671. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5672. sv.SetType(type);
  5673. Designate(sv,res);
  5674. Emit(Push(position,res.tag));
  5675. Emit(Push(position,res.op));
  5676. ReleaseOperand(res);
  5677. CallProcedure;
  5678. END;
  5679. END String;
  5680. PROCEDURE Integer(op: IntermediateCode.Operand);
  5681. BEGIN
  5682. IF GetProcedure("Int") THEN
  5683. Emit(Push(position,op));
  5684. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5685. CallProcedure;
  5686. END;
  5687. END Integer;
  5688. PROCEDURE Float(op: IntermediateCode.Operand);
  5689. BEGIN
  5690. IF GetProcedure("HIntHex") THEN
  5691. Emit(Push(position,op));
  5692. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5693. CallProcedure;
  5694. END;
  5695. END Float;
  5696. PROCEDURE Set(op: IntermediateCode.Operand);
  5697. BEGIN
  5698. IF GetProcedure("Set") THEN
  5699. Emit(Push(position,op));
  5700. (*
  5701. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5702. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5703. *)
  5704. CallProcedure;
  5705. END;
  5706. END Set;
  5707. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5708. BEGIN
  5709. IF GetProcedure("Boolean") THEN
  5710. Emit(Push(position,op));
  5711. CallProcedure;
  5712. END;
  5713. END Boolean;
  5714. PROCEDURE Char(op: IntermediateCode.Operand);
  5715. BEGIN
  5716. IF GetProcedure("Char") THEN
  5717. Emit(Push(position,op));
  5718. CallProcedure;
  5719. END;
  5720. END Char;
  5721. PROCEDURE Address(op: IntermediateCode.Operand);
  5722. BEGIN
  5723. IF GetProcedure("Address") THEN
  5724. Emit(Push(position,op));
  5725. CallProcedure;
  5726. END;
  5727. END Address;
  5728. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5729. VAR len: IntermediateCode.Operand;
  5730. BEGIN
  5731. IF GetProcedure("String") THEN
  5732. len := GetArrayLength(type, op.tag);
  5733. Emit(Push(position,len));
  5734. ReleaseIntermediateOperand(len);
  5735. Emit(Push(position,op.op));
  5736. CallProcedure;
  5737. END;
  5738. END StringOperand;
  5739. PROCEDURE Ln;
  5740. BEGIN
  5741. IF GetProcedure("Ln") THEN
  5742. CallProcedure;
  5743. END;
  5744. END Ln;
  5745. BEGIN
  5746. IF backend.traceModuleName = "" THEN RETURN END;
  5747. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5748. IF GetProcedure("Enter") THEN
  5749. CallProcedure
  5750. END;
  5751. NEW(stringWriter,LEN(s));
  5752. FOR i := 0 TO x.Length()-1 DO
  5753. msg := "";
  5754. expression := x.GetExpression(i);
  5755. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5756. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5757. ELSE
  5758. Global.GetModuleName(module.module, s);
  5759. END;
  5760. IF i = 0 THEN
  5761. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5762. stringWriter.String(":");
  5763. END;
  5764. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5765. IF ~(expression IS SyntaxTree.StringValue) THEN
  5766. printout.Expression(expression);
  5767. stringWriter.Get(s);
  5768. Strings.Append(msg,s);
  5769. Strings.Append(msg,"= ");
  5770. ELSE stringWriter.Get(s); (* remove from string writer *)
  5771. Strings.Append(msg, s);
  5772. END;
  5773. String(msg);
  5774. IF SemanticChecker.IsStringType(expression.type) THEN
  5775. Designate(expression,res);
  5776. StringOperand(res, expression.type);
  5777. ELSE
  5778. Evaluate(expression,res);
  5779. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5780. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5781. Convert(res.op,int64);
  5782. END;
  5783. Integer(res.op);
  5784. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5785. Boolean(res.op);
  5786. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5787. Set(res.op);
  5788. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5789. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5790. Convert(res.op,float64);
  5791. END;
  5792. Float(res.op);
  5793. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5794. Char(res.op);
  5795. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5796. Address(res.op);String("H");
  5797. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5798. Address(res.op);String("H");
  5799. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5800. Address(res.op);String("H");
  5801. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5802. Address(res.op);String("H");
  5803. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5804. String("NIL");
  5805. ELSE HALT(200);
  5806. END;
  5807. END;
  5808. ReleaseOperand(res);
  5809. String("; ");
  5810. END;
  5811. IF GetProcedure("Exit") THEN
  5812. CallProcedure
  5813. ELSE
  5814. Ln;
  5815. END;
  5816. END;
  5817. END SystemTrace;
  5818. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5819. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5820. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5821. BEGIN
  5822. type := type.resolved;
  5823. IF type IS SyntaxTree.RecordType THEN
  5824. WITH type: SyntaxTree.RecordType DO
  5825. baseType := type.baseType;
  5826. IF baseType # NIL THEN
  5827. baseType := baseType.resolved;
  5828. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5829. InitFields(baseType,adr,offset);
  5830. END;
  5831. variable := type.recordScope.firstVariable;
  5832. WHILE variable # NIL DO
  5833. IF variable.initializer # NIL THEN
  5834. Evaluate(variable.initializer,initializerOp);
  5835. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5836. Emit(Mov(position,mem,initializerOp.op));
  5837. ReleaseOperand(initializerOp);
  5838. ReleaseIntermediateOperand(mem);
  5839. END;
  5840. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5841. variable := variable.nextVariable
  5842. END;
  5843. END;
  5844. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5845. WITH type: SyntaxTree.ArrayType DO
  5846. IF type.form = SyntaxTree.Static THEN
  5847. baseType := type.arrayBase;
  5848. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5849. FOR i := 0 TO type.staticLength-1 DO
  5850. InitFields(baseType,adr,offset+i*size);
  5851. END;
  5852. END;
  5853. END;
  5854. ELSIF type IS SyntaxTree.MathArrayType THEN
  5855. WITH type: SyntaxTree.MathArrayType DO
  5856. IF type.form = SyntaxTree.Open THEN
  5857. dim := DynamicDim(type);
  5858. imm := IntermediateCode.Immediate(addressType,dim);
  5859. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5860. baseType := SemanticChecker.ArrayBase(type,dim);
  5861. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5862. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5863. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5864. ReleaseIntermediateOperand(imm);
  5865. (* flags remain empty (=0) for open array *)
  5866. ELSIF type.form = SyntaxTree.Static THEN
  5867. baseType := type.arrayBase;
  5868. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5869. ASSERT(type.staticLength < 1024*1024*1024);
  5870. FOR i := 0 TO type.staticLength-1 DO
  5871. InitFields(baseType,adr,offset+i*size);
  5872. END;
  5873. END;
  5874. END;
  5875. END;
  5876. END InitFields;
  5877. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5878. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5879. BEGIN
  5880. type := variable.type.resolved;
  5881. IF (type IS SyntaxTree.MathArrayType) THEN
  5882. WITH type: SyntaxTree.MathArrayType DO
  5883. IF type.form = SyntaxTree.Open THEN
  5884. Symbol(variable,operand);
  5885. InitFields(type, operand.tag,0);
  5886. ELSIF type.form = SyntaxTree.Tensor THEN
  5887. Symbol(variable, operand);
  5888. MakeMemory(tmp,operand.op,addressType,0);
  5889. ReleaseOperand(operand);
  5890. Emit(Mov(position,tmp, nil ) );
  5891. ReleaseIntermediateOperand(tmp);
  5892. END;
  5893. END;
  5894. ELSE
  5895. Symbol(variable,operand);
  5896. IF variable.initializer # NIL THEN
  5897. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5898. reference.SetType(variable.type.resolved);
  5899. reference.SetAssignable(TRUE);
  5900. Assign(reference,variable.initializer);
  5901. END;
  5902. InitFields(type, operand.op,0);
  5903. ReleaseOperand(operand);
  5904. END;
  5905. END InitVariable;
  5906. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5907. VAR end: Label;
  5908. BEGIN
  5909. IF type.form = SyntaxTree.Tensor THEN
  5910. InitOperand(result,ModeValue);
  5911. ReuseCopy(result.op,base);
  5912. end := NewLabel();
  5913. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5914. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5915. SetLabel(end);
  5916. Convert(result.op,int32);
  5917. ELSE
  5918. InitOperand(result,ModeValue);
  5919. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5920. END
  5921. END MathArrayDim;
  5922. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5923. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5924. BEGIN
  5925. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5926. MakeMemory(mem,base,addressType,offset);
  5927. Emit(Mov(position,mem,value));
  5928. ReleaseIntermediateOperand(mem);
  5929. END PutMathArrayField;
  5930. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5931. VAR mem: IntermediateCode.Operand;
  5932. BEGIN
  5933. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5934. MakeMemory(mem,base,addressType,offset);
  5935. Emit(Mov(position,mem,value));
  5936. ReleaseIntermediateOperand(mem);
  5937. END PutMathArrayFieldOffset;
  5938. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5939. BEGIN
  5940. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5941. MakeMemory(value,base,addressType,offset);
  5942. END GetMathArrayField;
  5943. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5944. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5945. BEGIN
  5946. IF incr THEN
  5947. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5948. ELSE
  5949. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5950. END;
  5951. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5952. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5953. ELSE
  5954. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5955. Emit(Mov(position,reg,dim));
  5956. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5957. Emit(Add(position,reg,reg,base));
  5958. MakeMemory(mem, reg, addressType, offset);
  5959. ReleaseIntermediateOperand(reg);
  5960. Emit(Mov(position,mem,value));
  5961. ReleaseIntermediateOperand(mem);
  5962. END;
  5963. END PutMathArrayLenOrIncr;
  5964. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5965. BEGIN
  5966. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5967. END PutMathArrayLength;
  5968. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5969. BEGIN
  5970. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5971. END PutMathArrayIncrement;
  5972. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5973. BEGIN
  5974. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5975. END GetMathArrayIncrement;
  5976. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5977. BEGIN
  5978. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5979. END GetMathArrayLength;
  5980. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5981. VAR dimOp: IntermediateCode.Operand;
  5982. BEGIN
  5983. dimOp := IntermediateCode.Immediate(int32, dim);
  5984. GetMathArrayLength(type, operand, dimOp, check, result);
  5985. END GetMathArrayLengthAt;
  5986. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5987. VAR dimOp: IntermediateCode.Operand;
  5988. BEGIN
  5989. dimOp := IntermediateCode.Immediate(int32, dim);
  5990. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5991. END GetMathArrayIncrementAt;
  5992. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5993. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5994. offset: LONGINT;
  5995. BEGIN
  5996. IF increment THEN
  5997. offset := MathIncrOffset;
  5998. ELSE
  5999. offset := MathLenOffset;
  6000. END;
  6001. INC(offset,operand.dimOffset*2);
  6002. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  6003. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  6004. END;
  6005. (* static dimension *)
  6006. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6007. IF check & (type.form = SyntaxTree.Tensor) THEN
  6008. DimensionCheck(operand.tag,dim,BrltL);
  6009. END;
  6010. val := SHORT(dim.intValue);
  6011. IF type.form # SyntaxTree.Tensor THEN
  6012. t := SemanticChecker.ArrayBase(type,val);
  6013. type := t.resolved(SyntaxTree.MathArrayType);
  6014. IF type.form = SyntaxTree.Static THEN
  6015. IF increment THEN
  6016. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6017. ELSE
  6018. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6019. END;
  6020. InitOperand(result,ModeValue);
  6021. result.op := res;
  6022. RETURN;
  6023. END;
  6024. END;
  6025. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6026. MakeMemory(res,operand.tag,addressType,offset);
  6027. (*
  6028. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6029. *)
  6030. InitOperand(result,ModeValue);
  6031. result.op := res;
  6032. ELSE
  6033. Convert(dim,addressType);
  6034. IF check THEN
  6035. IF type.form = SyntaxTree.Tensor THEN
  6036. DimensionCheck(operand.tag,dim,BrltL);
  6037. ELSIF isUnchecked THEN (* do nothing *)
  6038. ELSE
  6039. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6040. END;
  6041. END;
  6042. end := NewLabel(); next := NIL;
  6043. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6044. Emit(Mov(position,res,dim));
  6045. Convert(res,int32);
  6046. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6047. WHILE t IS SyntaxTree.MathArrayType DO
  6048. type := t(SyntaxTree.MathArrayType);
  6049. IF type.form = SyntaxTree.Static THEN
  6050. imm := IntermediateCode.Immediate(int32,val);
  6051. next := NewLabel();
  6052. BrneL(next,imm,res);
  6053. IF increment THEN
  6054. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6055. ELSE
  6056. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6057. END;
  6058. Emit(MovReplace(position,res,imm));
  6059. BrL(end);
  6060. ELSE hasDynamicPart := TRUE;
  6061. END;
  6062. t := type.arrayBase.resolved;
  6063. val := val + 1;
  6064. IF next # NIL THEN SetLabel(next) END;
  6065. END;
  6066. IF hasDynamicPart THEN
  6067. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6068. Emit(Mov(position,res2,dim));
  6069. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6070. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6071. Emit(Add(position,res2,res2,imm));
  6072. Emit(Add(position,res2,res2,operand.tag));
  6073. IntermediateCode.MakeMemory(res2,int32);
  6074. Emit(MovReplace(position,res,res2));
  6075. ReleaseIntermediateOperand(res2);
  6076. END;
  6077. SetLabel(end);
  6078. Convert(res,int32);
  6079. InitOperand(result,ModeValue);
  6080. result.op := res;
  6081. END;
  6082. END MathArrayLenOrIncr;
  6083. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6084. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6085. offset: LONGINT;
  6086. BEGIN
  6087. offset := operand.dimOffset+DynamicDim(type)-1;
  6088. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6089. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6090. val := SHORT(dim.intValue);
  6091. t := SemanticChecker.ArrayBase(type,val);
  6092. type := t.resolved(SyntaxTree.ArrayType);
  6093. IF type.form = SyntaxTree.Static THEN
  6094. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6095. ELSE
  6096. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6097. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6098. END;
  6099. UseIntermediateOperand(res);
  6100. InitOperand(result,ModeValue);
  6101. result.op := res;
  6102. ELSE
  6103. Convert(dim,addressType);
  6104. IF ~isUnchecked THEN
  6105. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6106. END;
  6107. end := NewLabel(); next := NIL;
  6108. (* ReuseCopy(dim,res); *)
  6109. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6110. Emit(Mov(position,res,dim));
  6111. Convert(res,int32);
  6112. Convert(res,int32);
  6113. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6114. WHILE t IS SyntaxTree.ArrayType DO
  6115. type := t(SyntaxTree.ArrayType);
  6116. IF type.form = SyntaxTree.Static THEN
  6117. imm := IntermediateCode.Immediate(int32,val);
  6118. next := NewLabel();
  6119. BrneL(next,imm,res);
  6120. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6121. Emit(MovReplace(position,res,imm));
  6122. BrL(end);
  6123. ELSE hasDynamicPart := TRUE;
  6124. END;
  6125. t := type.arrayBase.resolved;
  6126. val := val + 1;
  6127. IF next # NIL THEN SetLabel(next) END;
  6128. END;
  6129. IF hasDynamicPart THEN
  6130. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6131. Convert(res2,addressType);
  6132. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6133. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6134. Emit(Sub(position,res2,imm,res2));
  6135. Emit(Add(position,res2,res2,operand.tag));
  6136. IntermediateCode.MakeMemory(res2,int32);
  6137. Emit(MovReplace(position,res,res2));
  6138. ReleaseIntermediateOperand(res2);
  6139. END;
  6140. SetLabel(end);
  6141. Convert(res,int32);
  6142. InitOperand(result,ModeValue);
  6143. result.op := res;
  6144. END;
  6145. END ArrayLen;
  6146. (**
  6147. create a temporary variable in current scope
  6148. **)
  6149. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6150. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6151. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6152. BEGIN
  6153. IF ~register THEN
  6154. variable := temporaries.GetFreeVariable(type, index);
  6155. ELSE
  6156. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6157. END;
  6158. scope := currentScope;
  6159. IF variable = NIL THEN
  6160. COPY("@hiddenIRVar",string);
  6161. Basic.AppendNumber(string,index);
  6162. name := SyntaxTree.NewIdentifier(string);
  6163. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6164. variable.SetType(type);
  6165. variable.SetAccess(SyntaxTree.Hidden);
  6166. IF ~register THEN
  6167. temporaries.AddVariable(variable);
  6168. IF scope.lastVariable # NIL THEN
  6169. offset := scope.lastVariable.offsetInBits;
  6170. ELSE
  6171. offset := 0;
  6172. END;
  6173. DEC(offset,system.SizeOf(variable.type));
  6174. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6175. variable(SyntaxTree.Variable).SetOffset(offset);
  6176. scope.AddVariable(variable(SyntaxTree.Variable));
  6177. scope.EnterSymbol(variable, duplicate);
  6178. ASSERT(~duplicate);
  6179. InitVariable(variable(SyntaxTree.Variable));
  6180. ELSE
  6181. variable.SetUseRegister(TRUE);
  6182. variable(SyntaxTree.Variable).SetOffset(0);
  6183. END;
  6184. ELSE
  6185. InitVariable(variable(SyntaxTree.Variable));
  6186. (*
  6187. ASSERT(variable.type.resolved = type.resolved)
  6188. *)
  6189. END;
  6190. RETURN variable(SyntaxTree.Variable)
  6191. END GetTemporaryVariable;
  6192. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6193. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6194. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6195. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6196. i: LONGINT; duplicate: BOOLEAN;
  6197. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6198. VAR variable: SyntaxTree.Variable;
  6199. BEGIN
  6200. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6201. variable.SetType(type);
  6202. recordScope.AddVariable(variable);
  6203. END AddVariable;
  6204. BEGIN
  6205. name := "@ArrayDescriptor";
  6206. Basic.AppendNumber(name,dimensions);
  6207. identifier := SyntaxTree.NewIdentifier(name);
  6208. parentScope := module.module.moduleScope;
  6209. symbol := parentScope.FindSymbol(identifier);
  6210. IF symbol # NIL THEN
  6211. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6212. type := typeDeclaration.declaredType;
  6213. ELSE
  6214. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6215. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6216. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6217. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6218. recordType.SetTypeDeclaration(typeDeclaration);
  6219. recordType.SetState(SyntaxTree.Resolved);
  6220. typeDeclaration.SetDeclaredType(recordType);
  6221. AddVariable("@ptr",system.anyType);
  6222. AddVariable("@adr",system.addressType);
  6223. AddVariable("@flags",system.addressType);
  6224. AddVariable("@dim",system.addressType);
  6225. AddVariable("@elementSize",system.addressType);
  6226. FOR i := 0 TO dimensions-1 DO
  6227. name := "@len";
  6228. Basic.AppendNumber(name,i);
  6229. AddVariable(name,system.addressType);
  6230. name := "@incr";
  6231. Basic.AppendNumber(name,i);
  6232. AddVariable(name,system.addressType);
  6233. END;
  6234. parentScope.AddTypeDeclaration(typeDeclaration);
  6235. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6236. ASSERT(~duplicate);
  6237. type := recordType;
  6238. END;
  6239. RETURN type
  6240. END GetMathArrayDescriptorType;
  6241. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6242. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6243. BEGIN
  6244. type := GetMathArrayDescriptorType(dimensions);
  6245. Emit(Push(position,op.op));
  6246. (* push type descriptor *)
  6247. reg := TypeDescriptorAdr(type);
  6248. IF ~newObjectFile THEN
  6249. IntermediateCode.MakeMemory(reg,addressType);
  6250. END;
  6251. Emit(Push(position,reg));
  6252. ReleaseIntermediateOperand(reg);
  6253. (* push realtime flag: false by default *)
  6254. Emit(Push(position,false));
  6255. CallThis(position,"Heaps","NewRec",3);
  6256. END NewMathArrayDescriptor;
  6257. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6258. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6259. BEGIN
  6260. NEW(string, LEN(s)); COPY(s, string^);
  6261. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6262. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6263. sv.SetType(type);
  6264. Designate(sv,res);
  6265. Emit(Push(position,res.tag));
  6266. Emit(Push(position,res.op));
  6267. ReleaseOperand(res);
  6268. END PushConstString;
  6269. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6270. BEGIN
  6271. IF b THEN
  6272. Emit(Push(Basic.invalidPosition, true));
  6273. ELSE
  6274. Emit(Push(Basic.invalidPosition, false));
  6275. END;
  6276. END PushConstBoolean;
  6277. PROCEDURE PushConstSet(v: SET);
  6278. VAR value: SyntaxTree.Value; op: Operand;
  6279. BEGIN
  6280. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6281. value.SetType(system.setType);
  6282. Evaluate(value, op);
  6283. Emit(Push(Basic.invalidPosition, op.op));
  6284. ReleaseOperand(op);
  6285. END PushConstSet;
  6286. PROCEDURE PushConstInteger(v: LONGINT);
  6287. VAR value: SyntaxTree.Value; op: Operand;
  6288. BEGIN
  6289. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6290. value.SetType(system.longintType);
  6291. Evaluate(value, op);
  6292. Emit(Push(Basic.invalidPosition, op.op));
  6293. ReleaseOperand(op);
  6294. END PushConstInteger;
  6295. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6296. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6297. section: IntermediateCode.Section;
  6298. BEGIN
  6299. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6300. Global.GetSymbolSegmentedName(symbol, name);
  6301. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6302. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6303. procedure.SetScope(moduleScope);
  6304. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6305. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6306. procedure.SetAccess(SyntaxTree.Hidden);
  6307. currentScope := procedureScope;
  6308. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6309. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6310. RETURN section;
  6311. END OpenInitializer;
  6312. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6313. BEGIN
  6314. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6315. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6316. section := prev;
  6317. END CloseInitializer;
  6318. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6319. VAR name: SyntaxTree.IdentifierString;
  6320. variable: SyntaxTree.Variable;
  6321. parameter: SyntaxTree.Parameter;
  6322. type: SyntaxTree.Type;
  6323. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6324. BEGIN
  6325. InitOperand(op,ModeReference);
  6326. op.op := fp;
  6327. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6328. Dereference(op, x, FALSE);
  6329. result := op;
  6330. Symbol(symbol, op);
  6331. END Field;
  6332. PROCEDURE Direction(direction: LONGINT): SET;
  6333. BEGIN
  6334. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6335. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6336. ELSE HALT(100);
  6337. END;
  6338. END Direction;
  6339. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6340. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6341. BEGIN
  6342. Field(port, op);
  6343. ToMemory(op.op,addressType,0);
  6344. Emit(Push(Basic.invalidPosition, op.op));
  6345. ReleaseOperand(op);
  6346. Basic.GetString(modifier.identifier, name);
  6347. PushConstString(name);
  6348. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6349. ASSERT(SemanticChecker.IsStringType(value.type));
  6350. Designate(value, op);
  6351. Emit(Push(modifier.position, op.tag));
  6352. Emit(Push(modifier.position, op.op));
  6353. ReleaseOperand(op);
  6354. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6355. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6356. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6357. Evaluate(value, op);
  6358. Emit(Push(modifier.position, op.op));
  6359. ReleaseOperand(op);
  6360. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6361. ELSE
  6362. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6363. END;
  6364. END AddPortProperty;
  6365. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6366. VAR modifier: SyntaxTree.Modifier;
  6367. BEGIN
  6368. modifier := parameter.modifiers;
  6369. WHILE modifier # NIL DO
  6370. AddPortProperty(parameter,modifier, modifier.expression);
  6371. modifier := modifier.nextModifier;
  6372. END;
  6373. END AddPortProperties;
  6374. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6375. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6376. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6377. PROCEDURE PushLens(type: SyntaxTree.Type);
  6378. BEGIN
  6379. IF IsSemiDynamicArray(type) THEN
  6380. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6381. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6382. Emit(Push(Basic.invalidPosition, op.op));
  6383. ReleaseOperand(op);
  6384. INC(dim);
  6385. ELSIF IsStaticArray(type) THEN
  6386. len := len * type(SyntaxTree.ArrayType).staticLength;
  6387. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6388. INC(dim);
  6389. ELSE
  6390. baseType := type;
  6391. END;
  6392. END PushLens;
  6393. BEGIN
  6394. (* cell *)
  6395. IF parameter.type IS SyntaxTree.ArrayType THEN
  6396. type := parameter.type;
  6397. dim := 0;
  6398. len := 1;
  6399. PushLens(type);
  6400. portType := baseType.resolved(SyntaxTree.PortType);
  6401. ELSE
  6402. portType := parameter.type(SyntaxTree.PortType);
  6403. END;
  6404. PushSelfPointer();
  6405. (* port / array of ports *)
  6406. IF IsStaticArray(type) THEN
  6407. PushConstInteger(len);
  6408. END;
  6409. Field(parameter, op);
  6410. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6411. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6412. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6413. Designate(d, op);*)
  6414. Emit(Push(Basic.invalidPosition, op.op));
  6415. ReleaseOperand(op);
  6416. (* name *)
  6417. PushConstString(name);
  6418. (* inout *)
  6419. PushConstSet(Direction(portType.direction));
  6420. (* width *)
  6421. PushConstInteger(portType.sizeInBits);
  6422. IF parameter.type IS SyntaxTree.PortType THEN
  6423. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6424. AddPortProperties(parameter);
  6425. ELSIF IsStaticArray(type)THEN
  6426. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6427. ELSIF IsSemiDynamicArray(type) THEN
  6428. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6429. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6430. Emit(Add(position,reg, sp, size));
  6431. (* dim *)
  6432. PushConstInteger(dim);
  6433. (* len array *)
  6434. Emit(Push(position, reg));
  6435. ReleaseIntermediateOperand(reg);
  6436. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6437. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6438. Emit(Add(position, sp,sp, size));
  6439. ELSE
  6440. HALT(100);
  6441. END;
  6442. END Parameter;
  6443. BEGIN
  6444. IF backend.cellsAreObjects THEN
  6445. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6446. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6447. END;
  6448. parameter := x.firstParameter;
  6449. WHILE (parameter # NIL) DO
  6450. type := parameter.type.resolved;
  6451. WHILE (type IS SyntaxTree.ArrayType) DO
  6452. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6453. END;
  6454. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6455. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6456. Parameter(name,parameter);
  6457. END;
  6458. parameter := parameter.nextParameter;
  6459. END;
  6460. ELSE HALT(200)
  6461. END;
  6462. END AddPorts;
  6463. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6464. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6465. BEGIN
  6466. Symbol(cell,op);
  6467. ToMemory(op.op,addressType,0);
  6468. Emit(Push(position,op.op));
  6469. ReleaseOperand(op);
  6470. property.GetName(name);
  6471. (* does not work when inheritance is used:
  6472. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6473. *)
  6474. PushConstString(name);
  6475. IF (value # NIL) THEN
  6476. ASSERT(
  6477. SemanticChecker.IsStringType(property.type)
  6478. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6479. OR (property.type.resolved IS SyntaxTree.FloatType)
  6480. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6481. OR (property.type.resolved IS SyntaxTree.SetType)
  6482. );
  6483. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6484. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6485. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6486. Designate(d, op);
  6487. IF SemanticChecker.IsStringType(property.type) THEN
  6488. Emit(Push(Basic.invalidPosition, op.tag))
  6489. END;
  6490. Emit(Push(Basic.invalidPosition, op.op));
  6491. ReleaseOperand(op);
  6492. END;
  6493. IF value = NIL THEN
  6494. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6495. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6496. ASSERT(SemanticChecker.IsStringType(value.type));
  6497. Designate(value, op);
  6498. PushString(op, value.type);
  6499. ReleaseOperand(op);
  6500. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6501. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6502. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6503. Evaluate(value, op);
  6504. Emit(Push(property.position, op.op));
  6505. ReleaseOperand(op);
  6506. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6507. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6508. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6509. Evaluate(value, op);
  6510. Emit(Push(property.position, op.op));
  6511. ReleaseOperand(op);
  6512. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6513. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6514. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6515. Evaluate(value, op);
  6516. Emit(Push(property.position, op.op));
  6517. ReleaseOperand(op);
  6518. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6519. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6520. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6521. Evaluate(value, op);
  6522. Emit(Push(property.position, op.op));
  6523. ReleaseOperand(op);
  6524. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6525. ELSE
  6526. HALT(200);
  6527. END;
  6528. END AddProperty;
  6529. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6530. VAR symbol: SyntaxTree.Symbol;
  6531. BEGIN
  6532. WHILE modifier # NIL DO
  6533. symbol := cellType.FindProperty(modifier.identifier);
  6534. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6535. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6536. modifier := modifier.nextModifier;
  6537. END;
  6538. END AddModifiers;
  6539. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6540. VAR last: SyntaxTree.Modifier;
  6541. BEGIN
  6542. IF to = NIL THEN
  6543. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6544. ELSE
  6545. last := to;
  6546. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6547. last := last.nextModifier;
  6548. END;
  6549. IF last.identifier # this.identifier THEN
  6550. ASSERT(last.nextModifier = NIL);
  6551. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6552. END;
  6553. END;
  6554. END AppendModifier;
  6555. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6556. BEGIN
  6557. WHILE this # NIL DO
  6558. AppendModifier(to, this);
  6559. this := this.nextModifier;
  6560. END;
  6561. END AppendModifiers;
  6562. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6563. VAR base: SyntaxTree.Type;
  6564. BEGIN
  6565. AppendModifiers(to, c.modifiers);
  6566. base := c.GetBaseValueType();
  6567. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6568. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6569. END;
  6570. END AppendCellTypeModifiers;
  6571. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6572. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6573. BEGIN
  6574. Basic.GetString(modifier.identifier, name);
  6575. PushConstString(name);
  6576. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6577. ASSERT(SemanticChecker.IsStringType(value.type));
  6578. Designate(value, op);
  6579. PushString(op, value.type);
  6580. ReleaseOperand(op);
  6581. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6582. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6583. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6584. Evaluate(value, op);
  6585. Emit(Push(modifier.position, op.op));
  6586. ReleaseOperand(op);
  6587. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6588. ELSE
  6589. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6590. END;
  6591. END AddPortProperty;
  6592. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6593. BEGIN
  6594. WHILE modifier # NIL DO
  6595. AddPortProperty(modifier, modifier.expression);
  6596. modifier := modifier.nextModifier;
  6597. END;
  6598. END AddPortProperties;
  6599. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6600. VAR op: Operand;
  6601. BEGIN
  6602. Evaluate(p, op);
  6603. Emit(Push(p.position, op.op));
  6604. ReleaseOperand(op);
  6605. IF p IS SyntaxTree.Designator THEN
  6606. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6607. END;
  6608. END PushPort;
  6609. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6610. VAR tmp: IntermediateCode.Operand;
  6611. BEGIN
  6612. actualType := actualType.resolved;
  6613. IF actualType IS SyntaxTree.StringType THEN
  6614. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6615. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6616. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6617. ELSE
  6618. tmp := op.tag;
  6619. IntermediateCode.MakeMemory(tmp,addressType);
  6620. Emit(Push(position,tmp));
  6621. END;
  6622. Emit(Push(position,op.op))
  6623. END PushString;
  6624. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6625. VAR
  6626. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6627. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6628. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6629. tmp:IntermediateCode.Operand;
  6630. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6631. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6632. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6633. dest: IntermediateCode.Operand;
  6634. staticLength: LONGINT; itype: IntermediateCode.Type;
  6635. convert,isTensor: BOOLEAN;
  6636. recordType: SyntaxTree.RecordType;
  6637. baseType: SyntaxTree.Type;
  6638. flags: SET;
  6639. left: SyntaxTree.Expression;
  6640. call: SyntaxTree.Designator;
  6641. procedure: SyntaxTree.Procedure;
  6642. temporaryVariable: SyntaxTree.Variable;
  6643. dummy: IntermediateCode.Operand;
  6644. customBuiltin: SyntaxTree.CustomBuiltin;
  6645. isVarPar: ARRAY 3 OF BOOLEAN;
  6646. callsection: Sections.Section;
  6647. segmentedName: Basic.SegmentedName;
  6648. needsTrace: BOOLEAN;
  6649. n: ARRAY 256 OF CHAR;
  6650. modifier: SyntaxTree.Modifier;
  6651. previous, init: IntermediateCode.Section;
  6652. prevScope: SyntaxTree.Scope;
  6653. firstPar: LONGINT;
  6654. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6655. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6656. priority: IntermediateCode.Operand;
  6657. op,callop: Operand;
  6658. BEGIN
  6659. IF type = NIL THEN RETURN END;
  6660. type := type.resolved;
  6661. IF type IS SyntaxTree.PointerType THEN
  6662. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6663. END;
  6664. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6665. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6666. recordScope := type(SyntaxTree.RecordType).recordScope;
  6667. IF recordScope.bodyProcedure # NIL THEN
  6668. procedure := recordScope.bodyProcedure;
  6669. body := procedure.procedureScope.body;
  6670. Emit(Push(position,self));
  6671. IF body.isActive THEN
  6672. StaticCallOperand(callop,procedure);
  6673. Emit(Push(position,callop.op));
  6674. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6675. Convert(priority,sizeType);
  6676. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6677. END;
  6678. Emit(Push(position,priority));
  6679. ReleaseIntermediateOperand(priority);
  6680. IF backend.cooperative THEN
  6681. Emit(Push(position,self));
  6682. CallThis(position,"Activities","Create",3)
  6683. ELSE
  6684. flags := 0;
  6685. IF body.isSafe THEN
  6686. flags := 1;
  6687. END;
  6688. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6689. Emit(Push(position,self));
  6690. CallThis(position,"Objects","CreateProcess",4)
  6691. END;
  6692. ELSE
  6693. Emit(Push(position,self));
  6694. StaticCallOperand(callop,procedure);
  6695. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6696. END;
  6697. Emit(Pop(position,self));
  6698. END;
  6699. END CallBodies;
  6700. PROCEDURE PushTD(type: SyntaxTree.Type);
  6701. VAR op: IntermediateCode.Operand;
  6702. BEGIN
  6703. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6704. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6705. ELSE
  6706. IF type.resolved IS SyntaxTree.PointerType THEN
  6707. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6708. END;
  6709. op := TypeDescriptorAdr(type.resolved);
  6710. IF ~newObjectFile THEN
  6711. IntermediateCode.MakeMemory(op,addressType);
  6712. END;
  6713. Emit(Push(position,op));
  6714. END
  6715. END PushTD;
  6716. BEGIN
  6717. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6718. dest := destination; destination := emptyOperand;
  6719. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6720. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6721. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6722. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6723. CASE x.id OF
  6724. (* ---- COPY ----- *)
  6725. |Global.Copy:
  6726. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6727. (* ---- EXCL, INCL----- *)
  6728. |Global.Excl,Global.Incl:
  6729. Evaluate(p0,s0);
  6730. Evaluate(p1,s1);
  6731. Convert(s1.op,setType);
  6732. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6733. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6734. END;
  6735. ReuseCopy(res,s0.op);
  6736. ReleaseOperand(s0);
  6737. Reuse1(tmp,s1.op);
  6738. ReleaseOperand(s1);
  6739. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6740. IF x.id = Global.Excl THEN
  6741. Emit(Not(position,tmp,tmp));
  6742. Emit(And(position,res,res,tmp));
  6743. ELSE
  6744. Emit(Or(position,res,res,tmp));
  6745. END;
  6746. ReleaseIntermediateOperand(tmp);
  6747. Designate(p0,s0);
  6748. ToMemory(s0.op,setType,0);
  6749. Emit(Mov(position,s0.op,res));
  6750. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6751. (* ---- DISPOSE ----- *)
  6752. |Global.Dispose:
  6753. Designate(p0,s0);
  6754. Emit(Push(position,s0.op));
  6755. ReleaseOperand(s0);
  6756. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6757. (* ---- GETPROCEDURE ----- *)
  6758. |Global.GetProcedure:
  6759. Designate(p0,s0);
  6760. PushString(s0,p0.type);
  6761. Designate(p1,s1);
  6762. PushString(s1,p1.type);
  6763. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6764. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6765. ELSE PushTD(procedureType.firstParameter.type)
  6766. END;
  6767. PushTD(procedureType.returnType);
  6768. Designate(p2,s2);
  6769. Emit(Push(position,s2.op));
  6770. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6771. CallThis(position,"Modules","GetProcedure", 7);
  6772. (* ---- ASH, LSH, ROT ----- *)
  6773. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6774. Evaluate(p0,s0);
  6775. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6776. (* make unsigned arguments in order to produced a logical shift *)
  6777. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6778. convert:= TRUE;
  6779. itype := s0.op.type;
  6780. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6781. Convert(s0.op,itype);
  6782. ELSE
  6783. convert := FALSE;
  6784. END;
  6785. END;
  6786. Evaluate(p1,s1);
  6787. IF IsIntegerConstant(p1,hint) THEN
  6788. ReuseCopy(reg,s0.op);
  6789. IF hint > 0 THEN
  6790. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6791. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6792. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6793. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6794. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6795. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6796. END;
  6797. ELSIF hint < 0 THEN
  6798. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6799. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6800. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6801. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6802. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6803. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6804. END;
  6805. END;
  6806. ReleaseOperand(s0); ReleaseOperand(s1);
  6807. ELSE
  6808. exit := NewLabel();
  6809. end := NewLabel();
  6810. ReuseCopy(reg,s0.op);
  6811. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6812. Reuse1(tmp,s1.op);
  6813. Emit(Neg(position,tmp,s1.op));
  6814. Convert(tmp,s1.op.type);
  6815. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6816. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6817. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6818. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6819. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6820. END;
  6821. ReleaseIntermediateOperand(tmp);
  6822. BrL(end);
  6823. SetLabel(exit);
  6824. ReuseCopy(tmp,s1.op);
  6825. Convert(tmp,s1.op.type);
  6826. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6827. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6828. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6829. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6830. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6831. END;
  6832. ReleaseIntermediateOperand(tmp);
  6833. SetLabel(end);
  6834. ReleaseOperand(s0); ReleaseOperand(s1);
  6835. END;
  6836. InitOperand(result,ModeValue);
  6837. IF convert THEN
  6838. itype := reg.type;
  6839. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6840. Convert(reg,itype);
  6841. END;
  6842. result.op := reg;
  6843. (* ---- CAP ----- *)
  6844. |Global.Cap:
  6845. Evaluate(p0,result);
  6846. ReuseCopy(reg,result.op);
  6847. ReleaseIntermediateOperand(result.op);
  6848. ignore := NewLabel();
  6849. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6850. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6851. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6852. SetLabel(ignore);
  6853. result.op := reg;
  6854. (* ---- CHR ----- *)
  6855. |Global.Chr, Global.Chr32:
  6856. Evaluate(p0,result);
  6857. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6858. |Global.Entier, Global.EntierH:
  6859. Evaluate(p0,result);
  6860. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6861. (* ---- MIN and MAX ----- *)
  6862. |Global.Max,Global.Min:
  6863. Evaluate(p0,s0);
  6864. Evaluate(p1,s1);
  6865. Reuse2(res,s0.op,s1.op);
  6866. else := NewLabel();
  6867. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6868. ELSE BrltL(else,s1.op,s0.op) END;
  6869. Emit(Mov(position,res,s0.op));
  6870. ReleaseOperand(s0);
  6871. end := NewLabel();
  6872. BrL(end);
  6873. SetLabel(else);
  6874. Emit(MovReplace(position,res,s1.op));
  6875. SetLabel(end);
  6876. ReleaseOperand(s1);
  6877. InitOperand(result,ModeValue);
  6878. result.op := res;
  6879. (* ---- ODD ----- *)
  6880. |Global.Odd:
  6881. IF ~conditional THEN
  6882. ConditionToValue(x)
  6883. ELSE
  6884. Evaluate(p0,result);
  6885. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6886. Reuse1(res,result.op);
  6887. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6888. ReleaseIntermediateOperand(result.op);
  6889. result.op := res;
  6890. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6891. ReleaseOperand(result);
  6892. BrL(falseLabel);
  6893. END;
  6894. (* ---- ORD ----- *)
  6895. |Global.Ord, Global.Ord32:
  6896. Evaluate(p0,result);
  6897. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6898. (* ---- SHORT, LONG ----- *)
  6899. |Global.Short, Global.Long:
  6900. Evaluate(p0,result);
  6901. IF x.type IS SyntaxTree.ComplexType THEN
  6902. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6903. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6904. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6905. ELSE
  6906. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6907. END
  6908. (* ---- HALT, SYSTEM.HALT----- *)
  6909. |Global.Halt, Global.systemHalt:
  6910. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6911. EmitTrap (position, val);
  6912. (* ---- ASSERT ----- *)
  6913. |Global.Assert:
  6914. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6915. trueL := NewLabel();
  6916. falseL := NewLabel();
  6917. Condition(p0,trueL,falseL);
  6918. IF p1 = NIL THEN val := AssertTrap
  6919. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6920. END;
  6921. SetLabel(falseL);
  6922. EmitTrap(position,val);
  6923. SetLabel(trueL);
  6924. END;
  6925. (*
  6926. Emit(TrapC(result.op,val);
  6927. *)
  6928. (* ---- INC, DEC----- *)
  6929. |Global.Inc,Global.Dec:
  6930. Expression(p0); adr := result.op;
  6931. LoadValue(result,p0.type); l := result;
  6932. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6933. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6934. END;
  6935. IF x.id = Global.Inc THEN
  6936. Emit(Add(position,l.op,l.op,r.op));
  6937. ELSE
  6938. Emit(Sub(position,l.op,l.op,r.op));
  6939. END;
  6940. ReleaseOperand(l); ReleaseOperand(r);
  6941. (* ---- LEN ----- *)
  6942. |Global.Len: (* dynamic length, static length done by checker *)
  6943. Designate(p0,operand);
  6944. IF p1 = NIL THEN
  6945. InitOperand(l,ModeValue);
  6946. l.op := IntermediateCode.Immediate(int32,0);
  6947. ELSE
  6948. Evaluate(p1,l);
  6949. END;
  6950. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6951. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6952. Dereference(operand, p0.type.resolved, FALSE);
  6953. END;
  6954. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6955. ReleaseOperand(operand); ReleaseOperand(l);
  6956. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6957. ASSERT(p1 # NIL);
  6958. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6959. Dereference(operand,p0.type.resolved,FALSE);
  6960. END;
  6961. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6962. ReleaseOperand(operand); ReleaseOperand(l);
  6963. ELSE HALT(100);
  6964. END;
  6965. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6966. (* ---- FIRST ---- *)
  6967. |Global.First:
  6968. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6969. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6970. ELSE
  6971. Designate(p0, result)
  6972. END
  6973. (* ---- LAST ---- *)
  6974. |Global.Last:
  6975. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6976. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6977. ELSE
  6978. Designate(p0, result);
  6979. (* make sure result.op is a register *)
  6980. tmp := result.op;
  6981. ReuseCopy(result.op, result.op);
  6982. ReleaseIntermediateOperand(tmp);
  6983. (* add offset to result.op *)
  6984. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6985. END
  6986. (* ---- STEP ---- *)
  6987. |Global.Step:
  6988. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6989. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6990. ELSE
  6991. Designate(p0, result);
  6992. (* make sure result.op is a register *)
  6993. tmp := result.op;
  6994. ReuseCopy(result.op, result.op);
  6995. ReleaseIntermediateOperand(tmp);
  6996. (* add offset to result.op *)
  6997. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6998. END
  6999. (* ---- RE ---- *)
  7000. |Global.Re:
  7001. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  7002. Designate(p0, result)
  7003. ELSE
  7004. Evaluate(p0, result)
  7005. END
  7006. (* ---- IM ---- *)
  7007. |Global.Im:
  7008. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  7009. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  7010. Designate(p0, result);
  7011. (* make sure result.op is a register *)
  7012. tmp := result.op;
  7013. ReuseCopy(result.op, result.op);
  7014. ReleaseIntermediateOperand(tmp);
  7015. (* add offset to result.op *)
  7016. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7017. (* ---- ABS ----- *)
  7018. |Global.Abs:
  7019. Evaluate(p0,operand);
  7020. type := p0.type.resolved;
  7021. InitOperand(result,ModeValue);
  7022. Reuse1a(result.op,operand.op,dest);
  7023. Emit(Abs(position,result.op,operand.op));
  7024. ReleaseOperand(operand);
  7025. (* ---- WAIT ----- *)
  7026. |Global.Wait:
  7027. Evaluate(p0,operand);
  7028. Emit(Push(position,operand.op));
  7029. ReleaseOperand(operand);
  7030. CallThis(position,"Activities","Wait", 1);
  7031. (* ---- NEW ----- *)
  7032. |Global.New:
  7033. (*! the following code is only correct for "standard" Oberon calling convention *)
  7034. IF x.type # NIL THEN
  7035. type := x.type.resolved;
  7036. firstPar := 0;
  7037. ELSE
  7038. type := p0.type.resolved;
  7039. firstPar := 1;
  7040. END;
  7041. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7042. THEN
  7043. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7044. IF backend.cooperative THEN
  7045. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7046. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7047. IF recordType.isObject THEN
  7048. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7049. IF recordType.IsActive() THEN
  7050. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7051. END;
  7052. IF recordType.IsProtected() THEN
  7053. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7054. END;
  7055. ELSE
  7056. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7057. END;
  7058. END;
  7059. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7060. CallThis(position,"Runtime","New", 1);
  7061. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7062. Emit(Result(position, pointer));
  7063. exit := NewLabel();
  7064. BreqL(exit,pointer,nil);
  7065. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7066. GetRecordTypeName (recordType,name);
  7067. IF ~recordType.isObject THEN
  7068. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7069. END;
  7070. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7071. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7072. IF recordType.isObject THEN
  7073. IF recordType.IsProtected() THEN
  7074. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7075. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7076. END;
  7077. IF recordType.IsActive() THEN
  7078. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7079. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7080. END;
  7081. END;
  7082. END;
  7083. (* initialize fields *)
  7084. IF type(SyntaxTree.PointerType).isPlain THEN
  7085. size := 0;
  7086. ELSIF recordType.isObject THEN
  7087. size := BaseObjectTypeSize;
  7088. ELSE
  7089. size := BaseRecordTypeSize;
  7090. END;
  7091. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7092. (* call initializer *)
  7093. constructor := GetConstructor(recordType);
  7094. IF constructor # NIL THEN
  7095. (*! should be unified with ProcedureCallDesignator *)
  7096. Emit(Push(position,pointer));
  7097. ReleaseIntermediateOperand(pointer);
  7098. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7099. FOR i := firstPar TO x.parameters.Length()-1 DO
  7100. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7101. formalParameter := formalParameter.nextParameter;
  7102. END;
  7103. (* static call of the constructor *)
  7104. GetCodeSectionNameForSymbol(constructor,name);
  7105. ASSERT(~constructor.isInline);
  7106. IF constructor.scope.ownerModule # module.module THEN
  7107. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7108. ELSE
  7109. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7110. END;
  7111. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7112. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7113. Emit(Pop(position,pointer));
  7114. END;
  7115. (* call bodies *)
  7116. CallBodies(pointer,type);
  7117. SetLabel(exit);
  7118. needsTrace := p0.NeedsTrace();
  7119. IF needsTrace THEN ModifyAssignments(true) END;
  7120. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7121. Emit(Push(position, pointer));
  7122. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7123. Emit(Pop(position, pointer));
  7124. END;
  7125. Designate(p0,l);
  7126. IF needsTrace THEN
  7127. CallAssignPointer(l.op, pointer);
  7128. ELSE
  7129. ToMemory(l.op,addressType,0);
  7130. Emit(Mov(position,l.op,pointer));
  7131. END;
  7132. ReleaseIntermediateOperand(pointer);
  7133. ReleaseOperand(l);
  7134. IF needsTrace THEN ModifyAssignments(false) END;
  7135. ELSE (* not cooperative backend *)
  7136. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7137. IF temporaryVariable # NIL THEN
  7138. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7139. ELSE
  7140. Designate(p0,l);
  7141. END;
  7142. (* l.op contains address of pointer to record *)
  7143. Emit(Push(position,l.op)); (* address for use after syscall *)
  7144. Emit(Push(position,l.op));
  7145. ReleaseOperand(l);
  7146. (* push type descriptor *)
  7147. reg := TypeDescriptorAdr(recordType);
  7148. IF ~newObjectFile THEN
  7149. IntermediateCode.MakeMemory(reg,addressType);
  7150. END;
  7151. Emit(Push(position,reg));
  7152. ReleaseIntermediateOperand(reg);
  7153. (* push realtime flag *)
  7154. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7155. ELSE Emit(Push(position,false));
  7156. END;
  7157. CallThis(position,"Heaps","NewRec", 3);
  7158. (* check allocation success, if not successful then do not call initializers and bodies *)
  7159. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7160. Emit(Pop(position,pointer));
  7161. MakeMemory(reg,pointer,addressType,0);
  7162. ReleaseIntermediateOperand(pointer);
  7163. pointer := reg;
  7164. exit := NewLabel();
  7165. BreqL(exit,pointer,nil);
  7166. Emit(Push(position,pointer));
  7167. (* initialize fields *)
  7168. InitFields(recordType, pointer,0);
  7169. (* call initializer *)
  7170. constructor := GetConstructor(recordType);
  7171. IF constructor # NIL THEN
  7172. (*! should be unified with ProcedureCallDesignator *)
  7173. Emit(Push(position,pointer));
  7174. ReleaseIntermediateOperand(pointer);
  7175. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7176. FOR i := firstPar TO x.parameters.Length()-1 DO
  7177. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7178. formalParameter := formalParameter.nextParameter;
  7179. END;
  7180. (* static call of the constructor *)
  7181. GetCodeSectionNameForSymbol(constructor,name);
  7182. ASSERT(~constructor.isInline);
  7183. IF constructor.scope.ownerModule # module.module THEN
  7184. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7185. ELSE
  7186. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7187. END;
  7188. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7189. ELSE
  7190. ReleaseIntermediateOperand(pointer);
  7191. END;
  7192. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7193. Emit(Pop(position,pointer));
  7194. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7195. Designate(p0,l);
  7196. ToMemory(l.op,addressType,0);
  7197. Emit(Mov(position,l.op,pointer));
  7198. ReleaseOperand(l);
  7199. result.tag := emptyOperand;
  7200. ELSIF (x.type # NIL) THEN
  7201. result := l; (* temporary variable is the result of NEW Type() *)
  7202. END;
  7203. (* call bodies *)
  7204. CallBodies(pointer,type);
  7205. ReleaseIntermediateOperand(pointer);
  7206. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7207. end := NewLabel();
  7208. BrL(end);
  7209. SetLabel(exit);
  7210. Designate(p0,l);
  7211. ToMemory(l.op,addressType,0);
  7212. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7213. ReleaseOperand(l);
  7214. SetLabel(end);
  7215. ELSE
  7216. SetLabel(exit);
  7217. END;
  7218. END;
  7219. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7220. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7221. dim := 0;
  7222. IntermediateCode.InitOperand(reg);
  7223. IF p1 # NIL THEN
  7224. FOR i := firstPar TO x.parameters.Length()-1 DO
  7225. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7226. parameter := x.parameters.GetExpression(i);
  7227. Evaluate(parameter,r);
  7228. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7229. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7230. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7231. END;
  7232. Emit(Push(position,r.op));
  7233. IF i=1 THEN
  7234. CopyInt(reg,r.op);
  7235. ELSE
  7236. MulInt(reg, reg, r.op);
  7237. END;
  7238. ReleaseOperand(r);
  7239. INC(dim);
  7240. END;
  7241. Convert(reg,addressType);
  7242. ELSE
  7243. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7244. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7245. END;
  7246. openDim := dim;
  7247. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7248. IF backend.cooperative THEN
  7249. size := ToMemoryUnits(system,system.SizeOf(type));
  7250. WHILE type IS SyntaxTree.ArrayType DO
  7251. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7252. END;
  7253. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7254. IF (size # 1) THEN
  7255. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7256. END;
  7257. Emit(Push(position,reg));
  7258. size := ToMemoryUnits(system,system.SizeOf(type));
  7259. IF (size # 1) THEN
  7260. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7261. END;
  7262. AddInt(reg, reg, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize)));
  7263. (*Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));*)
  7264. Emit(Push(position,reg));
  7265. ReleaseIntermediateOperand(reg);
  7266. CallThis(position,"Runtime","New", 1);
  7267. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7268. Emit(Result(position, pointer));
  7269. exit := NewLabel();
  7270. else := NewLabel();
  7271. BreqL(else,pointer,nil);
  7272. IF ~type.hasPointers THEN
  7273. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7274. ELSIF type IS SyntaxTree.RecordType THEN
  7275. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7276. ELSIF type IS SyntaxTree.ProcedureType THEN
  7277. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7278. ELSE
  7279. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7280. END;
  7281. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7282. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7283. 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))));
  7284. IF type IS SyntaxTree.RecordType THEN
  7285. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7286. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7287. ELSE
  7288. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7289. END;
  7290. i := openDim;
  7291. WHILE i > 0 DO
  7292. DEC (i);
  7293. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7294. END;
  7295. needsTrace := p0.NeedsTrace();
  7296. IF needsTrace THEN ModifyAssignments(true) END;
  7297. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7298. Emit(Push(position, pointer));
  7299. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7300. Emit(Pop(position, pointer));
  7301. END;
  7302. Designate(p0,l);
  7303. IF needsTrace THEN
  7304. CallAssignPointer(l.op, pointer);
  7305. ModifyAssignments(false);
  7306. ELSE
  7307. ToMemory(l.op,addressType,0);
  7308. Emit(Mov(position,l.op,pointer));
  7309. END;
  7310. ReleaseIntermediateOperand(pointer);
  7311. ReleaseOperand(l);
  7312. BrL(exit);
  7313. SetLabel(else);
  7314. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7315. Designate(p0,l);
  7316. IF needsTrace THEN
  7317. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7318. ELSE
  7319. ToMemory(l.op,addressType,0);
  7320. Emit(Mov(position,l.op,pointer));
  7321. END;
  7322. ReleaseOperand(l);
  7323. SetLabel(exit);
  7324. ELSE
  7325. (*! the following code is only correct for "standard" Oberon calling convention *)
  7326. IF SemanticChecker.ContainsPointer(type) THEN
  7327. IF type IS SyntaxTree.ArrayType THEN
  7328. staticLength := 1;
  7329. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7330. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7331. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7332. END;
  7333. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7334. MulInt(reg,reg,tmp);
  7335. END;
  7336. Designate(p0,l);
  7337. IF openDim > 0 THEN
  7338. Emit(Push(position,l.op)); (* address for use after syscall *)
  7339. END;
  7340. Emit(Push(position,l.op)); (* address *)
  7341. ReleaseOperand(l);
  7342. tmp := TypeDescriptorAdr(type);
  7343. IF ~newObjectFile THEN
  7344. IntermediateCode.MakeMemory(tmp,addressType);
  7345. END;
  7346. Emit(Push(position,tmp)); (* type descriptor *)
  7347. ReleaseIntermediateOperand(tmp);
  7348. Emit(Push(position,reg)); (* number Elements *)
  7349. ReleaseIntermediateOperand(reg);
  7350. tmp := IntermediateCode.Immediate(addressType,dim);
  7351. Emit(Push(position,tmp)); (* dimensions *)
  7352. (* push realtime flag *)
  7353. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7354. ELSE Emit(Push(position,false));
  7355. END;
  7356. CallThis(position,"Heaps","NewArr",5)
  7357. ELSE
  7358. size := ToMemoryUnits(system,system.SizeOf(type));
  7359. IF (size # 1) THEN
  7360. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7361. (*
  7362. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7363. *)
  7364. END;
  7365. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7366. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7367. AddInt(reg, reg, tmp);
  7368. (*
  7369. Emit(Add(position,reg,reg,tmp));
  7370. *)
  7371. Designate(p0,l);
  7372. IF openDim >0 THEN
  7373. Emit(Push(position,l.op)); (* address for use after syscall *)
  7374. END;
  7375. Emit(Push(position,l.op)); (* address for syscall *)
  7376. ReleaseOperand(l); (* pointer address *)
  7377. Emit(Push(position,reg)); (* size *)
  7378. ReleaseIntermediateOperand(reg);
  7379. (* push realtime flag *)
  7380. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7381. ELSE Emit(Push(position,false));
  7382. END;
  7383. CallThis(position,"Heaps","NewSys", 3)
  7384. END;
  7385. IF openDim > 0 THEN
  7386. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7387. Emit(Pop(position,adr));
  7388. ToMemory(adr,addressType,0);
  7389. ReuseCopy(tmp,adr);
  7390. ReleaseIntermediateOperand(adr);
  7391. adr := tmp;
  7392. else := NewLabel();
  7393. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7394. i := openDim-1;
  7395. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7396. WHILE (i >= 0) DO
  7397. Emit(Pop(position,reg));
  7398. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7399. Emit(Mov(position,res,reg));
  7400. DEC(i);
  7401. END;
  7402. ReleaseIntermediateOperand(adr);
  7403. ReleaseIntermediateOperand(reg);
  7404. exit := NewLabel();
  7405. BrL(exit);
  7406. SetLabel(else);
  7407. (* else part: array could not be allocated *)
  7408. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7409. Emit(Add(position,sp,sp,tmp));
  7410. SetLabel(exit);
  7411. END;
  7412. END;
  7413. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7414. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7415. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7416. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7417. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7418. left.SetType(procedure.type);
  7419. formalParameter := procedureType.firstParameter;
  7420. (* push array to allocate *)
  7421. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7422. formalParameter :=formalParameter.nextParameter;
  7423. (* push length array *)
  7424. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7425. (* push size *)
  7426. type := t0;
  7427. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7428. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7429. END;
  7430. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7431. Emit(Push(position,tmp));
  7432. (* *)
  7433. IF SemanticChecker.ContainsPointer(type) THEN
  7434. tmp := TypeDescriptorAdr(type);
  7435. IF ~newObjectFile THEN
  7436. IntermediateCode.MakeMemory(tmp,addressType);
  7437. END;
  7438. ELSE
  7439. tmp := IntermediateCode.Immediate(addressType, 0);
  7440. END;
  7441. Emit(Push(position,tmp)); (* type descriptor *)
  7442. StaticCallOperand(result,procedure);
  7443. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7444. ReleaseOperand(result);
  7445. END;
  7446. (*
  7447. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7448. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7449. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7450. *)
  7451. ELSE
  7452. dim := 0;
  7453. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7454. (* generate geometry descriptor *)
  7455. Designate(p0,l);
  7456. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7457. ReleaseOperand(l);
  7458. isTensor := TRUE;
  7459. ELSE
  7460. isTensor := FALSE;
  7461. END;
  7462. FOR i := firstPar TO x.parameters.Length()-1 DO
  7463. IF ~isTensor THEN
  7464. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7465. END;
  7466. parameter := x.parameters.GetExpression(i);
  7467. Evaluate(parameter,r);
  7468. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7469. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7470. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7471. END;
  7472. Emit(Push(position,r.op));
  7473. IF i=1 THEN
  7474. CopyInt(reg, r.op);
  7475. ELSE
  7476. MulInt(reg, reg, r.op);
  7477. END;
  7478. ReleaseOperand(r);
  7479. INC(dim);
  7480. END;
  7481. Convert(reg,addressType);
  7482. openDim := dim;
  7483. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7484. (*! the following code is only correct for "standard" Oberon calling convention *)
  7485. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7486. t := type;
  7487. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7488. staticLength := 1;
  7489. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7490. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7491. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7492. END;
  7493. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7494. MulInt(reg,reg,tmp);
  7495. END;
  7496. Designate(p0,l);
  7497. IF isTensor THEN
  7498. Dereference(l,type,FALSE);
  7499. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7500. END;
  7501. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7502. Emit(Push(position,l.tag)); (* address *)
  7503. ReleaseOperand(l);
  7504. tmp := TypeDescriptorAdr(t);
  7505. IF ~newObjectFile THEN
  7506. IntermediateCode.MakeMemory(tmp,addressType);
  7507. END;
  7508. Emit(Push(position,tmp)); (* type descriptor *)
  7509. ReleaseIntermediateOperand(tmp);
  7510. Emit(Push(position,reg)); (* number Elements *)
  7511. ReleaseIntermediateOperand(reg);
  7512. tmp := IntermediateCode.Immediate(addressType,0);
  7513. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7514. (* push realtime flag: false by default *)
  7515. Emit(Push(position,false));
  7516. CallThis(position,"Heaps","NewArr",5);
  7517. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7518. Emit(Pop(position,adr));
  7519. GetMathArrayField(tmp,adr,MathPtrOffset);
  7520. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7521. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7522. PutMathArrayField(adr,reg,MathAdrOffset);
  7523. ReleaseIntermediateOperand(tmp);
  7524. ReleaseIntermediateOperand(reg);
  7525. ELSE
  7526. IF isTensor THEN
  7527. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7528. ELSE
  7529. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7530. END;
  7531. IF (size # 1) THEN
  7532. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7533. END;
  7534. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7535. AddInt(reg,reg,tmp);
  7536. Designate(p0,l);
  7537. IF isTensor THEN
  7538. Dereference(l,type,FALSE);
  7539. END;
  7540. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7541. Emit(Push(position,l.tag)); (* address for syscall *)
  7542. ReleaseOperand(l); (* pointer address *)
  7543. Emit(Push(position,reg)); (* size *)
  7544. ReleaseIntermediateOperand(reg);
  7545. (* push realtime flag: false by default *)
  7546. Emit(Push(position,false));
  7547. CallThis(position,"Heaps","NewSys",3);
  7548. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7549. Emit(Pop(position,adr));
  7550. GetMathArrayField(tmp,adr,MathPtrOffset);
  7551. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7552. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7553. PutMathArrayField(adr,reg,MathAdrOffset);
  7554. ReleaseIntermediateOperand(tmp);
  7555. ReleaseIntermediateOperand(reg);
  7556. END;
  7557. flags := {};
  7558. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7559. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7560. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7561. PutMathArrayField(adr,tmp,MathDimOffset);
  7562. else := NewLabel();
  7563. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7564. i := openDim-1;
  7565. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7566. IF isTensor THEN
  7567. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7568. ELSE
  7569. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7570. END;
  7571. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7572. WHILE (i >= 0) DO
  7573. Emit(Pop(position,reg));
  7574. PutMathArrayLength(adr,reg,i);
  7575. PutMathArrayIncrement(adr,tmp,i);
  7576. IF i > 0 THEN
  7577. IF i=openDim-1 THEN
  7578. CopyInt(tmp,tmp);
  7579. END;
  7580. MulInt(tmp,tmp,reg);
  7581. END;
  7582. DEC(i);
  7583. END;
  7584. ReleaseIntermediateOperand(adr);
  7585. ReleaseIntermediateOperand(reg);
  7586. ReleaseIntermediateOperand(tmp);
  7587. exit := NewLabel();
  7588. BrL(exit);
  7589. SetLabel(else);
  7590. (* else part: array could not be allocated *)
  7591. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7592. Emit(Add(position,sp,sp,tmp));
  7593. SetLabel(exit);
  7594. END;
  7595. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7596. THEN
  7597. IF ~backend.cellsAreObjects THEN RETURN END;
  7598. IF InCellScope(currentScope) THEN
  7599. PushSelfPointer()
  7600. ELSE
  7601. Emit(Push(position, nil));
  7602. END;
  7603. (* push temp address *)
  7604. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7605. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7606. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7607. (* l.op contains address of pointer to record *)
  7608. Emit(Push(position,l.op)); (* address for use after syscall *)
  7609. ReleaseOperand(l);
  7610. (* push type descriptor *)
  7611. reg := TypeDescriptorAdr(baseType);
  7612. IF ~newObjectFile THEN
  7613. IntermediateCode.MakeMemory(reg,addressType);
  7614. END;
  7615. Emit(Push(position,reg));
  7616. ReleaseIntermediateOperand(reg);
  7617. (* push name *)
  7618. (*Global.GetSymbolName(p0, n);*)
  7619. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7620. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7621. ELSE
  7622. Global.GetModuleName(module.module, n);
  7623. END;
  7624. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7625. (*type.typeDeclaration.GetName(n);*)
  7626. PushConstString(n);
  7627. (* push cellnet boolean *)
  7628. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7629. (* push engine boolean *)
  7630. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7631. (* allocate *)
  7632. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7633. (* add capabilities *)
  7634. modifier := p0(SyntaxTree.Designator).modifiers;
  7635. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7636. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7637. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7638. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7639. END;
  7640. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7641. (*
  7642. modifier := baseType(SyntaxTree.CellType).modifiers;
  7643. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7644. modifier := p0(SyntaxTree.Designator).modifiers;
  7645. *)
  7646. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7647. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7648. (* l.op contains address of pointer to record *)
  7649. ToMemory(l.op,addressType,0);
  7650. (* l.op contains value of pointer to record *)
  7651. Emit(Push(position,l.op)); (* address for use after syscall *)
  7652. ReleaseOperand(l);
  7653. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7654. prevScope := currentScope;
  7655. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7656. previous := section;
  7657. section := init;
  7658. (* add ports *)
  7659. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7660. CloseInitializer(previous);
  7661. currentScope := prevScope;
  7662. Symbol(temporaryVariable,l);
  7663. ToMemory(l.op,addressType,0);
  7664. Emit(Push(position,l.op));
  7665. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7666. (*
  7667. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7668. IF constructor # NIL THEN
  7669. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7670. FOR i := 1 TO x.parameters.Length()-1 DO
  7671. p := x.parameters.GetExpression(i);
  7672. Global.GetSymbolName(parameter,name);
  7673. Evaluate(p, value);
  7674. ASSERT(value.type # NIL);
  7675. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7676. par := instance.AddParameter(name);
  7677. par.SetInteger(value.integer);
  7678. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7679. par := instance.AddParameter(name);
  7680. par.SetBoolean(value.boolean);
  7681. ELSE Error(x.position,NotYetImplemented)
  7682. END;
  7683. parameter := parameter.nextParameter
  7684. END;
  7685. END;
  7686. *)
  7687. (* call initializer *)
  7688. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7689. IF constructor # NIL THEN
  7690. (*! should be unified with ProcedureCallDesignator *)
  7691. IF backend.cellsAreObjects THEN
  7692. Symbol(temporaryVariable,l);
  7693. ToMemory(l.op,addressType,0);
  7694. Emit(Push(position,l.op));
  7695. ReleaseOperand(l);
  7696. END;
  7697. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7698. FOR i := firstPar TO x.parameters.Length()-1 DO
  7699. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7700. formalParameter := formalParameter.nextParameter;
  7701. END;
  7702. (* static call of the constructor *)
  7703. Global.GetSymbolSegmentedName(constructor,name);
  7704. ASSERT(~constructor.isInline);
  7705. IF constructor.scope.ownerModule # module.module THEN
  7706. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7707. ELSE
  7708. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7709. END;
  7710. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7711. (*ELSE
  7712. ReleaseIntermediateOperand(pointer);*)
  7713. END;
  7714. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7715. ToMemory(l.op, addressType, 0);
  7716. Designate(p0,s0);
  7717. ToMemory(s0.op,addressType,0);
  7718. Emit(Mov(position,s0.op,l.op));
  7719. ReleaseOperand(l);
  7720. ReleaseOperand(s0);
  7721. result.tag := emptyOperand;
  7722. (* start *)
  7723. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7724. (* push cell *)
  7725. Symbol(temporaryVariable, l);
  7726. ToMemory(l.op,addressType,0);
  7727. Emit(Push(Basic.invalidPosition,l.op));
  7728. (* push delegate *)
  7729. Emit(Push(Basic.invalidPosition,l.op));
  7730. ReleaseOperand(l);
  7731. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7732. Emit(Push(position, s1.op));
  7733. ReleaseOperand(s1);
  7734. CallThis(position,"ActiveCellsRuntime","Start",3);
  7735. END;
  7736. (*IF temporaryVariable # NIL THEN
  7737. end := NewLabel();
  7738. BrL(end);
  7739. SetLabel(exit);
  7740. Designate(p0,l);
  7741. ToMemory(l.op,addressType,0);
  7742. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7743. ReleaseOperand(l);
  7744. SetLabel(end);
  7745. ELSE
  7746. SetLabel(exit);
  7747. END;
  7748. *)
  7749. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7750. ELSE (* no pointer to record, no pointer to array *)
  7751. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7752. (* ignore new statement *)
  7753. Warning(p0.position, "cannot run on final hardware");
  7754. ELSE
  7755. HALT(200);
  7756. END;
  7757. END;
  7758. (* ---- ADDRESSOF----- *)
  7759. |Global.systemAdr:
  7760. Designate(p0,s0);
  7761. s0.mode := ModeValue;
  7762. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7763. ReleaseIntermediateOperand(s0.op);
  7764. s0.op := s0.tag;
  7765. IntermediateCode.InitOperand(s0.tag);
  7766. END;
  7767. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7768. result := s0;
  7769. (* ---- BIT ----- *)
  7770. |Global.systemBit:
  7771. Evaluate(p0,s0);
  7772. ToMemory(s0.op,addressType,0);
  7773. ReuseCopy(res,s0.op);
  7774. ReleaseOperand(s0);
  7775. Evaluate(p1,s1);
  7776. Emit(Ror(position,res,res,s1.op));
  7777. ReleaseOperand(s1);
  7778. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7779. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7780. IF ~conditional THEN
  7781. InitOperand(result,ModeValue); result.op := res;
  7782. ELSE
  7783. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7784. BrL(falseLabel);
  7785. ReleaseIntermediateOperand(res);
  7786. END;
  7787. (* --- MSK ----*)
  7788. |Global.systemMsk:
  7789. Evaluate(p0,s0);
  7790. Evaluate(p1,s1);
  7791. ReuseCopy(res,s0.op);
  7792. ReleaseOperand(s0);
  7793. Emit(And(position,res,res,s1.op));
  7794. ReleaseOperand(s1);
  7795. InitOperand(result,ModeValue);
  7796. result.op := res;
  7797. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7798. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7799. Evaluate(p0,s0);
  7800. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7801. ReleaseOperand(s0);
  7802. InitOperand(result,ModeValue);
  7803. result.op := res;
  7804. (* ---- SYSTEM.GetStackPointer ----- *)
  7805. |Global.systemGetStackPointer:
  7806. InitOperand(result,ModeValue);
  7807. result.op := sp;
  7808. (* ---- SYSTEM.GetFramePointer ----- *)
  7809. |Global.systemGetFramePointer:
  7810. InitOperand(result,ModeValue);
  7811. result.op := fp;
  7812. (* ---- SYSTEM.GetActivity ----- *)
  7813. |Global.systemGetActivity:
  7814. ASSERT(backend.cooperative);
  7815. InitOperand(result,ModeValue);
  7816. result.op := ap;
  7817. (* ---- SYSTEM.SetStackPointer ----- *)
  7818. |Global.systemSetStackPointer:
  7819. Evaluate(p0,s0); (* *)
  7820. Emit(Mov(position,sp,s0.op));
  7821. ReleaseOperand(s0);
  7822. (* ---- SYSTEM.SetFramePointer ----- *)
  7823. |Global.systemSetFramePointer:
  7824. Evaluate(p0,s0); (* *)
  7825. Emit(Mov(position,fp,s0.op));
  7826. ReleaseOperand(s0);
  7827. (* ---- SYSTEM.Activity ----- *)
  7828. |Global.systemSetActivity:
  7829. ASSERT(backend.cooperative);
  7830. Evaluate(p0,s0); (* *)
  7831. Emit(Mov(position,ap,s0.op));
  7832. ReleaseOperand(s0);
  7833. (* ---- SYSTEM.VAL ----- *)
  7834. |Global.systemVal:
  7835. Expression(p1);
  7836. s1 := result;
  7837. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7838. IF s1.mode = ModeReference THEN
  7839. (* nothing to be done if not record type, just take over new type *)
  7840. IF (type IS SyntaxTree.RecordType) THEN
  7841. ReleaseIntermediateOperand(s1.tag);
  7842. s1.tag := TypeDescriptorAdr(type);
  7843. IF ~newObjectFile THEN
  7844. IntermediateCode.MakeMemory(s1.tag,addressType);
  7845. END;
  7846. UseIntermediateOperand(s1.tag);
  7847. END;
  7848. result := s1;
  7849. ELSE (* copy over result to different type, may not use convert *)
  7850. itype := IntermediateCode.GetType(system,type);
  7851. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7852. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7853. Emit(Mov(position,s0.op,s1.op));
  7854. ReleaseOperand(s1);
  7855. InitOperand(result,ModeValue);
  7856. result.op := s0.op;
  7857. ELSE (* different size, must convert *)
  7858. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7859. Convert(s1.op, IntermediateCode.GetType(system,type));
  7860. result := s1;
  7861. END;
  7862. END;
  7863. (* ---- SYSTEM.GET ----- *)
  7864. |Global.systemGet:
  7865. Evaluate(p0,s0); (* adr *)
  7866. Designate(p1,s1); (* variable *)
  7867. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7868. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7869. Emit(Mov(position,s1.op,s0.op));
  7870. ReleaseOperand(s1);
  7871. ReleaseOperand(s0);
  7872. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7873. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7874. Evaluate(p0,s0); (* *)
  7875. Evaluate(p1,s1); (* variable *)
  7876. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7877. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7878. (* real part *)
  7879. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7880. Emit(Mov(position,res, s1.op));
  7881. ReleaseIntermediateOperand(res);
  7882. (* imaginary part *)
  7883. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7884. Emit(Mov(position,res, s1.tag));
  7885. ReleaseIntermediateOperand(res);
  7886. ReleaseOperand(s1);
  7887. ReleaseOperand(s0);
  7888. ELSE
  7889. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7890. ReleaseOperand(s0);
  7891. Emit(Mov(position,res,s1.op));
  7892. ReleaseIntermediateOperand(res);
  7893. ReleaseOperand(s1);
  7894. END;
  7895. (* ---- SYSTEM.MOVE ----- *)
  7896. |Global.systemMove:
  7897. Evaluate(p0,s0);
  7898. Evaluate(p1,s1);
  7899. Evaluate(p2,s2);
  7900. Emit(Copy(position,s1.op,s0.op,s2.op));
  7901. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7902. (* ---- SYSTEM.NEW ----- *)
  7903. |Global.systemNew:
  7904. Designate(p0,s0);
  7905. Emit(Push(position,s0.op));
  7906. ReleaseOperand(s0);
  7907. Evaluate(p1,s1);
  7908. Emit(Push(position,s1.op));
  7909. ReleaseOperand(s1);
  7910. (* push realtime flag: false by default *)
  7911. Emit(Push(position,false));
  7912. CallThis(position,"Heaps","NewSys",3);
  7913. (* ---- SYSTEM.CALL ----- *)
  7914. |Global.systemRef:
  7915. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7916. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7917. s0.mode := ModeValue;
  7918. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7919. result := s0
  7920. (* ---- INCR ----- *)
  7921. |Global.Incr:
  7922. Designate(p0,operand);
  7923. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7924. Dereference(operand,p0.type.resolved,FALSE);
  7925. END;
  7926. ASSERT(p1 # NIL);
  7927. Evaluate(p1,l);
  7928. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7929. ReleaseOperand(operand); ReleaseOperand(l);
  7930. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7931. (* ---- SUM ----- *)
  7932. |Global.Sum: HALT(200);
  7933. (* ---- ALL ----- *)
  7934. |Global.All: HALT(200);
  7935. (* ---- CAS ----- *)
  7936. |Global.Cas:
  7937. needsTrace := p0.NeedsTrace();
  7938. IF needsTrace THEN ModifyAssignments(true) END;
  7939. Designate(p0,s0);
  7940. Evaluate(p1,s1);
  7941. Evaluate(p2,s2);
  7942. IF needsTrace THEN
  7943. Emit(Push(position, s0.op));
  7944. Emit(Push(position, s1.op));
  7945. Emit(Push(position, s2.op));
  7946. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7947. ELSE
  7948. Emit(Cas(position,s0.op,s1.op,s2.op));
  7949. END;
  7950. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7951. IF needsTrace THEN ModifyAssignments(false) END;
  7952. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7953. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7954. IF conditional THEN
  7955. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7956. BrL(falseLabel);
  7957. ReleaseIntermediateOperand(res);
  7958. END;
  7959. (* ---- DIM ----- *)
  7960. |Global.Dim:
  7961. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7962. Designate(p0,s0);
  7963. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7964. Dereference(s0,p0.type.resolved,FALSE);
  7965. END;
  7966. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7967. ReleaseOperand(s0);
  7968. (* ---- RESHAPE ----- *)
  7969. |Global.Reshape:
  7970. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7971. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7972. left.SetType(procedure.type);
  7973. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7974. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7975. END;
  7976. (* ---- SYSTEM.TYPECODE ----- *)
  7977. |Global.systemTypeCode:
  7978. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7979. IF type.resolved IS SyntaxTree.PointerType THEN
  7980. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7981. END;
  7982. result.op := TypeDescriptorAdr(type);
  7983. IF ~newObjectFile THEN
  7984. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7985. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7986. END;
  7987. result.mode := ModeValue;
  7988. (* ---- SYSTEM.TRACE ----- *)
  7989. |Global.systemTrace:
  7990. SystemTrace(x.parameters, x.position);
  7991. (* ----- CONNECT ------*)
  7992. |Global.Connect:
  7993. IF backend.cellsAreObjects THEN
  7994. PushPort(p0);
  7995. PushPort(p1);
  7996. IF p2 # NIL THEN
  7997. Evaluate(p2, s2);
  7998. Emit(Push(p2.position, s2.op));
  7999. ReleaseOperand(s2);
  8000. ELSE
  8001. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  8002. END;
  8003. CallThis(position,"ActiveCellsRuntime","Connect",3);
  8004. ELSE
  8005. Warning(x.position, "cannot run on final hardware");
  8006. END;
  8007. (* ----- DELEGATE ------*)
  8008. |Global.Delegate:
  8009. IF backend.cellsAreObjects THEN
  8010. PushPort(p0);
  8011. PushPort(p1);
  8012. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  8013. ELSE
  8014. Warning(x.position, "cannot run on final hardware");
  8015. END;
  8016. (* ----- SEND ------*)
  8017. |Global.Send:
  8018. Evaluate(p0,s0);
  8019. Evaluate(p1,s1);
  8020. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8021. Emit(Push(position,s0.op));
  8022. Emit(Push(position,s1.op));
  8023. (*
  8024. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8025. *)
  8026. IF ~backend.cellsAreObjects THEN
  8027. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8028. END;
  8029. ReleaseOperand(s0);
  8030. ReleaseOperand(s1);
  8031. IF backend.cellsAreObjects THEN
  8032. CallThis(position,"ActiveCellsRuntime","Send",2);
  8033. ELSE
  8034. CallThis(position,ChannelModuleName,"Send",2);
  8035. END;
  8036. (* ----- RECEIVE ------*)
  8037. |Global.Receive:
  8038. Evaluate(p0,s0);
  8039. Emit(Push(position,s0.op));
  8040. Designate(p1,s1);
  8041. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8042. Emit(Push(position,s1.op));
  8043. IF p2 # NIL THEN
  8044. Designate(p2,s2);
  8045. Emit(Push(position,s2.op));
  8046. END;
  8047. (*
  8048. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8049. *)
  8050. IF ~backend.cellsAreObjects THEN
  8051. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8052. END;
  8053. ReleaseOperand(s0);
  8054. ReleaseOperand(s1);
  8055. ReleaseOperand(s2);
  8056. IF backend.cellsAreObjects THEN
  8057. IF p2 = NIL THEN
  8058. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8059. ELSE
  8060. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8061. END;
  8062. ELSE
  8063. IF p2 = NIL THEN
  8064. CallThis(position,ChannelModuleName,"Receive",2)
  8065. ELSE
  8066. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8067. END;
  8068. END;
  8069. | Global.systemSpecial:
  8070. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8071. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8072. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8073. (* determine if parameters are of the VAR kind *)
  8074. ASSERT(x.parameters.Length() <= 3);
  8075. formalParameter := procedureType.firstParameter;
  8076. FOR i := 0 TO x.parameters.Length() - 1 DO
  8077. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8078. formalParameter := formalParameter.nextParameter
  8079. END;
  8080. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8081. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8082. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8083. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8084. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8085. IF procedureType.returnType # NIL THEN
  8086. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8087. Emit(Result(position, res));
  8088. (*InitOperand(result,ModeValue);
  8089. result.op := res;
  8090. *)
  8091. IF ~conditional THEN
  8092. InitOperand(result,ModeValue); result.op := res;
  8093. ELSE
  8094. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8095. BrL(falseLabel);
  8096. ReleaseIntermediateOperand(res);
  8097. END;
  8098. END
  8099. ELSE (* function not yet implemented *)
  8100. Error(position,"not yet implemented");
  8101. END;
  8102. destination := dest;
  8103. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8104. END VisitBuiltinCallDesignator;
  8105. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8106. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8107. BEGIN
  8108. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8109. dest := destination; destination := emptyOperand;
  8110. Expression(x.left);
  8111. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8112. ELSIF isUnchecked THEN (* no check *)
  8113. ELSE
  8114. trueL := NewLabel();
  8115. falseL := NewLabel();
  8116. IF IsPointerToRecord(x.left.type,recordType) THEN
  8117. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8118. Emit(Mov(position,tag, result.op));
  8119. IF result.mode # ModeValue THEN
  8120. ptr := tag;
  8121. IntermediateCode.MakeMemory(ptr,addressType);
  8122. Emit(Mov(position,tag, ptr));
  8123. END;
  8124. IF ~backend.cooperative THEN
  8125. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8126. END;
  8127. IntermediateCode.MakeMemory(tag,addressType);
  8128. ELSE
  8129. tag := result.tag;
  8130. UseIntermediateOperand(tag);
  8131. END;
  8132. TypeTest(tag,x.type,trueL,falseL);
  8133. ReleaseIntermediateOperand(tag);
  8134. SetLabel(falseL);
  8135. EmitTrap(position,TypeCheckTrap);
  8136. SetLabel(trueL);
  8137. END;
  8138. destination := dest;
  8139. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8140. END VisitTypeGuardDesignator;
  8141. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8142. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8143. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8144. VAR label: Label; pc: LONGINT;
  8145. BEGIN
  8146. IF backend.cooperative & ~isUnchecked THEN
  8147. pc := section.pc;
  8148. label := NewLabel();
  8149. BrneL(label, operand.op, nil);
  8150. EmitTrap(position, NilPointerTrap);
  8151. SetLabel(label);
  8152. INC(statCoopNilCheck, section.pc - pc);
  8153. END;
  8154. END NilCheck;
  8155. BEGIN
  8156. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8157. ReuseCopy(dereferenced,operand.op);
  8158. ReleaseOperand(operand);
  8159. operand.mode := ModeReference;
  8160. operand.op := dereferenced;
  8161. operand.tag := dereferenced;
  8162. UseIntermediateOperand(operand.tag);
  8163. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8164. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8165. ReleaseIntermediateOperand(operand.tag);
  8166. operand.tag := TypeDescriptorAdr(type);
  8167. IF ~newObjectFile THEN
  8168. IntermediateCode.MakeMemory(operand.tag,addressType);
  8169. END;
  8170. ELSE
  8171. IF ~backend.cooperative THEN
  8172. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8173. END;
  8174. IntermediateCode.MakeMemory(operand.tag,addressType);
  8175. END;
  8176. NilCheck(operand.op);
  8177. ELSIF type IS SyntaxTree.ArrayType THEN
  8178. IF isUnsafe THEN
  8179. NilCheck(operand.op);
  8180. ReleaseIntermediateOperand(operand.tag);
  8181. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8182. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8183. ELSE
  8184. operand.tag := emptyOperand;
  8185. END;
  8186. ELSE
  8187. NilCheck(operand.op);
  8188. IF backend.cooperative THEN
  8189. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8190. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8191. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8192. ELSE
  8193. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8194. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8195. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8196. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8197. END;
  8198. END;
  8199. ELSIF type IS SyntaxTree.MathArrayType THEN
  8200. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8201. IntermediateCode.MakeMemory(operand.op,addressType);
  8202. ELSE HALT(100);
  8203. END;
  8204. END Dereference;
  8205. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8206. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8207. BEGIN
  8208. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8209. dest := destination; destination := emptyOperand;
  8210. Evaluate(x.left,d);
  8211. type := x.type.resolved;
  8212. prevIsUnchecked := isUnchecked;
  8213. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8214. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8215. END;
  8216. Dereference(d,type,IsUnsafePointer(x.left.type));
  8217. isUnchecked := prevIsUnchecked;
  8218. result := d;
  8219. 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
  8220. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8221. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8222. END;
  8223. END;
  8224. destination := dest;
  8225. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8226. END VisitDereferenceDesignator;
  8227. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8228. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8229. BEGIN
  8230. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8231. dest := destination; destination := emptyOperand;
  8232. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8233. tag := result.op;
  8234. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8235. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8236. StaticCallOperand(result,procedure.super);
  8237. ReleaseIntermediateOperand(result.tag);
  8238. UseIntermediateOperand(tag); (* necessary ? *)
  8239. result.tag := tag;
  8240. destination := dest;
  8241. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8242. END VisitSupercallDesignator;
  8243. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8244. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8245. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8246. name: Basic.SegmentedName;
  8247. procedure: SyntaxTree.Procedure;
  8248. procedureType: SyntaxTree.ProcedureType;
  8249. BEGIN
  8250. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8251. dest := destination; destination := emptyOperand;
  8252. scope := currentScope;
  8253. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8254. scope := scope.outerScope;
  8255. END;
  8256. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8257. IF newObjectFile THEN
  8258. moduleSection := meta.ModuleSection();
  8259. IF backend.cooperative THEN
  8260. moduleOffset := 0;
  8261. ELSE
  8262. moduleOffset := moduleSection.pc;
  8263. END;
  8264. result.mode := ModeValue;
  8265. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8266. ELSE
  8267. Symbol(moduleSelf,result);
  8268. IntermediateCode.MakeMemory(result.op,addressType);
  8269. END
  8270. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8271. result.mode := ModeValue;
  8272. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8273. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8274. ELSE
  8275. GetBaseRegister(basereg,currentScope,scope);
  8276. InitOperand(result,ModeReference);
  8277. result.op := basereg;
  8278. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8279. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8280. parametersSize := ProcedureParametersSize(system,procedure);
  8281. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8282. IF backend.cooperative THEN
  8283. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8284. END;
  8285. IF ~(x.type.resolved.IsPointer() OR (x.type.resolved IS SyntaxTree.CellType) & backend.cellsAreObjects) THEN (* var par ! *)
  8286. MakeMemory(result.op, result.op, addressType, 0);
  8287. END;
  8288. (* tag must be loaded when dereferencing SELF pointer *)
  8289. END;
  8290. destination := dest;
  8291. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8292. END VisitSelfDesignator;
  8293. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8294. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; map: SymbolMap;
  8295. BEGIN
  8296. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8297. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8298. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8299. parameter := procedureType.returnParameter;
  8300. IF currentIsInline THEN
  8301. map := currentMapper.Get(NIL);
  8302. IF map # NIL THEN
  8303. Designate(map.to, result);
  8304. ELSE
  8305. HALT(200);
  8306. END;
  8307. RETURN;
  8308. END;
  8309. VisitParameter(parameter);
  8310. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8311. END VisitResultDesignator;
  8312. (** values *)
  8313. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8314. BEGIN
  8315. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8316. IF conditional THEN
  8317. IF x.value THEN BrL(trueLabel)
  8318. ELSE BrL(falseLabel)
  8319. END;
  8320. ELSE
  8321. InitOperand(result,ModeValue);
  8322. IF x.value THEN result.op := true ELSE result.op := false END;
  8323. END;
  8324. END VisitBooleanValue;
  8325. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8326. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8327. BEGIN
  8328. Global.GetModuleSegmentedName(module.module, name);
  8329. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8330. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8331. RETURN section
  8332. END GetDataSection;
  8333. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8334. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8335. BEGIN
  8336. type := vop.type;
  8337. data := GetDataSection();
  8338. pc := EnterImmediate(data,vop);
  8339. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8340. IntermediateCode.MakeMemory(vop, type);
  8341. END GetImmediateMem;
  8342. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8343. BEGIN
  8344. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8345. InitOperand(result,ModeValue);
  8346. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8347. IF ~supportedImmediate(result.op) &~inData THEN
  8348. GetImmediateMem(result.op)
  8349. END;
  8350. END VisitIntegerValue;
  8351. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8352. BEGIN
  8353. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8354. InitOperand(result,ModeValue);
  8355. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8356. END VisitCharacterValue;
  8357. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8358. BEGIN
  8359. IF Trace THEN TraceEnter("VisitSetValue") END;
  8360. InitOperand(result,ModeValue);
  8361. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8362. END VisitSetValue;
  8363. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8364. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8365. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8366. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8367. BEGIN
  8368. numberElements := x.elements.Length();
  8369. FOR i := 0 TO numberElements-1 DO
  8370. expression := x.elements.GetExpression(i);
  8371. IF expression IS SyntaxTree.MathArrayExpression THEN
  8372. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8373. ELSE
  8374. inData := TRUE;
  8375. Evaluate(expression,op);
  8376. irv.Emit(Data(position,op.op));
  8377. inData := FALSE;
  8378. ReleaseOperand(op);
  8379. END;
  8380. END;
  8381. END RecursiveData;
  8382. BEGIN
  8383. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8384. IF ~TryConstantDeclaration() THEN
  8385. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8386. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8387. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8388. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8389. ELSE
  8390. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8391. END;
  8392. RecursiveData(x.array);
  8393. InitOperand(result,ModeReference);
  8394. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8395. END
  8396. END VisitMathArrayValue;
  8397. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8398. VAR constant: Sections.Section;
  8399. BEGIN
  8400. IF constantDeclaration = NIL THEN
  8401. RETURN FALSE
  8402. ELSE
  8403. (* Is a constant in this module: did we generate it already? *)
  8404. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8405. IF constant # NIL THEN
  8406. InitOperand(result,ModeReference);
  8407. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8408. RETURN TRUE;
  8409. END;
  8410. END;
  8411. RETURN FALSE
  8412. END TryConstantDeclaration;
  8413. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8414. BEGIN
  8415. constantDeclaration := x;
  8416. x.value.resolved.Accept(SELF);
  8417. END VisitConstant;
  8418. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8419. BEGIN
  8420. IF Trace THEN TraceEnter("VisitRealValue") END;
  8421. InitOperand(result,ModeValue);
  8422. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8423. END VisitRealValue;
  8424. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8425. VAR
  8426. componentType: SyntaxTree.Type;
  8427. BEGIN
  8428. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8429. ASSERT(x.type IS SyntaxTree.ComplexType);
  8430. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8431. InitOperand(result,ModeValue);
  8432. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8433. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8434. END VisitComplexValue;
  8435. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8436. VAR i: LONGINT; name: Basic.SegmentedName;
  8437. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8438. BEGIN
  8439. IF Trace THEN TraceEnter("VisitStringValue") END;
  8440. IF ~TryConstantDeclaration() THEN
  8441. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8442. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8443. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8444. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8445. ELSE
  8446. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8447. END;
  8448. FOR i := 0 TO x.length-1 DO
  8449. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8450. irv.Emit(Data(position,op));
  8451. END;
  8452. InitOperand(result,ModeReference);
  8453. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8454. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8455. END
  8456. END VisitStringValue;
  8457. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8458. BEGIN
  8459. IF Trace THEN TraceEnter("VisitNilValue") END;
  8460. InitOperand(result,ModeValue);
  8461. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8462. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8463. END VisitNilValue;
  8464. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8465. BEGIN
  8466. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8467. InitOperand(result,ModeValue);
  8468. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8469. END VisitEnumerationValue;
  8470. (** symbols *)
  8471. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8472. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8473. END VisitImport;
  8474. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8475. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8476. BEGIN
  8477. IF scope # baseScope THEN
  8478. (* left := [fp+8] *)
  8479. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8480. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8481. ReuseCopy(left,right);
  8482. ReleaseIntermediateOperand(right);
  8483. scope := scope.outerScope; DEC(level);
  8484. (* { left := [left+8] } *)
  8485. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8486. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8487. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8488. Emit(Mov(position,left,right));
  8489. scope := scope.outerScope; DEC(level);
  8490. END;
  8491. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8492. result := left;
  8493. ELSE
  8494. result := fp;
  8495. END;
  8496. END GetBaseRegister;
  8497. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8498. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8499. BEGIN
  8500. IF Trace THEN TraceEnter("VisitVariable"); END;
  8501. type := x.type.resolved;
  8502. IF (x.useRegister) THEN
  8503. InitOperand(result, ModeValue);
  8504. IF x.registerNumber < 0 THEN
  8505. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8506. reg := x.registerNumber;
  8507. ELSE
  8508. reg := registerUsageCount.Map(x.registerNumber);
  8509. UseRegister(reg);
  8510. END;
  8511. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8512. ELSIF x.externalName # NIL THEN
  8513. InitOperand(result,ModeReference);
  8514. Basic.ToSegmentedName(x.externalName^, name);
  8515. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8516. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8517. InitOperand(result,ModeReference);
  8518. GetBaseRegister(result.op,currentScope,x.scope);
  8519. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8520. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8521. InitOperand(result,ModeReference);
  8522. GetCodeSectionNameForSymbol(x,name);
  8523. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8524. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8525. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8526. InitOperand(result,ModeReference);
  8527. GetCodeSectionNameForSymbol(x,name);
  8528. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8529. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8530. ELSE (* field, left designator must have been emitted *)
  8531. ASSERT(result.mode = ModeReference);
  8532. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8533. ReuseCopy(temp,result.op);
  8534. ReleaseIntermediateOperand(result.op);
  8535. result.op := temp;
  8536. END;
  8537. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8538. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8539. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8540. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8541. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8542. END;
  8543. END;
  8544. END;
  8545. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8546. ValueToCondition(result);
  8547. ELSIF type IS SyntaxTree.ProcedureType THEN
  8548. ReleaseIntermediateOperand(result.tag);
  8549. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8550. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8551. UseIntermediateOperand(result.tag);
  8552. ELSE
  8553. result.tag := nil; (* nil *)
  8554. END;
  8555. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8556. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8557. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8558. ReleaseIntermediateOperand(result.tag);
  8559. Global.GetSymbolSegmentedName(x,name);
  8560. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8561. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8562. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8563. ELSE
  8564. END;
  8565. ELSE
  8566. ReleaseIntermediateOperand(result.tag);
  8567. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8568. END;
  8569. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8570. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8571. ReleaseIntermediateOperand(result.tag);
  8572. result.tag := result.op;
  8573. UseIntermediateOperand(result.tag);
  8574. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8575. IntermediateCode.MakeMemory(result.op,addressType);
  8576. END;
  8577. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8578. ReleaseIntermediateOperand(result.tag);
  8579. result.tag := TypeDescriptorAdr(type);
  8580. IF ~newObjectFile THEN
  8581. IntermediateCode.MakeMemory(result.tag,addressType);
  8582. END;
  8583. UseIntermediateOperand(result.tag);
  8584. (* tag for pointer type computed not here but during dereferencing *)
  8585. END;
  8586. IF Trace THEN TraceExit("VisitVariable") END;
  8587. END VisitVariable;
  8588. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8589. BEGIN
  8590. VisitVariable(property);
  8591. END VisitProperty;
  8592. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8593. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8594. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8595. BEGIN
  8596. type := x.type.resolved;
  8597. IF Trace THEN TraceEnter("VisitParameter") END;
  8598. IF x.ownerType IS SyntaxTree.CellType THEN
  8599. ptype := x.type.resolved;
  8600. IF backend.cellsAreObjects THEN
  8601. ASSERT(result.mode = ModeReference);
  8602. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8603. ReuseCopy(temp,result.op);
  8604. ReleaseIntermediateOperand(result.op);
  8605. result.op := temp;
  8606. END;
  8607. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8608. RETURN;
  8609. ELSE
  8610. InitOperand(result,ModeReference);
  8611. GetCodeSectionNameForSymbol(x,name);
  8612. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8613. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8614. RETURN;
  8615. END;
  8616. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8617. InitOperand(result,ModeReference);
  8618. GetCodeSectionNameForSymbol(x,name);
  8619. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8620. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8621. RETURN
  8622. ELSE
  8623. GetBaseRegister(basereg,currentScope,x.scope);
  8624. InitOperand(result,ModeReference);
  8625. result.op := basereg;
  8626. END;
  8627. IF IsOpenArray(type) THEN
  8628. result.tag := basereg;
  8629. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8630. IntermediateCode.MakeMemory(result.op,addressType);
  8631. IF Global.IsOberonProcedure(x.ownerType) THEN
  8632. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8633. UseIntermediateOperand(result.tag);
  8634. ELSE
  8635. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8636. END;
  8637. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8638. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8639. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8640. ELSIF IsStaticArray(type) THEN
  8641. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8642. IntermediateCode.MakeMemory(result.op,addressType);
  8643. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8644. ELSIF type IS SyntaxTree.MathArrayType THEN
  8645. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8646. WITH type: SyntaxTree.MathArrayType DO
  8647. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8648. IF type.form = SyntaxTree.Tensor THEN
  8649. ELSIF type.form = SyntaxTree.Open THEN
  8650. result.tag := result.op;
  8651. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8652. IntermediateCode.MakeMemory(result.op,addressType);
  8653. UseIntermediateOperand(result.tag);
  8654. ELSIF type.form = SyntaxTree.Static THEN
  8655. IF x.kind = SyntaxTree.ConstParameter THEN
  8656. IntermediateCode.MakeMemory(result.op,addressType);
  8657. END;
  8658. ELSE HALT(100)
  8659. END;
  8660. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8661. IF type.form = SyntaxTree.Tensor THEN
  8662. ToMemory(result.op,addressType,0);
  8663. ELSIF type.form = SyntaxTree.Open THEN
  8664. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8665. ReuseCopy(result.tag, mem);
  8666. ReleaseIntermediateOperand(mem);
  8667. ReleaseIntermediateOperand(result.op);
  8668. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8669. ELSIF type.form = SyntaxTree.Static THEN
  8670. IntermediateCode.MakeMemory(result.op,addressType);
  8671. ELSE HALT(100)
  8672. END;
  8673. ELSE HALT(100)
  8674. END;
  8675. END;
  8676. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8677. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8678. IntermediateCode.MakeMemory(result.op,addressType);
  8679. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8680. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8681. END;
  8682. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8683. ValueToCondition(result);
  8684. ELSIF type IS SyntaxTree.ProcedureType THEN
  8685. ReleaseIntermediateOperand(result.tag);
  8686. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8687. IF x.kind = SyntaxTree.VarParameter THEN
  8688. ReuseCopy(result.tag,result.op);
  8689. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8690. IntermediateCode.MakeMemory(result.tag,addressType);
  8691. ELSE
  8692. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8693. UseIntermediateOperand(result.tag);
  8694. END;
  8695. ELSE
  8696. result.tag := nil;
  8697. END;
  8698. (* tag for pointer type computed not here but during dereferencing *)
  8699. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) & ~(x.selfParameter) THEN
  8700. ReleaseIntermediateOperand(result.tag);
  8701. result.tag := basereg;
  8702. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8703. IntermediateCode.MakeMemory(result.tag,addressType);
  8704. UseIntermediateOperand(result.tag);
  8705. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & ((x.kind = SyntaxTree.ValueParameter) OR x.selfParameter) THEN
  8706. ReleaseIntermediateOperand(result.tag);
  8707. result.tag := TypeDescriptorAdr(type);
  8708. IF ~newObjectFile THEN
  8709. IntermediateCode.MakeMemory(result.tag,addressType);
  8710. END;
  8711. UseIntermediateOperand(result.tag);
  8712. END;
  8713. IF Trace THEN TraceExit("VisitParameter") END;
  8714. END VisitParameter;
  8715. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8716. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8717. BEGIN
  8718. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8719. (* left.p: left already emitted *)
  8720. tag := result.op; (* value of pointer to left *)
  8721. (* get type desc *)
  8722. tmp := result.tag;
  8723. IntermediateCode.MakeMemory(tmp,addressType);
  8724. (* get method adr *)
  8725. Reuse1(reg,tmp);
  8726. ReleaseIntermediateOperand(tmp);
  8727. IF backend.cooperative THEN
  8728. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8729. WHILE recordType.baseType # NIL DO
  8730. recordType := recordType.GetBaseRecord ();
  8731. END;
  8732. GetRecordTypeName (recordType,name);
  8733. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8734. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8735. offset := 0;
  8736. ELSE
  8737. offset := 2;
  8738. END;
  8739. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8740. ELSE
  8741. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8742. END;
  8743. InitOperand(operand,ModeReference);
  8744. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8745. operand.op := reg;
  8746. operand.tag := tag;
  8747. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8748. END DynamicCallOperand;
  8749. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8750. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8751. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8752. BEGIN
  8753. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8754. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8755. tag := operand.op;
  8756. ReleaseIntermediateOperand(operand.tag);
  8757. ELSE tag := nil
  8758. END;
  8759. IF x.isInline THEN
  8760. sectionType := Sections.InlineCodeSection;
  8761. ELSE
  8762. sectionType := Sections.CodeSection;
  8763. END;
  8764. IF x.externalName # NIL THEN
  8765. Basic.ToSegmentedName(x.externalName^, name);
  8766. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8767. ELSE
  8768. GetCodeSectionNameForSymbol(x, name);
  8769. IF (x.scope.ownerModule = module.module) THEN
  8770. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8771. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8772. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8773. IF source.pc = 0 THEN (* no code yet *)
  8774. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8775. END;
  8776. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8777. bits := x.procedureScope.body.code.inlineCode;
  8778. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8779. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8780. binary.CopyBits(bits, 0, bits.GetSize());
  8781. source.SetResolved(binary);
  8782. ELSE
  8783. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8784. END;
  8785. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8786. END;
  8787. InitOperand(operand,ModeValue);
  8788. operand.op := reg;
  8789. operand.tag := tag;
  8790. IF Trace THEN TraceExit("StaticCallOperand") END;
  8791. END StaticCallOperand;
  8792. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8793. (* handle expressions of the form designator.procedure or procedure *)
  8794. BEGIN
  8795. IF Trace THEN TraceEnter("VisitProcedure") END;
  8796. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) & ~(result.tag.mode = IntermediateCode.ModeImmediate) THEN
  8797. DynamicCallOperand(result,x);
  8798. ELSIF x.isInline THEN
  8799. StaticCallOperand(result,x);
  8800. ELSE
  8801. StaticCallOperand(result,x);
  8802. END;
  8803. IF Trace THEN TraceExit("VisitProcedure") END;
  8804. END VisitProcedure;
  8805. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8806. BEGIN
  8807. VisitProcedure(x);
  8808. END VisitOperator;
  8809. (** statements *)
  8810. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8811. BEGIN
  8812. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8813. Expression(x.call);
  8814. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8815. ReleaseOperand(result)
  8816. END;
  8817. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8818. END VisitProcedureCallStatement;
  8819. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8820. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8821. leftBase, rightBase: SyntaxTree.Type;
  8822. procedureName,s: SyntaxTree.IdentifierString;
  8823. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8824. size: LONGINT; rightKind: LONGINT;
  8825. dummy: IntermediateCode.Operand;
  8826. CONST moduleName = "FoxArrayBase";
  8827. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8828. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8829. BEGIN
  8830. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8831. IF base IS SyntaxTree.MathArrayType THEN
  8832. base := OpenArray(base(SyntaxTree.MathArrayType));
  8833. END;
  8834. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8835. result.SetArrayBase(base);
  8836. RETURN result
  8837. END OpenArray;
  8838. BEGIN
  8839. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8840. SaveRegisters();ReleaseUsedRegisters(saved);
  8841. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8842. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8843. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8844. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8845. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8846. IF leftType.form = SyntaxTree.Tensor THEN
  8847. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8848. ELSIF leftType.form = SyntaxTree.Open THEN
  8849. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8850. ELSIF leftType.form = SyntaxTree.Static THEN
  8851. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8852. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8853. END;
  8854. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8855. IF procedure = NIL THEN
  8856. s := "Instruction not supported on target, emulation procedure ";
  8857. Strings.Append(s,moduleName);
  8858. Strings.Append(s,".");
  8859. Strings.Append(s,procedureName);
  8860. Strings.Append(s," not present");
  8861. Error(position,s);
  8862. ELSE
  8863. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8864. parameter.SetType(leftType);
  8865. parameter.SetAccess(SyntaxTree.Internal);
  8866. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8867. parameter.SetKind(rightKind);
  8868. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8869. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8870. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8871. StaticCallOperand(result,procedure);
  8872. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8873. ReleaseOperand(result);
  8874. END;
  8875. RestoreRegisters(saved);
  8876. END;
  8877. END AssignMathArray;
  8878. VAR modifyAssignmentCounter := 0: LONGINT;
  8879. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8880. VAR processor,mem,dst: IntermediateCode.Operand;
  8881. BEGIN
  8882. IF value.intValue = true.intValue THEN
  8883. INC(modifyAssignmentCounter);
  8884. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8885. modifyAssignmentsPC := section.pc;
  8886. ELSE
  8887. DEC(modifyAssignmentCounter);
  8888. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8889. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8890. END;
  8891. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8892. dst := NewRegisterOperand (addressType);
  8893. Emit(Mov(position,dst, processor));
  8894. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8895. Emit(Mov(position,mem, value));
  8896. ReleaseIntermediateOperand(dst);
  8897. END ModifyAssignments;
  8898. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8899. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8900. BEGIN
  8901. type := left.type.resolved;
  8902. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8903. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8904. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8905. IF procedureType.returnParameter = parameter THEN
  8906. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8907. END;
  8908. END;
  8909. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8910. END CopySize;
  8911. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8912. VAR
  8913. leftO, rightO: Operand;
  8914. mem, sizeOp: IntermediateCode.Operand;
  8915. leftType, rightType, componentType: SyntaxTree.Type;
  8916. size: LONGINT;
  8917. parameters: SyntaxTree.ExpressionList;
  8918. procedure: SyntaxTree.Procedure;
  8919. call: SyntaxTree.ProcedureCallDesignator;
  8920. designator: SyntaxTree.Designator;
  8921. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8922. VAR procedureType: SyntaxTree.ProcedureType;
  8923. BEGIN
  8924. IF SemanticChecker.ReturnedAsParameter(right.type) THEN
  8925. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8926. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8927. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8928. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8929. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8930. IF right.id = Global.Reshape THEN RETURN TRUE
  8931. END;
  8932. END;
  8933. END;
  8934. END;
  8935. RETURN FALSE
  8936. END CanPassAsResultParameter;
  8937. BEGIN
  8938. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8939. leftType := left.type.resolved; rightType:= right.type.resolved;
  8940. IF backend.cooperative & left.NeedsTrace() THEN
  8941. ModifyAssignments(true);
  8942. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8943. Designate(right, rightO);
  8944. Designate(left, leftO);
  8945. ASSERT(leftO.mode = ModeReference);
  8946. TransferToRegister(leftO.op, leftO.op);
  8947. TransferToRegister(rightO.op, rightO.op);
  8948. Emit(Push(position, leftO.op));
  8949. Emit(Push(position, rightO.op));
  8950. CallAssignMethod(leftO.op, rightO.op, left.type);
  8951. Emit(Pop(position, rightO.op));
  8952. Emit(Pop(position, leftO.op));
  8953. sizeOp := CopySize(left);
  8954. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8955. ReleaseOperand(leftO);
  8956. ReleaseOperand(rightO);
  8957. ELSE
  8958. Evaluate(right,rightO);
  8959. Designate(left,leftO);
  8960. ASSERT(leftO.mode = ModeReference);
  8961. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8962. (* copy procedure address first *)
  8963. MakeMemory(mem,leftO.op,addressType,0);
  8964. Emit(Mov(position,mem,rightO.op));
  8965. ReleaseIntermediateOperand(mem);
  8966. (* copy pointer address *)
  8967. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8968. CallAssignPointer(leftO.tag, rightO.tag);
  8969. ELSE
  8970. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8971. CallAssignPointer(leftO.op, rightO.op);
  8972. END;
  8973. ReleaseOperand(leftO);
  8974. ReleaseOperand(rightO);
  8975. END;
  8976. ModifyAssignments(false);
  8977. RETURN;
  8978. END;
  8979. IF CanPassAsResultParameter(right) THEN
  8980. procedureResultDesignator := left(SyntaxTree.Designator);
  8981. Expression(right);
  8982. procedureResultDesignator := NIL;
  8983. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8984. (* left <-- ALIAS OF right: zerocopy *)
  8985. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8986. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8987. designator.SetType(procedure.type);
  8988. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8989. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8990. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8991. END;
  8992. ELSIF leftType IS SyntaxTree.RangeType THEN
  8993. (* LHS is of array range type *)
  8994. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8995. Evaluate(right, rightO);
  8996. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8997. (* first *)
  8998. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8999. Emit(Mov(position,mem, rightO.op));
  9000. ReleaseIntermediateOperand(mem);
  9001. (* last *)
  9002. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9003. Emit(Mov(position,mem, rightO.tag));
  9004. ReleaseIntermediateOperand(mem);
  9005. (* step *)
  9006. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9007. Emit(Mov(position,mem, rightO.extra));
  9008. ReleaseIntermediateOperand(mem);
  9009. ReleaseOperand(rightO);
  9010. ReleaseOperand(leftO)
  9011. ELSIF leftType IS SyntaxTree.ComplexType THEN
  9012. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  9013. Evaluate(right, rightO);
  9014. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  9015. componentType := leftType(SyntaxTree.ComplexType).componentType;
  9016. (* real part *)
  9017. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  9018. Emit(Mov(position,mem, rightO.op));
  9019. ReleaseIntermediateOperand(mem);
  9020. (* imaginary part *)
  9021. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9022. Emit(Mov(position,mem, rightO.tag));
  9023. ReleaseIntermediateOperand(mem);
  9024. ReleaseOperand(rightO);
  9025. ReleaseOperand(leftO)
  9026. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9027. OR (leftType IS SyntaxTree.PortType) THEN
  9028. (* rightO := leftO;*)
  9029. Evaluate(right,rightO);
  9030. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9031. Designate(left,leftO);
  9032. IF leftO.mode = ModeReference THEN
  9033. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9034. destination := mem;
  9035. ELSE
  9036. destination := leftO.op;
  9037. END;
  9038. ReleaseOperand(leftO);
  9039. IF destination.mode # IntermediateCode.Undefined THEN
  9040. Emit(Mov(position,destination,rightO.op));
  9041. END;
  9042. ReleaseOperand(rightO);
  9043. ReleaseIntermediateOperand(mem);
  9044. IntermediateCode.InitOperand(destination);
  9045. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9046. Evaluate(right,rightO);
  9047. Designate(left,leftO);
  9048. MakeMemory(mem,leftO.op,addressType,0);
  9049. Emit(Mov(position,mem,rightO.op));
  9050. ReleaseIntermediateOperand(mem);
  9051. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9052. (* delegate *)
  9053. (*
  9054. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9055. *)
  9056. Emit(Mov(position,leftO.tag,rightO.tag));
  9057. END;
  9058. ReleaseOperand(leftO);
  9059. ReleaseOperand(rightO);
  9060. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9061. Designate(right,rightO);
  9062. Designate(left,leftO);
  9063. sizeOp := CopySize(left);
  9064. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9065. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9066. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9067. IF (rightType IS SyntaxTree.StringType) THEN
  9068. CopyString(left,right);
  9069. 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
  9070. Designate(right,rightO);
  9071. Designate(left,leftO);
  9072. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9073. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9074. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9075. ELSE
  9076. HALT(201)
  9077. END;
  9078. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9079. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9080. Designate(right,rightO);
  9081. Designate(left,leftO);
  9082. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9083. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9084. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9085. END;
  9086. AssignMathArray(left,right);
  9087. ELSE
  9088. HALT(200);
  9089. END;
  9090. END Assign;
  9091. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9092. BEGIN
  9093. IF Trace THEN TraceEnter("VisitAssignment") END;
  9094. Assign(x.left,x.right);
  9095. IF Trace THEN TraceExit("VisitAssignment") END;
  9096. END VisitAssignment;
  9097. PROCEDURE EmitCooperativeSwitch;
  9098. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9099. BEGIN
  9100. ASSERT (cooperativeSwitches);
  9101. pc := section.pc;
  9102. IF lastSwitchPC = section.pc THEN RETURN END;
  9103. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9104. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9105. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9106. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9107. INC(statCoopSwitch, section.pc - pc);
  9108. END EmitCooperativeSwitch;
  9109. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9110. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9111. BEGIN
  9112. p0 := communication.left; p1 := communication.right;
  9113. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9114. Evaluate(p0,s0);
  9115. Evaluate(p1,s1);
  9116. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9117. Emit(Push(position,s0.op));
  9118. Emit(Push(position,s1.op));
  9119. (*
  9120. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9121. *)
  9122. IF ~backend.cellsAreObjects THEN
  9123. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9124. END;
  9125. ReleaseOperand(s0);
  9126. ReleaseOperand(s1);
  9127. IF backend.cellsAreObjects THEN
  9128. CallThis(position,"ActiveCellsRuntime","Send",2);
  9129. ELSE
  9130. CallThis(position,ChannelModuleName,"Send",2);
  9131. END;
  9132. (* ----- RECEIVE ------*)
  9133. ELSE
  9134. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9135. tmp := p0; p0 := p1; p1 := tmp;
  9136. END;
  9137. Evaluate(p0,s0);
  9138. Emit(Push(position,s0.op));
  9139. Designate(p1,s1);
  9140. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9141. Emit(Push(position,s1.op));
  9142. (*
  9143. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9144. *)
  9145. IF ~backend.cellsAreObjects THEN
  9146. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9147. END;
  9148. ReleaseOperand(s0);
  9149. ReleaseOperand(s1);
  9150. IF backend.cellsAreObjects THEN
  9151. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9152. ELSE
  9153. CallThis(position,ChannelModuleName,"Receive",2)
  9154. END;
  9155. END;
  9156. END VisitCommunicationStatement;
  9157. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9158. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9159. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9160. VAR true, false: Label; condition, value: BOOLEAN;
  9161. BEGIN
  9162. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9163. IF condition THEN
  9164. true := NewLabel();
  9165. false := NewLabel();
  9166. Condition(if.condition,true,false);
  9167. SetLabel(true);
  9168. StatementSequence(if.statements);
  9169. BrL(end);
  9170. SetLabel(false);
  9171. ELSE
  9172. IF value THEN (* always true *)
  9173. escape := TRUE;
  9174. StatementSequence(if.statements);
  9175. (* no branch necessary -- rest skipped *)
  9176. END;
  9177. END;
  9178. END IfPart;
  9179. BEGIN
  9180. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9181. end := NewLabel();
  9182. elsifs := x.ElsifParts();
  9183. IfPart(x.ifPart);
  9184. FOR i := 0 TO elsifs-1 DO
  9185. IF ~escape THEN
  9186. elsif := x.GetElsifPart(i);
  9187. IfPart(elsif);
  9188. END;
  9189. END;
  9190. IF (x.elsePart # NIL) & ~escape THEN
  9191. StatementSequence(x.elsePart);
  9192. END;
  9193. SetLabel(end);
  9194. IF Trace THEN TraceExit("VisitIfStatement") END;
  9195. END VisitIfStatement;
  9196. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9197. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9198. BEGIN
  9199. (*IF x.variable.type.resolved = x.type.resolved THEN
  9200. (* always true, do nothing *)
  9201. ELSE*)
  9202. Designate(x.variable,res);
  9203. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9204. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9205. END;
  9206. trueL := NewLabel();
  9207. TypeTest(res.tag,x.type,trueL,falseL);
  9208. ReleaseOperand(res);
  9209. SetLabel(trueL);
  9210. StatementSequence(x.statements);
  9211. BrL(endL);
  9212. END WithPart;
  9213. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9214. VAR endL,falseL: Label;i: LONGINT;
  9215. BEGIN
  9216. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9217. endL := NewLabel();
  9218. FOR i := 0 TO x.WithParts()-1 DO
  9219. falseL := NewLabel();
  9220. WithPart(x.GetWithPart(i),falseL,endL);
  9221. SetLabel(falseL);
  9222. END;
  9223. IF x.elsePart = NIL THEN
  9224. IF ~isUnchecked THEN
  9225. EmitTrap(position,WithTrap);
  9226. END;
  9227. ELSE
  9228. StatementSequence(x.elsePart)
  9229. END;
  9230. SetLabel(endL);
  9231. IF Trace THEN TraceExit("VisitWithStatement") END;
  9232. END VisitWithStatement;
  9233. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9234. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9235. out,else: Label; label: Label;
  9236. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9237. symbol: SyntaxTree.Symbol;
  9238. BEGIN
  9239. (*! split case statement into if-elsif statements for large case label lists *)
  9240. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9241. size := x.max-x.min+1;
  9242. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9243. END;
  9244. Evaluate(x.variable,var);
  9245. ReuseCopy(tmp,var.op);
  9246. ReleaseIntermediateOperand(var.op);
  9247. var.op := tmp;
  9248. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9249. Convert(var.op,addressType);
  9250. size := x.max-x.min+1;
  9251. else := NewLabel();
  9252. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9253. (*
  9254. UniqueId(name,module.module,"case",caseId);
  9255. *)
  9256. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9257. Global.GetModuleSegmentedName(module.module, name);
  9258. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9259. symbol := SyntaxTree.NewSymbol(name[1]);
  9260. symbol.SetScope(moduleScope);
  9261. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9262. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9263. section.isCaseTable := TRUE;
  9264. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9265. ReuseCopy(res,var.op);
  9266. ReleaseOperand(var);
  9267. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9268. Emit(Add(position,res,res,jmp));
  9269. IntermediateCode.MakeMemory(res,addressType);
  9270. Emit(Br(position,res));
  9271. ReleaseIntermediateOperand(res);
  9272. out := NewLabel();
  9273. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9274. part := x.GetCasePart(i);
  9275. constant := part.firstConstant;
  9276. label := NewLabel();
  9277. SetLabel(label);
  9278. WHILE(constant # NIL) DO (* case labels for this case part *)
  9279. FOR j := constant.min TO constant.max DO
  9280. fixups[j-x.min] := label;
  9281. END;
  9282. constant := constant.next;
  9283. END;
  9284. StatementSequence(part.statements);
  9285. BrL(out);
  9286. END;
  9287. SetLabel(else);
  9288. FOR i := 0 TO size-1 DO
  9289. IF fixups[i] = NIL THEN
  9290. fixups[i] := else;
  9291. END;
  9292. END;
  9293. IF x.elsePart # NIL THEN
  9294. StatementSequence(x.elsePart);
  9295. ELSIF ~isUnchecked THEN
  9296. EmitTrap(position,CaseTrap);
  9297. END;
  9298. SetLabel(out);
  9299. FOR i := 0 TO size-1 DO
  9300. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9301. section.Emit(Data(position,op));
  9302. END;
  9303. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9304. END VisitCaseStatement;
  9305. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9306. VAR start: Label; true,false: Label;
  9307. BEGIN
  9308. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9309. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9310. start := NewLabel();
  9311. true := NewLabel();
  9312. false := NewLabel();
  9313. SetLabel(start);
  9314. Condition(x.condition,true,false);
  9315. SetLabel(true);
  9316. StatementSequence(x.statements);
  9317. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9318. BrL(start);
  9319. SetLabel(false);
  9320. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9321. END VisitWhileStatement;
  9322. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9323. VAR false,true: Label;
  9324. BEGIN
  9325. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9326. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9327. true := NewLabel();
  9328. false := NewLabel();
  9329. SetLabel(false);
  9330. StatementSequence(x.statements);
  9331. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9332. Condition(x.condition,true,false);
  9333. SetLabel(true);
  9334. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9335. END VisitRepeatStatement;
  9336. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9337. VAR
  9338. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9339. temporaryVariable: SyntaxTree.Variable;
  9340. temporaryVariableDesignator : SyntaxTree.Designator;
  9341. BEGIN
  9342. IF Trace THEN TraceEnter("VisitForStatement") END;
  9343. true := NewLabel();
  9344. false := NewLabel();
  9345. start := NewLabel();
  9346. Assign(x.variable,x.from);
  9347. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9348. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9349. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9350. Assign(temporaryVariableDesignator,x.to);
  9351. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9352. IF by > 0 THEN
  9353. cmp := Scanner.LessEqual
  9354. ELSE
  9355. cmp := Scanner.GreaterEqual
  9356. END;
  9357. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9358. binary.SetType(system.booleanType);
  9359. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9360. SetLabel(start);
  9361. Condition(binary,true,false);
  9362. SetLabel(true);
  9363. StatementSequence(x.statements);
  9364. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9365. binary.SetType(x.variable.type);
  9366. Assign(x.variable,binary);
  9367. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9368. BrL(start);
  9369. SetLabel(false);
  9370. IF Trace THEN TraceExit("VisitForStatement") END;
  9371. END VisitForStatement;
  9372. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9373. VAR prevLoop: Label;
  9374. BEGIN
  9375. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9376. prevLoop := currentLoop;
  9377. currentLoop := NewLabel();
  9378. StatementSequence(x.statements);
  9379. SetLabel(currentLoop);
  9380. currentLoop := prevLoop;
  9381. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9382. END VisitExitableBlock;
  9383. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9384. VAR prevLoop,start: Label;
  9385. BEGIN
  9386. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9387. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9388. start := NewLabel();
  9389. prevLoop := currentLoop;
  9390. SetLabel(start);
  9391. currentLoop := NewLabel();
  9392. StatementSequence(x.statements);
  9393. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9394. BrL(start);
  9395. SetLabel(currentLoop);
  9396. currentLoop := prevLoop;
  9397. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9398. END VisitLoopStatement;
  9399. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9400. VAR outer: SyntaxTree.Statement;
  9401. BEGIN
  9402. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9403. IF locked THEN (* r if we jump out of an exclusive block *)
  9404. outer := x.outer;
  9405. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9406. outer := outer.outer;
  9407. END;
  9408. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9409. Lock(FALSE);
  9410. END;
  9411. END;
  9412. BrL(currentLoop);
  9413. IF Trace THEN TraceExit("VisitExitStatement") END;
  9414. END VisitExitStatement;
  9415. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9416. VAR
  9417. expression, parameterDesignator: SyntaxTree.Expression;
  9418. type, componentType: SyntaxTree.Type;
  9419. res, right: Operand;
  9420. left, mem, reg: IntermediateCode.Operand;
  9421. parameter: SyntaxTree.Parameter;
  9422. procedure: SyntaxTree.Procedure;
  9423. procedureType: SyntaxTree.ProcedureType;
  9424. returnTypeOffset: LONGINT;
  9425. delegate: BOOLEAN;
  9426. map: SymbolMap;
  9427. cc, parametersSize: LONGINT;
  9428. BEGIN
  9429. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9430. expression := x.returnValue;
  9431. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9432. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9433. IF currentIsInline THEN
  9434. IF expression # NIL THEN
  9435. map := currentMapper.Get(NIL);
  9436. IF map # NIL THEN
  9437. Assign(map.to, expression);
  9438. ELSE
  9439. Evaluate(expression,res);
  9440. Emit(Return(position,res.op));
  9441. ReleaseOperand(res);
  9442. END;
  9443. END;
  9444. BrL(currentInlineExit);
  9445. RETURN;
  9446. END;
  9447. IF expression # NIL THEN
  9448. type := expression.type.resolved;
  9449. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9450. IF locked THEN Lock(FALSE) END;
  9451. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9452. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9453. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9454. (* return without structured return parameter *)
  9455. Evaluate(expression,res);
  9456. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9457. IF locked OR profile THEN
  9458. Emit(Push(position,res.op));
  9459. IF delegate THEN HALT(200) END;
  9460. ReleaseOperand(res);
  9461. IF locked THEN Lock(FALSE) END;
  9462. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9463. reg := NewRegisterOperand(res.op.type);
  9464. Emit(Pop(position,reg));
  9465. Emit(Return(position,reg));
  9466. ReleaseIntermediateOperand(reg);
  9467. ELSE
  9468. Emit(Return(position,res.op));
  9469. ReleaseOperand(res);
  9470. END;
  9471. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9472. THEN
  9473. (* return using structured return parameter *)
  9474. 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)
  9475. OR SemanticChecker.IsPointerType(type));
  9476. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9477. parameter :=procedureType.returnParameter;
  9478. ASSERT(parameter # NIL);
  9479. returnTypeOffset := parameter.offsetInBits;
  9480. (*
  9481. IF parameter# NIL THEN
  9482. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9483. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9484. ELSE
  9485. returnTypeOffset := system.offsetFirstParameter
  9486. END;
  9487. *)
  9488. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9489. IF type IS SyntaxTree.RangeType THEN
  9490. (* array range type *)
  9491. Evaluate(expression, right);
  9492. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9493. Emit(Mov(position,mem, right.op)); (* first *)
  9494. ReleaseIntermediateOperand(mem);
  9495. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9496. Emit(Mov(position,mem, right.tag)); (* last *)
  9497. ReleaseIntermediateOperand(mem);
  9498. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9499. Emit(Mov(position,mem, right.extra)); (* step *)
  9500. ReleaseIntermediateOperand(mem);
  9501. ReleaseOperand(right);
  9502. ELSIF type IS SyntaxTree.ComplexType THEN
  9503. Evaluate(expression, right);
  9504. componentType := type(SyntaxTree.ComplexType).componentType;
  9505. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9506. Emit(Mov(position,mem, right.op)); (* real part *)
  9507. ReleaseIntermediateOperand(mem);
  9508. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9509. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9510. ReleaseIntermediateOperand(mem);
  9511. ReleaseOperand(right);
  9512. ELSE (* covers cases: pointer / record / array *)
  9513. parameter := procedureType.returnParameter;
  9514. checker.SetCurrentScope(currentScope);
  9515. ASSERT(parameter # NIL);
  9516. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9517. Assign(parameterDesignator,expression);
  9518. END;
  9519. ReleaseIntermediateOperand(left);
  9520. IF locked THEN Lock(FALSE) END;
  9521. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9522. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9523. parameter := procedureType.returnParameter;
  9524. checker.SetCurrentScope(currentScope);
  9525. IF parameter = NIL THEN
  9526. Error(procedure.position, "structured return of parameter of procedure not found");
  9527. ELSE
  9528. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9529. Assign(parameterDesignator,expression);
  9530. END;
  9531. IF locked THEN Lock(FALSE) END;
  9532. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9533. ELSE
  9534. HALT(200);
  9535. END;
  9536. ELSE
  9537. IF locked THEN Lock(FALSE) END;
  9538. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9539. END;
  9540. IF backend.cooperative THEN
  9541. BrL(exitLabel);
  9542. ELSE
  9543. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9544. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9545. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9546. ELSE
  9547. parametersSize := 0;
  9548. END;
  9549. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9550. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9551. END;
  9552. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9553. END VisitReturnStatement;
  9554. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9555. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9556. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9557. statements: SyntaxTree.StatementSequence;
  9558. name, suffix: SyntaxTree.IdentifierString;
  9559. BEGIN
  9560. Strings.IntToStr(awaitProcCounter,suffix);
  9561. Strings.Concat("@AwaitProcedure",suffix,name);
  9562. identifier := SyntaxTree.NewIdentifier(name);
  9563. INC(awaitProcCounter);
  9564. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9565. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9566. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9567. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9568. procedure.SetAccess(SyntaxTree.Hidden);
  9569. procedure.SetScope(currentScope);
  9570. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9571. procedureType.SetReturnType(system.booleanType);
  9572. procedure.SetType(procedureType);
  9573. body := SyntaxTree.NewBody(x.position,procedureScope);
  9574. procedureScope.SetBody(body);
  9575. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9576. returnStatement.SetReturnValue(x.condition);
  9577. statements := SyntaxTree.NewStatementSequence();
  9578. statements.AddStatement(returnStatement);
  9579. body.SetStatementSequence(statements);
  9580. currentScope.AddProcedure(procedure);
  9581. RETURN procedure
  9582. END MakeAwaitProcedure;
  9583. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9584. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9585. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9586. BEGIN
  9587. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9588. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9589. IF backend.cooperative THEN
  9590. start := NewLabel();
  9591. true := NewLabel();
  9592. false := NewLabel();
  9593. SetLabel(start);
  9594. Condition(x.condition,true,false);
  9595. SetLabel(false);
  9596. PushSelfPointer();
  9597. CallThis(position,"ExclusiveBlocks","Await",1);
  9598. BrL(start);
  9599. SetLabel(true);
  9600. PushSelfPointer();
  9601. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9602. ELSE
  9603. proc := MakeAwaitProcedure(x);
  9604. Emit(Push(position,fp));
  9605. GetCodeSectionNameForSymbol(proc,name);
  9606. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9607. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9608. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9609. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9610. Emit(Result(position,res));
  9611. (*
  9612. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9613. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9614. *)
  9615. InitOperand(result,ModeValue);
  9616. result.op := res;
  9617. label := NewLabel();
  9618. BreqL(label, result.op, SELF.true);
  9619. ReleaseOperand(result);
  9620. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9621. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9622. Emit(Push(position,res));
  9623. Emit(Push(position,fp));
  9624. PushSelfPointer();
  9625. Emit(Push(position,nil));
  9626. CallThis(position,"Objects","Await",4);
  9627. SetLabel(label);
  9628. END;
  9629. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9630. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9631. END VisitAwaitStatement;
  9632. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9633. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9634. BEGIN
  9635. FOR i := 0 TO x.Length() - 1 DO
  9636. statement := x.GetStatement( i );
  9637. Statement(statement);
  9638. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9639. END;
  9640. END StatementSequence;
  9641. PROCEDURE PushSelfPointer;
  9642. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9643. procedureType: SyntaxTree.ProcedureType;
  9644. BEGIN
  9645. scope := currentScope;
  9646. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9647. scope := scope.outerScope;
  9648. END;
  9649. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9650. IF ~newObjectFile THEN
  9651. Symbol(moduleSelf,op);
  9652. IntermediateCode.MakeMemory(op.op,addressType);
  9653. ELSE
  9654. moduleSection := meta.ModuleSection();
  9655. IF backend.cooperative THEN
  9656. moduleOffset := 0;
  9657. ELSE
  9658. moduleOffset := moduleSection.pc;
  9659. END;
  9660. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9661. END;
  9662. ELSE
  9663. GetBaseRegister(op.op,currentScope,scope);
  9664. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9665. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9666. parametersSize := ProcedureParametersSize(system,procedure);
  9667. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9668. IF backend.cooperative THEN
  9669. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9670. END;
  9671. IntermediateCode.MakeMemory(op.op,addressType);
  9672. END;
  9673. Emit(Push(position,op.op));
  9674. ReleaseOperand(op);
  9675. END PushSelfPointer;
  9676. PROCEDURE Lock(lock: BOOLEAN);
  9677. BEGIN
  9678. IF Trace THEN TraceEnter("Lock") END;
  9679. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9680. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9681. ASSERT(modifyAssignmentCounter = 0);
  9682. IF dump # NIL THEN
  9683. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9684. dump.Ln;dump.Update;
  9685. END;
  9686. PushSelfPointer;
  9687. IF backend.cooperative THEN
  9688. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9689. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9690. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9691. END;
  9692. ELSE
  9693. Emit(Push(position,true));
  9694. IF lock THEN CallThis(position,"Objects","Lock",2)
  9695. ELSE CallThis(position,"Objects","Unlock",2);
  9696. END;
  9697. END;
  9698. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9699. IF Trace THEN TraceExit("Lock") END;
  9700. END Lock;
  9701. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9702. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9703. BEGIN
  9704. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9705. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9706. previouslyUnchecked := isUnchecked;
  9707. isUnchecked := isUnchecked OR x.isUnchecked;
  9708. previouslyCooperativeSwitches := cooperativeSwitches;
  9709. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9710. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9711. IF x.statements # NIL THEN
  9712. StatementSequence(x.statements);
  9713. END;
  9714. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9715. isUnchecked := previouslyUnchecked;
  9716. cooperativeSwitches := previouslyCooperativeSwitches;
  9717. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9718. END VisitStatementBlock;
  9719. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9720. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9721. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9722. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9723. procedure: SyntaxTree.Procedure;
  9724. map: SymbolMap;
  9725. cc, parametersSize: LONGINT;
  9726. BEGIN
  9727. scope := currentScope;
  9728. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9729. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9730. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9731. return := emptyOperand;
  9732. IF Trace THEN TraceEnter("VisitCode") END;
  9733. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9734. NEW(in, x.inRules.Length());
  9735. FOR i := 0 TO LEN(in)-1 DO
  9736. statement := x.inRules.GetStatement(i);
  9737. WITH statement: SyntaxTree.Assignment DO
  9738. Evaluate(statement.right, operand);
  9739. result := operand.op;
  9740. NEW(str, 64);
  9741. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9742. in[i] := result; IntermediateCode.SetString(in[i], str);
  9743. ReleaseIntermediateOperand(operand.tag);
  9744. END;
  9745. END;
  9746. ELSE in := NIL
  9747. END;
  9748. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9749. NEW(out, x.outRules.Length());
  9750. FOR i := 0 TO LEN(out)-1 DO
  9751. statement := x.outRules.GetStatement(i);
  9752. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9753. WITH statement: SyntaxTree.Assignment DO
  9754. Designate(statement.left, operand);
  9755. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9756. NEW(str, 64);
  9757. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9758. out[i] := result; IntermediateCode.SetString(out[i], str);
  9759. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9760. |statement: SyntaxTree.ReturnStatement DO
  9761. NEW(str, 64);
  9762. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9763. IF currentIsInline THEN
  9764. map := currentMapper.Get(NIL);
  9765. Designate(map.to, operand);
  9766. IF map.isAddress THEN
  9767. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9768. ELSE
  9769. result := operand.op;
  9770. END;
  9771. (*! only if it does not fit into register
  9772. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9773. *)
  9774. (*Evaluate(map.to, operand);*)
  9775. out[i] := result;
  9776. ELSE
  9777. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9778. END;
  9779. IntermediateCode.SetString(out[i], str);
  9780. ReleaseIntermediateOperand(operand.tag);
  9781. return := out[i];
  9782. ELSE
  9783. END;
  9784. END;
  9785. ELSE out := NIL
  9786. END;
  9787. Emit(Asm(x.position,x.sourceCode, in, out));
  9788. IF in # NIL THEN
  9789. FOR i := 0 TO LEN(in)-1 DO
  9790. ReleaseIntermediateOperand(in[i]);
  9791. END;
  9792. END;
  9793. IF out # NIL THEN
  9794. FOR i := 0 TO LEN(out)-1 DO
  9795. WITH statement: SyntaxTree.Assignment DO
  9796. ReleaseIntermediateOperand(out[i]);
  9797. |statement: SyntaxTree.ReturnStatement DO
  9798. (* release happens below *)
  9799. ELSE
  9800. END;
  9801. statement := x.outRules.GetStatement(i);
  9802. END;
  9803. END;
  9804. IF return.mode # IntermediateCode.Undefined THEN
  9805. IF currentIsInline THEN
  9806. ELSIF SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  9807. Symbol(procedureType.returnParameter, par);
  9808. MakeMemory(mem, par.op, return.type, 0);
  9809. ReleaseOperand(par);
  9810. Emit(Mov(position, mem, return));
  9811. ReleaseIntermediateOperand(mem);
  9812. ELSE
  9813. Emit(Return(position,return));
  9814. END;
  9815. ReleaseIntermediateOperand(return);
  9816. IF currentIsInline THEN RETURN END;
  9817. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9818. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9819. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9820. ELSE
  9821. parametersSize := 0;
  9822. END;
  9823. EmitLeave(section, position,procedure, cc);
  9824. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9825. END;
  9826. IF Trace THEN TraceExit("VisitCode") END;
  9827. END VisitCode;
  9828. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9829. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9830. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9831. const, call: IntermediateCode.Operand;
  9832. parameterDesignator: SyntaxTree.Expression;
  9833. saved: RegisterEntry;
  9834. name: Basic.SegmentedName;
  9835. BEGIN
  9836. IF Trace THEN TraceEnter("ParameterCopies") END;
  9837. parameter := x.firstParameter;
  9838. WHILE parameter # NIL DO
  9839. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9840. type := parameter.type.resolved;
  9841. IF IsOpenArray(type) THEN
  9842. VisitParameter(parameter);
  9843. op := result;
  9844. IF backend.cooperative & parameter.NeedsTrace() THEN
  9845. length := GetArrayLength(type, op.tag);
  9846. size := NewRegisterOperand(addressType);
  9847. base := ArrayBaseType(type);
  9848. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9849. Emit(Mul(position, size, length, const));
  9850. dst := NewRegisterOperand (addressType);
  9851. Emit(Mov(position, dst, size));
  9852. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9853. Emit(Sub(position,dst,sp,dst));
  9854. Emit(And(position,dst,dst,const));
  9855. Emit(Mov(position,sp,dst));
  9856. par := fp;
  9857. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9858. IntermediateCode.InitImmediate(null, byteType, 0);
  9859. Emit(Fill(position, dst, size, null));
  9860. ReleaseIntermediateOperand(dst);
  9861. ReleaseIntermediateOperand(length);
  9862. SaveRegisters();ReleaseUsedRegisters(saved);
  9863. (* register dst has been freed before SaveRegisters already *)
  9864. base := ArrayBaseType(type);
  9865. (* assign method of open array *)
  9866. IF base.IsRecordType() THEN
  9867. Emit (Push(position, length));
  9868. Emit (Push(position, dst));
  9869. Emit (Push(position, op.op));
  9870. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9871. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9872. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9873. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9874. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9875. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9876. Emit (Push(position,length));
  9877. Emit (Push(position, dst));
  9878. Emit (Push(position, length));
  9879. Emit (Push(position, op.op));
  9880. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9881. ELSE
  9882. Emit (Push(position, length));
  9883. Emit (Push(position, dst));
  9884. Emit (Push(position, length));
  9885. Emit (Push(position, op.op));
  9886. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9887. ASSERT(ArrayBaseType(type).IsPointer());
  9888. END;
  9889. RestoreRegisters(saved);
  9890. ELSE
  9891. temp := GetDynamicSize(type,op.tag);
  9892. ReuseCopy(size,temp);
  9893. ReleaseIntermediateOperand(temp);
  9894. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9895. Emit(Sub(position,size,sp,size));
  9896. Emit(And(position,size,size,const));
  9897. Emit(Mov(position,sp,size));
  9898. par := fp;
  9899. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9900. ReleaseIntermediateOperand(size);
  9901. size := GetDynamicSize(type,op.tag);
  9902. END;
  9903. Emit(Copy(position,sp,op.op,size));
  9904. ReleaseIntermediateOperand(size);
  9905. ReleaseOperand(op);
  9906. IntermediateCode.MakeMemory(par,addressType);
  9907. Emit(Mov(position,par,sp));
  9908. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9909. checker.SetCurrentScope(currentScope);
  9910. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9911. Assign(parameterDesignator,parameterDesignator);
  9912. END;
  9913. END;
  9914. parameter := parameter.nextParameter;
  9915. END;
  9916. IF Trace THEN TraceExit("ParameterCopies") END;
  9917. END ParameterCopies;
  9918. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9919. VAR x: SyntaxTree.Variable;
  9920. BEGIN
  9921. x := scope.firstVariable;
  9922. WHILE x # NIL DO
  9923. InitVariable(x);
  9924. x := x.nextVariable;
  9925. END;
  9926. END InitVariables;
  9927. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9928. BEGIN
  9929. IF (symbol # NIL) THEN
  9930. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9931. ELSE
  9932. RETURN 0
  9933. END;
  9934. END GetFingerprint;
  9935. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9936. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9937. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9938. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9939. name: Basic.SegmentedName;
  9940. offset: LONGINT;
  9941. BEGIN
  9942. IF Trace THEN TraceEnter("Body") END;
  9943. ReleaseUsedRegisters(saved); (* just in case ... *)
  9944. section := ir;
  9945. exitLabel := NewLabel ();
  9946. IF moduleBody THEN moduleBodySection := section END;
  9947. IF ir.comments # NIL THEN
  9948. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9949. commentPrintout.SingleStatement(TRUE);
  9950. dump := ir.comments;
  9951. ELSE
  9952. commentPrintout := NIL;
  9953. dump := NIL;
  9954. END;
  9955. prevScope := currentScope;
  9956. currentScope := scope;
  9957. lastSwitchPC := 0;
  9958. cooperativeSwitches := backend.cooperative;
  9959. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9960. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9961. IF x # NIL THEN
  9962. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9963. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9964. IF moduleBody THEN
  9965. ProfilerInit();
  9966. ELSE
  9967. Basic.SegmentedNameToString(ir.name, string);
  9968. ProfilerAddProcedure(numberProcedures,string);
  9969. ProfilerEnterExit(numberProcedures,TRUE);
  9970. END;
  9971. END;
  9972. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9973. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9974. END;
  9975. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9976. IF moduleBody & ~newObjectFile THEN
  9977. InitVariables(moduleScope)
  9978. END;
  9979. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9980. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9981. IF procedure = cellScope.bodyProcedure THEN
  9982. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9983. StaticCallOperand(op, cellScope.constructor);
  9984. Emit(Call(position,op.op,0));
  9985. END;
  9986. END;
  9987. END;
  9988. ParameterCopies(procedureType);
  9989. InitVariables(scope);
  9990. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9991. GetCodeSectionNameForSymbol(procedure, name);
  9992. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9993. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9994. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9995. IntermediateCode.SetOffset(right,offset); (* tag *)
  9996. IntermediateCode.InitMemory(left,addressType,fp,0);
  9997. Emit(Mov(position, left, right));
  9998. END;
  9999. IF x.code = NIL THEN
  10000. VisitStatementBlock(x);
  10001. ELSE
  10002. VisitCode(x.code)
  10003. END;
  10004. IF x.finally # NIL THEN (*! mark finally block for object file *)
  10005. ir.SetFinally(ir.pc);
  10006. StatementSequence(x.finally)
  10007. END;
  10008. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  10009. IF ~backend.cooperative THEN
  10010. ProfilerEnterExit(numberProcedures,FALSE);
  10011. END;
  10012. INC(numberProcedures);
  10013. END;
  10014. END;
  10015. IF backend.cooperative THEN
  10016. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  10017. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  10018. END;
  10019. IF x # NIL THEN
  10020. SELF.position := x.position;
  10021. END;
  10022. CheckRegistersFree();
  10023. ASSERT(modifyAssignmentCounter = 0);
  10024. currentScope := prevScope;
  10025. IF Trace THEN TraceExit("Body") END;
  10026. END Body;
  10027. END ImplementationVisitor;
  10028. MetaDataGenerator=OBJECT
  10029. VAR
  10030. implementationVisitor: ImplementationVisitor;
  10031. declarationVisitor: DeclarationVisitor;
  10032. module: Sections.Module;
  10033. moduleName: ARRAY 128 OF CHAR;
  10034. moduleNamePool: Basic.HashTableInt;
  10035. moduleNamePoolSection: IntermediateCode.Section;
  10036. modulePointerSection: IntermediateCode.Section;
  10037. modulePointerSizePC: LONGINT;
  10038. modulePointerSectionOffset: LONGINT;
  10039. modulePointers: LONGINT;
  10040. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10041. RecordBaseOffset: LONGINT;
  10042. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10043. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10044. TypeTags: LONGINT; (* type extension level support *)
  10045. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10046. patchInfoPC: LONGINT;
  10047. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10048. BEGIN
  10049. IF implementationVisitor.backend.cooperative THEN
  10050. TypeTags := MAX(LONGINT);
  10051. BaseTypesTableOffset := 0;
  10052. MethodTableOffset := 2;
  10053. TypeRecordBaseOffset := 0;
  10054. RecordBaseOffset := 0;
  10055. ELSIF simple THEN
  10056. TypeTags := 3; (* only 3 extensions allowed *)
  10057. BaseTypesTableOffset := 1;
  10058. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10059. TypeRecordBaseOffset := 0;
  10060. RecordBaseOffset := 1;
  10061. ELSE
  10062. TypeTags := 16;
  10063. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10064. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10065. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10066. RecordBaseOffset := 8;
  10067. END;
  10068. SELF.simple := simple;
  10069. SELF.implementationVisitor := implementationVisitor;
  10070. SELF.declarationVisitor := declarationVisitor;
  10071. implementationVisitor.meta := SELF;
  10072. declarationVisitor.meta := SELF;
  10073. END InitMetaDataGenerator;
  10074. PROCEDURE SetModule(module: Sections.Module);
  10075. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10076. BEGIN
  10077. SELF.module := module;
  10078. Global.GetModuleName(module.module,moduleName);
  10079. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10080. NEW(moduleNamePool, 32);
  10081. (*! require GC protection *)
  10082. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10083. name := "Heaps.AnyPtr";
  10084. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10085. (* set base pointer *)
  10086. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10087. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10088. modulePointers := 0;
  10089. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10090. AddPointer(moduleNamePoolSection, namePoolOffset);
  10091. END;
  10092. END SetModule;
  10093. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10094. BEGIN
  10095. IF ~implementationVisitor.backend.cooperative THEN
  10096. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10097. INC(modulePointers);
  10098. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10099. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10100. END;
  10101. END AddPointer;
  10102. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10103. BEGIN
  10104. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10105. END GetTypeRecordBaseOffset;
  10106. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10107. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10108. BEGIN
  10109. INC(dataAdrOffset,6);
  10110. Info(section,"headerAdr");
  10111. Address(section,0);
  10112. Info(section,"typeDesc");
  10113. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10114. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10115. NamedSymbol(section, name, symbol, 0, offset);
  10116. Info(section,"mark: LONGINT;");
  10117. Longint(section,-1);
  10118. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10119. Info(section,"dataAdr-: ADDRESS");
  10120. Symbol(section,section, dataAdrOffset,0);
  10121. Info(section,"size-: SIZE");
  10122. Address(section,0);
  10123. Info(section,"nextRealtime: HeapBlock;");
  10124. Address(section,0);
  10125. END HeapBlock;
  10126. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10127. VAR i: LONGINT;
  10128. BEGIN
  10129. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10130. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10131. Info(section,"count*: LONGINT");
  10132. Longint(section,0);
  10133. Info(section,"locked*: BOOLEAN");
  10134. Longint(section,0);
  10135. Info(section,"awaitingLock*: ProcessQueue");
  10136. Address(section,0);
  10137. Address(section,0);
  10138. Info(section,"awaitingCond*: ProcessQueue");
  10139. Address(section,0);
  10140. Address(section,0);
  10141. Info(section,"lockedBy*: ANY");
  10142. Address(section,0);
  10143. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10144. Longint(section,1);
  10145. FOR i := 2 TO 6 DO
  10146. Longint(section,0);
  10147. END;
  10148. Info(section,"lock*: ANY");
  10149. Address(section,0);
  10150. END ProtectedHeapBlock;
  10151. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10152. BEGIN
  10153. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10154. END Info;
  10155. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10156. VAR op: IntermediateCode.Operand;
  10157. BEGIN
  10158. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10159. section.Emit(Data(Basic.invalidPosition,op));
  10160. END Address;
  10161. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10162. VAR op: IntermediateCode.Operand;
  10163. BEGIN
  10164. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10165. section.Emit(Data(Basic.invalidPosition,op));
  10166. END Size;
  10167. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10168. VAR op: IntermediateCode.Operand;
  10169. BEGIN
  10170. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10171. section.Emit(Data(Basic.invalidPosition,op));
  10172. END Set;
  10173. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10174. VAR op: IntermediateCode.Operand;
  10175. BEGIN
  10176. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10177. section.Emit(Data(Basic.invalidPosition,op));
  10178. END Longint;
  10179. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10180. VAR op,noOperand: IntermediateCode.Operand;
  10181. BEGIN
  10182. IntermediateCode.InitOperand(noOperand);
  10183. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10184. section.PatchOperands(pc,op,noOperand,noOperand);
  10185. END PatchAddress;
  10186. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10187. VAR op,noOperand: IntermediateCode.Operand;
  10188. BEGIN
  10189. IntermediateCode.InitOperand(noOperand);
  10190. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10191. section.PatchOperands(pc,op,noOperand,noOperand);
  10192. END PatchSize;
  10193. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10194. VAR op,noOperand: IntermediateCode.Operand;
  10195. BEGIN
  10196. IntermediateCode.InitOperand(noOperand);
  10197. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10198. section.PatchOperands(pc,op,noOperand,noOperand);
  10199. END PatchLongint;
  10200. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10201. VAR op, noOperand: IntermediateCode.Operand;
  10202. BEGIN
  10203. IntermediateCode.InitOperand(noOperand);
  10204. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10205. section.PatchOperands(pc,op,noOperand,noOperand);
  10206. END PatchSymbol;
  10207. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10208. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10209. BEGIN
  10210. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10211. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10212. section.Emit(Data(Basic.invalidPosition,op));
  10213. END Boolean;
  10214. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10215. VAR op: IntermediateCode.Operand;
  10216. BEGIN
  10217. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10218. section.Emit(Data(Basic.invalidPosition,op));
  10219. END Char;
  10220. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10221. VAR op: IntermediateCode.Operand;
  10222. BEGIN
  10223. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10224. section.Emit(Data(Basic.invalidPosition,op));
  10225. END Integer;
  10226. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10227. VAR i: LONGINT;
  10228. BEGIN
  10229. Info(section,str);
  10230. i := 0;
  10231. WHILE(str[i] # 0X) DO
  10232. Char(section,str[i]);
  10233. INC(i);
  10234. END;
  10235. Char(section,0X);
  10236. END String;
  10237. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10238. VAR s: Basic.SectionName;
  10239. BEGIN
  10240. StringPool.GetString(str, s);
  10241. String(section, s);
  10242. END String0;
  10243. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10244. VAR op: IntermediateCode.Operand;
  10245. BEGIN
  10246. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10247. IntermediateCode.SetOffset(op,realOffset);
  10248. section.Emit(Data(Basic.invalidPosition,op));
  10249. END NamedSymbol;
  10250. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10251. VAR op: IntermediateCode.Operand;
  10252. BEGIN
  10253. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10254. IntermediateCode.SetOffset(op,realOffset);
  10255. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10256. END NamedSymbolAt;
  10257. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10258. BEGIN
  10259. IF symbol= NIL THEN
  10260. Address( section, realOffset);
  10261. ASSERT(virtualOffset = 0);
  10262. ELSE
  10263. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10264. END;
  10265. END Symbol;
  10266. (* OutPointers delivers
  10267. {pointerOffset}
  10268. *)
  10269. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10270. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10271. BEGIN
  10272. type := type.resolved;
  10273. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10274. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10275. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10276. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10277. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10278. ELSIF type IS SyntaxTree.PointerType THEN
  10279. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10280. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10281. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10282. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10283. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10284. ELSIF (type IS SyntaxTree.RecordType) THEN
  10285. (* never treat a record like a pointer, even if the pointer field is set! *)
  10286. WITH type: SyntaxTree.RecordType DO
  10287. base := type.GetBaseRecord();
  10288. IF base # NIL THEN
  10289. Pointers(offset,symbol,section, base,numberPointers);
  10290. END;
  10291. variable := type.recordScope.firstVariable;
  10292. WHILE(variable # NIL) DO
  10293. IF ~(variable.untraced) THEN
  10294. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10295. END;
  10296. variable := variable.nextVariable;
  10297. END;
  10298. END;
  10299. ELSIF (type IS SyntaxTree.CellType) THEN
  10300. WITH type: SyntaxTree.CellType DO
  10301. base := type.GetBaseRecord();
  10302. IF base # NIL THEN
  10303. Pointers(offset,symbol,section, base,numberPointers);
  10304. END;
  10305. variable := type.cellScope.firstVariable;
  10306. WHILE(variable # NIL) DO
  10307. IF ~(variable.untraced) THEN
  10308. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10309. END;
  10310. variable := variable.nextVariable;
  10311. END;
  10312. property := type.firstProperty;
  10313. WHILE(property # NIL) DO
  10314. IF ~(property.untraced) THEN
  10315. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10316. END;
  10317. property := property.nextProperty;
  10318. END;
  10319. parameter := type.firstParameter;
  10320. WHILE(parameter # NIL) DO
  10321. IF ~(parameter.untraced) THEN
  10322. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10323. END;
  10324. parameter := parameter.nextParameter;
  10325. END;
  10326. END;
  10327. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10328. WITH type: SyntaxTree.ArrayType DO
  10329. IF type.form= SyntaxTree.Static THEN
  10330. n := type.staticLength;
  10331. base := type.arrayBase.resolved;
  10332. WHILE(base IS SyntaxTree.ArrayType) DO
  10333. type := base(SyntaxTree.ArrayType);
  10334. n := n* type.staticLength;
  10335. base := type.arrayBase.resolved;
  10336. END;
  10337. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10338. IF SemanticChecker.ContainsPointer(base) THEN
  10339. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10340. FOR i := 0 TO n-1 DO
  10341. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10342. END;
  10343. END;
  10344. ELSE
  10345. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10346. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10347. END;
  10348. END;
  10349. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10350. WITH type: SyntaxTree.MathArrayType DO
  10351. IF type.form = SyntaxTree.Static THEN
  10352. n := type.staticLength;
  10353. base := type.arrayBase.resolved;
  10354. WHILE(base IS SyntaxTree.MathArrayType) DO
  10355. type := base(SyntaxTree.MathArrayType);
  10356. n := n* type.staticLength;
  10357. base := type.arrayBase.resolved;
  10358. END;
  10359. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10360. IF SemanticChecker.ContainsPointer(base) THEN
  10361. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10362. FOR i := 0 TO n-1 DO
  10363. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10364. END;
  10365. END;
  10366. ELSE
  10367. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10368. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10369. END
  10370. END;
  10371. (* ELSE no pointers in type *)
  10372. END;
  10373. END Pointers;
  10374. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10375. VAR position,i: LONGINT; ch: CHAR;
  10376. BEGIN
  10377. IF pool.Has(index) THEN
  10378. RETURN pool.GetInt(index)
  10379. ELSE
  10380. position := source.pc;
  10381. pool.PutInt(index, position);
  10382. Info(source, name);
  10383. i := 0;
  10384. REPEAT
  10385. ch := name[i]; INC(i);
  10386. Char( source, ch);
  10387. UNTIL ch = 0X;
  10388. END;
  10389. RETURN position;
  10390. END EnterDynamicName;
  10391. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10392. VAR name: Basic.SectionName; position: LONGINT;
  10393. BEGIN
  10394. IF pool.Has(index) THEN
  10395. RETURN pool.GetInt(index)
  10396. ELSE
  10397. StringPool.GetString(index, name);
  10398. position := EnterDynamicName(source,name,index, pool);
  10399. END;
  10400. RETURN position;
  10401. END DynamicName;
  10402. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10403. VAR section: IntermediateCode.Section;
  10404. BEGIN
  10405. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10406. IF implementationVisitor.backend.cooperative THEN
  10407. Info(section, "TypeDescriptor");
  10408. Basic.ToSegmentedName("BaseTypes.Array", name);
  10409. NamedSymbol(section, name,NIL, 0, 0);
  10410. BasePointer(section);
  10411. offset := 0;
  10412. ELSE
  10413. HeapBlock(mName,typeName,section,2);
  10414. Info(section, "HeapBlock");
  10415. (*
  10416. Symbol(section,section,2,0);
  10417. *)
  10418. Address(section,0); (* empty such that GC does not go on traversing *)
  10419. Info(section, "TypeDescriptor");
  10420. Address(section,0);
  10421. offset := section.pc;
  10422. END;
  10423. RETURN section
  10424. END NamedBlock;
  10425. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10426. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10427. BEGIN
  10428. COPY(moduleName,name);
  10429. Strings.Append(name,suffix);
  10430. Basic.ToSegmentedName(name, pooledName);
  10431. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10432. END Block;
  10433. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10434. VAR name: Basic.SegmentedName;
  10435. BEGIN
  10436. Info(source,"ArrayHeader");
  10437. IF implementationVisitor.backend.cooperative THEN
  10438. sizePC := source.pc;
  10439. Address(source,0);
  10440. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10441. IF baseType # "" THEN
  10442. Basic.ToSegmentedName(baseType, name);
  10443. NamedSymbol(source, name,NIL, 0, 0);
  10444. ELSE
  10445. Address(source,0);
  10446. END;
  10447. Address(source,0);
  10448. ELSE
  10449. Address(source,0);
  10450. Address(source,0);
  10451. (* first pointer for GC *)
  10452. IF hasPointer THEN
  10453. (* points to first element in the array, this is NOT the base type descriptor *)
  10454. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10455. ELSE
  10456. Address(source,0);
  10457. END;
  10458. sizePC := source.pc;
  10459. Address(source,0);
  10460. Info(source,"array data");
  10461. END;
  10462. END ArrayBlock;
  10463. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10464. BEGIN
  10465. IF implementationVisitor.backend.cooperative THEN
  10466. PatchSize(section, pc, size);
  10467. PatchSize(section, pc + 3, size);
  10468. ELSE
  10469. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10470. PatchSize(section, pc, size);
  10471. END;
  10472. END PatchArray;
  10473. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10474. VAR
  10475. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10476. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10477. poolMap: Basic.HashTableInt;
  10478. namePool: IntermediateCode.Section;
  10479. namePoolOffset: LONGINT;
  10480. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10481. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10482. BEGIN
  10483. Basic.SegmentedNameToString(s1.name,n1);
  10484. Basic.SegmentedNameToString(s2.name,n2);
  10485. index := 0;
  10486. ch1 := n1[index];
  10487. ch2 := n2[index];
  10488. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10489. INC(index);
  10490. ch1 := n1[index];
  10491. ch2 := n2[index];
  10492. END;
  10493. RETURN ch1 < ch2;
  10494. END Compare;
  10495. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10496. VAR
  10497. i, j: LONGINT;
  10498. x, t: Sections.Section;
  10499. BEGIN
  10500. IF lo < hi THEN
  10501. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10502. WHILE i <= j DO
  10503. WHILE Compare(list[i], x) DO INC(i) END;
  10504. WHILE Compare(x, list[j]) DO DEC(j) END;
  10505. IF i <= j THEN
  10506. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10507. INC(i); DEC(j)
  10508. END
  10509. END;
  10510. IF lo < j THEN QuickSort(list, lo, j) END;
  10511. IF i < hi THEN QuickSort(list, i, hi) END
  10512. END;
  10513. END QuickSort;
  10514. (*
  10515. ExportDesc* = RECORD
  10516. fp*: ADDRESS;
  10517. name* {UNTRACED}: DynamicName;
  10518. adr*: ADDRESS;
  10519. exports*: LONGINT;
  10520. dsc* {UNTRACED}: ExportArray
  10521. END;
  10522. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10523. *)
  10524. PROCEDURE ExportDesc2(
  10525. source: IntermediateCode.Section;
  10526. namePool: IntermediateCode.Section;
  10527. fingerPrinter: FingerPrinter.FingerPrinter;
  10528. symbol: Sections.Section;
  10529. name: StringPool.Index;
  10530. VAR patchAdr: LONGINT
  10531. ): BOOLEAN;
  10532. VAR fingerPrint: SyntaxTree.FingerPrint;
  10533. BEGIN
  10534. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10535. & (symbol.type # Sections.InlineCodeSection)
  10536. THEN
  10537. *)
  10538. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10539. & (symbol.type # Sections.InlineCodeSection))
  10540. THEN
  10541. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10542. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10543. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10544. Longint(source,fingerPrint.shallow);
  10545. ELSE
  10546. Longint(source, 0);
  10547. END;
  10548. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10549. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10550. Symbol(source, symbol,0,0);
  10551. patchAdr := source.pc;
  10552. Longint(source, 0);
  10553. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10554. Address(source,0);
  10555. END;
  10556. RETURN TRUE
  10557. ELSE
  10558. RETURN FALSE
  10559. END;
  10560. END ExportDesc2;
  10561. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10562. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10563. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10564. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10565. nextPatch: LONGINT;
  10566. TYPE
  10567. Scope = RECORD
  10568. elements: LONGINT;
  10569. gelements: LONGINT;
  10570. section: IntermediateCode.Section;
  10571. patchAdr: LONGINT;
  10572. arraySizePC: LONGINT;
  10573. beginPC: LONGINT; (* current scope start pc *)
  10574. END;
  10575. BEGIN
  10576. Basic.InitSegmentedName(prev);
  10577. olevel := -1;
  10578. scopes[0].section := source;
  10579. scopes[0].arraySizePC := MIN(LONGINT);
  10580. FOR s := 0 TO LEN(sections)-1 DO
  10581. symbol := sections[s];
  10582. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10583. this := sections[s].name;
  10584. level := 0;
  10585. WHILE (this[level] > 0) DO
  10586. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10587. INC(level);
  10588. END;
  10589. WHILE level < olevel DO
  10590. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10591. IF olevel > 0 THEN
  10592. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10593. nextPatch := scopes[olevel-1].patchAdr+1;
  10594. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10595. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10596. END;
  10597. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10598. DEC(olevel);
  10599. END;
  10600. IF (this[level] > 0) THEN
  10601. IF level > olevel THEN
  10602. (*TRACE("opening",level); *)
  10603. IF scopes[level].section = NIL THEN
  10604. arrayName := ".@ExportArray";
  10605. Strings.AppendInt(arrayName, level);
  10606. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10607. AddPointer(scopes[level].section,offset);
  10608. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10609. END;
  10610. scopes[level].beginPC := scopes[level].section.pc;
  10611. olevel := level;
  10612. scopes[olevel].elements := 0;
  10613. END;
  10614. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10615. sym := sections[s];
  10616. ELSE
  10617. sym := NIL;
  10618. END;
  10619. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10620. THEN
  10621. INC(scopes[olevel].elements);
  10622. END;
  10623. (* enter string in scope *)
  10624. INC(level);
  10625. END;
  10626. END;
  10627. Basic.SegmentedNameToString(this, name);
  10628. prev := this;
  10629. END;
  10630. END;
  10631. WHILE 0 <= olevel DO
  10632. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10633. IF olevel > 0 THEN
  10634. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10635. nextPatch := scopes[olevel-1].patchAdr+1;
  10636. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10637. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10638. END;
  10639. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10640. DEC(olevel);
  10641. END;
  10642. level := 0;
  10643. WHILE (level < LEN(scopes)) DO
  10644. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10645. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10646. END;
  10647. INC(level);
  10648. END;
  10649. END Export;
  10650. BEGIN
  10651. NEW(fingerPrinter);
  10652. NEW(poolMap, 64);
  10653. (* 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 *)
  10654. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10655. NEW(sectionArray, module.allSections.Length());
  10656. FOR i := 0 TO module.allSections.Length() - 1 DO
  10657. section := module.allSections.GetSection(i);
  10658. sectionArray[i] := section;
  10659. END;
  10660. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10661. Export(sectionArray^);
  10662. END ExportDesc;
  10663. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10664. VAR
  10665. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10666. BEGIN
  10667. Info(source, "exception table offsets array descriptor");
  10668. size := 0;
  10669. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10670. Info(source, "exception table content");
  10671. FOR i := 0 TO module.allSections.Length() - 1 DO
  10672. p := module.allSections.GetSection(i);
  10673. IF p.type = Sections.CodeSection THEN
  10674. finallyPC := p(IntermediateCode.Section).finally;
  10675. IF finallyPC>=0 THEN
  10676. Symbol( source, p, 0,0);
  10677. Symbol( source, p, finallyPC, 0);
  10678. Symbol( source, p, finallyPC,0);
  10679. INC(size);
  10680. END;
  10681. END
  10682. END;
  10683. PatchArray(source,sizePC,size);
  10684. END ExceptionArray;
  10685. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10686. VAR i: LONGINT; ch: CHAR;
  10687. BEGIN
  10688. i := 0;
  10689. REPEAT
  10690. ch := name[i]; INC(i);
  10691. Char( section, ch);
  10692. UNTIL ch = 0X;
  10693. WHILE i < 32 DO
  10694. Char( section, 0X); INC(i);
  10695. END;
  10696. END Name;
  10697. PROCEDURE References(section: IntermediateCode.Section);
  10698. CONST
  10699. sfTypeNone = 0X;
  10700. sfTypeCHAR = 01X;
  10701. sfTypeCHAR8 = 02X;
  10702. sfTypeCHAR16 = 03X;
  10703. sfTypeCHAR32 = 04X;
  10704. sfTypeRANGE = 05X;
  10705. sfTypeSHORTINT = 06X;
  10706. sfTypeINTEGER = 07X;
  10707. sfTypeLONGINT = 08X;
  10708. sfTypeHUGEINT = 09X;
  10709. sfTypeWORD = 0AX;
  10710. sfTypeLONGWORD = 0BX;
  10711. sfTypeSIGNED8 = 0CX;
  10712. sfTypeSIGNED16 = 0DX;
  10713. sfTypeSIGNED32 = 0EX;
  10714. sfTypeSIGNED64 = 0FX;
  10715. sfTypeUNSIGNED8 = 10X;
  10716. sfTypeUNSIGNED16 = 11X;
  10717. sfTypeUNSIGNED32 = 12X;
  10718. sfTypeUNSIGNED64 = 13X;
  10719. sfTypeREAL = 14X;
  10720. sfTypeLONGREAL = 15X;
  10721. sfTypeCOMPLEX = 16X;
  10722. sfTypeLONGCOMPLEX = 17X;
  10723. sfTypeBOOLEAN = 18X;
  10724. sfTypeSET = 19X;
  10725. sfTypeANY = 1AX;
  10726. sfTypeOBJECT = 1BX;
  10727. sfTypeBYTE = 1CX;
  10728. sfTypeADDRESS = 1DX;
  10729. sfTypeSIZE = 1EX;
  10730. sfTypeIndirect = 1FX;
  10731. sfTypeRecord = 20X;
  10732. sfTypePointerToRecord = 21X;
  10733. sfTypePointerToArray = 22X;
  10734. sfTypeOpenArray = 23X;
  10735. sfTypeStaticArray = 24X;
  10736. sfTypeDynamicArray = 25X;
  10737. sfTypeMathStaticArray = 26X;
  10738. sfTypeMathOpenArray = 27X;
  10739. sfTypeMathTensor = 28X;
  10740. sfTypeDelegate = 29X;
  10741. sfTypeENUM = 2AX;
  10742. sfTypeCELL = 2BX;
  10743. sfTypePORT = 2CX;
  10744. sfIN = 0X;
  10745. sfOUT = 1X;
  10746. flagDelegate = 0;
  10747. flagConstructor = 1;
  10748. (* variable / parameter addressing modes *)
  10749. sfAbsolute = 0X; (* global vars *)
  10750. sfRelative = 1X; (* variables, value parameters *)
  10751. sfIndirect = 2X; (* var parameters *)
  10752. sfScopeBegin = 0F0X;
  10753. sfScopeEnd = 0F1X;
  10754. sfProcedure = 0F2X;
  10755. sfVariable = 0F3X;
  10756. sfTypeDeclaration = 0F4X;
  10757. sfModule = 0FFX;
  10758. RefInfo = TRUE;
  10759. VAR
  10760. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10761. indirectTypes: Basic.HashTable;
  10762. PROCEDURE CurrentIndex(): SIZE;
  10763. VAR i: LONGINT;
  10764. BEGIN
  10765. FOR i := startPC TO section.pc -1 DO
  10766. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10767. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10768. END;
  10769. startPC := section.pc;
  10770. RETURN lastOffset;
  10771. END CurrentIndex;
  10772. (*
  10773. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10774. Module = sfModule prevSymbol:SIZE name:String Scope.
  10775. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10776. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10777. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10778. Type =
  10779. sfTypePointerToRecord
  10780. | sfTypePointerToArray Type
  10781. | sfTypeOpenArray Type
  10782. | sfTypeDynamicArray Type
  10783. | sfTypeStaticArray length:SIZE Type
  10784. | sfTypeMathOpenArray Type
  10785. | sfTypeMathStaticArray length:SIZE Type
  10786. | sfTypeMathTensor Type
  10787. | sfTypeRecord tdAdr:ADDRESS
  10788. | sfTypeDelegate {Parameter} return:Type
  10789. | sfTypePort (sfIN | sfOUT)
  10790. | sfTypeBOOLEAN
  10791. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10792. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10793. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10794. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10795. | sfTypeWORD | sfTypeLONGWORD
  10796. | sfTypeREAL | sfTypeLONGREAL
  10797. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10798. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10799. | sfTypeIndirect offset:SIZE.
  10800. *)
  10801. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10802. VAR offset: SIZE;
  10803. BEGIN
  10804. IF indirectTypes.Has(type) THEN
  10805. offset := indirectTypes.GetInt(type);
  10806. Char(section, sfTypeIndirect);
  10807. Size(section, offset);
  10808. RETURN TRUE;
  10809. ELSE
  10810. indirectTypes.PutInt(type, CurrentIndex());
  10811. RETURN FALSE;
  10812. END;
  10813. END Indirect;
  10814. PROCEDURE NType(type: SyntaxTree.Type);
  10815. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10816. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10817. BEGIN
  10818. IF type = NIL THEN
  10819. Char(section, sfTypeNone)
  10820. ELSE
  10821. type := type.resolved;
  10822. size := type.sizeInBits;
  10823. WITH type:SyntaxTree.PointerType DO
  10824. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10825. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10826. Char(section, sfTypePointerToRecord);
  10827. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10828. ELSE
  10829. IF RefInfo THEN Info(section,"PointerToArray") END;
  10830. Char(section, sfTypePointerToArray);
  10831. NType(type.pointerBase);
  10832. END;
  10833. | type: SyntaxTree.ArrayType DO
  10834. IF ~Indirect(type) THEN
  10835. IF type.form = SyntaxTree.Open THEN
  10836. IF RefInfo THEN Info(section,"OpenArray") END;
  10837. Char(section, sfTypeOpenArray);
  10838. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10839. IF RefInfo THEN Info(section,"DynamicArray") END;
  10840. Char(section, sfTypeDynamicArray);
  10841. ELSIF type.form = SyntaxTree.Static THEN
  10842. IF RefInfo THEN Info(section,"StaticArray") END;
  10843. Char(section, sfTypeStaticArray);
  10844. Size(section, type.staticLength);
  10845. ELSE
  10846. HALT(100);
  10847. END;
  10848. NType(type.arrayBase);
  10849. END;
  10850. | type: SyntaxTree.MathArrayType DO
  10851. IF ~Indirect(type) THEN
  10852. IF type.form = SyntaxTree.Open THEN
  10853. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10854. Char(section, sfTypeMathOpenArray);
  10855. ELSIF type.form = SyntaxTree.Static THEN
  10856. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10857. Char(section, sfTypeMathStaticArray);
  10858. Size(section, type.staticLength);
  10859. ELSIF type.form = SyntaxTree.Tensor THEN
  10860. IF RefInfo THEN Info(section,"MathTensor") END;
  10861. Char(section, sfTypeMathTensor);
  10862. ELSE
  10863. HALT(100);
  10864. END;
  10865. NType(type.arrayBase);
  10866. END;
  10867. | type: SyntaxTree.RecordType DO
  10868. IF ~Indirect(type) THEN
  10869. IF type.pointerType # NIL (* OBJECT *) THEN
  10870. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10871. Char(section, sfTypePointerToRecord)
  10872. ELSE
  10873. IF RefInfo THEN Info(section,"Record") END;
  10874. Char(section, sfTypeRecord);
  10875. td := type.typeDeclaration;
  10876. IF RefInfo THEN Info(section,"TD") END;
  10877. IF (td # NIL) THEN
  10878. Global.GetSymbolSegmentedName(td,segmentedName);
  10879. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10880. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10881. ELSE
  10882. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10883. END;
  10884. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10885. Symbol(section, tir, 0, offset);
  10886. ELSE
  10887. Address(section, 0);
  10888. END;
  10889. END;
  10890. END;
  10891. | type: SyntaxTree.CellType DO
  10892. IF ~Indirect(type) THEN
  10893. IF RefInfo THEN Info(section,"Record") END;
  10894. Char(section, sfTypeRecord);
  10895. td := type.typeDeclaration;
  10896. IF RefInfo THEN Info(section,"TD") END;
  10897. IF (td # NIL) THEN
  10898. Global.GetSymbolSegmentedName(td,segmentedName);
  10899. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10900. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10901. ELSE
  10902. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10903. END;
  10904. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10905. Symbol(section, tir, 0, offset);
  10906. ELSE
  10907. Address(section, 0);
  10908. END;
  10909. END;
  10910. | type: SyntaxTree.PortType DO
  10911. Char(section, sfTypePORT);
  10912. IF type.direction = SyntaxTree.OutPort THEN
  10913. Char(section, sfOUT)
  10914. ELSE
  10915. Char(section, sfIN)
  10916. END;
  10917. | type: SyntaxTree.ProcedureType DO
  10918. IF ~Indirect(type) THEN
  10919. Char(section, sfTypeDelegate);
  10920. parameter := type.firstParameter;
  10921. WHILE(parameter # NIL) DO
  10922. NParameter(parameter, -1);
  10923. parameter := parameter.nextParameter;
  10924. END;
  10925. NType(type.returnType);
  10926. END;
  10927. | type:SyntaxTree.EnumerationType DO
  10928. Char(section, sfTypeENUM);
  10929. | type: SyntaxTree.BasicType DO
  10930. WITH type: SyntaxTree.BooleanType DO
  10931. IF RefInfo THEN Info(section,"Boolean") END;
  10932. Char(section, sfTypeBOOLEAN);
  10933. | type: SyntaxTree.CharacterType DO
  10934. IF type = module.system.characterType THEN
  10935. IF RefInfo THEN Info(section,"CHAR") END;
  10936. Char(section, sfTypeCHAR);
  10937. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10938. IF RefInfo THEN Info(section,"CHAR8") END;
  10939. Char(section, sfTypeCHAR8)
  10940. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10941. IF RefInfo THEN Info(section,"CHAR16") END;
  10942. Char(section, sfTypeCHAR16);
  10943. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10944. IF RefInfo THEN Info(section,"CHAR32") END;
  10945. Char(section, sfTypeCHAR32);
  10946. ELSE
  10947. HALT(100);
  10948. END;
  10949. |type: SyntaxTree.IntegerType DO
  10950. IF type(SyntaxTree.IntegerType).signed THEN
  10951. IF (type = module.system.shortintType) THEN
  10952. IF RefInfo THEN Info(section,"SHORTINT") END;
  10953. Char(section, sfTypeSHORTINT)
  10954. ELSIF (type = module.system.integerType) THEN
  10955. IF RefInfo THEN Info(section,"INTEGER") END;
  10956. Char(section, sfTypeINTEGER)
  10957. ELSIF (type = module.system.longintType) THEN
  10958. IF RefInfo THEN Info(section,"LONGINT") END;
  10959. Char(section, sfTypeLONGINT)
  10960. ELSIF (type = module.system.hugeintType) THEN
  10961. IF RefInfo THEN Info(section,"HUGEINT") END;
  10962. Char(section, sfTypeHUGEINT)
  10963. ELSIF (type = module.system.wordType) THEN
  10964. IF RefInfo THEN Info(section,"WORD") END;
  10965. Char(section, sfTypeWORD)
  10966. ELSIF (type = module.system.longWordType) THEN
  10967. IF RefInfo THEN Info(section,"LONGWORD") END;
  10968. Char(section, sfTypeLONGWORD);
  10969. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10970. IF RefInfo THEN Info(section,"SIGNED8") END;
  10971. Char(section, sfTypeSIGNED8)
  10972. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10973. IF RefInfo THEN Info(section,"SIGNED16") END;
  10974. Char(section, sfTypeSIGNED16)
  10975. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10976. IF RefInfo THEN Info(section,"SIGNED32") END;
  10977. Char(section, sfTypeSIGNED32)
  10978. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10979. IF RefInfo THEN Info(section,"SIGNED64") END;
  10980. Char(section, sfTypeSIGNED64)
  10981. ELSE
  10982. HALT(100);
  10983. END
  10984. ELSE (* unsigned *)
  10985. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10986. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10987. Char(section, sfTypeUNSIGNED8)
  10988. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10989. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10990. Char(section, sfTypeUNSIGNED16)
  10991. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10992. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10993. Char(section, sfTypeUNSIGNED32)
  10994. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10995. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10996. Char(section, sfTypeUNSIGNED64)
  10997. ELSE
  10998. HALT(100)
  10999. END
  11000. END;
  11001. | type: SyntaxTree.FloatType DO
  11002. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  11003. IF RefInfo THEN Info(section,"REAL") END;
  11004. Char(section, sfTypeREAL);
  11005. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  11006. IF RefInfo THEN Info(section,"LONGREAL") END;
  11007. Char(section, sfTypeLONGREAL);
  11008. ELSE
  11009. HALT(100);
  11010. END;
  11011. |type: SyntaxTree.ComplexType DO
  11012. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  11013. IF RefInfo THEN Info(section,"COMPLEX") END;
  11014. Char(section, sfTypeCOMPLEX);
  11015. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  11016. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  11017. Char(section, sfTypeLONGCOMPLEX);
  11018. ELSE
  11019. HALT(100);
  11020. END;
  11021. |type:SyntaxTree.SetType DO
  11022. IF RefInfo THEN Info(section,"SET") END;
  11023. Char(section, sfTypeSET);
  11024. |type:SyntaxTree.AnyType DO
  11025. IF RefInfo THEN Info(section,"ANY") END;
  11026. Char(section, sfTypeANY);
  11027. |type:SyntaxTree.ObjectType DO
  11028. IF RefInfo THEN Info(section,"OBJECT") END;
  11029. Char(section, sfTypeOBJECT);
  11030. |type:SyntaxTree.ByteType DO
  11031. IF RefInfo THEN Info(section,"BYTE") END;
  11032. Char(section, sfTypeBYTE);
  11033. |type:SyntaxTree.RangeType DO
  11034. IF RefInfo THEN Info(section,"RANGE") END;
  11035. Char(section, sfTypeRANGE)
  11036. |type:SyntaxTree.AddressType DO
  11037. IF RefInfo THEN Info(section,"ADDRESS") END;
  11038. Char(section, sfTypeADDRESS)
  11039. |type:SyntaxTree.SizeType DO
  11040. IF RefInfo THEN Info(section,"SIZE") END;
  11041. Char(section, sfTypeSIZE)
  11042. ELSE
  11043. HALT(100)
  11044. END;
  11045. ELSE HALT(101);
  11046. END;
  11047. END;
  11048. END NType;
  11049. (*
  11050. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11051. *)
  11052. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11053. VAR pos: LONGINT; type: SyntaxTree.Type;
  11054. BEGIN
  11055. IF RefInfo THEN Info(section, "Parameter") END;
  11056. Char(section, sfVariable);
  11057. Size(section, procOffset);
  11058. String0(section, parameter.name);
  11059. type := parameter.type.resolved;
  11060. IF parameter.kind = SyntaxTree.VarParameter THEN
  11061. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11062. ELSE Char(section, sfIndirect)
  11063. END;
  11064. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11065. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11066. Char(section, sfIndirect);
  11067. ELSE
  11068. Char(section, sfRelative);
  11069. END;
  11070. ELSE
  11071. Char(section, sfRelative);
  11072. END;
  11073. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11074. NType(parameter.type);
  11075. END NParameter;
  11076. (*
  11077. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11078. *)
  11079. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11080. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11081. name: Basic.SegmentedName; flags: SET;
  11082. BEGIN
  11083. IF RefInfo THEN Info(section, "Procedure") END;
  11084. pos := CurrentIndex();
  11085. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11086. Char(section, sfProcedure);
  11087. Size(section, scopeOffset);
  11088. String0(section,procedure.name);
  11089. s := module.allSections.FindBySymbol(procedure);
  11090. Symbol(section,s,0,0); (* start *)
  11091. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11092. flags := {};
  11093. IF procedureType.isDelegate THEN
  11094. INCL(flags, flagDelegate);
  11095. END;
  11096. IF procedure.isConstructor THEN
  11097. INCL(flags, flagConstructor);
  11098. END;
  11099. Set(section, flags);
  11100. Global.GetSymbolSegmentedName(procedure,name);
  11101. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11102. IF RefInfo THEN Info(section, "Parameters") END;
  11103. parameter := procedureType.firstParameter;
  11104. WHILE(parameter # NIL) DO
  11105. NParameter(parameter, pos);
  11106. parameter := parameter.nextParameter;
  11107. END;
  11108. IF RefInfo THEN Info(section, "ReturnType") END;
  11109. NType(procedureType.returnType);
  11110. NScope(procedure.procedureScope, pos);
  11111. END NProcedure;
  11112. (*
  11113. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11114. *)
  11115. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11116. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11117. BEGIN
  11118. IF RefInfo THEN Info(section, "Variable") END;
  11119. pos := CurrentIndex();
  11120. Char(section, sfVariable);
  11121. Size(section, scopeOffset);
  11122. String0(section, variable.name);
  11123. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11124. Char(section, sfAbsolute);
  11125. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11126. NamedSymbol(section, sn,variable, 0,0);
  11127. ELSE
  11128. Char(section, sfRelative);
  11129. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11130. END;
  11131. NType(variable.type);
  11132. s := module.allSections.FindBySymbol(variable);
  11133. END NVariable;
  11134. (*
  11135. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11136. *)
  11137. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11138. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11139. BEGIN
  11140. IF typeDeclaration = NIL THEN RETURN END;
  11141. pos := CurrentIndex();
  11142. s := module.allSections.FindBySymbol(typeDeclaration);
  11143. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11144. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11145. Char(section, sfTypeDeclaration);
  11146. Size(section, scopeOffset);
  11147. String0(section, typeDeclaration.name);
  11148. declared := typeDeclaration.declaredType.resolved;
  11149. IF (declared IS SyntaxTree.PointerType) THEN
  11150. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11151. END;
  11152. WITH declared: SyntaxTree.RecordType DO
  11153. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11154. Symbol(section, s, 0, offset);
  11155. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11156. Basic.AppendToSegmentedName(name,".@Info");
  11157. s := module.allSections.FindByName(name);
  11158. IF s # NIL THEN (* does not work for coop *)
  11159. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11160. NScope(declared.recordScope, pos);
  11161. END;
  11162. |declared: SyntaxTree.CellType DO
  11163. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11164. Symbol(section, s, 0, offset);
  11165. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11166. Basic.AppendToSegmentedName(name,".@Info");
  11167. s := module.allSections.FindByName(name);
  11168. IF s # NIL THEN
  11169. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11170. NScope(declared.cellScope, pos);
  11171. END;
  11172. ELSE
  11173. Address(section, 0);
  11174. END;
  11175. END NTypeDeclaration;
  11176. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11177. VAR pos: LONGINT;
  11178. BEGIN
  11179. pos := CurrentIndex();
  11180. Char(section,sfModule);
  11181. Size(section, prevSymbol);
  11182. String0(section, module.name);
  11183. NScope(module.moduleScope, pos);
  11184. END NModule;
  11185. (*
  11186. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11187. *)
  11188. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11189. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11190. BEGIN
  11191. IF scope = NIL THEN RETURN END;
  11192. IF RefInfo THEN Info(section, "Scope") END;
  11193. Char(section, sfScopeBegin);
  11194. variable := scope.firstVariable;
  11195. WHILE (variable # NIL) DO
  11196. NVariable(variable, prevSymbol);
  11197. variable := variable.nextVariable;
  11198. END;
  11199. WITH scope: SyntaxTree.ModuleScope DO
  11200. bodyProcedure := scope.bodyProcedure;
  11201. |scope: SyntaxTree.RecordScope DO
  11202. bodyProcedure := scope.bodyProcedure;
  11203. ELSE
  11204. bodyProcedure := NIL;
  11205. END;
  11206. IF bodyProcedure # NIL THEN
  11207. NProcedure(bodyProcedure, prevSymbol)
  11208. END;
  11209. procedure := scope.firstProcedure;
  11210. WHILE procedure # NIL DO
  11211. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11212. procedure := procedure.nextProcedure;
  11213. END;
  11214. typeDeclaration := scope.firstTypeDeclaration;
  11215. WHILE typeDeclaration # NIL DO
  11216. NTypeDeclaration(typeDeclaration, prevSymbol);
  11217. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11218. END;
  11219. Char(section, sfScopeEnd); (* scope ends *)
  11220. END NScope;
  11221. BEGIN
  11222. NEW(indirectTypes, 32);
  11223. ArrayBlock(section,sizePC,"", FALSE);
  11224. IF ~implementationVisitor.backend.cooperative THEN
  11225. startPC := section.pc;
  11226. NModule(module.module, -1);
  11227. PatchArray(section,sizePC,CurrentIndex());
  11228. END;
  11229. END References;
  11230. (*
  11231. Command* = RECORD
  11232. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11233. name*: Name; (* name of the procedure *)
  11234. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11235. entryAdr* : ADDRESS; (* entry address of procedure *)
  11236. END;
  11237. *)
  11238. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11239. VAR
  11240. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11241. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11242. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11243. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11244. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11245. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11246. BEGIN
  11247. RETURN
  11248. (type = NIL) OR
  11249. (type.resolved IS SyntaxTree.RecordType) OR
  11250. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11251. (type.resolved IS SyntaxTree.AnyType);
  11252. END TypeAllowed;
  11253. BEGIN
  11254. numberParameters := procedureType.numberParameters;
  11255. RETURN
  11256. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11257. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11258. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11259. END GetProcedureAllowed;
  11260. PROCEDURE WriteType(type : SyntaxTree.Type);
  11261. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11262. name: Basic.SegmentedName; offset: LONGINT;
  11263. BEGIN
  11264. IF type = NIL THEN
  11265. Address(source,0);
  11266. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11267. Address(source,1);
  11268. ELSE
  11269. type := type.resolved;
  11270. IF type IS SyntaxTree.PointerType THEN
  11271. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11272. END;
  11273. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11274. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11275. name[0] := typeDeclaration.name; name[1] := -1;
  11276. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11277. ASSERT(section # NIL);
  11278. ELSE
  11279. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11280. (* TODO *)
  11281. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11282. END;
  11283. IF implementationVisitor.backend.cooperative THEN
  11284. offset := 0;
  11285. ELSE
  11286. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11287. END;
  11288. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11289. END;
  11290. END WriteType;
  11291. BEGIN
  11292. Info(source, "command array descriptor");
  11293. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11294. numberCommands := 0;
  11295. Info(source, "command array content");
  11296. FOR i := 0 TO module.allSections.Length() - 1 DO
  11297. p := module.allSections.GetSection(i);
  11298. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11299. procedure := p.symbol(SyntaxTree.Procedure);
  11300. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11301. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11302. procedure.GetName(name);
  11303. Name(source,name);
  11304. numberParameters := procedureType.numberParameters;
  11305. (* offset of type of first parameter *)
  11306. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11307. ELSE WriteType(procedureType.firstParameter.type)
  11308. END;
  11309. (* offset of type of return parameter *)
  11310. WriteType(procedureType.returnType);
  11311. (* command name *)
  11312. (* command code offset *)
  11313. Symbol(source,p,0,0);
  11314. INC(numberCommands);
  11315. IF Trace THEN
  11316. D.Ln;
  11317. END;
  11318. END;
  11319. END
  11320. END;
  11321. PatchArray(source,sizePC,numberCommands);
  11322. END CommandArray;
  11323. (* to prevent from double import of different module aliases *)
  11324. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11325. VAR i: SyntaxTree.Import;
  11326. BEGIN
  11327. i := module.module.moduleScope.firstImport;
  11328. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11329. i := i.nextImport;
  11330. END;
  11331. RETURN i = import
  11332. END IsFirstDirectOccurence;
  11333. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11334. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11335. BEGIN
  11336. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11337. ArrayBlock(source,pc,"", FALSE);
  11338. Info(source, "import module array data");
  11339. IF implementationVisitor.backend.cooperative THEN
  11340. offset := 0;
  11341. ELSE
  11342. IF module.system.addressType.sizeInBits = 64 THEN
  11343. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11344. ELSE
  11345. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11346. END;
  11347. END;
  11348. import := module.module.moduleScope.firstImport;
  11349. numberImports := 0;
  11350. WHILE import # NIL DO
  11351. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11352. Global.GetModuleSegmentedName(import.module,name);
  11353. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11354. NamedSymbol(source, name, NIL, 0, offset);
  11355. INC(numberImports);
  11356. END;
  11357. import := import.nextImport
  11358. END;
  11359. PatchArray(source,pc,numberImports);
  11360. END ImportsArray;
  11361. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11362. VAR
  11363. p: Sections.Section; sizePC, size, i: LONGINT;
  11364. BEGIN
  11365. Info(source, "Type info section");
  11366. size := 0;
  11367. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11368. FOR i := 0 TO module.allSections.Length() - 1 DO
  11369. p := module.allSections.GetSection(i);
  11370. WITH p: IntermediateCode.Section DO
  11371. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11372. Symbol(source,p,0,0);
  11373. INC(size);
  11374. END;
  11375. END
  11376. END;
  11377. PatchArray(source,sizePC,size);
  11378. END TypeInfoSection;
  11379. (*
  11380. ProcTableEntry* = RECORD
  11381. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11382. noPtr*: LONGINT;
  11383. END;
  11384. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11385. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11386. *)
  11387. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11388. VAR
  11389. numberPointers: SIZE;
  11390. procedure: SyntaxTree.Procedure;
  11391. BEGIN
  11392. Info(section,"pcFrom");
  11393. Symbol(section,procedureSection,0,0);
  11394. Info(section,"pcTo");
  11395. Symbol(section, procedureSection, procedureSection.pc, 0);
  11396. Info(section,"pointer to offsets array");
  11397. Symbol(section, section,section.pc+1,0);
  11398. Info(section,"offsets array");
  11399. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11400. PointerArray(section, procedure.procedureScope, numberPointers);
  11401. END ProcedureDescriptor;
  11402. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11403. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11404. BEGIN
  11405. name := procedureSection.name;
  11406. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11407. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11408. ProcedureDescriptor(dest, procedureSection);
  11409. Symbol(section, dest, offset, 0);
  11410. END ProcedureDescriptorPointer;
  11411. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11412. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11413. BEGIN
  11414. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11415. numberProcs := 0;
  11416. FOR i := 0 TO module.allSections.Length() - 1 DO
  11417. destination := module.allSections.GetSection(i);
  11418. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11419. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11420. INC(numberProcs);
  11421. END
  11422. END;
  11423. PatchArray(section, sizePC, numberProcs);
  11424. END ProcedureDescriptorArray;
  11425. (*
  11426. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11427. VAR
  11428. next*: Module; (** once a module is published, all fields are read-only *)
  11429. name*: Name;
  11430. init, published: BOOLEAN;
  11431. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11432. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11433. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11434. command*: POINTER TO ARRAY OF Command;
  11435. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11436. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11437. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11438. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11439. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11440. data*, code*: Bytes;
  11441. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11442. export*: ExportDesc;
  11443. term*: TerminationHandler;
  11444. exTable*: ExceptionTable;
  11445. noProcs*: LONGINT;
  11446. firstProc*: ADDRESS; <- done by loader
  11447. maxPtrs*: LONGINT;
  11448. crc*: LONGINT;
  11449. *)
  11450. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11451. BEGIN
  11452. Info(section, "cycle");
  11453. Size(section,0);
  11454. Info(section, "references");
  11455. Size(section,0);
  11456. Info(section, "nextMarked");
  11457. Address(section,0);
  11458. Info(section, "nextWatched");
  11459. Address(section,0);
  11460. END BasePointer;
  11461. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11462. BEGIN
  11463. BasePointer(section);
  11464. Info(section, "action");
  11465. Address(section,0);
  11466. Info(section, "monitor");
  11467. Address(section,0);
  11468. END BaseObject;
  11469. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11470. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11471. pooledName: Basic.SegmentedName;
  11472. symbol: SyntaxTree.Symbol;
  11473. BEGIN
  11474. Global.GetModuleName(module.module,name);
  11475. Strings.Append(name,".@Module.@Descriptor");
  11476. Basic.ToSegmentedName(name, pooledName);
  11477. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11478. Symbol(section,descriptorSection,0,0);
  11479. Info(descriptorSection, "descriptor");
  11480. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11481. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11482. Address(descriptorSection,0);
  11483. Global.GetModuleName(module.module,name);
  11484. Strings.Append(name,".@Trace");
  11485. Basic.ToSegmentedName(name, pooledName);
  11486. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11487. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11488. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11489. END ModuleDescriptor;
  11490. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11491. VAR name: ARRAY 128 OF CHAR;
  11492. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11493. symbol: SyntaxTree.Symbol;
  11494. BEGIN
  11495. Global.GetModuleName(module.module,name);
  11496. Strings.Append(name,".@Module");
  11497. Basic.ToSegmentedName(name, pooledName);
  11498. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11499. moduleSection.SetExported(TRUE);
  11500. IF moduleSection.pc = 0 THEN
  11501. IF implementationVisitor.backend.cooperative THEN
  11502. Info(moduleSection, "descriptor");
  11503. ModuleDescriptor(moduleSection);
  11504. BaseObject(moduleSection);
  11505. implementationVisitor.CreateTraceModuleMethod(module.module);
  11506. ELSE
  11507. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11508. Info(moduleSection, "HeapBlock");
  11509. Symbol(moduleSection,moduleSection,2,0);
  11510. Info(moduleSection, "TypeDescriptor");
  11511. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11512. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11513. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11514. END;
  11515. END;
  11516. RETURN moduleSection;
  11517. END ModuleSection;
  11518. PROCEDURE NewModuleInfo();
  11519. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11520. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11521. sectionName: Basic.SectionName;
  11522. CONST MPO=-40000000H;
  11523. BEGIN
  11524. (*
  11525. TypeDesc* = POINTER TO RECORD
  11526. descSize: LONGINT;
  11527. sentinel: LONGINT; (* = MPO-4 *)
  11528. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11529. flags*: SET;
  11530. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11531. name*: Name;
  11532. END;
  11533. *)
  11534. Global.GetSymbolSegmentedName(module.module,name);
  11535. Basic.AppendToSegmentedName(name,".@Info");
  11536. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11537. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11538. Address(source,MPO-4);
  11539. Info(source, "type tag pointer");
  11540. Address( source,0);
  11541. Info(source, "type flags");
  11542. flags := {};
  11543. Set( source, flags);
  11544. Info(source, "pointer to module");
  11545. moduleSection := ModuleSection();
  11546. Symbol( source, moduleSection, moduleSection.pc,0);
  11547. Info(source, "type name");
  11548. i := 0;
  11549. sectionName := "@Self";
  11550. (*
  11551. Global.GetSymbolSegmentedName(td,name);
  11552. Basic.SegmentedNameToString(name, sectionName);
  11553. *)
  11554. Name(source,sectionName);
  11555. source.SetReferenced(FALSE);
  11556. patchInfoPC := source.pc;
  11557. Size(source, 0);
  11558. END NewModuleInfo;
  11559. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11560. VAR
  11561. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11562. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11563. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11564. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11565. referenceSectionOffset : LONGINT;
  11566. name: Basic.SegmentedName; offset: LONGINT;
  11567. BEGIN
  11568. NewModuleInfo();
  11569. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11570. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11571. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11572. ImportsArray(importSection);
  11573. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11574. CommandArray(commandsSection);
  11575. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11576. ExceptionArray(exceptionSection);
  11577. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11578. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11579. TypeInfoSection(typeInfoSection);
  11580. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11581. References(referenceSection);
  11582. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11583. IF ~implementationVisitor.backend.cooperative THEN
  11584. ProcedureDescriptorArray(procTableSection, numberProcs);
  11585. name := "Heaps.AnyPtr";
  11586. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11587. (* set base pointer *)
  11588. NamedSymbolAt(procTableSection, procTableSectionOffset -1 , name, NIL, 0, offset);
  11589. END;
  11590. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11591. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11592. moduleSection := ModuleSection();
  11593. Info(moduleSection, "nextRoot*: RootObject");
  11594. Address(moduleSection,0);
  11595. Info(moduleSection, "next*: Module");
  11596. Address(moduleSection,0);
  11597. Info(moduleSection, "name*: Name");
  11598. Name(moduleSection,moduleName);
  11599. Info(moduleSection, "init, published: BOOLEAN");
  11600. Boolean(moduleSection,FALSE);
  11601. Boolean(moduleSection,FALSE);
  11602. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11603. Boolean(moduleSection,FALSE);
  11604. Boolean(moduleSection,FALSE);
  11605. Info(moduleSection, "refcnt*: LONGINT");
  11606. Longint(moduleSection,0);
  11607. Info(moduleSection, "sb*: ADDRESS");
  11608. Address(moduleSection,0);
  11609. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11610. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11611. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11612. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11613. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11614. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11615. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11616. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11617. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11618. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11619. Info(moduleSection, "procTable*: ProcTable");
  11620. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11621. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11622. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11623. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11624. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11625. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11626. Info(moduleSection, "export*: ExportDesc");
  11627. ExportDesc(moduleSection);
  11628. Info(moduleSection, "term*: TerminationHandler");
  11629. Address(moduleSection,0);
  11630. Info(moduleSection, "exTable*: ExceptionTable");
  11631. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11632. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11633. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11634. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11635. Info(moduleSection, "crc*: LONGINT");
  11636. Longint(moduleSection, 0); (*! must be implemented *)
  11637. Info(moduleSection, "body*: ADDRESS");
  11638. Symbol(moduleSection, bodyProc, 0,0);
  11639. IF implementationVisitor.backend.cooperative THEN
  11640. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11641. Info(moduleSection, "monitor.owner");
  11642. Address(moduleSection,0);
  11643. Info(moduleSection, "monitor.nestingLevel");
  11644. Address(moduleSection,0);
  11645. Info(moduleSection, "monitor.blockedQueue");
  11646. Address(moduleSection,0); Address(moduleSection,0);
  11647. Info(moduleSection, "monitor.waitingQueue");
  11648. Address(moduleSection,0); Address(moduleSection,0);
  11649. Info(moduleSection, "monitor.waitingSentinel");
  11650. Address(moduleSection,0);
  11651. END;
  11652. END Module;
  11653. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11654. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11655. BEGIN
  11656. ArrayBlock(source,pc,"",FALSE);
  11657. Info(source, "pointer offsets array data");
  11658. IF scope IS SyntaxTree.RecordScope THEN
  11659. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11660. ELSIF scope IS SyntaxTree.CellScope THEN
  11661. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11662. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11663. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11664. WHILE variable # NIL DO
  11665. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11666. symbol := module.allSections.FindBySymbol(variable);
  11667. ASSERT(symbol # NIL);
  11668. Pointers(0,symbol, source,variable.type,numberPointers);
  11669. END;
  11670. variable := variable.nextVariable;
  11671. END;
  11672. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11673. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11674. WHILE parameter # NIL DO
  11675. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11676. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11677. END;
  11678. parameter := parameter.nextParameter;
  11679. END;
  11680. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11681. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11682. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11683. IF implementationVisitor.backend.preciseGC THEN
  11684. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11685. END;
  11686. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11687. END;
  11688. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11689. WHILE(variable # NIL) DO
  11690. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11691. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11692. END;
  11693. variable := variable.nextVariable
  11694. END;
  11695. END;
  11696. PatchArray(source,pc,numberPointers);
  11697. END PointerArray;
  11698. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11699. VAR
  11700. name: Basic.SegmentedName;
  11701. section: IntermediateCode.Section;
  11702. BEGIN
  11703. ASSERT(implementationVisitor.newObjectFile);
  11704. Global.GetSymbolSegmentedName(symbol,name);
  11705. Basic.AppendToSegmentedName(name,suffix);
  11706. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11707. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11708. Info(section, "HeapBlock");
  11709. Address(section,0); (* empty such that GC does not go on traversing *)
  11710. Info(section, suffix);
  11711. Address(section,0);
  11712. pc := section.pc;
  11713. RETURN section;
  11714. END SymbolSection;
  11715. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11716. VAR recordType: SyntaxTree.RecordType;
  11717. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11718. section: Sections.Section; cellType: SyntaxTree.CellType;
  11719. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11720. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11721. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11722. sectionName: Basic.SectionName;
  11723. CONST MPO=-40000000H;
  11724. BEGIN
  11725. (*
  11726. TypeDesc* = POINTER TO RECORD
  11727. descSize: LONGINT;
  11728. sentinel: LONGINT; (* = MPO-4 *)
  11729. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11730. flags*: SET;
  11731. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11732. name*: Name;
  11733. END;
  11734. *)
  11735. (* source := module.sections.FindByName(...) *)
  11736. Global.GetSymbolSegmentedName(td,name);
  11737. Basic.AppendToSegmentedName(name,".@Info");
  11738. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11739. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11740. Address(source,MPO-4);
  11741. Info(source, "type tag pointer");
  11742. Symbol( source, tag, offset, 0);
  11743. Info(source, "type flags");
  11744. flags := {};
  11745. IF isProtected THEN INCL(flags,31) END;
  11746. Set( source, flags);
  11747. Info(source, "pointer to module");
  11748. moduleSection := ModuleSection();
  11749. Symbol( source, moduleSection, moduleSection.pc,0);
  11750. Info(source, "type name");
  11751. i := 0;
  11752. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11753. (*
  11754. Global.GetSymbolSegmentedName(td,name);
  11755. Basic.SegmentedNameToString(name, sectionName);
  11756. *)
  11757. Name(source,sectionName);
  11758. source.SetReferenced(FALSE);
  11759. Size(source, 0);
  11760. RETURN source;
  11761. END NewTypeDescriptorInfo;
  11762. PROCEDURE NewTypeDescriptor;
  11763. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11764. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11765. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11766. numberPointers: LONGINT; padding, i: LONGINT;
  11767. CONST MPO=-40000000H;
  11768. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11769. VAR i: LONGINT;
  11770. PROCEDURE Td(record: SyntaxTree.RecordType);
  11771. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11772. BEGIN
  11773. IF record # NIL THEN
  11774. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11775. baseTD := record.typeDeclaration;
  11776. Global.GetSymbolSegmentedName(baseTD,name);
  11777. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11778. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11779. ELSE
  11780. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11781. END;
  11782. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11783. Symbol(source, tir, 0, offset);
  11784. IF reverse THEN Td(record.GetBaseRecord()) END;
  11785. END;
  11786. END Td;
  11787. BEGIN
  11788. Info(source, "tag table");
  11789. baseRecord := recordType;
  11790. i := 0;
  11791. WHILE baseRecord # NIL DO
  11792. INC(i);
  11793. baseRecord := baseRecord.GetBaseRecord();
  11794. END;
  11795. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11796. IF ~reverse THEN Td(recordType) END;
  11797. WHILE i < size DO
  11798. Address(source,0);
  11799. INC(i);
  11800. END;
  11801. IF reverse THEN Td(recordType) END;
  11802. END TdTable;
  11803. PROCEDURE MethodTable(reverse: BOOLEAN);
  11804. VAR i,methods: LONGINT;
  11805. BEGIN
  11806. Info(source, "method table");
  11807. IF recordType # NIL THEN
  11808. methods := recordType.recordScope.numberMethods;
  11809. IF reverse THEN
  11810. FOR i := methods-1 TO 0 BY -1 DO
  11811. procedure := recordType.recordScope.FindMethod(i);
  11812. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11813. NamedSymbol(source, name,procedure, 0,0);
  11814. END;
  11815. ELSE
  11816. FOR i := 0 TO methods-1 DO
  11817. procedure := recordType.recordScope.FindMethod(i);
  11818. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11819. NamedSymbol(source, name,procedure, 0,0);
  11820. END;
  11821. END;
  11822. END;
  11823. END MethodTable;
  11824. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11825. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11826. BEGIN
  11827. Info(source, "method table");
  11828. baseRecord := recordType;
  11829. WHILE baseRecord.baseType # NIL DO
  11830. baseRecord := baseRecord.GetBaseRecord ();
  11831. END;
  11832. GetRecordTypeName (baseRecord, name);
  11833. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11834. IF name = stackFrame THEN
  11835. start := 0;
  11836. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11837. baseRecord := recordType;
  11838. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11839. baseRecord := baseRecord.GetBaseRecord ();
  11840. END;
  11841. IF baseRecord # NIL THEN
  11842. GetRecordTypeName (baseRecord, name);
  11843. IF pointer & ~baseRecord.isObject THEN
  11844. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11845. END;
  11846. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11847. ELSIF recordType.isObject THEN
  11848. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11849. ELSIF pointer THEN
  11850. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11851. ELSE
  11852. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11853. END;
  11854. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11855. Symbol(source, tir, 0, 0);
  11856. start := 0;
  11857. baseRecord := recordType;
  11858. WHILE (baseRecord # NIL) DO
  11859. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11860. baseRecord := baseRecord.GetBaseRecord ();
  11861. END;
  11862. ELSE
  11863. (* explicit trace method: *)
  11864. procedure := recordType.recordScope.FindMethod(0);
  11865. IF ~procedure.isFinalizer THEN
  11866. Global.GetSymbolSegmentedName(procedure, name);
  11867. NamedSymbol(source, name,procedure, 0,0);
  11868. END;
  11869. start := 1;
  11870. END;
  11871. IF (name # stackFrame) & recordType.isObject THEN
  11872. baseRecord := recordType;
  11873. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11874. baseRecord := baseRecord.GetBaseRecord ();
  11875. END;
  11876. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11877. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11878. ELSE
  11879. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11880. END;
  11881. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11882. Symbol(source, tir, 0, 0);
  11883. END;
  11884. methods := recordType.recordScope.numberMethods;
  11885. FOR i := start TO methods-1 DO
  11886. procedure := recordType.recordScope.FindMethod(i);
  11887. IF ~procedure.isFinalizer THEN
  11888. Global.GetSymbolSegmentedName(procedure, name);
  11889. NamedSymbol(source, name,procedure, 0,0);
  11890. END;
  11891. END;
  11892. END CooperativeMethodTable;
  11893. BEGIN
  11894. Global.GetSymbolSegmentedName(td,name);
  11895. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11896. source.SetExported(IsExported(td));
  11897. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11898. IF implementationVisitor.backend.cooperative THEN
  11899. base := NIL;
  11900. baseRecord := recordType.GetBaseRecord();
  11901. IF baseRecord # NIL THEN
  11902. baseTD := baseRecord.typeDeclaration;
  11903. END;
  11904. IF ~recordType.isObject THEN
  11905. Info(source, "parent");
  11906. IF baseRecord # NIL THEN
  11907. Global.GetSymbolSegmentedName(baseTD,name);
  11908. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11909. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11910. ELSE
  11911. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11912. END;
  11913. Symbol(source, tir, 0, 0);
  11914. ELSE
  11915. Address(source,0);
  11916. END;
  11917. Info(source, "record size");
  11918. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11919. source.SetReferenced(FALSE);
  11920. CooperativeMethodTable(FALSE);
  11921. base := source;
  11922. Global.GetSymbolSegmentedName(td,name);
  11923. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11924. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11925. source.SetExported(IsExported(td));
  11926. source.SetReferenced(FALSE);
  11927. END;
  11928. Info(source, "parent");
  11929. IF baseRecord # NIL THEN
  11930. Global.GetSymbolSegmentedName(baseTD,name);
  11931. sym := baseTD;
  11932. IF ~recordType.isObject THEN
  11933. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11934. sym := NIL;
  11935. END;
  11936. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11937. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11938. ELSE
  11939. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11940. END;
  11941. Symbol(source, tir, 0, 0);
  11942. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11943. Address(source,0);
  11944. ELSE
  11945. IF recordType.isObject THEN
  11946. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11947. ELSE
  11948. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11949. END;
  11950. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11951. Symbol(source, tir, 0, 0);
  11952. END;
  11953. Info(source, "base record descriptor");
  11954. Symbol(source, base, 0, 0);
  11955. CooperativeMethodTable(TRUE);
  11956. source.SetReferenced(FALSE);
  11957. IF recordType.hasPointers THEN
  11958. IF ~HasExplicitTraceMethod (recordType) THEN
  11959. implementationVisitor.CreateTraceMethod(recordType);
  11960. END;
  11961. implementationVisitor.CreateResetProcedure(recordType);
  11962. implementationVisitor.CreateAssignProcedure(recordType);
  11963. END;
  11964. ELSIF ~simple THEN
  11965. (*
  11966. MethodEnd = MPO
  11967. ---
  11968. methods (# methods)
  11969. ---
  11970. tags (16)
  11971. ---
  11972. TypeDesc = TypeInfoAdr
  11973. ---
  11974. td adr ---> rec size
  11975. ----
  11976. pointer offsets
  11977. ----
  11978. (padding)
  11979. -----
  11980. empty [2 addresses aligned]
  11981. empty
  11982. empty
  11983. numPtrs
  11984. ---
  11985. pointer offsets
  11986. ---
  11987. *)
  11988. Info(source, "MethodEnd = MPO");
  11989. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11990. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11991. MethodTable(TRUE);
  11992. TdTable(TypeTags, TRUE);
  11993. Info(source, "type descriptor info pointer");
  11994. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11995. IF (cellType # NIL) THEN
  11996. IF cellType.sizeInBits < 0 THEN
  11997. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11998. END;
  11999. Info(source, "cell size");
  12000. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  12001. ELSE
  12002. Info(source, "record size");
  12003. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12004. END;
  12005. Info(source, "pointer offsets pointer");
  12006. padding := 1- source.pc MOD 2;
  12007. Symbol(source, source, source.pc+1+padding,0);
  12008. IF padding >0 THEN
  12009. Info(source, "padding");
  12010. FOR i := 1 TO padding DO Address(source,0) END;
  12011. END;
  12012. IF cellType # NIL THEN
  12013. PointerArray(source, cellType.cellScope, numberPointers);
  12014. ELSE
  12015. PointerArray(source, recordType.recordScope, numberPointers);
  12016. END;
  12017. ELSE
  12018. (*
  12019. simple:
  12020. td adr --> size
  12021. tag(1)
  12022. tag(2)
  12023. tag(3)
  12024. methods ->
  12025. *)
  12026. Info(source, "record size");
  12027. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12028. TdTable(TypeTags, FALSE);
  12029. MethodTable(FALSE);
  12030. source.SetReferenced(FALSE);
  12031. END;
  12032. END NewTypeDescriptor;
  12033. BEGIN
  12034. x := x.resolved;
  12035. IF (x IS SyntaxTree.PointerType) THEN
  12036. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12037. END;
  12038. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12039. recordType := x(SyntaxTree.RecordType);
  12040. td := x.typeDeclaration;
  12041. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12042. ASSERT(td # NIL);
  12043. section := module.allSections.FindBySymbol(td); (* TODO *)
  12044. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12045. IF implementationVisitor.newObjectFile THEN
  12046. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12047. NewTypeDescriptor
  12048. END;
  12049. ELSE
  12050. (* data section in intermediate code *)
  12051. Global.GetSymbolSegmentedName(td,name);
  12052. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12053. Basic.SuffixSegmentedName (name, module.module.name);
  12054. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12055. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12056. tir.Emit(Data(Basic.invalidPosition,op));
  12057. END;
  12058. END;
  12059. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12060. cellType := x(SyntaxTree.CellType);
  12061. td := x.typeDeclaration;
  12062. section := module.allSections.FindBySymbol(td); (* TODO *)
  12063. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12064. IF implementationVisitor.newObjectFile THEN
  12065. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12066. NewTypeDescriptor
  12067. END;
  12068. END;
  12069. END;
  12070. END
  12071. END CheckTypeDeclaration
  12072. END MetaDataGenerator;
  12073. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12074. VAR
  12075. trace-: BOOLEAN;
  12076. traceString-: SyntaxTree.IdentifierString;
  12077. traceModuleName-: SyntaxTree.IdentifierString;
  12078. newObjectFile-: BOOLEAN;
  12079. profile-: BOOLEAN;
  12080. noRuntimeChecks: BOOLEAN;
  12081. simpleMetaData-: BOOLEAN;
  12082. noAsserts: BOOLEAN;
  12083. optimize-: BOOLEAN;
  12084. cooperative-: BOOLEAN;
  12085. preregisterStatic-: BOOLEAN;
  12086. dump-: Basic.Writer;
  12087. cellsAreObjects: BOOLEAN;
  12088. preciseGC: BOOLEAN;
  12089. PROCEDURE &InitIntermediateBackend*;
  12090. BEGIN
  12091. simpleMetaData := FALSE;
  12092. newObjectFile := FALSE;
  12093. InitBackend;
  12094. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12095. SetTraceModuleName(DefaultTraceModuleName);
  12096. END InitIntermediateBackend;
  12097. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12098. VAR
  12099. declarationVisitor: DeclarationVisitor;
  12100. implementationVisitor: ImplementationVisitor;
  12101. module: Sections.Module;
  12102. name, platformName: SyntaxTree.IdentifierString;
  12103. meta: MetaDataGenerator;
  12104. BEGIN
  12105. ResetError;
  12106. Global.GetSymbolName(x,name);
  12107. NEW(module,x,system); (* backend structures *)
  12108. Global.GetModuleName(x, name);
  12109. module.SetModuleName(name);
  12110. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12111. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12112. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12113. declarationVisitor.Module(x,module);
  12114. IF newObjectFile & ~meta.simple THEN
  12115. meta.Module(implementationVisitor.moduleBodySection);
  12116. END;
  12117. GetDescription(platformName);
  12118. module.SetPlatformName(platformName);
  12119. RETURN module
  12120. END GenerateIntermediate;
  12121. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12122. BEGIN RETURN TRUE
  12123. END SupportedImmediate;
  12124. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12125. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12126. END ProcessSyntaxTreeModule;
  12127. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12128. VAR
  12129. result: Sections.Module;
  12130. traceName: Basic.MessageString;
  12131. BEGIN
  12132. ASSERT(intermediateCodeModule IS Sections.Module);
  12133. result := intermediateCodeModule(Sections.Module);
  12134. IF trace THEN
  12135. traceName := "intermediate code trace: ";
  12136. Strings.Append(traceName,traceString);
  12137. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12138. IF (traceString="") OR (traceString="*") THEN
  12139. result.Dump(dump);
  12140. dump.Update
  12141. ELSE
  12142. Sections.DumpFiltered(dump, result, traceString);
  12143. END
  12144. END;
  12145. RETURN result
  12146. END ProcessIntermediateCodeModule;
  12147. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12148. BEGIN instructionSet := "Intermediate";
  12149. END GetDescription;
  12150. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12151. BEGIN
  12152. SELF.newObjectFile := newObjectFile;
  12153. SELF.simpleMetaData := simpleMetaData;
  12154. END SetNewObjectFile;
  12155. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12156. BEGIN COPY(name, traceModuleName)
  12157. END SetTraceModuleName;
  12158. PROCEDURE DefineOptions(options: Options.Options);
  12159. BEGIN
  12160. DefineOptions^(options);
  12161. options.Add(0X,"trace",Options.String);
  12162. options.Add(0X,"runtime",Options.String);
  12163. options.Add(0X,"newObjectFile",Options.Flag);
  12164. options.Add(0X,"traceModule",Options.String);
  12165. options.Add(0X,"profile",Options.Flag);
  12166. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12167. options.Add(0X,"noAsserts",Options.Flag);
  12168. options.Add(0X,"metaData",Options.String);
  12169. options.Add('o',"optimize", Options.Flag);
  12170. options.Add(0X,"preregisterStatic", Options.Flag);
  12171. options.Add(0X,"cellsAreObjects", Options.Flag);
  12172. options.Add(0X,"preciseGC", Options.Flag);
  12173. END DefineOptions;
  12174. PROCEDURE GetOptions(options: Options.Options);
  12175. VAR name,string: SyntaxTree.IdentifierString;
  12176. BEGIN
  12177. GetOptions^(options);
  12178. trace := options.GetString("trace",traceString);
  12179. profile := options.GetFlag("profile");
  12180. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12181. noAsserts := options.GetFlag("noAsserts");
  12182. cooperative := options.GetFlag("cooperative");
  12183. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12184. IF cooperative THEN
  12185. SetRuntimeModuleName("CPU") END
  12186. END;
  12187. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12188. simpleMetaData := TRUE
  12189. END;
  12190. IF options.GetString("metaData",string) THEN
  12191. IF string = "simple" THEN simpleMetaData := TRUE
  12192. ELSIF string ="full" THEN simpleMetaData := FALSE
  12193. END;
  12194. END;
  12195. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12196. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12197. optimize := options.GetFlag("optimize");
  12198. preregisterStatic := options.GetFlag("preregisterStatic");
  12199. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12200. preciseGC := options.GetFlag("preciseGC");
  12201. END GetOptions;
  12202. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12203. BEGIN RETURN SymbolFileFormat.Get()
  12204. END DefaultSymbolFileFormat;
  12205. END IntermediateBackend;
  12206. (* ----------------------------------- register allocation ------------------------------------- *)
  12207. (* register mapping scheme
  12208. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12209. spill offset
  12210. part(n) --> ticket <--> physical register
  12211. spill offset
  12212. *)
  12213. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12214. emptyOperand: IntermediateCode.Operand;
  12215. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12216. statCoopResetVariables: LONGINT;
  12217. statCoopModifyAssignments: LONGINT;
  12218. modifyAssignmentsPC : LONGINT;
  12219. statCoopNilCheck: LONGINT;
  12220. statCoopSwitch: LONGINT;
  12221. statCoopAssignProcedure: LONGINT;
  12222. statCoopTraceMethod: LONGINT;
  12223. statCoopResetProcedure: LONGINT;
  12224. statCoopTraceModule: LONGINT;
  12225. PROCEDURE ResetStatistics*;
  12226. BEGIN
  12227. statCoopResetVariables := 0;
  12228. statCoopModifyAssignments := 0;
  12229. statCoopNilCheck:= 0;
  12230. statCoopSwitch:= 0;
  12231. statCoopAssignProcedure:= 0;
  12232. statCoopTraceMethod:= 0;
  12233. statCoopResetProcedure:= 0;
  12234. statCoopTraceModule:= 0;
  12235. END ResetStatistics;
  12236. PROCEDURE Statistics*;
  12237. BEGIN
  12238. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12239. TRACE(statCoopNilCheck, statCoopSwitch);
  12240. TRACE(statCoopAssignProcedure,
  12241. statCoopTraceMethod,
  12242. statCoopResetProcedure,
  12243. statCoopTraceModule)
  12244. END Statistics;
  12245. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12246. VAR h: LONGINT;
  12247. BEGIN
  12248. WHILE b # 0 DO
  12249. h := a MOD b;
  12250. a := b;
  12251. b := h;
  12252. END;
  12253. RETURN a
  12254. END GCD;
  12255. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12256. BEGIN
  12257. RETURN a*b DIV GCD(a,b)
  12258. END SCM;
  12259. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12260. BEGIN
  12261. (*TRACE(a,b);*)
  12262. IF a = 0 THEN RETURN b
  12263. ELSIF b = 0 THEN RETURN a
  12264. ELSE RETURN SCM(a,b)
  12265. END;
  12266. END CommonAlignment;
  12267. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12268. BEGIN
  12269. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12270. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12271. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12272. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12273. 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)
  12274. END
  12275. END PassBySingleReference;
  12276. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12277. BEGIN
  12278. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12279. END PassInRegister;
  12280. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12281. VAR new: RegisterEntry;
  12282. BEGIN
  12283. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12284. IF queue = NIL THEN
  12285. queue := new
  12286. ELSE
  12287. new.next := queue;
  12288. IF queue#NIL THEN queue.prev := new END;
  12289. queue := new
  12290. END;
  12291. END AddRegisterEntry;
  12292. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12293. VAR this: RegisterEntry;
  12294. BEGIN
  12295. this := queue;
  12296. WHILE (this # NIL) & (this.register # register) DO
  12297. this := this.next;
  12298. END;
  12299. IF this = NIL THEN
  12300. RETURN FALSE
  12301. END;
  12302. ASSERT(this # NIL);
  12303. IF this = queue THEN queue := queue.next END;
  12304. IF this.prev # NIL THEN this.prev.next := this.next END;
  12305. IF this.next # NIL THEN this.next.prev := this.prev END;
  12306. RETURN TRUE
  12307. END RemoveRegisterEntry;
  12308. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12309. BEGIN ASSERT(cond);
  12310. END Assert;
  12311. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12312. BEGIN
  12313. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12314. END ReusableRegister;
  12315. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12316. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12317. BEGIN
  12318. procedure := moduleScope.bodyProcedure;
  12319. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12320. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12321. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12322. procedure.SetScope(moduleScope);
  12323. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12324. procedure.SetAccess(SyntaxTree.Hidden);
  12325. moduleScope.SetBodyProcedure(procedure);
  12326. moduleScope.AddProcedure(procedure);
  12327. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12328. END;
  12329. END EnsureBodyProcedure;
  12330. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12331. VAR import: SyntaxTree.Import;
  12332. selfName: SyntaxTree.IdentifierString;
  12333. module: SyntaxTree.Module;
  12334. BEGIN
  12335. scope.ownerModule.GetName(selfName);
  12336. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12337. module := scope.ownerModule
  12338. ELSE
  12339. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12340. IF import = NIL THEN
  12341. RETURN NIL
  12342. ELSIF import.module = NIL THEN
  12343. RETURN NIL
  12344. ELSE module := import.module
  12345. END;
  12346. END;
  12347. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12348. END GetSymbol;
  12349. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12350. BEGIN
  12351. op.mode := mode;
  12352. IntermediateCode.InitOperand(op.op);
  12353. IntermediateCode.InitOperand(op.tag);
  12354. IntermediateCode.InitOperand(op.extra);
  12355. op.dimOffset := 0;
  12356. END InitOperand;
  12357. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12358. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12359. BEGIN RETURN IntermediateCode.GetType(system, type)
  12360. END GetType;
  12361. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12362. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12363. BEGIN
  12364. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12365. (*
  12366. UniqueId(name,module,name,adr);
  12367. *)
  12368. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12369. constant.SetValue(value);
  12370. constant.SetAccess(SyntaxTree.Hidden);
  12371. module.moduleScope.AddConstant(constant);
  12372. constant.SetScope(module.moduleScope);
  12373. RETURN constant
  12374. END BuildConstant;
  12375. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12376. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12377. BEGIN
  12378. variable := scope.firstVariable;
  12379. WHILE variable # NIL DO
  12380. IF variable.NeedsTrace() THEN
  12381. RETURN TRUE;
  12382. END;
  12383. variable := variable.nextVariable;
  12384. END;
  12385. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12386. WHILE parameter # NIL DO
  12387. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12388. RETURN TRUE;
  12389. END;
  12390. parameter := parameter.nextParameter;
  12391. END;
  12392. RETURN FALSE;
  12393. END HasPointers;
  12394. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12395. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12396. END IsVariableParameter;
  12397. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12398. VAR parameter: SyntaxTree.Parameter;
  12399. BEGIN
  12400. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12401. WHILE parameter # NIL DO
  12402. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12403. IF parameter.movable THEN RETURN TRUE END;
  12404. parameter := parameter.nextParameter;
  12405. END;
  12406. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12407. END HasVariableParameters;
  12408. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12409. BEGIN
  12410. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12411. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12412. END HasExplicitTraceMethod;
  12413. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12414. BEGIN
  12415. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12416. IF (expression IS SyntaxTree.IntegerValue) THEN
  12417. val := expression(SyntaxTree.IntegerValue).value;
  12418. RETURN TRUE
  12419. ELSE
  12420. RETURN FALSE
  12421. END;
  12422. END IsIntegerConstant;
  12423. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12424. BEGIN
  12425. IF val <= 0 THEN RETURN FALSE END;
  12426. exp := 0;
  12427. WHILE ~ODD(val) DO
  12428. val := val DIV 2;
  12429. INC(exp)
  12430. END;
  12431. RETURN val = 1
  12432. END PowerOf2;
  12433. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12434. VAR procedure: SyntaxTree.Procedure;
  12435. BEGIN
  12436. procedure := record.recordScope.constructor;
  12437. IF procedure = NIL THEN
  12438. record := record.GetBaseRecord();
  12439. IF record # NIL THEN
  12440. procedure := GetConstructor(record)
  12441. END;
  12442. END;
  12443. RETURN procedure;
  12444. END GetConstructor;
  12445. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12446. BEGIN
  12447. value := SHORT(op.intValue);
  12448. RETURN op.mode = IntermediateCode.ModeImmediate;
  12449. END IsIntegerImmediate;
  12450. (** whether a type strictily is a pointer to record or object type
  12451. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12452. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12453. BEGIN
  12454. IF type = NIL THEN
  12455. RETURN FALSE
  12456. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12457. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12458. ELSE
  12459. RETURN FALSE
  12460. END
  12461. END IsStrictlyPointerToRecord;
  12462. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12463. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12464. END IsUnsafePointer;
  12465. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12466. BEGIN type := type.resolved;
  12467. IF type IS SyntaxTree.PointerType THEN
  12468. type := type(SyntaxTree.PointerType).pointerBase;
  12469. type := type.resolved;
  12470. IF type IS SyntaxTree.RecordType THEN
  12471. recordType := type(SyntaxTree.RecordType);
  12472. RETURN TRUE
  12473. ELSE
  12474. RETURN FALSE
  12475. END
  12476. ELSIF type IS SyntaxTree.RecordType THEN
  12477. recordType := type(SyntaxTree.RecordType);
  12478. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12479. ELSIF type IS SyntaxTree.ObjectType THEN
  12480. RETURN TRUE
  12481. ELSIF type IS SyntaxTree.AnyType THEN
  12482. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12483. ELSE
  12484. RETURN FALSE
  12485. END;
  12486. END IsPointerToRecord;
  12487. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12488. BEGIN
  12489. type := type.resolved;
  12490. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12491. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12492. END IsArrayOfSystemByte;
  12493. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12494. BEGIN
  12495. IF type = NIL THEN RETURN FALSE END;
  12496. type := type.resolved;
  12497. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12498. END IsOpenArray;
  12499. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12500. BEGIN
  12501. IF type = NIL THEN RETURN FALSE END;
  12502. type := type.resolved;
  12503. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12504. END IsSemiDynamicArray;
  12505. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12506. BEGIN
  12507. IF type = NIL THEN RETURN FALSE END;
  12508. type := type.resolved;
  12509. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12510. END IsStaticArray;
  12511. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12512. VAR size: LONGINT;
  12513. BEGIN
  12514. size := 1;
  12515. WHILE (IsStaticArray(type)) DO
  12516. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12517. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12518. END;
  12519. RETURN size;
  12520. END StaticArrayNumElements;
  12521. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12522. BEGIN
  12523. WHILE (IsStaticArray(type)) DO
  12524. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12525. END;
  12526. RETURN type;
  12527. END StaticArrayBaseType;
  12528. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12529. BEGIN
  12530. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12531. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12532. END;
  12533. RETURN type;
  12534. END ArrayBaseType;
  12535. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12536. BEGIN
  12537. IF type = NIL THEN RETURN FALSE END;
  12538. type := type.resolved;
  12539. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12540. END IsDelegate;
  12541. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12542. VAR i: LONGINT;
  12543. BEGIN
  12544. i := 0; type := type.resolved;
  12545. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12546. INC(i);
  12547. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12548. END;
  12549. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12550. INC(i);
  12551. type := type(SyntaxTree.MathArrayType).arrayBase;
  12552. IF type # NIL THEN type := type.resolved END;
  12553. END;
  12554. RETURN i
  12555. END DynamicDim;
  12556. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12557. BEGIN
  12558. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12559. type := type(SyntaxTree.ArrayType).arrayBase;
  12560. END;
  12561. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12562. type := type(SyntaxTree.MathArrayType).arrayBase;
  12563. END;
  12564. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12565. END StaticSize;
  12566. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12567. BEGIN
  12568. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12569. END IsImmediate;
  12570. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12571. BEGIN
  12572. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12573. END IsAddress;
  12574. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12575. BEGIN
  12576. RETURN (x.mode = IntermediateCode.ModeRegister);
  12577. END IsRegister;
  12578. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12579. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12580. BEGIN
  12581. typeDeclaration := recordType.typeDeclaration;
  12582. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12583. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12584. END GetRecordTypeName;
  12585. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12586. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12587. BEGIN
  12588. parSize := 0;
  12589. IF SemanticChecker.StructuredReturnType(procedureType) THEN
  12590. parameter := procedureType.returnParameter;
  12591. INC(parSize,system.SizeOfParameter(parameter));
  12592. parSize := parSize + (-parSize) MOD system.addressSize;
  12593. END;
  12594. parameter :=procedureType.lastParameter;
  12595. WHILE (parameter # NIL) DO
  12596. INC(parSize,system.SizeOfParameter(parameter));
  12597. parSize := parSize + (-parSize) MOD system.addressSize;
  12598. parameter := parameter.prevParameter;
  12599. END;
  12600. IF procedureType.selfParameter # NIL THEN
  12601. parameter := procedureType.selfParameter;
  12602. INC(parSize,system.SizeOfParameter(parameter));
  12603. parSize := parSize + (-parSize) MOD system.addressSize;
  12604. ELSIF procedureType.isDelegate THEN INC(parSize,system.addressSize)
  12605. END; (* method => self pointer *)
  12606. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12607. RETURN ToMemoryUnits(system,parSize)
  12608. END ParametersSize;
  12609. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12610. BEGIN
  12611. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12612. END IsNested;
  12613. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12614. BEGIN
  12615. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12616. scope := scope.outerScope;
  12617. END;
  12618. RETURN scope # NIL;
  12619. END InCellScope;
  12620. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12621. BEGIN
  12622. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12623. RETURN 0
  12624. ELSE
  12625. *)
  12626. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12627. (*END;*)
  12628. END ProcedureParametersSize;
  12629. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12630. VAR dataUnit: LONGINT;
  12631. BEGIN dataUnit := system.dataUnit;
  12632. ASSERT(size MOD system.dataUnit = 0);
  12633. RETURN size DIV system.dataUnit
  12634. END ToMemoryUnits;
  12635. PROCEDURE Get*(): Backend.Backend;
  12636. VAR backend: IntermediateBackend;
  12637. BEGIN NEW(backend); RETURN backend
  12638. END Get;
  12639. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12640. VAR instruction: IntermediateCode.Instruction;
  12641. BEGIN
  12642. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12643. RETURN instruction
  12644. END Nop;
  12645. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12646. VAR instruction: IntermediateCode.Instruction;
  12647. BEGIN
  12648. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12649. RETURN instruction
  12650. END Mov;
  12651. (* like Mov but ensures that no new register will be allocated for dest *)
  12652. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12653. VAR instruction: IntermediateCode.Instruction;
  12654. BEGIN
  12655. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12656. RETURN instruction
  12657. END MovReplace;
  12658. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12659. VAR instruction: IntermediateCode.Instruction;
  12660. BEGIN
  12661. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12662. RETURN instruction
  12663. END Conv;
  12664. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12665. BEGIN
  12666. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12667. END SysvABI;
  12668. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12669. BEGIN
  12670. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12671. END SysvABIorWINAPI;
  12672. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12673. VAR instruction: IntermediateCode.Instruction;
  12674. BEGIN
  12675. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12676. RETURN instruction
  12677. END Call;
  12678. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12679. VAR op1, op2, op3: IntermediateCode.Operand;
  12680. VAR instruction: IntermediateCode.Instruction;
  12681. BEGIN
  12682. IntermediateCode.InitNumber(op1,pcOffset);
  12683. IntermediateCode.InitNumber(op2,callingConvention);
  12684. IntermediateCode.InitNumber(op3,unwind);
  12685. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12686. RETURN instruction
  12687. END Exit;
  12688. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12689. VAR instruction: IntermediateCode.Instruction;
  12690. BEGIN
  12691. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12692. RETURN instruction
  12693. END Return;
  12694. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12695. VAR instruction: IntermediateCode.Instruction;
  12696. BEGIN
  12697. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12698. RETURN instruction
  12699. END Result;
  12700. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12701. VAR op1: IntermediateCode.Operand;
  12702. VAR instruction: IntermediateCode.Instruction;
  12703. BEGIN
  12704. IntermediateCode.InitNumber(op1,nr);
  12705. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12706. RETURN instruction
  12707. END Trap;
  12708. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12709. VAR instruction: IntermediateCode.Instruction;
  12710. BEGIN
  12711. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12712. RETURN instruction
  12713. END Br;
  12714. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12715. VAR instruction: IntermediateCode.Instruction;
  12716. BEGIN
  12717. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12718. RETURN instruction
  12719. END Breq;
  12720. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12721. VAR instruction: IntermediateCode.Instruction;
  12722. BEGIN
  12723. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12724. RETURN instruction
  12725. END Brne;
  12726. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12727. VAR instruction: IntermediateCode.Instruction;
  12728. BEGIN
  12729. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12730. RETURN instruction
  12731. END Brge;
  12732. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12733. VAR instruction: IntermediateCode.Instruction;
  12734. BEGIN
  12735. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12736. RETURN instruction
  12737. END Brlt;
  12738. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12739. VAR instruction: IntermediateCode.Instruction;
  12740. BEGIN
  12741. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12742. RETURN instruction
  12743. END Pop;
  12744. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12745. VAR instruction: IntermediateCode.Instruction;
  12746. BEGIN
  12747. ASSERT(op.mode # IntermediateCode.Undefined);
  12748. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12749. RETURN instruction
  12750. END Push;
  12751. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12752. VAR instruction: IntermediateCode.Instruction;
  12753. BEGIN
  12754. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12755. RETURN instruction
  12756. END Neg;
  12757. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12758. VAR instruction: IntermediateCode.Instruction;
  12759. BEGIN
  12760. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12761. RETURN instruction
  12762. END Not;
  12763. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12764. VAR instruction: IntermediateCode.Instruction;
  12765. BEGIN
  12766. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12767. RETURN instruction
  12768. END Abs;
  12769. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12770. VAR instruction: IntermediateCode.Instruction;
  12771. BEGIN
  12772. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12773. ASSERT(~IsImmediate(instruction.op1));
  12774. RETURN instruction
  12775. END Mul;
  12776. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12777. VAR instruction: IntermediateCode.Instruction;
  12778. BEGIN
  12779. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12780. RETURN instruction
  12781. END Div;
  12782. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12783. VAR instruction: IntermediateCode.Instruction;
  12784. BEGIN
  12785. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12786. RETURN instruction
  12787. END Mod;
  12788. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12789. VAR instruction: IntermediateCode.Instruction;
  12790. BEGIN
  12791. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12792. RETURN instruction
  12793. END Sub;
  12794. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12795. VAR instruction: IntermediateCode.Instruction;
  12796. BEGIN
  12797. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12798. RETURN instruction
  12799. END Add;
  12800. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12801. VAR instruction: IntermediateCode.Instruction;
  12802. BEGIN
  12803. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12804. RETURN instruction
  12805. END And;
  12806. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12807. VAR instruction: IntermediateCode.Instruction;
  12808. BEGIN
  12809. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12810. RETURN instruction
  12811. END Or;
  12812. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12813. VAR instruction: IntermediateCode.Instruction;
  12814. BEGIN
  12815. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12816. RETURN instruction
  12817. END Xor;
  12818. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12819. VAR instruction: IntermediateCode.Instruction;
  12820. BEGIN
  12821. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12822. RETURN instruction
  12823. END Shl;
  12824. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12825. VAR instruction: IntermediateCode.Instruction;
  12826. BEGIN
  12827. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12828. RETURN instruction
  12829. END Shr;
  12830. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12831. VAR instruction: IntermediateCode.Instruction;
  12832. BEGIN
  12833. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12834. RETURN instruction
  12835. END Rol;
  12836. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12837. VAR instruction: IntermediateCode.Instruction;
  12838. BEGIN
  12839. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12840. RETURN instruction
  12841. END Ror;
  12842. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12843. VAR instruction: IntermediateCode.Instruction;
  12844. BEGIN
  12845. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12846. RETURN instruction
  12847. END Cas;
  12848. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12849. VAR instruction: IntermediateCode.Instruction;
  12850. BEGIN
  12851. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12852. RETURN instruction
  12853. END Copy;
  12854. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12855. VAR instruction: IntermediateCode.Instruction;
  12856. BEGIN
  12857. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12858. RETURN instruction
  12859. END Fill;
  12860. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12861. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12862. BEGIN
  12863. string := IntermediateCode.String(s);
  12864. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12865. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12866. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12867. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12868. RETURN instruction
  12869. END Asm;
  12870. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12871. VAR instruction: IntermediateCode.Instruction;
  12872. BEGIN
  12873. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12874. RETURN instruction
  12875. END Data;
  12876. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12877. VAR instruction: IntermediateCode.Instruction;
  12878. BEGIN
  12879. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12880. IntermediateCode.SetSubType(instruction, subtype);
  12881. RETURN instruction
  12882. END SpecialInstruction;
  12883. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12884. VAR op1: IntermediateCode.Operand;
  12885. VAR instruction: IntermediateCode.Instruction;
  12886. BEGIN
  12887. (*! generate a warning if size exceeds a certain limit *)
  12888. (*
  12889. ASSERT(bytes < 1000000); (* sanity check *)
  12890. *)
  12891. ASSERT(0 <= units); (* sanity check *)
  12892. IntermediateCode.InitNumber(op1,units);
  12893. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12894. RETURN instruction
  12895. END Reserve;
  12896. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12897. VAR op1: IntermediateCode.Operand;
  12898. VAR instruction: IntermediateCode.Instruction;
  12899. BEGIN
  12900. IntermediateCode.InitNumber(op1,position.start);
  12901. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12902. RETURN instruction
  12903. END LabelInstruction;
  12904. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12905. VAR pc: LONGINT;
  12906. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12907. BEGIN
  12908. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12909. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12910. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12911. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12912. IF instr.op1.type.form = IntermediateCode.Float THEN
  12913. RETURN instr.op1.floatValue = op.floatValue
  12914. ELSE
  12915. RETURN instr.op1.intValue = op.intValue
  12916. END;
  12917. END ProvidesValue;
  12918. BEGIN
  12919. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12920. pc := 0;
  12921. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12922. INC(pc);
  12923. END;
  12924. IF pc = data.pc THEN
  12925. data.Emit(Data(Basic.invalidPosition,vop));
  12926. END;
  12927. RETURN pc
  12928. END EnterImmediate;
  12929. PROCEDURE Init;
  12930. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12931. BEGIN
  12932. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12933. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12934. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12935. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12936. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12937. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12938. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12939. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12940. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12941. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12942. IntermediateCode.InitOperand(emptyOperand);
  12943. FOR i := 0 TO NumberSystemCalls-1 DO
  12944. name := "@SystemCall";
  12945. Basic.AppendNumber(name,i);
  12946. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12947. END;
  12948. END Init;
  12949. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12950. BEGIN
  12951. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12952. END IsExported;
  12953. BEGIN
  12954. Init;
  12955. END FoxIntermediateBackend.
  12956. Compiler.Compile FoxIntermediateBackend.Mod ~
  12957. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12958. # 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 ~
  12959. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12960. # 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 ~
  12961. FSTools.CloseFiles A2Z.exe ~
  12962. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12963. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12964. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12965. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12966. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12967. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12968. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12969. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12970. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12971. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12972. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod