FoxIntermediateBackend.Mod 558 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207
  1. MODULE FoxIntermediateBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, Backend := FoxBackend, Global := FoxGlobal,
  3. Scanner := FoxScanner, IntermediateCode := FoxIntermediateCode, Sections := FoxSections, BinaryCode := FoxBinaryCode, Printout := FoxPrintout,
  4. SYSTEM, Diagnostics, Strings, Options, Streams, Compiler, Formats := FoxFormats, SymbolFileFormat := FoxTextualSymbolFile, D := Debugging,
  5. FingerPrinter := FoxFingerPrinter, StringPool;
  6. CONST
  7. (* operand modes *)
  8. ModeUndefined = 0;
  9. ModeReference = 1;
  10. ModeValue = 2;
  11. (* heap data offsets *)
  12. ArrayDimTable = 3; (* dimension table in dyn arrays *)
  13. (* math array offsets *)
  14. MathPtrOffset=0;
  15. MathAdrOffset=1;
  16. MathFlagsOffset=2;
  17. MathDimOffset=3;
  18. MathElementSizeOffset=4;
  19. MathLenOffset=5;
  20. MathIncrOffset=6;
  21. SysDataArrayOffset* = 0; (* array offset in system bl ock, for 32 byte alignment *)
  22. ArrDataArrayOffset*= 16*8; (* 16 bytes array offset in array block, to be compatible with the GC scheme of POINTER TO ARRAY OF ... *)
  23. TensorFlag* = 0; (* flag indicating a tensor array *)
  24. RangeFlag* = 1; (* flag indicating a range, e.g. an array derived from A[..,..] *)
  25. StackFlag* = 2; (* flag indicates temporary result *)
  26. StaticFlag* = 1; (* flag indicating a static array, may not be reallocated *)
  27. (** compiler generated traps *)
  28. WithTrap* = 1; (* generated when a WITH statement fails *)
  29. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  30. ReturnTrap* = 3;
  31. TypeEqualTrap* = 5;
  32. TypeCheckTrap* = 6;
  33. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  34. AssertTrap* = 8; (* generated when an assert fails *)
  35. ArraySizeTrap* = 9;
  36. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  37. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  38. NegativeDivisorTrap*=12;
  39. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  40. NilPointerTrap*=17; (* indicates that a nil pointer was being dereferenced *)
  41. Trace = FALSE;
  42. TraceRegisterUsageCount=TRUE;
  43. ArrayAlignment = 8*8; (* first array element of ArrayBlock and first data element of SystemBlock must be aligned to 0 MOD ArrayAlignment *)
  44. (** system call numbers *)
  45. NumberSystemCalls* = 12;
  46. SysNewRec* = 0; SysNewArr* = 1; SysNewSys* = 2; SysCaseTable* = 3; SysProcAddr* = 4;
  47. SysLock* = 5; SysUnlock* = 6; SysStart* = 7; SysAwait* = 8; SysInterfaceLookup* = 9;
  48. SysRegisterInterface* = 10; SysGetProcedure* = 11;
  49. DefaultRuntimeModuleName ="Runtime";
  50. DefaultTraceModuleName ="KernelLog";
  51. ChannelModuleName = "Channels";
  52. NonPointer = -1; (* special pointer values *)
  53. NoType = 0; (* special type info values *)
  54. LhsIsPointer = 0; (* for the operator kind *)
  55. RhsIsPointer = 1;
  56. (* priority values, lower means higher priority *)
  57. EntryPriority=-4;
  58. FirstPriority=-3;
  59. InitPriority=-2;
  60. ExitPriority=-1;
  61. BasePointerTypeSize = 5;
  62. BaseArrayTypeSize = BasePointerTypeSize + 3;
  63. LengthOffset = BasePointerTypeSize + 0;
  64. DataOffset = BasePointerTypeSize + 1;
  65. DescriptorOffset = BasePointerTypeSize + 2;
  66. BaseRecordTypeSize = BasePointerTypeSize + 2;
  67. ActionOffset = BasePointerTypeSize + 0;
  68. MonitorOffset = BasePointerTypeSize + 1;
  69. BaseObjectTypeSize = BaseRecordTypeSize;
  70. ActionTypeSize = 3;
  71. MonitorTypeSize = 7;
  72. ProcessorOffset = BaseObjectTypeSize + 1;
  73. StackLimitOffset* = BaseObjectTypeSize + 3;
  74. QuantumOffset = BaseObjectTypeSize + 4;
  75. (* flags for optimizations with small matricies and vectors (Alexey Morozov) *)
  76. SmallMatrixFlag = 3; (* flag for identification of a small matrix *)
  77. SmallVectorFlag = 3; (* flag for identification of a small vector *)
  78. Size2Flag = 4; (* size = 2 *)
  79. Size3Flag = 5; (* size = 3 *)
  80. Size4Flag = 6; (* size = 4 *)
  81. Size5Flag = 7; (* size = 5 *)
  82. Size6Flag = 8; (* size = 6 *)
  83. Size7Flag = 9; (* size = 7 *)
  84. Size8Flag = 10; (* size = 8 *)
  85. ReflectionSupport = TRUE;
  86. (* Solution for identifying procedure descriptors on the stack and for being able to differentiate "old school" stack frames from the underlying operating system stack frames:
  87. push a procedure desriptor plus one to where the BP pointer would be located. The misalignment of the procedure descriptor makes it possible to identify that it is not
  88. a base pointer but a procedure descriptor. The base pointer itself is in such cases located at BP + address size.
  89. *)
  90. TYPE
  91. Position=SyntaxTree.Position;
  92. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  93. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  94. Operand = RECORD
  95. mode: SHORTINT;
  96. op: IntermediateCode.Operand;
  97. tag: IntermediateCode.Operand;
  98. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  99. dimOffset: LONGINT;
  100. END;
  101. Fixup= POINTER TO RECORD
  102. pc: LONGINT;
  103. nextFixup: Fixup;
  104. END;
  105. WriteBackCall = POINTER TO RECORD
  106. call: SyntaxTree.ProcedureCallDesignator;
  107. next: WriteBackCall;
  108. END;
  109. Label= OBJECT
  110. VAR
  111. fixups: Fixup;
  112. section: IntermediateCode.Section;
  113. pc: LONGINT;
  114. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  115. BEGIN
  116. SELF.section := section; pc := -1;
  117. END InitLabel;
  118. PROCEDURE Resolve(pc: LONGINT);
  119. VAR at: LONGINT;
  120. BEGIN
  121. SELF.pc := pc;
  122. WHILE(fixups # NIL) DO
  123. at := fixups.pc;
  124. section.PatchAddress(at,pc);
  125. fixups := fixups.nextFixup;
  126. END;
  127. END Resolve;
  128. PROCEDURE AddFixup(at: LONGINT);
  129. VAR fixup: Fixup;
  130. BEGIN
  131. ASSERT(pc=-1);
  132. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  133. END AddFixup;
  134. END Label;
  135. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  136. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  137. VAR
  138. backend: IntermediateBackend;
  139. implementationVisitor: ImplementationVisitor;
  140. meta: MetaDataGenerator;
  141. system: Global.System;
  142. currentScope: SyntaxTree.Scope;
  143. module: Sections.Module;
  144. moduleSelf: SyntaxTree.Variable;
  145. dump: BOOLEAN;
  146. forceModuleBody: BOOLEAN;
  147. addressType: IntermediateCode.Type;
  148. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  149. BEGIN
  150. currentScope := NIL; module := NIL; moduleSelf := NIL;
  151. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  152. SELF.dump := dump;
  153. SELF.backend := backend;
  154. SELF.forceModuleBody := forceModuleBody;
  155. addressType := IntermediateCode.GetType(system,system.addressType)
  156. END Init;
  157. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  158. BEGIN
  159. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  160. END Error;
  161. PROCEDURE Type(x: SyntaxTree.Type);
  162. BEGIN
  163. x.Accept(SELF);
  164. END Type;
  165. (** types **)
  166. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  167. BEGIN (* no code emission *) END VisitBasicType;
  168. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  169. BEGIN (* no code emission *) END VisitCharacterType;
  170. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  171. BEGIN (* no code emission *) END VisitIntegerType;
  172. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  173. BEGIN (* no code emission *) END VisitFloatType;
  174. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  175. BEGIN (* no code emission *) END VisitComplexType;
  176. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  177. VAR type: SyntaxTree.Type;
  178. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  179. type := x.resolved;
  180. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  181. meta.CheckTypeDeclaration(type);
  182. END;
  183. END VisitQualifiedType;
  184. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  185. BEGIN (* no code emission *) END VisitStringType;
  186. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  187. BEGIN (* no code emission *)
  188. END VisitArrayRangeType;
  189. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  190. BEGIN (* no code emission *) END VisitArrayType;
  191. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  192. BEGIN (* no code emission *) END VisitPortType;
  193. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  194. BEGIN
  195. meta.CheckTypeDeclaration(x);
  196. END VisitMathArrayType;
  197. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  198. BEGIN
  199. meta.CheckTypeDeclaration(x);
  200. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  201. END VisitPointerType;
  202. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  203. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  204. BEGIN (* no code emission *)
  205. meta.CheckTypeDeclaration(x);
  206. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  207. IF x.pointerType.typeDeclaration # NIL THEN
  208. td := x.pointerType.typeDeclaration
  209. ELSE
  210. td := x.typeDeclaration
  211. END;
  212. Global.GetSymbolName(td,name);
  213. (* code section for object *)
  214. END;
  215. Scope(x.recordScope);
  216. END VisitRecordType;
  217. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  218. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  219. BEGIN
  220. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  221. WHILE (this # NIL) & (this.identifier# id) DO
  222. this := this.nextModifier;
  223. END;
  224. RETURN this # NIL
  225. END HasFlag;
  226. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  227. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  228. BEGIN
  229. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  230. capabilities := {};
  231. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  232. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  233. backend.SetCapabilities(capabilities);
  234. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  235. Scope(x.cellScope);
  236. END;
  237. END VisitCellType;
  238. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  239. BEGIN (* no code emission *) END VisitProcedureType;
  240. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  241. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  242. END VisitEnumerationType;
  243. (* symbols *)
  244. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  245. BEGIN
  246. Procedure(x);
  247. END VisitProcedure;
  248. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  249. BEGIN
  250. Procedure(x);
  251. END VisitOperator;
  252. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  253. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  254. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  255. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  256. BEGIN
  257. type := type.resolved;
  258. IF type IS SyntaxTree.RecordType THEN
  259. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  260. ELSIF (type IS SyntaxTree.ArrayType) THEN
  261. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  262. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  263. END;
  264. ELSIF type IS SyntaxTree.MathArrayType THEN
  265. WITH type: SyntaxTree.MathArrayType DO
  266. IF type.form = SyntaxTree.Open THEN
  267. RETURN TRUE
  268. ELSIF type.form = SyntaxTree.Static THEN
  269. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  270. END;
  271. END;
  272. END;
  273. RETURN FALSE
  274. END TypeNeedsInitialization;
  275. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  276. VAR variable: SyntaxTree.Variable;
  277. BEGIN
  278. variable := scope.firstVariable;
  279. WHILE variable # NIL DO
  280. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  281. IF variable.initializer # NIL THEN RETURN TRUE END;
  282. variable := variable.nextVariable;
  283. END;
  284. RETURN FALSE
  285. END ScopeNeedsInitialization;
  286. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  287. BEGIN
  288. size := offset - lastUpdated;
  289. IF size > 0 THEN
  290. irv.Emit(Reserve(x.position,size));
  291. END;
  292. irv.Emit(Data(x.position, op));
  293. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  294. END SingleInitialize;
  295. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  296. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  297. BEGIN
  298. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  299. WITH type: SyntaxTree.RecordType DO
  300. baseType := type.baseType;
  301. IF baseType # NIL THEN
  302. baseType := baseType.resolved;
  303. IF baseType IS SyntaxTree.PointerType THEN
  304. baseType := baseType(SyntaxTree.PointerType).pointerBase
  305. END;
  306. Initialize(baseType,NIL, offset);
  307. END;
  308. variable := type.recordScope.firstVariable;
  309. WHILE variable # NIL DO
  310. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  311. variable := variable.nextVariable
  312. END;
  313. | type: SyntaxTree.ArrayType DO
  314. IF type.form = SyntaxTree.Static THEN
  315. baseType := type.arrayBase;
  316. IF TypeNeedsInitialization(baseType) THEN
  317. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  318. FOR i := 0 TO type.staticLength-1 DO
  319. Initialize(baseType,NIL,offset+i*size);
  320. END;
  321. END;
  322. END;
  323. | type: SyntaxTree.MathArrayType DO
  324. IF type.form = SyntaxTree.Open THEN
  325. dim := DynamicDim(type);
  326. baseType := SemanticChecker.ArrayBase(type,dim);
  327. imm := IntermediateCode.Immediate(addressType,dim);
  328. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  329. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  330. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  331. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  332. (* flags remain empty (=0) for open array *)
  333. ELSIF type.form = SyntaxTree.Static THEN
  334. baseType := type.arrayBase;
  335. IF TypeNeedsInitialization(baseType) THEN
  336. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  337. ASSERT(type.staticLength < 1024*1024*1024);
  338. FOR i := 0 TO type.staticLength-1 DO
  339. Initialize(baseType,NIL,offset+i*size);
  340. END;
  341. END;
  342. END;
  343. ELSE
  344. IF initializer # NIL THEN
  345. implementationVisitor.Evaluate(initializer, op);
  346. SingleInitialize(op.op, offset);
  347. END;
  348. END;
  349. END Initialize;
  350. BEGIN
  351. IF x.externalName # NIL THEN RETURN END;
  352. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  353. (* code section for variable *)
  354. Global.GetSymbolSegmentedName(x,name);
  355. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  356. irv.SetExported(IsExported(x));
  357. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  358. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  359. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  360. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  361. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  362. FOR i := 0 TO DynamicDim(x.type)-1 DO
  363. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  364. END;
  365. ELSE
  366. lastUpdated:= 0;
  367. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  368. Initialize(x.type, x.initializer, 0);
  369. END;
  370. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  371. IF size > 0 THEN
  372. irv.Emit(Reserve(x.position,size));
  373. END;
  374. IF ~x.fixed THEN
  375. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  376. ELSE
  377. align := x.alignment;
  378. END;
  379. irv.SetPositionOrAlignment(x.fixed, align);
  380. meta.CheckTypeDeclaration(x.type);
  381. END;
  382. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  383. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  384. END;
  385. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  386. END VisitVariable;
  387. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  388. BEGIN
  389. VisitVariable(x)
  390. END VisitProperty;
  391. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  392. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  393. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  394. BEGIN
  395. ASSERT(currentScope IS SyntaxTree.CellScope);
  396. Global.GetSymbolSegmentedName(x,name);
  397. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  398. irv.SetExported(IsExported(x));
  399. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  400. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  401. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  403. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  404. FOR i := 0 TO DynamicDim(x.type)-1 DO
  405. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  406. END;
  407. ELSE
  408. lastUpdated:= 0;
  409. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  410. IF size > 0 THEN
  411. irv.Emit(Reserve(x.position,size));
  412. END;
  413. IF ~x.fixed THEN
  414. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  415. ELSE
  416. align := x.alignment;
  417. END;
  418. irv.SetPositionOrAlignment(x.fixed, align);
  419. meta.CheckTypeDeclaration(x.type);
  420. END;
  421. END VisitParameter;
  422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  423. BEGIN
  424. Type(x.declaredType); (* => code in objects *)
  425. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  426. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  427. END;
  428. END VisitTypeDeclaration;
  429. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  430. BEGIN
  431. IF (SyntaxTree.Public * x.access # {}) THEN
  432. implementationVisitor.VisitConstant(x);
  433. END;
  434. END VisitConstant;
  435. PROCEDURE Scope(x: SyntaxTree.Scope);
  436. VAR procedure: SyntaxTree.Procedure;
  437. constant: SyntaxTree.Constant;
  438. variable: SyntaxTree.Variable;
  439. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  440. cell: SyntaxTree.CellType;
  441. parameter: SyntaxTree.Parameter;
  442. property: SyntaxTree.Property;
  443. BEGIN
  444. prevScope := currentScope;
  445. currentScope := x;
  446. (* constants treated in implementation visitor *)
  447. WITH x: SyntaxTree.CellScope DO
  448. cell := x.ownerCell;
  449. parameter := cell.firstParameter;
  450. WHILE parameter # NIL DO
  451. VisitParameter(parameter);
  452. parameter := parameter.nextParameter;
  453. END;
  454. property := cell.firstProperty;
  455. WHILE property # NIL DO
  456. VisitProperty(property);
  457. property := property.nextProperty;
  458. END;
  459. ELSE
  460. END;
  461. typeDeclaration := x.firstTypeDeclaration;
  462. WHILE typeDeclaration # NIL DO
  463. VisitTypeDeclaration(typeDeclaration);
  464. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  465. END;
  466. variable := x.firstVariable;
  467. WHILE variable # NIL DO
  468. VisitVariable(variable);
  469. variable := variable.nextVariable;
  470. END;
  471. procedure := x.firstProcedure;
  472. WHILE procedure # NIL DO
  473. VisitProcedure(procedure);
  474. procedure := procedure.nextProcedure;
  475. END;
  476. constant := x.firstConstant;
  477. WHILE constant # NIL DO
  478. VisitConstant(constant);
  479. constant := constant.nextConstant;
  480. END;
  481. currentScope := prevScope;
  482. END Scope;
  483. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  484. VAR parameter: SyntaxTree.Parameter;
  485. BEGIN
  486. parameter := first;
  487. WHILE parameter # NIL DO
  488. VisitParameter(parameter);
  489. parameter := parameter.nextParameter;
  490. END;
  491. END Parameters;
  492. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  493. VAR scope: SyntaxTree.ProcedureScope;
  494. prevScope: SyntaxTree.Scope;
  495. inline, finalizer: BOOLEAN;
  496. procedureType: SyntaxTree.ProcedureType;
  497. pc: LONGINT;
  498. stackSize: LONGINT;
  499. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  500. null,size,src,dest,fp,res: IntermediateCode.Operand;
  501. cc: LONGINT;
  502. cellType: SyntaxTree.CellType;
  503. registerNumber: LONGINT;
  504. registerParameter: Backend.Registers;
  505. registerParameters: LONGINT;
  506. registerClass: IntermediateCode.RegisterClass;
  507. type: IntermediateCode.Type;
  508. formalParameter: SyntaxTree.Parameter;
  509. recordType: SyntaxTree.RecordType;
  510. isModuleBody: BOOLEAN;
  511. parametersSize: LONGINT;
  512. PROCEDURE Signature;
  513. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  514. BEGIN
  515. procedureType := x.type(SyntaxTree.ProcedureType);
  516. returnType := procedureType.returnType;
  517. IF returnType # NIL THEN
  518. meta.CheckTypeDeclaration(returnType)
  519. END;
  520. parameter := procedureType.firstParameter;
  521. WHILE parameter # NIL DO
  522. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  523. parameter := parameter.nextParameter;
  524. END;
  525. END Signature;
  526. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  527. VAR result: BOOLEAN;
  528. BEGIN
  529. result := FALSE;
  530. IF x = SyntaxTree.invalidExpression THEN
  531. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  532. result := TRUE;
  533. value := x.resolved(SyntaxTree.IntegerValue).value;
  534. ELSE
  535. Error(x.position,"expression is not an integer constant");
  536. END;
  537. RETURN result;
  538. END CheckIntegerValue;
  539. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  540. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  541. BEGIN
  542. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  543. WHILE (this # NIL) & (this.identifier # id) DO
  544. this := this.nextModifier;
  545. END;
  546. IF this # NIL THEN
  547. IF this.expression = NIL THEN
  548. Error(this.position,"expected expression value");
  549. ELSIF CheckIntegerValue(this.expression,value) THEN
  550. END;
  551. RETURN TRUE
  552. ELSE RETURN FALSE
  553. END;
  554. END HasValue;
  555. CONST DefaultDataMemorySize=512;
  556. BEGIN
  557. IF x.externalName # NIL THEN RETURN END;
  558. (*
  559. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  560. *)
  561. (* code section for this procedure *)
  562. scope := x.procedureScope;
  563. prevScope := currentScope;
  564. currentScope := scope;
  565. procedureType := x.type(SyntaxTree.ProcedureType);
  566. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  567. implementationVisitor.temporaries.Init;
  568. implementationVisitor.usedRegisters := NIL;
  569. implementationVisitor.registerUsageCount.Init;
  570. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  571. IF (scope.body # NIL) & (x.isInline) THEN
  572. inline := TRUE;
  573. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  574. ir.SetExported(IsExported(x));
  575. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  576. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  577. IF backend.cellsAreObjects THEN
  578. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  579. ir.SetExported(IsExported(x));
  580. ELSE
  581. RETURN; (* cellnet cannot be compiled for final static hardware *)
  582. END;
  583. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  584. inline := FALSE;
  585. AddBodyCallStub(x);
  586. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  587. ir.SetExported(IsExported(x));
  588. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  589. inline := FALSE;
  590. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  591. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  592. AddBodyCallStub(x);
  593. AddStackAllocation(x,stackSize);
  594. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  595. ir.SetExported(IsExported(x));
  596. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  597. inline := FALSE;
  598. Parameters(procedureType.firstParameter);
  599. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  600. ir.SetExported(IsExported(x));
  601. ELSE
  602. inline := FALSE;
  603. IF x.isEntry OR x.isExit THEN
  604. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  605. ir.SetExported(TRUE);
  606. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  607. ELSE
  608. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  609. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  610. END;
  611. END;
  612. cc := procedureType.callingConvention;
  613. IF cc = SyntaxTree.WinAPICallingConvention THEN
  614. parametersSize := ProcedureParametersSize(backend.system,x);
  615. ELSE
  616. parametersSize := 0;
  617. END;
  618. IF scope.body # NIL THEN
  619. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  620. registerNumber := 0;
  621. IF ~inline THEN
  622. IF scope.lastVariable = NIL THEN
  623. stackSize := 0
  624. ELSE
  625. stackSize := scope.lastVariable.offsetInBits;
  626. IF stackSize <0 THEN stackSize := -stackSize END;
  627. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  628. END;
  629. (*
  630. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  632. *)
  633. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  634. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  635. END;
  636. pc := ir.pc-1;
  637. (*
  638. ir.Emit(Nop(position)); (* placeholder for fill *)
  639. *)
  640. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  641. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  642. IF registerParameter = NIL THEN registerParameters := 0
  643. ELSE registerParameters := LEN(registerParameter)
  644. END;
  645. formalParameter := procedureType.lastParameter;
  646. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  647. IF ~PassInRegister(formalParameter) THEN
  648. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  649. ELSE
  650. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  651. type := GetType(system, formalParameter.type);
  652. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  653. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  654. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  655. implementationVisitor.ReleaseIntermediateOperand(src);
  656. INC(registerNumber);
  657. formalParameter := formalParameter.prevParameter;
  658. END;
  659. END;
  660. END;
  661. END;
  662. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  663. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  664. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  665. IF scope.lastVariable # NIL THEN
  666. stackSize := scope.lastVariable.offsetInBits;
  667. IF stackSize <0 THEN stackSize := -stackSize END;
  668. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  669. END;
  670. END;
  671. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  672. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  673. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  674. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  675. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  676. ir.EmitAt(pc,Push(size));
  677. implementationVisitor.StaticCallOperand(result,procedure);
  678. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  679. END;
  680. *)
  681. END;
  682. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  683. IF stackSize > 0 THEN
  684. IF (stackSize MOD system.addressSize = 0) THEN
  685. null := IntermediateCode.Immediate(addressType,0);
  686. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  687. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  688. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  689. ELSE
  690. null := IntermediateCode.Immediate(int8,0);
  691. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  692. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  693. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  694. END;
  695. (*! should potentially be called by backend -- enter might initialize
  696. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  697. *)
  698. END;
  699. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  700. parametersSize := ProcedureParametersSize(backend.system,x);
  701. ELSE
  702. parametersSize := 0;
  703. END;
  704. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  705. finalizer := FALSE;
  706. IF backend.cooperative & x.isFinalizer THEN
  707. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  708. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  709. GetRecordTypeName(recordType,name);
  710. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  711. END;
  712. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  713. IF backend.cooperative THEN
  714. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  715. IF implementationVisitor.profile & ~isModuleBody THEN
  716. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  717. END;
  718. END;
  719. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  720. IF finalizer THEN
  721. IF backend.hasLinkRegister THEN
  722. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  723. END;
  724. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  725. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  726. ir.Emit(Br(x.position,dest));
  727. ELSE
  728. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  729. END;
  730. ELSE
  731. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  732. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  733. END;
  734. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  735. IF backend.cooperative THEN
  736. IF HasPointers (scope) THEN
  737. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  738. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  739. ir.Emit(Result(x.position, res));
  740. ir.Emit(Push(x.position, res));
  741. implementationVisitor.ResetVariables(scope);
  742. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  743. ir.Emit(Pop(x.position, res));
  744. ir.Emit(Return(x.position, res));
  745. ELSE
  746. implementationVisitor.ResetVariables(scope);
  747. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  748. END;
  749. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  750. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  751. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  752. ir.Emit(Result(x.position, res));
  753. ir.Emit(Push(x.position, res));
  754. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  755. ir.Emit(Pop(x.position, res));
  756. ir.Emit(Return(x.position, res));
  757. ELSE
  758. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  759. END;
  760. END;
  761. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  762. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  763. ELSE
  764. ir.Emit(Nop(x.position));
  765. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  766. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  767. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  768. END;
  769. END;
  770. END
  771. END;
  772. ELSE (* force body for procedures *)
  773. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  774. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  775. (*IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;*)
  776. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  777. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  778. END;
  779. Scope(scope);
  780. Signature;
  781. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  782. currentScope := prevScope;
  783. END Procedure;
  784. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  785. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  786. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  787. BEGIN
  788. ASSERT (bodyProcedure # NIL);
  789. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  790. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  791. procedure.SetScope(bodyProcedure.scope);
  792. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  793. procedure.SetAccess(SyntaxTree.Hidden);
  794. Global.GetSymbolSegmentedName (procedure,name);
  795. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  796. ir.SetExported(TRUE);
  797. ir.SetPriority(InitPriority);
  798. Global.GetSymbolSegmentedName (bodyProcedure,name);
  799. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  800. implementationVisitor.currentScope := module.module.moduleScope;
  801. implementationVisitor.section := ir;
  802. implementationVisitor.PushSelfPointer();
  803. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  804. ELSIF backend.preregisterStatic THEN
  805. implementationVisitor.currentScope := module.module.moduleScope;
  806. implementationVisitor.section := ir;
  807. implementationVisitor.PushSelfPointer();
  808. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  809. ELSE
  810. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  811. ir.Emit(Call(bodyProcedure.position,op, 0));
  812. END;
  813. END AddBodyCallStub;
  814. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  815. VAR name: Basic.SegmentedName;
  816. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  817. BEGIN
  818. Global.GetSymbolSegmentedName (symbol,name);
  819. Basic.RemoveSuffix(name);
  820. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  821. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  822. ir.SetExported(TRUE);
  823. ir.SetPriority(FirstPriority);
  824. IntermediateCode.InitImmediate(op,addressType,initStack);
  825. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  826. END AddStackAllocation;
  827. (** entry function to visit a complete module *)
  828. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  829. VAR
  830. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  831. hasDynamicOperatorDeclarations: BOOLEAN;
  832. operator: SyntaxTree.Operator;
  833. import: SyntaxTree.Import;
  834. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  835. BEGIN
  836. type := type.resolved;
  837. IF type IS SyntaxTree.RecordType THEN
  838. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  839. ELSIF (type IS SyntaxTree.ArrayType) THEN
  840. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  841. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  842. END;
  843. ELSIF type IS SyntaxTree.MathArrayType THEN
  844. WITH type: SyntaxTree.MathArrayType DO
  845. IF type.form = SyntaxTree.Open THEN
  846. RETURN TRUE
  847. ELSIF type.form = SyntaxTree.Static THEN
  848. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  849. END;
  850. END;
  851. END;
  852. RETURN FALSE
  853. END TypeNeedsInitialization;
  854. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  855. VAR variable: SyntaxTree.Variable;
  856. BEGIN
  857. variable := scope.firstVariable;
  858. WHILE variable # NIL DO
  859. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  860. IF variable.initializer # NIL THEN RETURN TRUE END;
  861. variable := variable.nextVariable;
  862. END;
  863. RETURN FALSE
  864. END ScopeNeedsInitialization;
  865. BEGIN
  866. ASSERT(x # NIL); ASSERT(module # NIL);
  867. SELF.module := module;
  868. (* add import names to the generated Sections.Module *)
  869. import := x.moduleScope.firstImport;
  870. WHILE import # NIL DO
  871. import.module.GetName(idstr);
  872. module.imports.AddName(idstr);
  873. import := import.nextImport
  874. END;
  875. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  876. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  877. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  878. moduleSelf.SetType(system.anyType);
  879. moduleSelf.SetScope(x.moduleScope);
  880. moduleSelf.SetUntraced(TRUE);
  881. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  882. ir.SetExported(TRUE);
  883. IntermediateCode.InitImmediate(op,addressType,0);
  884. ir.Emit(Data(Basic.invalidPosition,op));
  885. END;
  886. implementationVisitor.module := module;
  887. implementationVisitor.moduleScope := x.moduleScope;
  888. implementationVisitor.moduleSelf := moduleSelf;
  889. implementationVisitor.canBeLoaded := TRUE;
  890. meta.SetModule(module);
  891. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  892. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  893. END;
  894. IF backend.profile THEN
  895. EnsureBodyProcedure(x.moduleScope);
  896. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  897. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  898. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  899. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  900. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  901. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  902. Global.GetModuleName(module.module,idstr);
  903. implementationVisitor.ProfilerAddModule(idstr);
  904. implementationVisitor.numberProcedures := 0;
  905. END;
  906. implementationVisitor.profile := backend.profile;
  907. (* check if there is at least one dynamic operator locally defined *)
  908. hasDynamicOperatorDeclarations := FALSE;
  909. operator := x.moduleScope.firstOperator;
  910. WHILE operator # NIL DO
  911. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  912. operator := operator.nextOperator
  913. END;
  914. (* add operator initialization code section *)
  915. IF hasDynamicOperatorDeclarations THEN
  916. EnsureBodyProcedure(x.moduleScope);
  917. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  918. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  919. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  920. END;
  921. Scope(x.moduleScope);
  922. IF hasDynamicOperatorDeclarations THEN
  923. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  924. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  925. END;
  926. IF backend.profile THEN
  927. implementationVisitor.ProfilerPatchInit;
  928. END;
  929. END Module;
  930. END DeclarationVisitor;
  931. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  932. RegisterUsageCount*=OBJECT
  933. VAR used: UsedArray; count: LONGINT;
  934. PROCEDURE &Init;
  935. VAR i: LONGINT;
  936. BEGIN
  937. count := 0;
  938. IF used = NIL THEN NEW(used,64); END;
  939. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  940. END Init;
  941. PROCEDURE Grow;
  942. VAR new: UsedArray; size,i: LONGINT;
  943. BEGIN
  944. size := LEN(used)*2;
  945. NEW(new,size);
  946. FOR i := 0 TO LEN(used)-1 DO
  947. new[i].count := used[i].count;
  948. new[i].type := used[i].type;
  949. new[i].map := used[i].map
  950. END;
  951. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  952. used := new
  953. END Grow;
  954. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  955. BEGIN
  956. INC(count);
  957. IF count = LEN(used) THEN Grow END;
  958. used[count].type := type;
  959. used[count].class := class;
  960. used[count].map := count;
  961. RETURN count;
  962. END Next;
  963. PROCEDURE IncUse(register: LONGINT);
  964. BEGIN
  965. INC(used[register].count);
  966. (*
  967. IF (register = 1) & (count > 30) THEN
  968. D.TraceBack;
  969. END;
  970. *)
  971. END IncUse;
  972. PROCEDURE DecUse(register: LONGINT);
  973. BEGIN
  974. DEC(used[register].count);
  975. END DecUse;
  976. PROCEDURE Map(register: LONGINT): LONGINT;
  977. VAR map : LONGINT;
  978. BEGIN
  979. IF register > 0 THEN
  980. map := used[register].map;
  981. WHILE register # map DO register := map; map := used[register].map END;
  982. END;
  983. RETURN register
  984. END Map;
  985. PROCEDURE Use(register: LONGINT): LONGINT;
  986. BEGIN
  987. IF register< LEN(used) THEN
  988. RETURN used[register].count
  989. ELSE
  990. RETURN 0
  991. END
  992. END Use;
  993. END RegisterUsageCount;
  994. RegisterEntry = POINTER TO RECORD
  995. prev,next: RegisterEntry;
  996. register: LONGINT;
  997. registerClass: IntermediateCode.RegisterClass;
  998. type: IntermediateCode.Type;
  999. END;
  1000. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1001. Variables = OBJECT (Basic.List)
  1002. VAR
  1003. inUse: VariableUse;
  1004. registerIndex: LONGINT;
  1005. PROCEDURE & Init;
  1006. VAR i: LONGINT;
  1007. BEGIN
  1008. InitList(16);
  1009. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1010. registerIndex := 1024;
  1011. END Init;
  1012. PROCEDURE GetUsage(VAR use: VariableUse);
  1013. BEGIN
  1014. use := inUse;
  1015. END GetUsage;
  1016. PROCEDURE SetUsage(CONST use: VariableUse);
  1017. BEGIN
  1018. inUse := use;
  1019. END SetUsage;
  1020. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1021. VAR any: ANY;
  1022. BEGIN
  1023. any := Get(i);;
  1024. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1025. END GetVariable;
  1026. PROCEDURE Occupy(pos: LONGINT);
  1027. BEGIN
  1028. INCL(inUse[pos DIV 32], pos MOD 32);
  1029. END Occupy;
  1030. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1031. BEGIN
  1032. Occupy(Length());
  1033. Add(v);
  1034. END AddVariable;
  1035. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1036. VAR var : SyntaxTree.Variable;
  1037. BEGIN
  1038. FOR pos := 0 TO Length()-1 DO
  1039. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1040. var := GetVariable(pos);
  1041. IF type.SameType(var.type) THEN
  1042. Occupy(pos); RETURN var
  1043. END;
  1044. END;
  1045. END;
  1046. pos := Length();
  1047. RETURN NIL
  1048. END GetFreeVariable;
  1049. END Variables;
  1050. SymbolMap = POINTER TO RECORD
  1051. this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap;
  1052. isAddress: BOOLEAN;
  1053. END;
  1054. SymbolMapper = OBJECT
  1055. VAR
  1056. first: SymbolMap;
  1057. PROCEDURE & Init;
  1058. BEGIN
  1059. first := NIL;
  1060. END Init;
  1061. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; isAddress: BOOLEAN);
  1062. VAR new: SymbolMap;
  1063. BEGIN
  1064. NEW(new); new.this := this; new.to := to; new.tag := tag; new.isAddress := isAddress;
  1065. new.next := first; first := new;
  1066. END Add;
  1067. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1068. VAR s: SymbolMap;
  1069. BEGIN
  1070. s := first;
  1071. WHILE (s # NIL) & (s.this # this) DO
  1072. s := s.next
  1073. END;
  1074. RETURN s
  1075. END Get;
  1076. END SymbolMapper;
  1077. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1078. VAR
  1079. system: Global.System;
  1080. section: IntermediateCode.Section;
  1081. module: Sections.Module;
  1082. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1083. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1084. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1085. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1086. checker: SemanticChecker.Checker;
  1087. backend: IntermediateBackend;
  1088. meta: MetaDataGenerator;
  1089. position: Position;
  1090. moduleSelf: SyntaxTree.Variable;
  1091. (* variables for hand over of variables / temporary state *)
  1092. currentScope: SyntaxTree.Scope;
  1093. constantDeclaration : SyntaxTree.Symbol;
  1094. result: Operand; (* result of the most recent expression / statement *)
  1095. destination: IntermediateCode.Operand;
  1096. arrayDestinationTag: IntermediateCode.Operand;
  1097. arrayDestinationDimension:LONGINT;
  1098. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1099. conditional: BOOLEAN;
  1100. trueLabel, falseLabel, exitLabel: Label;
  1101. locked: BOOLEAN;
  1102. (*
  1103. usedRegisters: Registers;
  1104. *)
  1105. registerUsageCount: RegisterUsageCount;
  1106. usedRegisters: RegisterEntry;
  1107. (* useful operands and types *)
  1108. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1109. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1110. commentPrintout: Printout.Printer;
  1111. dump: Streams.Writer;
  1112. tagsAvailable : BOOLEAN;
  1113. supportedInstruction: SupportedInstructionProcedure;
  1114. supportedImmediate: SupportedImmediateProcedure;
  1115. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1116. emitLabels: BOOLEAN;
  1117. runtimeModuleName : SyntaxTree.IdentifierString;
  1118. newObjectFile: BOOLEAN;
  1119. indexCounter: LONGINT;
  1120. profile: BOOLEAN;
  1121. profileId, profileInit: IntermediateCode.Section;
  1122. profileInitPatchPosition: LONGINT;
  1123. numberProcedures: LONGINT;
  1124. procedureResultDesignator : SyntaxTree.Designator;
  1125. operatorInitializationCodeSection: IntermediateCode.Section;
  1126. fingerPrinter: FingerPrinter.FingerPrinter;
  1127. temporaries: Variables;
  1128. canBeLoaded : BOOLEAN;
  1129. currentIsInline: BOOLEAN;
  1130. currentMapper: SymbolMapper;
  1131. currentInlineExit: Label;
  1132. moduleBodySection: IntermediateCode.Section;
  1133. NeedDescriptor : BOOLEAN;
  1134. cooperativeSwitches: BOOLEAN;
  1135. lastSwitchPC: LONGINT;
  1136. isUnchecked: BOOLEAN;
  1137. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1138. newObjectFile: BOOLEAN);
  1139. BEGIN
  1140. SELF.system := system;
  1141. SELF.runtimeModuleName := runtime;
  1142. currentScope := NIL;
  1143. hiddenPointerType := NIL;
  1144. delegatePointerType := NIL;
  1145. awaitProcCounter := 0;
  1146. labelId := 0; constId := 0; labelId := 0;
  1147. SELF.checker := checker;
  1148. SELF.backend := backend;
  1149. position := Basic.invalidPosition;
  1150. conditional := FALSE;
  1151. locked := FALSE;
  1152. InitOperand(result,ModeUndefined);
  1153. addressType := IntermediateCode.GetType(system,system.addressType);
  1154. setType := IntermediateCode.GetType(system,system.setType);
  1155. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1156. byteType := IntermediateCode.GetType(system, system.byteType);
  1157. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1158. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1159. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1160. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1161. nil := IntermediateCode.Immediate(addressType,0);
  1162. one := IntermediateCode.Immediate(addressType,1);
  1163. IntermediateCode.InitOperand(destination);
  1164. tagsAvailable := TRUE;
  1165. supportedInstruction := supportedInstructionProcedure;
  1166. supportedImmediate := supportedImmediateProcedure;
  1167. inData := FALSE;
  1168. SELF.emitLabels := emitLabels;
  1169. IntermediateCode.InitOperand(arrayDestinationTag);
  1170. bool := IntermediateCode.GetType(system,system.booleanType);
  1171. IntermediateCode.InitImmediate(false,bool,0);
  1172. IntermediateCode.InitImmediate(true,bool,1);
  1173. SELF.newObjectFile := newObjectFile;
  1174. indexCounter := 0;
  1175. NEW(registerUsageCount);
  1176. usedRegisters := NIL;
  1177. procedureResultDesignator := NIL;
  1178. NEW(fingerPrinter, system);
  1179. NEW(temporaries);
  1180. currentIsInline := FALSE;
  1181. NeedDescriptor := FALSE;
  1182. isUnchecked := backend.noRuntimeChecks;
  1183. END Init;
  1184. TYPE Context = RECORD
  1185. section: IntermediateCode.Section;
  1186. registerUsageCount: RegisterUsageCount;
  1187. usedRegisters: RegisterEntry;
  1188. END;
  1189. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1190. VAR context: Context;
  1191. BEGIN
  1192. context.section := section;
  1193. context.registerUsageCount := registerUsageCount;
  1194. context.usedRegisters := usedRegisters;
  1195. section := new;
  1196. NEW(registerUsageCount);
  1197. usedRegisters := NIL;
  1198. RETURN context;
  1199. END SwitchContext;
  1200. PROCEDURE ReturnToContext(context: Context);
  1201. BEGIN
  1202. section := context.section;
  1203. registerUsageCount := context.registerUsageCount;
  1204. usedRegisters := context.usedRegisters;
  1205. END ReturnToContext;
  1206. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1207. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1208. BEGIN
  1209. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1210. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1211. END;
  1212. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1213. section.SetExported(IsExported(syntaxTreeSymbol));
  1214. RETURN section
  1215. END NewSection;
  1216. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1217. VAR new: LONGINT;
  1218. BEGIN
  1219. new := registerUsageCount.Next(type,class);
  1220. UseRegister(new);
  1221. RETURN new
  1222. END AcquireRegister;
  1223. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1224. VAR
  1225. fingerPrint: SyntaxTree.FingerPrint;
  1226. fingerPrintString: ARRAY 32 OF CHAR;
  1227. BEGIN
  1228. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1229. string := "[";
  1230. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1231. Strings.Append(string, fingerPrintString);
  1232. Strings.Append(string, "]");
  1233. END GetFingerprintString;
  1234. (** get the name for the code section that represens a certain symbol
  1235. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1236. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1237. VAR string: ARRAY 32 OF CHAR;
  1238. BEGIN
  1239. Global.GetSymbolSegmentedName(symbol, name);
  1240. (* if the symbol is an operator, then append the fingerprint to the name *)
  1241. IF symbol IS SyntaxTree.Operator THEN
  1242. GetFingerprintString(symbol, string);
  1243. Basic.AppendToSegmentedName(name,string);
  1244. END
  1245. END GetCodeSectionNameForSymbol;
  1246. (** get the name for the code section that represens a certain symbol
  1247. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1248. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1249. VAR string: ARRAY 32 OF CHAR;
  1250. BEGIN
  1251. Global.GetSymbolNameInScope(symbol, scope, name);
  1252. (* if the symbol is an operator, then append the fingerprint to the name *)
  1253. IF symbol IS SyntaxTree.Operator THEN
  1254. GetFingerprintString(symbol, string);
  1255. Strings.Append(name, string);
  1256. END
  1257. END GetCodeSectionNameForSymbolInScope;
  1258. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1259. BEGIN
  1260. IF dump # NIL THEN
  1261. dump.String("enter "); dump.String(s); dump.Ln;
  1262. END;
  1263. END TraceEnter;
  1264. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1265. BEGIN
  1266. IF dump # NIL THEN
  1267. dump.String("exit "); dump.String(s); dump.Ln;
  1268. END;
  1269. END TraceExit;
  1270. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1271. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1272. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1273. VAR i: LONGINT;
  1274. BEGIN
  1275. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1276. IF op.rule # NIL THEN
  1277. FOR i := 0 TO LEN(op.rule)-1 DO
  1278. CheckRegister(op.rule[i])
  1279. END;
  1280. END;
  1281. END CheckRegister;
  1282. BEGIN
  1283. CheckRegister(instruction.op1);
  1284. CheckRegister(instruction.op2);
  1285. CheckRegister(instruction.op3);
  1286. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1287. ELSE section.Emit(instruction);
  1288. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1289. END;
  1290. END Emit;
  1291. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1292. BEGIN
  1293. IF backend.cooperative THEN
  1294. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1295. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1296. ELSE
  1297. Emit(Trap(position,trapNo));
  1298. END;
  1299. END EmitTrap;
  1300. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1301. VAR name: Basic.SegmentedName;
  1302. VAR op1, op2, reg: IntermediateCode.Operand;
  1303. VAR call, nocall: Label;
  1304. VAR parametersSize: LONGINT;
  1305. VAR prevSection: IntermediateCode.Section;
  1306. VAR prevDump: Streams.Writer;
  1307. VAR body: SyntaxTree.Body;
  1308. VAR procedureType: SyntaxTree.ProcedureType;
  1309. BEGIN
  1310. IF procedure # NIL THEN
  1311. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1312. ELSE procedureType := NIL;
  1313. END;
  1314. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1315. prevSection := SELF.section;
  1316. SELF.section := section;
  1317. prevDump := dump;
  1318. dump := section.comments;
  1319. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1320. IF backend.hasLinkRegister THEN
  1321. Emit(Push(Basic.invalidPosition, lr));
  1322. END;
  1323. Emit(Push(Basic.invalidPosition,fp));
  1324. END;
  1325. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1326. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1327. GetCodeSectionNameForSymbol(procedure, name);
  1328. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1329. ELSE
  1330. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1331. END;
  1332. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1333. Emit(Push(Basic.invalidPosition,op1));
  1334. Emit(Mov(Basic.invalidPosition,fp, sp));
  1335. body := procedure.procedureScope.body;
  1336. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1337. NEW(call, section);
  1338. NEW(nocall, section);
  1339. reg := NewRegisterOperand(addressType);
  1340. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1341. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1342. BrltL(call, sp, reg);
  1343. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1344. BrgeL(nocall, sp, op2);
  1345. call.Resolve(section.pc);
  1346. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1347. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1348. Emit(Push(Basic.invalidPosition, op2));
  1349. Emit(Push(Basic.invalidPosition, reg));
  1350. ReleaseIntermediateOperand(reg);
  1351. CallThis(position, "Activities","ExpandStack",2);
  1352. Emit(Result(Basic.invalidPosition, sp));
  1353. nocall.Resolve(section.pc);
  1354. END;
  1355. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1356. IF procedure # NIL THEN
  1357. body := procedure.procedureScope.body;
  1358. ELSE
  1359. body := NIL;
  1360. END;
  1361. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1362. Emit(Push(Basic.invalidPosition, one)) ;
  1363. procedureType.SetParametersOffset(1);
  1364. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1365. END;
  1366. Emit(Mov(Basic.invalidPosition, fp, sp));
  1367. END;
  1368. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1369. SELF.section := prevSection;
  1370. dump := prevDump;
  1371. END EmitEnter;
  1372. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1373. VAR op1,op2: IntermediateCode.Operand;
  1374. VAR instruction: IntermediateCode.Instruction;
  1375. BEGIN
  1376. IntermediateCode.InitNumber(op1,callconv);
  1377. IntermediateCode.InitNumber(op2,varSize);
  1378. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1379. RETURN instruction
  1380. END Enter;
  1381. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1382. VAR op1: IntermediateCode.Operand;
  1383. VAR instruction: IntermediateCode.Instruction;
  1384. BEGIN
  1385. IntermediateCode.InitNumber(op1,callconv);
  1386. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1387. RETURN instruction
  1388. END Leave;
  1389. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1390. VAR prevSection: IntermediateCode.Section;
  1391. VAR op2, size: IntermediateCode.Operand;
  1392. VAR body: SyntaxTree.Body;
  1393. BEGIN
  1394. prevSection := SELF.section;
  1395. SELF.section := section;
  1396. Emit(Leave(position, callconv));
  1397. IF procedure # NIL THEN
  1398. body := procedure.procedureScope.body;
  1399. ELSE
  1400. body := NIL;
  1401. END;
  1402. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1403. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1404. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1405. Emit(Add(position, sp, fp, op2));
  1406. ELSE
  1407. Emit(Mov(position, sp, fp));
  1408. END;
  1409. Emit(Pop(position, fp));
  1410. END;
  1411. SELF.section := prevSection;
  1412. END EmitLeave;
  1413. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1414. VAR m: SymbolMap;
  1415. BEGIN
  1416. position := x.position;
  1417. IF currentIsInline THEN
  1418. m := currentMapper.Get(x);
  1419. IF m # NIL THEN
  1420. (*
  1421. Printout.Info("mapping from", x);
  1422. Printout.Info("mapping to ", m.to);
  1423. *)
  1424. m.to.Accept(SELF);
  1425. op := result;
  1426. IF m.tag # NIL THEN
  1427. ReleaseIntermediateOperand(result.tag);
  1428. m.tag.Accept(SELF);
  1429. op.tag := result.op;
  1430. ReleaseIntermediateOperand(result.tag);
  1431. END;
  1432. RETURN
  1433. END;
  1434. END;
  1435. x.Accept(SELF);
  1436. op := result;
  1437. END Symbol;
  1438. PROCEDURE Expression(x: SyntaxTree.Expression);
  1439. BEGIN
  1440. position := x.position;
  1441. constantDeclaration := NIL;
  1442. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1443. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1444. END;
  1445. IF x.resolved # NIL THEN
  1446. x.resolved.Accept(SELF)
  1447. ELSE
  1448. x.Accept(SELF)
  1449. END;
  1450. (* check this, was commented out in ActiveCells3 *)
  1451. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1452. Error(x.position, "unsupported modifier");
  1453. END;
  1454. END Expression;
  1455. (*
  1456. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1457. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1458. BEGIN
  1459. temporaries.GetUsage(current);
  1460. FOR i := 0 TO LEN(current)-1 DO
  1461. set := current[i] - previous[i];
  1462. IF set # {} THEN
  1463. FOR j := 0 TO MAX(SET)-1 DO
  1464. IF j IN set THEN
  1465. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1466. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1467. Symbol(variable, op);
  1468. MakeMemory(tmp,op.op,addressType,0);
  1469. ReleaseOperand(op);
  1470. Emit(Mov(position,tmp, nil ) );
  1471. ReleaseIntermediateOperand(tmp);
  1472. END;
  1473. END;
  1474. END;
  1475. END;
  1476. END;
  1477. END ResetUsedTemporaries;
  1478. *)
  1479. PROCEDURE Statement(x: SyntaxTree.Statement);
  1480. VAR use: VariableUse;
  1481. BEGIN
  1482. temporaries.GetUsage(use);
  1483. position := x.position;
  1484. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1485. IF commentPrintout # NIL THEN
  1486. commentPrintout.Statement(x);
  1487. dump.Ln;
  1488. (*dump.Update;*)
  1489. END;
  1490. x.Accept(SELF);
  1491. (*
  1492. CheckRegistersFree();
  1493. *)
  1494. (*ResetUsedTemporaries(use);*)
  1495. temporaries.SetUsage(use);
  1496. END Statement;
  1497. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1498. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1499. *)
  1500. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1501. BEGIN
  1502. ASSERT(op.mode # IntermediateCode.Undefined);
  1503. IF op.mode = IntermediateCode.ModeMemory THEN
  1504. ReuseCopy(res,op);
  1505. ELSE
  1506. res := op;
  1507. UseIntermediateOperand(res);
  1508. END;
  1509. IntermediateCode.AddOffset(res,offset);
  1510. IntermediateCode.MakeMemory(res,type);
  1511. END MakeMemory;
  1512. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1513. VAR mem: IntermediateCode.Operand;
  1514. BEGIN
  1515. MakeMemory(mem,res,type,offset);
  1516. ReleaseIntermediateOperand(res);
  1517. res := mem;
  1518. END ToMemory;
  1519. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1520. VAR mem: IntermediateCode.Operand;
  1521. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1522. componentType: SyntaxTree.Type;
  1523. BEGIN
  1524. type := type.resolved;
  1525. IF operand.mode = ModeReference THEN
  1526. IF type IS SyntaxTree.RangeType THEN
  1527. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1528. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1529. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1530. ReleaseOperand(operand);
  1531. operand.op := firstOp;
  1532. operand.tag := lastOp;
  1533. operand.extra := stepOp;
  1534. ELSIF type IS SyntaxTree.ComplexType THEN
  1535. componentType := type(SyntaxTree.ComplexType).componentType;
  1536. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1537. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1538. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1539. ReleaseOperand(operand);
  1540. operand.op := firstOp;
  1541. operand.tag := lastOp
  1542. ELSE
  1543. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1544. ReleaseIntermediateOperand(operand.op);
  1545. operand.op := mem;
  1546. END;
  1547. operand.mode := ModeValue;
  1548. END;
  1549. ASSERT(operand.mode = ModeValue);
  1550. END LoadValue;
  1551. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1552. VAR prevConditional: BOOLEAN;
  1553. BEGIN
  1554. prevConditional := conditional;
  1555. conditional := FALSE;
  1556. InitOperand(result, ModeUndefined);
  1557. Expression(x);
  1558. op := result;
  1559. LoadValue(op,x.type.resolved);
  1560. conditional := prevConditional;
  1561. END Evaluate;
  1562. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1563. VAR prevConditional: BOOLEAN;
  1564. BEGIN
  1565. prevConditional := conditional;
  1566. conditional := FALSE;
  1567. InitOperand(result,ModeUndefined);
  1568. Expression(x);
  1569. op := result;
  1570. (*
  1571. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1572. *)
  1573. conditional := prevConditional;
  1574. END Designate;
  1575. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1576. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1577. BEGIN
  1578. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1579. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1580. conditional := TRUE;
  1581. trueLabel := trueL; falseLabel := falseL;
  1582. Expression(x);
  1583. trueL := trueLabel; falseL := falseLabel;
  1584. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1585. END Condition;
  1586. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1587. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1588. BEGIN
  1589. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1590. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1591. RETURN op
  1592. END NewRegisterOperand;
  1593. PROCEDURE UnuseRegister(register: LONGINT);
  1594. BEGIN
  1595. IF (register > 0) THEN
  1596. register := registerUsageCount.Map(register);
  1597. registerUsageCount.DecUse(register);
  1598. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1599. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1600. END;
  1601. IF registerUsageCount.Use(register)=0 THEN
  1602. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1603. Warning(position, "register cannot be removed");
  1604. END;
  1605. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1606. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1607. END;
  1608. ELSIF registerUsageCount.Use(register)<0 THEN
  1609. Warning(position, "register removed too often");
  1610. IF dump # NIL THEN
  1611. dump.String("register removed too often"); dump.Ln; dump.Update;
  1612. END;
  1613. END;
  1614. END;
  1615. END UnuseRegister;
  1616. PROCEDURE UseRegister(register: LONGINT);
  1617. BEGIN
  1618. IF (register > 0) THEN
  1619. register := registerUsageCount.Map(register);
  1620. registerUsageCount.IncUse(register);
  1621. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1622. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1623. END;
  1624. IF registerUsageCount.Use(register)=1 THEN
  1625. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1626. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1627. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1628. END;
  1629. END;
  1630. END;
  1631. END UseRegister;
  1632. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1633. BEGIN
  1634. UnuseRegister(op.register)
  1635. END ReleaseIntermediateOperand;
  1636. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1637. BEGIN
  1638. UseRegister(op.register)
  1639. END UseIntermediateOperand;
  1640. PROCEDURE ReleaseOperand(CONST op: Operand);
  1641. BEGIN
  1642. UnuseRegister(op.op.register);
  1643. UnuseRegister(op.tag.register);
  1644. UnuseRegister(op.extra.register);
  1645. END ReleaseOperand;
  1646. (* save registers marked in array "markedRegisters" to the stack
  1647. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1648. *)
  1649. PROCEDURE SaveRegisters();
  1650. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1651. BEGIN
  1652. entry := usedRegisters;
  1653. WHILE entry # NIL DO
  1654. type := registerUsageCount.used[entry.register].type;
  1655. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1656. Emit(Push(position,op));
  1657. entry := entry.next;
  1658. END;
  1659. END SaveRegisters;
  1660. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1661. BEGIN
  1662. saved := usedRegisters;
  1663. usedRegisters := NIL;
  1664. END ReleaseUsedRegisters;
  1665. (** remove parameter registers from used queue *)
  1666. PROCEDURE ReleaseParameterRegisters;
  1667. VAR entry,prev,next: RegisterEntry;
  1668. BEGIN
  1669. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1670. WHILE entry # NIL DO
  1671. next := entry.next;
  1672. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1673. registerUsageCount.DecUse(entry.register);
  1674. ASSERT(registerUsageCount.Use(entry.register)=0);
  1675. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1676. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1677. END;
  1678. ELSIF prev = NIL THEN
  1679. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1680. ELSE
  1681. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1682. END;
  1683. entry := next;
  1684. END;
  1685. END ReleaseParameterRegisters;
  1686. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1687. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1688. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1689. BEGIN
  1690. entry := saved;
  1691. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1692. entry := prev;
  1693. WHILE entry # NIL DO
  1694. prev := entry.prev;
  1695. type := entry.type;
  1696. class := entry.registerClass;
  1697. IntermediateCode.InitRegister(op,type,class,entry.register);
  1698. (*
  1699. new := registerUsageCount.Next(type,class);
  1700. registerUsageCount.Remap(entry.register,new);
  1701. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1702. dump.String("remap register "); dump.Int(entry.register,1);
  1703. dump.String("to "); dump.Int(new,1);
  1704. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1705. END;
  1706. entry.register := new;
  1707. *)
  1708. Emit(Pop(position,op));
  1709. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1710. entry := prev;
  1711. END;
  1712. (*
  1713. usedRegisters := saved;
  1714. *)
  1715. END RestoreRegisters;
  1716. PROCEDURE CheckRegistersFree;
  1717. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1718. BEGIN
  1719. IF usedRegisters # NIL THEN
  1720. r := usedRegisters;
  1721. WHILE r # NIL DO
  1722. warning := "register ";
  1723. Strings.AppendInt(warning, r.register);
  1724. Strings.Append(warning, " not released.");
  1725. Warning(position,warning);
  1726. r := r .next;
  1727. END;
  1728. END;
  1729. FOR i := 0 TO registerUsageCount.count-1 DO
  1730. IF registerUsageCount.used[i].count < 0 THEN
  1731. warning := "register ";
  1732. Strings.AppendInt(warning, i);
  1733. Strings.Append(warning, " unused too often.");
  1734. Warning(position,warning);
  1735. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1736. warning := "register ";
  1737. Strings.AppendInt(warning, i);
  1738. Strings.Append(warning, " not unused often enough.");
  1739. Warning(position,warning);
  1740. END;
  1741. END;
  1742. END CheckRegistersFree;
  1743. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1744. Otherwise allocate a new register.
  1745. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1746. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1747. BEGIN
  1748. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1749. UseIntermediateOperand(result);
  1750. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1751. UseIntermediateOperand(result);
  1752. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1753. END;
  1754. END Reuse2;
  1755. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1756. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1757. If not then allocate a new register.
  1758. Does NOT necessarily keep the content of src1 or src2 in result!
  1759. *)
  1760. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1761. BEGIN
  1762. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1765. UseIntermediateOperand(result);
  1766. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1767. result := alternative; alternative := emptyOperand;
  1768. UseIntermediateOperand(result);
  1769. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1770. END;
  1771. END Reuse2a;
  1772. (* like reuse2 but only one source *)
  1773. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1774. BEGIN
  1775. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1776. UseIntermediateOperand(result);
  1777. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1778. END;
  1779. END Reuse1;
  1780. (* like reuse2a but only one source *)
  1781. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1782. BEGIN
  1783. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1784. UseIntermediateOperand(result);
  1785. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1786. UseIntermediateOperand(result);
  1787. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1788. END;
  1789. END Reuse1a;
  1790. (* like reuse1 but guarantees that content of src1 is in result *)
  1791. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1792. BEGIN
  1793. IF ReusableRegister(src1) THEN
  1794. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1795. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1796. UseIntermediateOperand(result);
  1797. ELSE
  1798. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1799. Emit(Mov(position,result,src1));
  1800. END
  1801. END ReuseCopy;
  1802. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1803. BEGIN
  1804. IF ReusableRegister(src) THEN
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1806. ELSE
  1807. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1808. Emit(Mov(position,result,src));
  1809. ReleaseIntermediateOperand(src);
  1810. END
  1811. END TransferToRegister;
  1812. (** labels and branches **)
  1813. PROCEDURE NewLabel(): Label;
  1814. VAR label: Label;
  1815. BEGIN
  1816. NEW(label,section); RETURN label;
  1817. END NewLabel;
  1818. PROCEDURE SetLabel(label: Label);
  1819. BEGIN label.Resolve(section.pc);
  1820. END SetLabel;
  1821. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1822. BEGIN
  1823. ASSERT(label # NIL);
  1824. IF label.pc < 0 THEN (* label not yet set *)
  1825. label.AddFixup(section.pc);
  1826. END;
  1827. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1828. END LabelOperand;
  1829. PROCEDURE BrL(label: Label);
  1830. BEGIN
  1831. Emit(Br(position,LabelOperand(label)));
  1832. END BrL;
  1833. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1834. BEGIN
  1835. Emit(Brge(position,LabelOperand(label),left,right));
  1836. END BrgeL;
  1837. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1838. BEGIN
  1839. Emit(Brlt(position,LabelOperand(label),left,right));
  1840. END BrltL;
  1841. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1842. BEGIN
  1843. Emit(Breq(position,LabelOperand(label),left,right));
  1844. END BreqL;
  1845. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1846. BEGIN
  1847. Emit(Brne(position,LabelOperand(label),left,right));
  1848. END BrneL;
  1849. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1850. VAR new: IntermediateCode.Operand;
  1851. BEGIN
  1852. IF Trace THEN TraceEnter("Convert") END;
  1853. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1854. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1855. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1856. & (operand.offset = 0)
  1857. THEN
  1858. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1859. Emit(Conv(position,new,operand));
  1860. ELSE
  1861. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1862. Emit(Conv(position,new,operand));
  1863. ReleaseIntermediateOperand(operand);
  1864. END;
  1865. operand := new;
  1866. ELSIF (operand.mode = IntermediateCode.ModeImmediate) & (operand.symbol.name = "") & (operand.type.sizeInBits <= type.sizeInBits) & (operand.type.form IN IntermediateCode.Integer) & (type.form IN IntermediateCode.Integer) THEN
  1867. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1868. ELSE
  1869. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1870. Emit(Conv(position,new,operand));
  1871. ReleaseIntermediateOperand(operand);
  1872. operand := new;
  1873. END;
  1874. IF Trace THEN TraceExit("Convert") END;
  1875. END Convert;
  1876. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1877. VAR exit: Label;
  1878. BEGIN
  1879. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1880. exit := NewLabel();
  1881. br(exit,left,right);
  1882. EmitTrap(position,trapNo);
  1883. SetLabel(exit);
  1884. END TrapC;
  1885. (** expressions *)
  1886. (** emit necessary runtime check for set elements **)
  1887. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1888. VAR max: IntermediateCode.Operand;
  1889. BEGIN
  1890. IF isUnchecked THEN RETURN END;
  1891. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1892. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1893. TrapC(BrgeL, max, o, SetElementTrap);
  1894. END;
  1895. END CheckSetElement;
  1896. (** the set that a range represents **)
  1897. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1898. VAR
  1899. operand: Operand;
  1900. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1901. BEGIN
  1902. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1903. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1904. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1905. one := IntermediateCode.Immediate(setType, 1);
  1906. Evaluate(x, operand);
  1907. Convert(operand.op, setType);
  1908. Convert(operand.tag, setType);
  1909. CheckSetElement(operand.op);
  1910. CheckSetElement(operand.tag);
  1911. (* create mask for lower bound
  1912. i.e. shift 11111111 to the left by the value of the lower bound
  1913. *)
  1914. Reuse1(temp, operand.op);
  1915. Emit(Shl(position,temp, allBits, operand.op));
  1916. ReleaseIntermediateOperand(operand.op);
  1917. operand.op := temp;
  1918. (* create mask for upper bound
  1919. i.e. shift 11111111 to the right by the difference between the
  1920. upper bound and the maximum number of set elements
  1921. *)
  1922. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1923. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1924. Reuse1(temp, operand.tag);
  1925. ELSE
  1926. Reuse1(temp, operand.tag);
  1927. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1928. Emit(Sub(position,temp, size, operand.tag));
  1929. END;
  1930. Emit(Shr(position,temp, allBits, operand.tag));
  1931. ReleaseIntermediateOperand(operand.tag);
  1932. operand.tag := temp;
  1933. Reuse2(resultingSet, operand.op, operand.tag);
  1934. (* intersect the two masks *)
  1935. Emit(And(position,resultingSet, operand.op, operand.tag));
  1936. ReleaseOperand(operand);
  1937. RETURN resultingSet
  1938. END SetFromRange;
  1939. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1940. VAR
  1941. res, operand: Operand;
  1942. temp, one, noBits, dest: IntermediateCode.Operand;
  1943. expression: SyntaxTree.Expression;
  1944. i: LONGINT;
  1945. BEGIN
  1946. IF Trace THEN TraceEnter("VisitSet") END;
  1947. dest := destination;
  1948. destination := emptyOperand;
  1949. noBits := IntermediateCode.Immediate(setType, 0);
  1950. one := IntermediateCode.Immediate(setType, 1);
  1951. (* start off with the empty set *)
  1952. InitOperand(res, ModeValue);
  1953. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1954. Emit(Mov(position,res.op, noBits));
  1955. FOR i := 0 TO x.elements.Length() - 1 DO
  1956. expression := x.elements.GetExpression(i);
  1957. IF expression IS SyntaxTree.RangeExpression THEN
  1958. (* range of set elements *)
  1959. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1960. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1961. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1962. ReleaseIntermediateOperand(temp)
  1963. ELSE
  1964. (* singelton element *)
  1965. Evaluate(expression, operand);
  1966. Convert(operand.op, setType);
  1967. CheckSetElement(operand.op);
  1968. (* create subset containing single element *)
  1969. Reuse1(temp, operand.op);
  1970. Emit(Shl(position,temp, one, operand.op));
  1971. ReleaseOperand(operand);
  1972. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1973. ReleaseIntermediateOperand(temp);
  1974. END
  1975. END;
  1976. result := res;
  1977. destination := dest;
  1978. IF Trace THEN TraceExit("VisitSet") END;
  1979. END VisitSet;
  1980. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1981. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1982. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1983. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1984. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1985. element: SyntaxTree.IntegerValue;
  1986. BEGIN
  1987. numberElements := x.elements.Length();
  1988. expression := index.parameters.GetExpression(dim);
  1989. element := expression(SyntaxTree.IntegerValue);
  1990. FOR i := 0 TO numberElements-1 DO
  1991. expression := x.elements.GetExpression(i);
  1992. element.SetValue(i);
  1993. IF expression IS SyntaxTree.MathArrayExpression THEN
  1994. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1995. ELSE
  1996. Assign(index,expression);
  1997. END;
  1998. END;
  1999. END RecursiveAssignment;
  2000. BEGIN
  2001. variable := GetTemporaryVariable(x.type, FALSE);
  2002. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2003. designator.SetType(variable.type);
  2004. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2005. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2006. FOR i := 0 TO dim-1 DO
  2007. element := SyntaxTree.NewIntegerValue(x.position,0);
  2008. element.SetType(system.longintType);
  2009. index.parameters.AddExpression(element);
  2010. END;
  2011. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2012. RecursiveAssignment(x,0);
  2013. Expression(designator);
  2014. END VisitMathArrayExpression;
  2015. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2016. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2017. BEGIN
  2018. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2019. dest := destination; destination := emptyOperand;
  2020. IF x.operator = Scanner.Not THEN
  2021. IF conditional THEN
  2022. Condition(x.left,falseLabel,trueLabel)
  2023. ELSE
  2024. Evaluate(x.left,operand);
  2025. InitOperand(result,ModeValue);
  2026. Reuse1a(result.op,operand.op,dest);
  2027. Emit(Xor(position,result.op,operand.op,true));
  2028. ReleaseOperand(operand);
  2029. END;
  2030. ELSIF x.operator = Scanner.Minus THEN
  2031. Evaluate(x.left,operand);
  2032. InitOperand(result,ModeValue);
  2033. Reuse1a(result.op,operand.op,dest);
  2034. type := x.left.type.resolved;
  2035. IF type IS SyntaxTree.SetType THEN
  2036. Emit(Not(position,result.op,operand.op));
  2037. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2038. Reuse1(result.tag,operand.tag);
  2039. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2040. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2041. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2042. Emit(Neg(position,result.op,operand.op));
  2043. ELSE HALT(200)
  2044. END;
  2045. ReleaseOperand(operand);
  2046. ELSIF x.operator = Scanner.Address THEN
  2047. Designate(x.left,operand);
  2048. operand.mode := ModeValue;
  2049. t0 := x.left.type.resolved;
  2050. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2051. ReleaseIntermediateOperand(operand.op);
  2052. operand.op := operand.tag;
  2053. IntermediateCode.InitOperand(operand.tag);
  2054. END;
  2055. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2056. result := operand;
  2057. ELSE HALT(100)
  2058. END;
  2059. destination := dest;
  2060. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2061. END VisitUnaryExpression;
  2062. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2063. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2064. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2065. BEGIN
  2066. type := type.resolved;
  2067. originalType := type;
  2068. IF type IS SyntaxTree.PointerType THEN
  2069. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2070. END;
  2071. IF type IS SyntaxTree.ObjectType THEN
  2072. BrL(trueL);
  2073. ELSE
  2074. ASSERT(type IS SyntaxTree.RecordType);
  2075. (*
  2076. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2077. *)
  2078. ReuseCopy(left,tag);
  2079. right := TypeDescriptorAdr(type);
  2080. IF ~newObjectFile THEN
  2081. IntermediateCode.MakeMemory(right,addressType);
  2082. END;
  2083. IF backend.cooperative THEN
  2084. repeatL := NewLabel();
  2085. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2086. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2087. END;
  2088. SetLabel(repeatL);
  2089. BreqL(trueL,left,right);
  2090. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2091. BrneL(repeatL,left,nil);
  2092. ELSIF meta.simple THEN
  2093. level := type(SyntaxTree.RecordType).Level();
  2094. (* get type desc tag of level relative to base tag *)
  2095. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2096. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2097. IntermediateCode.MakeMemory(left,addressType);
  2098. BreqL(trueL,left,right);
  2099. ELSE
  2100. level := type(SyntaxTree.RecordType).Level();
  2101. (* get type desc tag of level relative to base tag *)
  2102. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2103. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2104. IntermediateCode.MakeMemory(left,addressType);
  2105. BreqL(trueL,left,right);
  2106. END;
  2107. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2108. BrL(falseL);
  2109. END;
  2110. END TypeTest;
  2111. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2112. BEGIN
  2113. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2114. IF dump # NIL THEN
  2115. dump.String(s); dump.Ln;
  2116. END;
  2117. END Error;
  2118. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2119. BEGIN
  2120. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2121. IF dump # NIL THEN
  2122. dump.String(s); dump.Ln; dump.Update;
  2123. END;
  2124. END Warning;
  2125. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2126. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2127. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2128. operand:Operand;
  2129. BEGIN
  2130. previousSection := section;
  2131. Global.GetModuleName(mod,name);
  2132. Strings.Append(name,".@Trace");
  2133. Basic.ToSegmentedName(name, pooledName);
  2134. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2135. IF dump # NIL THEN dump := section.comments END;
  2136. IF backend.hasLinkRegister THEN
  2137. Emit(Push(Basic.invalidPosition, lr));
  2138. END;
  2139. variable := mod.moduleScope.firstVariable;
  2140. WHILE variable # NIL DO
  2141. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2142. Symbol(variable, operand);
  2143. register := operand.op;
  2144. CallTraceMethod(register, variable.type);
  2145. ReleaseIntermediateOperand(register);
  2146. END;
  2147. variable := variable.nextVariable;
  2148. END;
  2149. IF backend.hasLinkRegister THEN
  2150. Emit(Pop(Basic.invalidPosition, lr));
  2151. END;
  2152. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2153. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2154. Emit(Br(position,op));
  2155. INC(statCoopTraceModule, section.pc);
  2156. section := previousSection;
  2157. IF dump # NIL THEN dump := section.comments END;
  2158. END CreateTraceModuleMethod;
  2159. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2160. BEGIN
  2161. Emit (Push(position, dst));
  2162. Emit (Push(position, src));
  2163. CallThis(position,"GarbageCollector","Assign", 2);
  2164. END CallAssignPointer;
  2165. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2166. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2167. BEGIN
  2168. IF SemanticChecker.IsPointerType (type) THEN
  2169. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2170. ELSIF type.IsRecordType() THEN
  2171. Emit (Push(position,dst));
  2172. Emit (Push(position,src));
  2173. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2174. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2175. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2176. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2177. ELSIF IsStaticArray(type) THEN
  2178. size := StaticArrayNumElements(type);
  2179. base := StaticArrayBaseType(type);
  2180. IF base.IsRecordType() THEN
  2181. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2182. Emit (Push(position, dst));
  2183. Emit (Push(position, src));
  2184. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2185. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2187. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2188. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2189. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, dst));
  2192. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2193. Emit (Push(position, src));
  2194. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2195. ELSE
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, dst));
  2198. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2199. Emit (Push(position, src));
  2200. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2201. ASSERT(StaticArrayBaseType(type).IsPointer());
  2202. END;
  2203. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2204. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2205. Emit (Push(position, dst));
  2206. Emit (Push(position, src));
  2207. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2208. ELSE HALT(100); (* missing ? *)
  2209. END;
  2210. END CallAssignMethod;
  2211. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2212. VAR name: Basic.SegmentedName;
  2213. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2214. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2215. context: Context;
  2216. BEGIN
  2217. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2218. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2219. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2220. GetRecordTypeName (recordType,name);
  2221. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2222. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2223. IF dump # NIL THEN dump := section.comments END;
  2224. IF backend.hasLinkRegister THEN
  2225. Emit(Push(Basic.invalidPosition, lr));
  2226. END;
  2227. variable := recordType.recordScope.firstVariable;
  2228. WHILE variable # NIL DO
  2229. IF variable.NeedsTrace() THEN
  2230. dst := NewRegisterOperand (addressType);
  2231. src := NewRegisterOperand (addressType);
  2232. Emit (Mov(position, dst, parameter1));
  2233. Emit (Mov(position, src, parameter2));
  2234. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2235. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2236. CallAssignMethod(dst, src, variable.type);
  2237. ReleaseIntermediateOperand(src);
  2238. ReleaseIntermediateOperand(dst);
  2239. END;
  2240. variable := variable.nextVariable;
  2241. END;
  2242. recordBase := recordType.GetBaseRecord();
  2243. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2244. IF backend.hasLinkRegister THEN
  2245. Emit(Pop(Basic.invalidPosition, lr));
  2246. END;
  2247. GetRecordTypeName (recordBase,name);
  2248. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2249. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2250. Emit(Br(position,op));
  2251. ELSE
  2252. Emit(Exit(position,0,0, 0));
  2253. END;
  2254. IF ~recordType.isObject THEN
  2255. GetRecordTypeName (recordType,name);
  2256. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2257. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2258. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2259. NEW(registerUsageCount);
  2260. usedRegisters := NIL;
  2261. dst := NewRegisterOperand (addressType);
  2262. src := NewRegisterOperand (addressType);
  2263. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2264. Emit(Mov(position, dst, parameter1));
  2265. Emit(Mov(position, src, parameter2));
  2266. label := NewLabel();
  2267. SetLabel(label);
  2268. Emit(Push(position, dst));
  2269. Emit(Push(position, src));
  2270. GetRecordTypeName (recordType,name);
  2271. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2272. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2273. Emit(Call(position, op, 0));
  2274. Emit(Pop(position, src));
  2275. Emit(Pop(position, dst));
  2276. Emit(Add(position, dst, dst, ofs));
  2277. Emit(Add(position, src, src, ofs));
  2278. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2279. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2280. Emit(Exit(position,0,0, 0));
  2281. END;
  2282. INC(statCoopAssignProcedure, section.pc);
  2283. ReturnToContext(context);
  2284. IF dump # NIL THEN dump := section.comments END;
  2285. END CreateAssignProcedure;
  2286. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2287. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2288. BEGIN
  2289. IF IsUnsafePointer (type) THEN
  2290. skip := NewLabel();
  2291. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2292. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2293. BreqL (skip, op, nil);
  2294. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2295. SetLabel (skip);
  2296. ELSIF SemanticChecker.IsPointerType (type) THEN
  2297. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2298. CallThis(position,"GarbageCollector","Mark", 1);
  2299. ELSIF type.IsRecordType() THEN
  2300. Emit (Push(position,register));
  2301. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2302. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2303. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2304. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2305. ELSIF IsStaticArray(type) THEN
  2306. size := StaticArrayNumElements(type);
  2307. base := StaticArrayBaseType(type);
  2308. IF base.IsRecordType() THEN
  2309. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2310. Emit (Push(position, register));
  2311. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2312. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2314. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2315. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2316. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2317. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2318. Emit (Push(position, register));
  2319. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2320. ELSE
  2321. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2322. Emit (Push(position, register));
  2323. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2324. ASSERT(base.IsPointer());
  2325. END;
  2326. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2327. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2328. CallThis(position,"GarbageCollector","Mark", 1);
  2329. ELSE HALT(100); (* missing ? *)
  2330. END;
  2331. END CallTraceMethod;
  2332. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2333. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2334. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2335. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2336. BEGIN
  2337. previousSection := section;
  2338. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2339. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2340. GetRecordTypeName (recordType,name);
  2341. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2342. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2343. IF dump # NIL THEN dump := section.comments END;
  2344. IF backend.hasLinkRegister THEN
  2345. Emit(Push(Basic.invalidPosition, lr));
  2346. END;
  2347. variable := recordType.recordScope.firstVariable;
  2348. WHILE variable # NIL DO
  2349. IF variable.NeedsTrace() THEN
  2350. register := NewRegisterOperand (addressType);
  2351. Emit (Mov(position,register,parameter1));
  2352. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2353. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2354. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2355. END;
  2356. CallTraceMethod(register, variable.type);
  2357. ReleaseIntermediateOperand(register);
  2358. END;
  2359. variable := variable.nextVariable;
  2360. END;
  2361. recordBase := recordType.GetBaseRecord();
  2362. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2363. recordBase := recordBase.GetBaseRecord();
  2364. END;
  2365. IF recordBase # NIL THEN
  2366. IF backend.hasLinkRegister THEN
  2367. Emit(Pop(Basic.invalidPosition, lr));
  2368. END;
  2369. GetRecordTypeName (recordBase,name);
  2370. IF HasExplicitTraceMethod (recordBase) THEN
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2372. ELSE
  2373. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2374. END;
  2375. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2376. Emit(Br(position,op));
  2377. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2378. Emit(Exit(position,0,0,0));
  2379. ELSE
  2380. IF backend.hasLinkRegister THEN
  2381. Emit(Pop(Basic.invalidPosition, lr));
  2382. END;
  2383. IF recordType.isObject THEN
  2384. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2385. ELSE
  2386. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2387. END;
  2388. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2389. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2390. Emit(Br(position,op));
  2391. END;
  2392. IF ~recordType.isObject THEN
  2393. GetRecordTypeName (recordType,name);
  2394. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2395. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2396. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2397. NEW(registerUsageCount);
  2398. usedRegisters := NIL;
  2399. IF dump # NIL THEN dump := section.comments END;
  2400. register := NewRegisterOperand (addressType);
  2401. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2402. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2403. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2404. END;
  2405. Emit (Push(position,register));
  2406. GetRecordTypeName (recordType,name);
  2407. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2408. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2409. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2410. ReleaseIntermediateOperand(register);
  2411. Emit(Exit(position,0,0,0));
  2412. GetRecordTypeName (recordType,name);
  2413. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2414. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2415. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2416. NEW(registerUsageCount);
  2417. usedRegisters := NIL;
  2418. register := NewRegisterOperand (addressType);
  2419. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2420. Emit(Mov(position, register, parameter1));
  2421. label := NewLabel();
  2422. SetLabel(label);
  2423. Emit(Push(position, register));
  2424. GetRecordTypeName (recordType,name);
  2425. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2426. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2427. Emit(Call(position, op, 0));
  2428. Emit(Pop(position, register));
  2429. Emit(Add(position, register, register, ofs));
  2430. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2431. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2432. Emit(Exit(position,0,0,0));
  2433. END;
  2434. INC(statCoopTraceMethod, section.pc);
  2435. ReturnToContext(context);
  2436. IF dump # NIL THEN dump := section.comments END;
  2437. END CreateTraceMethod;
  2438. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2439. VAR name: Basic.SegmentedName;
  2440. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2441. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2442. context: Context;
  2443. BEGIN
  2444. IF recordType.isObject THEN RETURN END;
  2445. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2446. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2447. GetRecordTypeName (recordType,name);
  2448. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2449. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2450. IF dump # NIL THEN dump := section.comments END;
  2451. IF backend.hasLinkRegister THEN
  2452. Emit(Push(Basic.invalidPosition, lr));
  2453. END;
  2454. variable := recordType.recordScope.firstVariable;
  2455. WHILE variable # NIL DO
  2456. IF variable.NeedsTrace() THEN
  2457. dst := NewRegisterOperand (addressType);
  2458. Emit (Mov(position, dst, parameter1));
  2459. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2460. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2461. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2462. END;
  2463. CallResetProcedure(dst, nil, variable.type);
  2464. ReleaseIntermediateOperand(dst);
  2465. END;
  2466. variable := variable.nextVariable;
  2467. END;
  2468. recordBase := recordType.GetBaseRecord();
  2469. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2470. IF backend.hasLinkRegister THEN
  2471. Emit(Pop(Basic.invalidPosition, lr));
  2472. END;
  2473. GetRecordTypeName (recordBase,name);
  2474. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2475. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2476. Emit(Br(position,op));
  2477. ELSE
  2478. Emit(Exit(position,0,0, 0));
  2479. END;
  2480. GetRecordTypeName (recordType,name);
  2481. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2482. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2483. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2484. NEW(registerUsageCount);
  2485. usedRegisters := NIL;
  2486. dst := NewRegisterOperand (addressType);
  2487. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2488. Emit(Mov(position, dst, parameter1));
  2489. label := NewLabel();
  2490. SetLabel(label);
  2491. Emit(Push(position, dst));
  2492. GetRecordTypeName (recordType,name);
  2493. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2494. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2495. Emit(Call(position, op, 0));
  2496. Emit(Pop(position, dst));
  2497. Emit(Add(position, dst, dst, ofs));
  2498. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2499. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2500. Emit(Exit(position,0,0, 0));
  2501. INC(statCoopResetProcedure, section.pc);
  2502. ReturnToContext(context);
  2503. IF dump # NIL THEN dump := section.comments END;
  2504. END CreateResetProcedure;
  2505. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2506. VAR name: Basic.SegmentedName; context: Context;
  2507. BEGIN
  2508. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2509. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2510. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2511. IF dump # NIL THEN dump := section.comments END;
  2512. IF backend.hasLinkRegister THEN
  2513. Emit(Push(Basic.invalidPosition, lr));
  2514. END;
  2515. Emit(Push(position,fp));
  2516. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2517. ResetVariables(scope);
  2518. Emit(Pop(position,fp));
  2519. Emit(Exit(position,0,0, 0));
  2520. ReturnToContext(context);
  2521. IF dump # NIL THEN dump := section.comments END;
  2522. END CreateResetMethod;
  2523. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2524. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2525. BEGIN
  2526. IF SemanticChecker.IsPointerType (type) THEN
  2527. Emit (Push(position, dest));
  2528. CallThis(position,"GarbageCollector","Reset", 1);
  2529. ELSIF type.IsRecordType() THEN
  2530. Emit (Push(position, dest));
  2531. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2532. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2533. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2534. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2535. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2536. size := GetArrayLength(type, tag);
  2537. base := ArrayBaseType(type);
  2538. IF base.IsRecordType() THEN
  2539. Emit (Push(position, size));
  2540. Emit (Push(position, dest));
  2541. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2542. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2543. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2544. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2545. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2546. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2547. Emit (Push(position, size));
  2548. Emit (Push(position, dest));
  2549. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2550. ELSE
  2551. Emit (Push(position, size));
  2552. Emit (Push(position, dest));
  2553. CallThis(position,"GarbageCollector","ResetArray", 2);
  2554. ASSERT(ArrayBaseType(type).IsPointer());
  2555. END;
  2556. ReleaseIntermediateOperand(size);
  2557. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2558. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2559. Emit (Push(position, dest));
  2560. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2561. ELSE HALT(100); (* missing ? *)
  2562. END;
  2563. END CallResetProcedure;
  2564. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2565. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2566. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2567. VAR operand: Operand;
  2568. BEGIN
  2569. Symbol (symbol, operand);
  2570. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2571. ReleaseOperand(operand);
  2572. END Reset;
  2573. BEGIN
  2574. previousScope := currentScope;
  2575. currentScope := scope;
  2576. pc := section.pc;
  2577. variable := scope.firstVariable;
  2578. WHILE variable # NIL DO
  2579. IF variable.NeedsTrace() THEN
  2580. Reset (variable);
  2581. END;
  2582. variable := variable.nextVariable;
  2583. END;
  2584. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2585. WHILE parameter # NIL DO
  2586. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2587. Reset (parameter);
  2588. END;
  2589. parameter := parameter.nextParameter;
  2590. END;
  2591. INC(statCoopResetVariables, section.pc - pc);
  2592. currentScope := previousScope;
  2593. END ResetVariables;
  2594. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2595. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2596. VAR op: IntermediateCode.Operand; context: Context;
  2597. BEGIN
  2598. previousSection := section;
  2599. GetCodeSectionNameForSymbol(procedure, name);
  2600. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2601. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2602. IF dump # NIL THEN dump := section.comments END;
  2603. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2604. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2605. Emit(Data(position,op));
  2606. Emit(Data(position,nil));
  2607. IF HasPointers (procedure.procedureScope) THEN
  2608. GetCodeSectionNameForSymbol(procedure, name);
  2609. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2610. ELSE
  2611. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2612. END;
  2613. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2614. Emit(Data(position,op));
  2615. ReturnToContext(context);
  2616. IF dump # NIL THEN dump := section.comments END;
  2617. END CreateProcedureDescriptor;
  2618. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2619. VAR import: SyntaxTree.Import;
  2620. s: Basic.MessageString;
  2621. selfName: SyntaxTree.IdentifierString;
  2622. BEGIN
  2623. moduleScope.ownerModule.GetName(selfName);
  2624. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2625. module := moduleScope.ownerModule
  2626. ELSE
  2627. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2628. IF import = NIL THEN
  2629. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2630. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2631. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2632. s := "Module ";
  2633. Strings.Append(s,moduleName);
  2634. Strings.Append(s," cannot be imported.");
  2635. IF force THEN
  2636. Error(position,s);
  2637. ELSIF canBeLoaded THEN
  2638. Strings.Append(s, "=> no dynamic linking.");
  2639. Warning(position, s);
  2640. canBeLoaded := FALSE;
  2641. END;
  2642. RETURN FALSE
  2643. ELSE
  2644. SELF.module.imports.AddName(moduleName)
  2645. END;
  2646. ELSIF import.module = NIL THEN (* already tried *)
  2647. RETURN FALSE
  2648. END;
  2649. module := import.module;
  2650. END;
  2651. RETURN TRUE
  2652. END AddImport;
  2653. (* needed for old binary object file format*)
  2654. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2655. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2656. BEGIN
  2657. IF AddImport(moduleName,module,TRUE) THEN
  2658. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2659. IF procedure = NIL THEN
  2660. s := "Instruction not supported on target, emulation procedure ";
  2661. Strings.Append(s,moduleName);
  2662. Strings.Append(s,".");
  2663. Strings.Append(s,procedureName);
  2664. Strings.Append(s," not present");
  2665. Error(position,s);
  2666. ELSE
  2667. StaticCallOperand(r,procedure);
  2668. ReleaseOperand(r);
  2669. fp := GetFingerprint(procedure);
  2670. END;
  2671. END;
  2672. END EnsureSymbol;
  2673. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2674. VAR exit: Label; trueL,falseL: Label;
  2675. BEGIN
  2676. trueL := NewLabel();
  2677. falseL := NewLabel();
  2678. exit := NewLabel();
  2679. Condition(x,trueL,falseL);
  2680. InitOperand(result,ModeValue);
  2681. SetLabel(trueL);
  2682. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2683. Emit(Mov(position,result.op,true));
  2684. BrL(exit);
  2685. SetLabel(falseL);
  2686. Emit(MovReplace(position,result.op,false));
  2687. SetLabel(exit);
  2688. END ConditionToValue;
  2689. PROCEDURE ValueToCondition(VAR op: Operand);
  2690. BEGIN
  2691. LoadValue(op,system.booleanType);
  2692. BrneL(trueLabel,op.op, false);
  2693. ReleaseOperand(op);
  2694. BrL(falseLabel);
  2695. END ValueToCondition;
  2696. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2697. VAR size: LONGINT;
  2698. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2699. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2700. BEGIN
  2701. ASSERT(type.form = SyntaxTree.Open);
  2702. baseType := type.arrayBase.resolved;
  2703. IF IsOpenArray(baseType) THEN
  2704. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2705. ELSE
  2706. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2707. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2708. END;
  2709. len := tag;
  2710. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2711. IntermediateCode.MakeMemory(len,addressType);
  2712. UseIntermediateOperand(len);
  2713. Reuse2(res,sizeOperand,len);
  2714. Emit(Mul(position,res,sizeOperand,len));
  2715. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2716. RETURN res
  2717. END GetArraySize;
  2718. BEGIN
  2719. type := type.resolved;
  2720. IF IsOpenArray(type) THEN
  2721. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2722. RETURN tag
  2723. ELSE
  2724. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2725. END;
  2726. ELSE
  2727. size := ToMemoryUnits(system,system.SizeOf(type));
  2728. RETURN IntermediateCode.Immediate(addressType,size)
  2729. END;
  2730. END GetDynamicSize;
  2731. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2732. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2733. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2734. BEGIN
  2735. ASSERT(type.form = SyntaxTree.Open);
  2736. baseType := type.arrayBase.resolved;
  2737. IF IsOpenArray(baseType) THEN
  2738. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2739. ELSE
  2740. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2741. END;
  2742. len := tag;
  2743. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2744. IntermediateCode.MakeMemory(len,addressType);
  2745. UseIntermediateOperand(len);
  2746. Reuse2(res,sizeOperand,len);
  2747. Emit(Mul(position,res,sizeOperand,len));
  2748. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2749. RETURN res
  2750. END GetLength;
  2751. BEGIN
  2752. type := type.resolved;
  2753. IF IsOpenArray(type) THEN
  2754. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2755. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2756. ELSIF type IS SyntaxTree.StringType THEN
  2757. RETURN tag;
  2758. ELSE
  2759. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2760. END;
  2761. END GetArrayLength;
  2762. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2763. VAR result: IntermediateCode.Operand;
  2764. BEGIN
  2765. IF backend.cooperative THEN
  2766. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2767. ELSE
  2768. MakeMemory(result, tag, addressType, 0);
  2769. END;
  2770. RETURN result
  2771. END GetSizeFromTag;
  2772. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2773. VAR parameter: SyntaxTree.Parameter;
  2774. BEGIN
  2775. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2776. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2777. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2779. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2780. END;
  2781. RETURN GetDynamicSize(e.type, tag);
  2782. END GetArrayOfBytesSize;
  2783. (*
  2784. to find imported symbol. not needed ?
  2785. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2786. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2787. BEGIN
  2788. IF AddImport(moduleName,importedModule,FALSE) THEN
  2789. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2790. RETURN symbol
  2791. ELSE
  2792. RETURN NIL
  2793. END
  2794. END SymbolByName;
  2795. *)
  2796. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2797. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2798. BEGIN
  2799. IF AddImport(moduleName,runtimeModule,force) THEN
  2800. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2801. IF procedure = NIL THEN
  2802. s := "Procedure ";
  2803. Strings.Append(s,moduleName);
  2804. Strings.Append(s,".");
  2805. Strings.Append(s,procedureName);
  2806. Strings.Append(s," not present");
  2807. Error(position,s);
  2808. RETURN FALSE
  2809. ELSE
  2810. RETURN TRUE
  2811. END;
  2812. ELSE RETURN FALSE
  2813. END;
  2814. END GetRuntimeProcedure;
  2815. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2816. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2817. s: Basic.MessageString;
  2818. BEGIN
  2819. Basic.InitSegmentedName(name);
  2820. name[0] := Basic.MakeString(moduleName);
  2821. name[1] := Basic.MakeString(typeName);
  2822. name[2] := -1;
  2823. IF AddImport(moduleName,importedModule, FALSE) THEN
  2824. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2825. IF symbol = NIL THEN
  2826. s := "type ";
  2827. Strings.Append(s,moduleName);
  2828. Strings.Append(s,".");
  2829. Strings.Append(s,typeName);
  2830. Strings.Append(s," not present");
  2831. Error(position,s);
  2832. END;
  2833. ELSE symbol := NIL;
  2834. END;
  2835. IF importedModule = moduleScope.ownerModule THEN
  2836. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. ELSE
  2838. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2839. END;
  2840. RETURN symbol
  2841. END GetTypeDescriptor;
  2842. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2843. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2844. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2845. pooledName: Basic.SegmentedName; size: LONGINT;
  2846. BEGIN
  2847. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2848. StaticCallOperand(result,procedure);
  2849. IF numberParameters < 0 THEN
  2850. size := ProcedureParametersSize(system,procedure);
  2851. ELSE
  2852. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2853. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2854. Error(position,"runtime call parameter count mismatch");
  2855. END;
  2856. END;
  2857. Emit(Call(position, result.op, size));
  2858. ReleaseOperand(result);
  2859. ELSE (* only static linking possible *)
  2860. ASSERT(numberParameters >= 0);
  2861. Basic.InitSegmentedName(pooledName);
  2862. pooledName[0] := Basic.MakeString(moduleName);
  2863. pooledName[1] := Basic.MakeString(procedureName);
  2864. pooledName[2] := -1;
  2865. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2866. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2867. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2868. END;
  2869. END CallThisChecked;
  2870. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2871. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2872. BEGIN
  2873. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2874. END CallThis;
  2875. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2876. VAR
  2877. left,right: Operand;
  2878. leftSize, rightSize: IntermediateCode.Operand;
  2879. saved: RegisterEntry;
  2880. reg: IntermediateCode.Operand;
  2881. procedureName: SyntaxTree.IdentifierString;
  2882. BEGIN
  2883. procedureName := "CompareString";
  2884. SaveRegisters();ReleaseUsedRegisters(saved);
  2885. Designate(leftExpression,left);
  2886. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2887. Emit(Push(position,leftSize));
  2888. ReleaseIntermediateOperand(leftSize);
  2889. Emit(Push(position,left.op));
  2890. ReleaseOperand(left);
  2891. Designate(rightExpression,right);
  2892. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2893. Emit(Push(position,rightSize));
  2894. ReleaseIntermediateOperand(rightSize);
  2895. Emit(Push(position,right.op));
  2896. ReleaseOperand(right);
  2897. IF backend.cooperative THEN
  2898. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2899. ELSE
  2900. CallThis(position,runtimeModuleName,procedureName, 4);
  2901. END;
  2902. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2903. Emit(Result(position,reg));
  2904. (*
  2905. AcquireThisRegister(int8,IntermediateCode.Result);
  2906. *)
  2907. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2908. (*
  2909. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2910. *)
  2911. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2912. ReleaseIntermediateOperand(reg);
  2913. BrL(falseLabel);
  2914. END CompareString;
  2915. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2916. VAR
  2917. left,right: Operand;
  2918. leftSize, rightSize: IntermediateCode.Operand;
  2919. saved: RegisterEntry;
  2920. procedureName: SyntaxTree.IdentifierString;
  2921. BEGIN
  2922. procedureName := "CopyString";
  2923. SaveRegisters();ReleaseUsedRegisters(saved);
  2924. Designate(leftExpression,left);
  2925. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2926. Emit(Push(position,leftSize));
  2927. ReleaseIntermediateOperand(leftSize);
  2928. Emit(Push(position,left.op));
  2929. ReleaseOperand(left);
  2930. Designate(rightExpression,right);
  2931. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2932. Emit(Push(position,rightSize));
  2933. ReleaseIntermediateOperand(rightSize);
  2934. Emit(Push(position,right.op));
  2935. ReleaseOperand(right);
  2936. IF backend.cooperative THEN
  2937. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2938. ELSE
  2939. CallThis(position,runtimeModuleName,procedureName,4);
  2940. END;
  2941. RestoreRegisters(saved);
  2942. END CopyString;
  2943. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2944. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2945. leftType,rightType: SyntaxTree.Type;
  2946. leftExpression,rightExpression : SyntaxTree.Expression;
  2947. componentType: IntermediateCode.Type;
  2948. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2949. size: LONGINT;
  2950. BEGIN
  2951. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2952. dest := destination; destination := emptyOperand;
  2953. leftType := x.left.type.resolved;
  2954. rightType := x.right.type.resolved;
  2955. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2956. CASE x.operator OF
  2957. Scanner.Or:
  2958. (* shortcut evaluation of left OR right *)
  2959. IF ~conditional THEN ConditionToValue(x);
  2960. ELSE
  2961. next := NewLabel();
  2962. Condition(x.left,trueLabel,next);
  2963. SetLabel(next);
  2964. Condition(x.right,trueLabel,falseLabel);
  2965. END;
  2966. |Scanner.And:
  2967. (* shortcut evaluation of left & right *)
  2968. IF ~conditional THEN ConditionToValue(x);
  2969. ELSE
  2970. next := NewLabel();
  2971. Condition(x.left,next,falseLabel);
  2972. SetLabel(next);
  2973. Condition(x.right,trueLabel,falseLabel);
  2974. END;
  2975. |Scanner.Is:
  2976. IF ~conditional THEN ConditionToValue(x);
  2977. ELSE
  2978. (* get type desc tag *)
  2979. IF IsPointerToRecord(leftType,recordType) THEN
  2980. Evaluate(x.left,left);
  2981. Dereference(left,recordType,IsUnsafePointer(leftType))
  2982. ELSE
  2983. Designate(x.left,left);
  2984. END;
  2985. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2986. ReleaseOperand(left);
  2987. END;
  2988. |Scanner.Plus:
  2989. Evaluate(x.left,left);
  2990. Evaluate(x.right,right);
  2991. IF leftType IS SyntaxTree.SetType THEN
  2992. InitOperand(result,ModeValue);
  2993. Reuse2a(result.op,left.op,right.op,dest);
  2994. Emit(Or(position,result.op,left.op,right.op));
  2995. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2996. InitOperand(result,ModeValue);
  2997. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2998. Reuse2(result.tag,left.tag,right.tag);
  2999. Emit(Add(position,result.op,left.op,right.op));
  3000. Emit(Add(position,result.tag,left.tag,right.tag))
  3001. ELSE
  3002. InitOperand(result,ModeValue);
  3003. Reuse2a(result.op,left.op,right.op,dest);
  3004. Emit(Add(position,result.op,left.op,right.op));
  3005. END;
  3006. ReleaseOperand(left); ReleaseOperand(right);
  3007. |Scanner.Minus:
  3008. Evaluate(x.left,left);
  3009. Evaluate(x.right,right);
  3010. IF leftType IS SyntaxTree.SetType THEN
  3011. InitOperand(result,ModeValue);
  3012. Reuse1(result.op,right.op);
  3013. Emit(Not(position,result.op,right.op));
  3014. ReleaseOperand(right);
  3015. Emit(And(position,result.op,result.op,left.op));
  3016. ReleaseOperand(left);
  3017. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3018. InitOperand(result,ModeValue);
  3019. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3020. Reuse2(result.tag,left.tag,right.tag);
  3021. Emit(Sub(position,result.op,left.op,right.op));
  3022. Emit(Sub(position,result.tag,left.tag,right.tag));
  3023. ReleaseOperand(left); ReleaseOperand(right)
  3024. ELSE
  3025. InitOperand(result,ModeValue);
  3026. Reuse2a(result.op,left.op,right.op,dest);
  3027. Emit(Sub(position,result.op,left.op,right.op));
  3028. ReleaseOperand(left); ReleaseOperand(right);
  3029. END;
  3030. |Scanner.Times:
  3031. Evaluate(x.left,left);
  3032. Evaluate(x.right,right);
  3033. IF leftType IS SyntaxTree.SetType THEN
  3034. InitOperand(result,ModeValue);
  3035. Reuse2a(result.op,left.op,right.op,dest);
  3036. Emit(And(position,result.op,left.op,right.op));
  3037. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3038. InitOperand(result, ModeValue);
  3039. componentType := left.op.type;
  3040. (* TODO: review this *)
  3041. (*
  3042. result.op = left.op * right.op - left.tag * right.tag
  3043. result.tag = left.tag * right.op + left.op * right.tag
  3044. *)
  3045. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3046. Emit(Mul(position,result.op, left.op, right.op));
  3047. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3048. Emit(Mul(position,tempReg, left.tag, right.tag));
  3049. Emit(Sub(position,result.op, result.op, tempReg));
  3050. Reuse2(result.tag, left.tag, right.op);
  3051. Emit(Mul(position,result.tag, left.tag, right.op));
  3052. Emit(Mul(position,tempReg, left.op, right.tag));
  3053. Emit(Add(position,result.tag, result.tag, tempReg));
  3054. ReleaseIntermediateOperand(tempReg)
  3055. ELSE
  3056. InitOperand(result,ModeValue);
  3057. Reuse2a(result.op,left.op,right.op,dest);
  3058. Emit(Mul(position,result.op,left.op,right.op));
  3059. END;
  3060. ReleaseOperand(left); ReleaseOperand(right);
  3061. |Scanner.Div:
  3062. Evaluate(x.left,left);
  3063. Evaluate(x.right,right);
  3064. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3065. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3066. IF ~isUnchecked THEN
  3067. exit := NewLabel();
  3068. BrltL(exit,zero,right.op);
  3069. EmitTrap(position,NegativeDivisorTrap);
  3070. SetLabel(exit);
  3071. END;
  3072. END;
  3073. InitOperand(result,ModeValue);
  3074. Reuse2a(result.op,left.op,right.op,dest);
  3075. Emit(Div(position,result.op,left.op,right.op));
  3076. ReleaseOperand(left); ReleaseOperand(right);
  3077. |Scanner.Mod:
  3078. Evaluate(x.left,left);
  3079. Evaluate(x.right,right);
  3080. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3081. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3082. IF ~isUnchecked THEN
  3083. exit := NewLabel();
  3084. BrltL(exit,zero,right.op);
  3085. EmitTrap(position,NegativeDivisorTrap);
  3086. SetLabel(exit);
  3087. END;
  3088. END;
  3089. InitOperand(result,ModeValue);
  3090. Reuse2a(result.op,left.op,right.op,dest);
  3091. Emit(Mod(position,result.op,left.op,right.op));
  3092. ReleaseOperand(left); ReleaseOperand(right);
  3093. |Scanner.Slash:
  3094. Evaluate(x.left,left);
  3095. Evaluate(x.right,right);
  3096. IF leftType IS SyntaxTree.SetType THEN
  3097. InitOperand(result,ModeValue);
  3098. Reuse2a(result.op,left.op,right.op,dest);
  3099. Emit(Xor(position,result.op,left.op,right.op));
  3100. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3101. InitOperand(result,ModeValue);
  3102. componentType := left.op.type;
  3103. (* review this *)
  3104. (*
  3105. divisor = right.op * right.op + right.tag * right.tag
  3106. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3107. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3108. *)
  3109. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3110. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3111. Emit(Mul(position,tempReg, right.op, right.op));
  3112. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3113. Emit(Add(position,tempReg, tempReg, tempReg2));
  3114. result.op := tempReg2;
  3115. Emit(Mul(position,result.op, left.op, right.op));
  3116. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3117. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3118. Emit(Add(position,result.op, result.op, tempReg2));
  3119. Emit(Div(position,result.op, result.op, tempReg));
  3120. Reuse2(result.tag, left.tag, right.op);
  3121. Emit(Mul(position,result.tag, left.tag, right.op));
  3122. Emit(Mul(position,tempReg2, left.op, right.tag));
  3123. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3124. Emit(Div(position,result.tag, result.tag, tempReg));
  3125. ReleaseIntermediateOperand(tempReg);
  3126. ReleaseIntermediateOperand(tempReg2)
  3127. ELSE
  3128. InitOperand(result,ModeValue);
  3129. Reuse2a(result.op,left.op,right.op,dest);
  3130. Emit(Div(position,result.op,left.op,right.op));
  3131. END;
  3132. ReleaseOperand(left); ReleaseOperand(right);
  3133. |Scanner.Equal:
  3134. IF ~conditional THEN ConditionToValue(x);
  3135. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3136. CompareString(BreqL,x.left,x.right);
  3137. ELSE
  3138. Evaluate(x.left,left);
  3139. Evaluate(x.right,right);
  3140. IF leftType IS SyntaxTree.RangeType THEN
  3141. ASSERT(rightType IS SyntaxTree.RangeType);
  3142. BrneL(falseLabel, left.op, right.op); (* first *)
  3143. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3144. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3145. ReleaseOperand(left); ReleaseOperand(right);
  3146. BrL(trueLabel)
  3147. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3148. BrneL(falseLabel, left.op, right.op); (* first *)
  3149. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3150. ReleaseOperand(left); ReleaseOperand(right);
  3151. BrL(trueLabel)
  3152. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3153. (* TODO: review this *)
  3154. BrneL(falseLabel, left.op, right.op); (* real part *)
  3155. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3156. ReleaseOperand(left); ReleaseOperand(right);
  3157. BrL(trueLabel)
  3158. ELSE
  3159. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3160. ReleaseOperand(left); ReleaseOperand(right);
  3161. BrL(trueLabel);
  3162. END;
  3163. END;
  3164. |Scanner.LessEqual:
  3165. IF ~conditional THEN ConditionToValue(x);
  3166. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3167. CompareString(BrgeL,x.right,x.left);
  3168. ELSE
  3169. Evaluate(x.left,left);
  3170. Evaluate(x.right,right);
  3171. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3172. Reuse1(temp.op,right.op);
  3173. Emit(And(position,temp.op,left.op,right.op));
  3174. ReleaseOperand(right);
  3175. BreqL(trueLabel,temp.op,left.op);
  3176. BrL(falseLabel);
  3177. ReleaseOperand(temp);ReleaseOperand(left);
  3178. ELSE
  3179. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3180. ReleaseOperand(left); ReleaseOperand(right);
  3181. BrL(trueLabel);
  3182. END;
  3183. END;
  3184. |Scanner.Less:
  3185. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3186. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3187. leftExpression.SetType(system.booleanType);
  3188. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3189. rightExpression.SetType(system.booleanType);
  3190. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3191. leftExpression.SetType(system.booleanType);
  3192. Expression(leftExpression);
  3193. ELSIF ~conditional THEN ConditionToValue(x);
  3194. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3195. CompareString(BrltL,x.left,x.right);
  3196. ELSE
  3197. Evaluate(x.left,left);
  3198. Evaluate(x.right,right);
  3199. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3200. ReleaseOperand(left); ReleaseOperand(right);
  3201. BrL(trueLabel);
  3202. END;
  3203. |Scanner.Greater:
  3204. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3205. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3206. leftExpression.SetType(system.booleanType);
  3207. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3208. rightExpression.SetType(system.booleanType);
  3209. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3210. leftExpression.SetType(system.booleanType);
  3211. Expression(leftExpression);
  3212. ELSIF ~conditional THEN ConditionToValue(x);
  3213. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3214. CompareString(BrltL,x.right,x.left);
  3215. ELSE
  3216. Evaluate(x.left,left);
  3217. Evaluate(x.right,right);
  3218. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3219. ReleaseOperand(left); ReleaseOperand(right);
  3220. BrL(trueLabel);
  3221. END;
  3222. |Scanner.GreaterEqual:
  3223. IF ~conditional THEN ConditionToValue(x);
  3224. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3225. CompareString(BrgeL,x.left,x.right);
  3226. ELSE
  3227. Evaluate(x.left,left);
  3228. Evaluate(x.right,right);
  3229. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3230. Reuse1(temp.op,left.op);
  3231. Emit(And(position,temp.op,left.op,right.op));
  3232. ReleaseOperand(left);
  3233. BreqL(trueLabel, temp.op,right.op);
  3234. ReleaseOperand(temp); ReleaseOperand(right);
  3235. BrL(falseLabel);
  3236. ELSE
  3237. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3238. ReleaseOperand(left); ReleaseOperand(right);
  3239. BrL(trueLabel);
  3240. END;
  3241. END;
  3242. |Scanner.Unequal:
  3243. IF ~conditional THEN ConditionToValue(x);
  3244. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3245. CompareString(BrneL,x.left,x.right);
  3246. ELSE
  3247. Evaluate(x.left,left);
  3248. Evaluate(x.right,right);
  3249. IF leftType IS SyntaxTree.RangeType THEN
  3250. ASSERT(rightType IS SyntaxTree.RangeType);
  3251. BrneL(trueLabel, left.op, right.op); (* first *)
  3252. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3253. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3254. ReleaseOperand(left); ReleaseOperand(right);
  3255. BrL(falseLabel)
  3256. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3257. BrneL(trueLabel, left.op, right.op); (* first *)
  3258. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3259. ReleaseOperand(left); ReleaseOperand(right);
  3260. BrL(falseLabel)
  3261. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3262. (* TODO: review this *)
  3263. BrneL(trueLabel, left.op, right.op); (* real part *)
  3264. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3265. ReleaseOperand(left); ReleaseOperand(right);
  3266. BrL(falseLabel)
  3267. ELSE
  3268. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3269. ReleaseOperand(left); ReleaseOperand(right);
  3270. BrL(trueLabel);
  3271. END;
  3272. END;
  3273. |Scanner.In:
  3274. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3275. Evaluate(x.left,left);
  3276. Evaluate(x.right,right);
  3277. Convert(left.op,setType);
  3278. ReuseCopy(temp.op,right.op);
  3279. Emit(Shr(position,temp.op,temp.op,left.op));
  3280. ReleaseOperand(right); ReleaseOperand(left);
  3281. IntermediateCode.InitImmediate(one,setType,1);
  3282. Emit(And(position,temp.op,temp.op,one));
  3283. IF conditional THEN
  3284. IntermediateCode.InitImmediate(zero,setType,0);
  3285. BrneL(trueLabel,temp.op,zero);
  3286. ReleaseOperand(temp);
  3287. BrL(falseLabel);
  3288. ELSE
  3289. Convert(temp.op,bool);
  3290. result.mode := ModeValue;
  3291. result.op := temp.op;
  3292. result.tag := nil; (* may be left over from calls to evaluate *)
  3293. END;
  3294. ELSE
  3295. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3296. IF x.operator = Scanner.Questionmarks THEN
  3297. leftExpression := x.left;
  3298. rightExpression := x.right;
  3299. ELSE
  3300. leftExpression := x.right;
  3301. rightExpression := x.left;
  3302. END;
  3303. Evaluate(leftExpression, left);
  3304. Emit(Push(position,left.op));
  3305. ReleaseOperand(left);
  3306. Designate(rightExpression, right);
  3307. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3308. IF ~backend.cellsAreObjects THEN
  3309. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3310. END;
  3311. Emit(Push(position,right.op));
  3312. ReleaseOperand(right);
  3313. IF backend.cellsAreObjects THEN
  3314. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3315. ELSE
  3316. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3317. END;
  3318. InitOperand(result, ModeValue);
  3319. result.op := NewRegisterOperand(bool);
  3320. Emit(Result(position,result.op));
  3321. IF conditional THEN
  3322. IntermediateCode.InitImmediate(zero,setType,0);
  3323. BrneL(trueLabel,result.op,zero);
  3324. ReleaseOperand(result);
  3325. BrL(falseLabel);
  3326. END;
  3327. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3328. leftExpression := x.left;
  3329. rightExpression := x.right;
  3330. Evaluate(leftExpression, left);
  3331. Emit(Push(position,left.op));
  3332. ReleaseOperand(left);
  3333. Evaluate(rightExpression, right);
  3334. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3335. IF ~backend.cellsAreObjects THEN
  3336. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3337. END;
  3338. Emit(Push(position,right.op));
  3339. ReleaseOperand(right);
  3340. IF backend.cellsAreObjects THEN
  3341. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3342. ELSE
  3343. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3344. END;
  3345. InitOperand(result, ModeValue);
  3346. result.op := NewRegisterOperand(bool);
  3347. Emit(Result(position,result.op));
  3348. IF conditional THEN
  3349. IntermediateCode.InitImmediate(zero,setType,0);
  3350. BrneL(trueLabel,result.op,zero);
  3351. ReleaseOperand(result);
  3352. BrL(falseLabel);
  3353. END;
  3354. ELSE
  3355. HALT(100);
  3356. END;
  3357. END;
  3358. destination := dest;
  3359. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3360. END VisitBinaryExpression;
  3361. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3362. VAR localResult, operand: Operand;
  3363. BEGIN
  3364. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3365. InitOperand(localResult, ModeValue);
  3366. ASSERT(x.first # NIL);
  3367. Evaluate(x.first, operand);
  3368. localResult.op := operand.op;
  3369. ReleaseOperand(operand);
  3370. UseIntermediateOperand(localResult.op);
  3371. ASSERT(x.last # NIL);
  3372. Evaluate(x.last, operand);
  3373. localResult.tag := operand.op;
  3374. ReleaseOperand(operand);
  3375. UseIntermediateOperand(localResult.tag);
  3376. IF x.step # NIL THEN
  3377. Evaluate(x.step, operand);
  3378. localResult.extra := operand.op;
  3379. ReleaseOperand(operand);
  3380. UseIntermediateOperand(localResult.extra);
  3381. END;
  3382. result := localResult;
  3383. IF Trace THEN TraceExit("VisitRangeExpression") END
  3384. END VisitRangeExpression;
  3385. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3386. BEGIN
  3387. HALT(100); (* should never be evaluated *)
  3388. END VisitTensorRangeExpression;
  3389. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3390. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3391. BEGIN
  3392. IF Trace THEN TraceEnter("VisitConversion") END;
  3393. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3394. dest := destination; destination := emptyOperand;
  3395. Evaluate(x.expression,old);
  3396. InitOperand(result,ModeValue);
  3397. result.op := old.op;
  3398. ASSERT(result.op.mode # 0);
  3399. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3400. (* convert TO a complex number *)
  3401. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3402. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3403. ASSERT(result.op.mode # 0);
  3404. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3405. (* convert FROM a complex number TO a complex number*)
  3406. result.tag := old.tag;
  3407. ASSERT(result.tag.mode # 0);
  3408. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3409. ASSERT(result.tag.mode # 0)
  3410. ELSE
  3411. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3412. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3413. END
  3414. ELSE
  3415. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3416. ASSERT(result.op.mode # 0);
  3417. result.tag := old.tag; (*! probably never used *)
  3418. END;
  3419. destination := dest;
  3420. IF Trace THEN TraceExit("VisitConversion") END;
  3421. END VisitConversion;
  3422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3423. BEGIN
  3424. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3425. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3426. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3427. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3428. END VisitTypeDeclaration;
  3429. (** designators (expressions) *)
  3430. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3431. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3432. BEGIN
  3433. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3434. IF x.left # NIL THEN Expression(x.left) END;
  3435. Symbol(x.symbol,result);
  3436. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3437. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3438. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3439. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3440. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3441. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3442. END;
  3443. END;
  3444. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3445. END VisitSymbolDesignator;
  3446. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3447. BEGIN
  3448. IF isUnchecked THEN RETURN END;
  3449. IF tagsAvailable THEN
  3450. TrapC(BrltL,index,length,IndexCheckTrap);
  3451. END;
  3452. END BoundCheck;
  3453. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3454. VAR d: IntermediateCode.Operand;
  3455. BEGIN
  3456. IF isUnchecked THEN RETURN END;
  3457. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3458. TrapC(op,dim,d,ArraySizeTrap);
  3459. ReleaseIntermediateOperand(d);
  3460. END DimensionCheck;
  3461. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3462. VAR
  3463. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3464. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3465. expression: SyntaxTree.Expression;
  3466. resultingType, leftType, baseType: SyntaxTree.Type;
  3467. skipLabel1: Label;
  3468. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3469. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3470. variableOp: Operand;
  3471. variable: SyntaxTree.Variable;
  3472. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3473. VAR expression: SyntaxTree.Expression;
  3474. BEGIN
  3475. (* count the number of indices, ranges and tensorRanges in the index list *)
  3476. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3477. FOR i := 0 TO parameters.Length() - 1 DO
  3478. expression := parameters.GetExpression(i);
  3479. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3480. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3481. ELSE INC(indexCount)
  3482. END
  3483. END;
  3484. END CountIndices;
  3485. BEGIN
  3486. ASSERT(tagsAvailable);
  3487. resultingType := x.type.resolved; (* resulting type *)
  3488. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3489. InitOperand(localResult, ModeReference);
  3490. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3491. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3492. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3493. (* a globally defined array destination tag is available -> use and invalidate it*)
  3494. localResult.tag := arrayDestinationTag;
  3495. IntermediateCode.InitOperand(arrayDestinationTag)
  3496. ELSE
  3497. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3498. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3499. Symbol(variable, variableOp);
  3500. ReuseCopy(localResult.tag, variableOp.op);
  3501. ReleaseOperand(variableOp);
  3502. END
  3503. END;
  3504. indexListSize := x.parameters.Length();
  3505. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3506. ASSERT(tensorRangeCount <= 1);
  3507. (* designate the array to be indexed over, perform tensor range check if known *)
  3508. Designate(x.left, array);
  3509. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3510. Dereference(array, leftType,FALSE);
  3511. IF tensorRangeCount=0 THEN
  3512. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3513. END
  3514. END;
  3515. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3516. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3517. srcDimOffset := -indexListSize;
  3518. destDimOffset := -rangeCount
  3519. ELSE
  3520. srcDimOffset := 0;
  3521. destDimOffset := 0
  3522. END;
  3523. indexDim := 0;
  3524. (* use address of source array as basis *)
  3525. (*
  3526. ReuseCopy(localResult.op, array.op);
  3527. *)
  3528. localResult.op := array.op;
  3529. UseIntermediateOperand(localResult.op);
  3530. (* go through the index list *)
  3531. FOR i := 0 TO indexListSize - 1 DO
  3532. expression := x.parameters.GetExpression(i);
  3533. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3534. (* nothing to do *)
  3535. ELSE
  3536. (* determine which dimension of source array is currently looked at *)
  3537. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3538. (* get the memory operand pointing to array descriptor dimension *)
  3539. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3540. (* make a reusable register from it *)
  3541. ReuseCopy(srcDim, tmp);
  3542. ReleaseIntermediateOperand(tmp);
  3543. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3544. ELSE
  3545. srcDim := IntermediateCode.Immediate(int32, i)
  3546. END;
  3547. (* get length and increment of source array for current dimension *)
  3548. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3549. Convert(sourceLength.op, sizeType);
  3550. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3551. Convert(sourceIncrement.op, sizeType);
  3552. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3553. ReleaseIntermediateOperand(srcDim);
  3554. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3555. (* SINGLE INDEX *)
  3556. Evaluate(expression, index);
  3557. ReleaseIntermediateOperand(index.tag);
  3558. index.tag := emptyOperand;
  3559. Convert(index.op, sizeType);
  3560. (* lower bound check *)
  3561. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3562. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3563. ELSIF isUnchecked THEN (* do nothing *)
  3564. ELSE
  3565. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3566. END;
  3567. (* upper bound check *)
  3568. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3569. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3570. ELSIF isUnchecked THEN (* do nothing *)
  3571. ELSE
  3572. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3573. END;
  3574. ReleaseOperand(sourceLength);
  3575. Convert(index.op, addressType);
  3576. summand := index.op;
  3577. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3578. (* RANGE OF INDICES *)
  3579. Evaluate(expression, range);
  3580. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3581. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3582. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3583. ReleaseOperand(range);
  3584. Convert(firstIndex, sizeType);
  3585. Convert(lastIndex, sizeType);
  3586. Convert(stepSize, sizeType);
  3587. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3588. if it is 'MAX(LONGINT)' *)
  3589. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3590. TransferToRegister(lastIndex, lastIndex);
  3591. skipLabel1 := NewLabel();
  3592. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3593. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3594. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3595. SetLabel(skipLabel1)
  3596. END;
  3597. (* check if step size is valid *)
  3598. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3599. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3600. ELSIF isUnchecked THEN (* do nothing *)
  3601. ELSE
  3602. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3603. END;
  3604. (* check lower bound check *)
  3605. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3606. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3607. ELSIF isUnchecked THEN (* do nothing *)
  3608. ELSE
  3609. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3610. END;
  3611. (* check upper bound check *)
  3612. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3613. (* statically open range: nothing to do *)
  3614. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3615. ASSERT(staticLastIndex < staticSourceLength)
  3616. ELSIF isUnchecked THEN (* do nothing *)
  3617. ELSE
  3618. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3619. END;
  3620. (* determine length of target array for current dimension *)
  3621. (* 1. incorporate last index: *)
  3622. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3623. (* last index is static *)
  3624. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3625. targetLength := sourceLength.op
  3626. ELSE
  3627. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3628. END;
  3629. UseIntermediateOperand(targetLength);
  3630. ELSE
  3631. (* targetLength := lastIndex + 1
  3632. Reuse1(targetLength, lastIndex);
  3633. *)
  3634. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3635. END;
  3636. ReleaseOperand(sourceLength);
  3637. ReleaseIntermediateOperand(lastIndex);
  3638. (* 2. incorporate first index: *)
  3639. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3640. (* first index and current target length are static *)
  3641. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3642. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3643. (* first index = 0: nothing to do *)
  3644. ELSE
  3645. (* targetLength := targetLength - firstIndex *)
  3646. TransferToRegister(targetLength, targetLength);
  3647. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3648. END;
  3649. (* clip negative lengths to 0 *)
  3650. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3651. IF staticTargetLength < 0 THEN
  3652. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3653. END
  3654. ELSE
  3655. skipLabel1 := NewLabel();
  3656. TransferToRegister(targetLength, targetLength);
  3657. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3658. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3659. SetLabel(skipLabel1)
  3660. END;
  3661. (* 3. incorporate index step size: *)
  3662. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3663. (*step size and current target length are static *)
  3664. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3665. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3666. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3667. (* step size = 1: nothing to do *)
  3668. ELSE
  3669. (* emit code for this:
  3670. targetLength := (targetLength-1) DIV stepSize +1;
  3671. *)
  3672. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3673. DivInt(targetLength, targetLength, stepSize);
  3674. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3675. END;
  3676. (* determine increment of target array for current dimension *)
  3677. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3678. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3679. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3680. (* step size = 1 *)
  3681. targetIncrement := sourceIncrement.op;
  3682. UseIntermediateOperand(targetIncrement)
  3683. ELSE
  3684. (* targetIncrement := sourceIncrement * stepSize *)
  3685. Reuse1(targetIncrement, stepSize);
  3686. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3687. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3688. END;
  3689. ReleaseIntermediateOperand(stepSize);
  3690. (* write length and increment of target array to descriptor *)
  3691. IF destDimOffset < 0 THEN
  3692. (* determine which dimension of target array is currently looked at *)
  3693. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3694. TransferToRegister(destDim, tmp);
  3695. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3696. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3697. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3698. ReleaseIntermediateOperand(destDim)
  3699. ELSE
  3700. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3701. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3702. END;
  3703. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3704. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3705. INC(indexDim);
  3706. Convert(firstIndex, addressType);
  3707. TransferToRegister(summand, firstIndex);
  3708. ELSE HALT(100);
  3709. END;
  3710. (*
  3711. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3712. *)
  3713. Convert(sourceIncrement.op, addressType);
  3714. Convert(summand, addressType);
  3715. MulInt(summand, summand, sourceIncrement.op);
  3716. ReleaseIntermediateOperand(sourceIncrement.op);
  3717. AddInt(localResult.op, localResult.op, summand);
  3718. ReleaseIntermediateOperand(summand);
  3719. END
  3720. END;
  3721. result := localResult;
  3722. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3723. ReleaseIntermediateOperand(result.tag);
  3724. result.tag := TypeDescriptorAdr(resultingType);
  3725. IF ~newObjectFile THEN
  3726. IntermediateCode.MakeMemory(result.tag,addressType);
  3727. END;
  3728. ELSIF IsDelegate(resultingType) THEN
  3729. ReleaseIntermediateOperand(result.tag);
  3730. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3731. UseIntermediateOperand(result.tag);
  3732. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3733. ReleaseIntermediateOperand(result.tag);
  3734. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3735. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3736. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3737. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3738. (* finalize target array descriptor *)
  3739. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3740. (* write lengths and increments of target array for remaining dimensions *)
  3741. i := indexListSize;
  3742. WHILE indexDim < targetArrayDimensionality DO
  3743. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3744. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3745. ReleaseOperand(sourceLength);
  3746. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3747. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3748. ReleaseOperand(sourceIncrement);
  3749. INC(i); INC(indexDim);
  3750. END;
  3751. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3752. tmp := nil;
  3753. ELSE
  3754. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3755. END;
  3756. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3757. ReleaseIntermediateOperand(tmp);
  3758. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3759. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3760. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3761. ELSE
  3762. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3763. END;
  3764. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3765. ReleaseIntermediateOperand(tmp);
  3766. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3767. (* write dimensionality *)
  3768. IF targetArrayDimensionality # 0 THEN
  3769. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3770. END;
  3771. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3772. END;
  3773. ReleaseOperand(array);
  3774. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3775. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3776. END;
  3777. END MathIndexDesignator;
  3778. (* get the length of an array , trying to make use of static information *)
  3779. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3780. VAR res: IntermediateCode.Operand; size: LONGINT;
  3781. BEGIN
  3782. type := type.resolved;
  3783. IF type IS SyntaxTree.ArrayType THEN
  3784. WITH type: SyntaxTree.ArrayType DO
  3785. IF type.form = SyntaxTree.Static THEN
  3786. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3787. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3788. Evaluate(type.length, op);
  3789. ReleaseIntermediateOperand(op.tag);
  3790. RETURN op.op;*)
  3791. ELSE
  3792. res := tag;
  3793. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3794. IntermediateCode.MakeMemory(res,addressType);
  3795. UseIntermediateOperand(res);
  3796. RETURN res
  3797. END
  3798. END;
  3799. ELSE
  3800. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3801. RETURN IntermediateCode.Immediate(addressType,size);
  3802. END;
  3803. END ArrayLength;
  3804. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3805. BEGIN
  3806. IF IsImmediate(x) THEN
  3807. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3808. ELSE
  3809. IF ~ReusableRegister(res) THEN
  3810. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3811. ELSE
  3812. UseIntermediateOperand(res);
  3813. END;
  3814. Emit(Mov(position,res,x))
  3815. END;
  3816. END CopyInt;
  3817. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3818. BEGIN
  3819. ReleaseIntermediateOperand(res);
  3820. IF IsImmediate(x) & IsImmediate(y) THEN
  3821. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3822. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3823. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3824. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3825. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3826. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3827. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3828. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3829. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3830. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3831. UseIntermediateOperand(res);
  3832. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3833. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3834. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3835. UseIntermediateOperand(res);
  3836. ELSE
  3837. IF ~ReusableRegister(res) THEN
  3838. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3839. ELSE
  3840. UseIntermediateOperand(res);
  3841. END;
  3842. IF IsImmediate(x) & (x.intValue = 0) THEN
  3843. Emit(Mov(position,res,y))
  3844. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3845. Emit(Mov(position,res,x))
  3846. ELSE
  3847. Emit(Add(position,res, x, y));
  3848. END;
  3849. END;
  3850. END AddInt;
  3851. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3852. BEGIN
  3853. ReleaseIntermediateOperand(res);
  3854. IF IsImmediate(x) & IsImmediate(y) THEN
  3855. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3856. ELSE
  3857. IF ~ReusableRegister(res) THEN
  3858. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3859. ELSE UseIntermediateOperand(res);
  3860. END;
  3861. IF IsImmediate(x) & (x.intValue = 1) THEN
  3862. Emit(Mov(position,res,y))
  3863. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3864. Emit(Mov(position,res,x))
  3865. ELSE
  3866. Emit(Mul(position,res, x, y));
  3867. END;
  3868. END;
  3869. END MulInt;
  3870. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3871. BEGIN
  3872. ReleaseIntermediateOperand(res);
  3873. IF IsImmediate(x) & IsImmediate(y) THEN
  3874. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3875. ELSE
  3876. IF ~ReusableRegister(res) THEN
  3877. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3878. ELSE UseIntermediateOperand(res);
  3879. END;
  3880. IF IsImmediate(x) & (x.intValue = 1) THEN
  3881. Emit(Mov(position,res,y))
  3882. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3883. Emit(Mov(position,res,x))
  3884. ELSE
  3885. Emit(Div(position,res, x, y));
  3886. END;
  3887. END;
  3888. END DivInt;
  3889. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3890. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3891. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3892. BEGIN
  3893. type := x.left.type.resolved;
  3894. IF type IS SyntaxTree.StringType THEN
  3895. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3896. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3897. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3898. type := atype;
  3899. x.left.SetType(type);
  3900. END;
  3901. IntermediateCode.InitImmediate(res,addressType,0);
  3902. maxDim := x.parameters.Length()-1;
  3903. (*
  3904. computation rule:
  3905. a: ARRAY X,Y,Z OF Element with size S
  3906. a[i,j,k] -->
  3907. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3908. *)
  3909. FOR i := 0 TO maxDim DO
  3910. e := x.parameters.GetExpression(i);
  3911. Evaluate(e,index);
  3912. Convert(index.op,addressType);
  3913. AddInt(res, res, index.op);
  3914. IF i = 0 THEN
  3915. (*
  3916. ReuseCopy(res, index.op);
  3917. *)
  3918. Designate(x.left,array);
  3919. type := x.left.type.resolved;
  3920. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3921. Dereference(array, type, FALSE);
  3922. END;
  3923. (*
  3924. ELSE AddInt(res, res, index.op);
  3925. *)
  3926. END;
  3927. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3928. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3929. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3930. BoundCheck(index.op, length);
  3931. END;
  3932. ReleaseIntermediateOperand(length);
  3933. END;
  3934. ReleaseOperand(index);
  3935. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3936. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3937. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3938. MulInt(res,res,length);
  3939. END;
  3940. ReleaseIntermediateOperand(length);
  3941. END;
  3942. (* remaining open dimensions -- compute address *)
  3943. i := maxDim+1;
  3944. IF type IS SyntaxTree.ArrayType THEN
  3945. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3946. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3947. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3948. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3949. MulInt(res,res,length);
  3950. END;
  3951. ReleaseIntermediateOperand(length);
  3952. INC(i);
  3953. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3954. END;
  3955. END;
  3956. IF (type IS SyntaxTree.ArrayType) THEN
  3957. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3958. size := StaticSize(system, type);
  3959. IF size # 1 THEN
  3960. length := IntermediateCode.Immediate(addressType,size);
  3961. MulInt(res,res,length);
  3962. END;
  3963. ELSE
  3964. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3965. IF size # 1 THEN
  3966. length := IntermediateCode.Immediate(addressType,size);
  3967. MulInt(res,res,length);
  3968. END;
  3969. END;
  3970. END;
  3971. AddInt(res,res,array.op);
  3972. InitOperand(result,ModeReference);
  3973. result.op := res;
  3974. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3975. ReleaseIntermediateOperand(result.tag);
  3976. result.tag := TypeDescriptorAdr(type);
  3977. IF ~newObjectFile THEN
  3978. IntermediateCode.MakeMemory(result.tag,addressType);
  3979. END
  3980. ELSIF IsDelegate(type) THEN
  3981. ReleaseIntermediateOperand(result.tag);
  3982. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3983. UseIntermediateOperand(result.tag);
  3984. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3985. ReleaseIntermediateOperand(result.tag);
  3986. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3987. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3988. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3989. END;
  3990. ReleaseOperand(array);
  3991. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3992. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3993. END;
  3994. END IndexDesignator;
  3995. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3996. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3997. BEGIN
  3998. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3999. dest := destination; destination := emptyOperand;
  4000. type := x.left.type.resolved;
  4001. IF type IS SyntaxTree.MathArrayType THEN
  4002. MathIndexDesignator(x);
  4003. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4004. IndexDesignator(x);
  4005. END;
  4006. destination := dest;
  4007. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4008. END VisitIndexDesignator;
  4009. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4010. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4011. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4012. procedure: SyntaxTree.Procedure;
  4013. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4014. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4015. BEGIN
  4016. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4017. parameters := expression.parameters;
  4018. moduleName := "FoxArrayBase";
  4019. procedureName := "CopyDescriptor";
  4020. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4021. SaveRegisters();ReleaseUsedRegisters(saved);
  4022. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4023. IF procedure = NIL THEN
  4024. s := "procedure ";
  4025. Strings.Append(s,moduleName);
  4026. Strings.Append(s,".");
  4027. Strings.Append(s,procedureName);
  4028. Strings.Append(s," not present");
  4029. Error(position,s);
  4030. ELSE
  4031. (* push address of temporary variable *)
  4032. Symbol(variable,destOperand);
  4033. Emit(Push(position,destOperand.op));
  4034. ReleaseOperand(destOperand);
  4035. (* push src *)
  4036. Evaluate(expression.left,srcOperand);
  4037. (*
  4038. Dereference(srcOperand,expression.type.resolved);
  4039. Emit(Push(position,srcOperand.tag));
  4040. *)
  4041. Emit(Push(position,srcOperand.op));
  4042. ReleaseOperand(srcOperand);
  4043. tensorFound := FALSE;
  4044. FOR i := 0 TO parameters.Length()-1 DO
  4045. e := parameters.GetExpression(i);
  4046. IF e IS SyntaxTree.TensorRangeExpression THEN
  4047. tensorFound := TRUE;
  4048. ELSIF e IS SyntaxTree.RangeExpression THEN
  4049. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4050. ELSE
  4051. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4052. END;
  4053. END;
  4054. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4055. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4056. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4057. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4058. StaticCallOperand(procOp,procedure);
  4059. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4060. ReleaseOperand(procOp);
  4061. END;
  4062. RestoreRegisters(saved);
  4063. END;
  4064. RETURN variable
  4065. END PrepareTensorDescriptor;
  4066. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4067. VAR
  4068. type, descriptorType, baseType: SyntaxTree.Type;
  4069. operand, tmpOperand, variableOp, variable2Op: Operand;
  4070. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4071. variable, variable2: SyntaxTree.Variable;
  4072. dim, i, size: LONGINT;
  4073. (* TODO: needed? *)
  4074. oldArrayDestinationTag: IntermediateCode.Operand;
  4075. oldArrayDestinationDimension: LONGINT;
  4076. position: Position;
  4077. saved: RegisterEntry;
  4078. arrayFlags: SET;
  4079. m, n: LONGINT;
  4080. PROCEDURE Pass(op: IntermediateCode.Operand);
  4081. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4082. BEGIN
  4083. IF numberRegister >= 0 THEN
  4084. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4085. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4086. Emit(Mov(position,parameterRegister, op));
  4087. ELSE
  4088. Emit(Push(position,op))
  4089. END
  4090. END Pass;
  4091. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4092. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4093. BEGIN
  4094. formalType := formalType.resolved; actualType := actualType.resolved;
  4095. IF IsOpenArray(formalType)THEN
  4096. IF actualType IS SyntaxTree.StringType THEN
  4097. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4098. RETURN;
  4099. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4100. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4101. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4102. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4103. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4104. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4105. ELSE
  4106. tmp := baseReg;
  4107. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4108. IntermediateCode.MakeMemory(tmp,addressType);
  4109. Pass((tmp));
  4110. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4111. END;
  4112. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4113. END;
  4114. END PushArrayLens;
  4115. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4116. BEGIN
  4117. CASE size OF
  4118. |2: INCL(flags,Size2Flag);
  4119. |3: INCL(flags,Size3Flag);
  4120. |4: INCL(flags,Size4Flag);
  4121. |5: INCL(flags,Size5Flag);
  4122. |6: INCL(flags,Size6Flag);
  4123. |7: INCL(flags,Size7Flag);
  4124. |8: INCL(flags,Size8Flag);
  4125. END;
  4126. END SetSmallArraySizeFlag;
  4127. BEGIN
  4128. IF Trace THEN TraceEnter("PushParameter") END;
  4129. position := expression.position;
  4130. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4131. type := expression.type.resolved;
  4132. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4133. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4134. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4135. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4136. );
  4137. (* TODO: needed? *)
  4138. oldArrayDestinationTag := arrayDestinationTag;
  4139. oldArrayDestinationDimension := arrayDestinationDimension;
  4140. IF IsArrayOfSystemByte(parameter.type) THEN
  4141. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4142. Designate(expression,operand);
  4143. ELSE
  4144. Evaluate(expression, tmpOperand);
  4145. variable := GetTemporaryVariable(expression.type, FALSE);
  4146. Symbol(variable, operand);
  4147. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4148. Emit(Mov(position,tmp, tmpOperand.op));
  4149. ReleaseOperand(tmpOperand);
  4150. END;
  4151. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4152. ReleaseIntermediateOperand(operand.tag);
  4153. operand.tag := tmp;
  4154. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4155. Pass((operand.tag));
  4156. END;
  4157. Pass((operand.op));
  4158. ELSIF IsOpenArray(parameter.type) THEN
  4159. Designate(expression,operand);
  4160. baseReg := operand.tag;
  4161. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4162. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4163. END;
  4164. Pass((operand.op)); (* address of the array *)
  4165. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4166. (* case 1 *)
  4167. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4168. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4169. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4170. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4171. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4172. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4173. arrayDestinationTag := sp;
  4174. (* case 1b *)
  4175. IF expression IS SyntaxTree.IndexDesignator THEN
  4176. (*
  4177. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4178. descriptorType := GetMathArrayDescriptorType(dim);
  4179. variable := GetTemporaryVariable(descriptorType,expression.position);
  4180. Symbol(variable,variableOp);
  4181. arrayDestinationTag := variableOp.op;
  4182. *)
  4183. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4184. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4185. arrayDestinationDimension := dim;
  4186. Designate(expression,operand);
  4187. (* case 1a *)
  4188. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4189. Designate(expression,operand);
  4190. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4191. i := 0;
  4192. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4193. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4194. INC(i);
  4195. END;
  4196. type := expression.type.resolved;
  4197. WHILE (i<dim) DO (* remaining static dimensions *)
  4198. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4199. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4200. ReleaseOperand(tmpOperand);
  4201. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4202. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4203. ReleaseOperand(tmpOperand);
  4204. INC(i);
  4205. END;
  4206. dimOp := IntermediateCode.Immediate(addressType,dim);
  4207. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4208. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4209. (* case 1d *)
  4210. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4211. Designate(expression,operand);
  4212. Dereference(operand,type.resolved,FALSE);
  4213. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4214. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4215. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4216. (* case 1f *)
  4217. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4218. Designate(expression,operand);
  4219. FOR i := 0 TO dim-1 DO
  4220. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4221. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4222. ReleaseOperand(tmpOperand);
  4223. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4224. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4225. ReleaseOperand(tmpOperand);
  4226. END;
  4227. (*******
  4228. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4229. *)
  4230. arrayFlags := {StaticFlag};
  4231. IF dim = 1 THEN
  4232. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4233. ReleaseOperand(tmpOperand);
  4234. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4235. m := LONGINT(tmpOperand.op.intValue);
  4236. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4237. ELSIF dim = 2 THEN
  4238. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4239. ReleaseOperand(tmpOperand);
  4240. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4241. m := LONGINT(tmpOperand.op.intValue);
  4242. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4243. ReleaseOperand(tmpOperand);
  4244. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4245. n := LONGINT(tmpOperand.op.intValue);
  4246. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4247. INCL(arrayFlags,SmallMatrixFlag);
  4248. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4249. END;
  4250. END;
  4251. (*******)
  4252. dimOp := IntermediateCode.Immediate(addressType,dim);
  4253. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4254. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4255. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4256. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4257. baseType := SemanticChecker.ArrayBase(type,dim);
  4258. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4259. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4260. ELSE HALT(100);
  4261. END;
  4262. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4263. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4264. (* case 2b *)
  4265. IF expression IS SyntaxTree.IndexDesignator THEN
  4266. descriptorType := GetMathArrayDescriptorType(dim);
  4267. variable := GetTemporaryVariable(descriptorType, FALSE);
  4268. Symbol(variable,variableOp);
  4269. arrayDestinationTag := variableOp.op;
  4270. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4271. arrayDestinationDimension := dim;
  4272. NeedDescriptor := TRUE;
  4273. Designate(expression,operand);
  4274. Pass((operand.tag));
  4275. NeedDescriptor := FALSE;
  4276. (* case 2a *)
  4277. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4278. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4279. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4280. INC(i);
  4281. END;
  4282. IF i = dim THEN
  4283. Designate(expression,operand);
  4284. Pass((operand.tag));
  4285. ELSE (* open-static *)
  4286. type := expression.type.resolved;
  4287. descriptorType := GetMathArrayDescriptorType(dim);
  4288. variable := GetTemporaryVariable(descriptorType, FALSE);
  4289. Symbol(variable,variableOp);
  4290. arrayDestinationTag := variableOp.op;
  4291. Designate(expression,operand);
  4292. FOR i := 0 TO dim-1 DO
  4293. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4294. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4295. ReleaseOperand(tmpOperand);
  4296. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4297. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4298. ReleaseOperand(tmpOperand);
  4299. END;
  4300. dimOp := IntermediateCode.Immediate(addressType,dim);
  4301. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4302. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4303. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4304. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4305. baseType := SemanticChecker.ArrayBase(type,dim);
  4306. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4307. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4308. Pass((arrayDestinationTag));
  4309. END;
  4310. (* case 2d *)
  4311. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4312. Designate(expression,operand);
  4313. Dereference(operand,type.resolved,FALSE);
  4314. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4315. Pass((operand.tag));
  4316. (* case 2f *)
  4317. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4318. descriptorType := GetMathArrayDescriptorType(dim);
  4319. variable := GetTemporaryVariable(descriptorType, FALSE);
  4320. Symbol(variable,variableOp);
  4321. arrayDestinationTag := variableOp.op;
  4322. Designate(expression,operand);
  4323. FOR i := 0 TO dim-1 DO
  4324. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4325. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4326. ReleaseOperand(tmpOperand);
  4327. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4328. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4329. ReleaseOperand(tmpOperand);
  4330. END;
  4331. (*
  4332. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4333. *)
  4334. arrayFlags := {StaticFlag};
  4335. IF dim = 1 THEN
  4336. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4337. ReleaseOperand(tmpOperand);
  4338. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4339. m := LONGINT(tmpOperand.op.intValue);
  4340. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4341. ELSIF dim = 2 THEN
  4342. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4343. ReleaseOperand(tmpOperand);
  4344. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4345. m := LONGINT(tmpOperand.op.intValue);
  4346. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4347. ReleaseOperand(tmpOperand);
  4348. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4349. n := LONGINT(tmpOperand.op.intValue);
  4350. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4351. INCL(arrayFlags,SmallMatrixFlag);
  4352. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4353. END;
  4354. END;
  4355. (*******)
  4356. dimOp := IntermediateCode.Immediate(addressType,dim);
  4357. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4358. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4359. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4360. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4361. baseType := SemanticChecker.ArrayBase(type,dim);
  4362. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4363. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4364. Pass((arrayDestinationTag));
  4365. ELSE HALT(100);
  4366. END;
  4367. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4368. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4369. (* case 3b *)
  4370. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4371. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4372. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4373. Symbol(variable,variableOp);
  4374. LoadValue(variableOp,system.addressType);
  4375. ELSE
  4376. descriptorType := GetMathArrayDescriptorType(dim);
  4377. variable := GetTemporaryVariable(descriptorType, FALSE);
  4378. Symbol(variable,variableOp);
  4379. END;
  4380. arrayDestinationTag := variableOp.op;
  4381. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4382. arrayDestinationDimension := 0;
  4383. Designate(expression,operand);
  4384. Pass((operand.tag));
  4385. (* case 3a *)
  4386. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4387. i := 0;
  4388. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4389. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4390. INC(i);
  4391. END;
  4392. IF i = dim THEN
  4393. Designate(expression,operand);
  4394. Pass((operand.tag));
  4395. ELSE (* open-static *)
  4396. type := expression.type.resolved;
  4397. descriptorType := GetMathArrayDescriptorType(dim);
  4398. variable := GetTemporaryVariable(descriptorType, FALSE);
  4399. Symbol(variable,variableOp);
  4400. arrayDestinationTag := variableOp.op;
  4401. Designate(expression,operand);
  4402. FOR i := 0 TO dim-1 DO
  4403. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4404. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4405. ReleaseOperand(tmpOperand);
  4406. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4407. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4408. ReleaseOperand(tmpOperand);
  4409. END;
  4410. dimOp := IntermediateCode.Immediate(addressType,dim);
  4411. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4412. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4413. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4414. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4415. baseType := SemanticChecker.ArrayBase(type,dim);
  4416. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4417. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4418. Pass((arrayDestinationTag));
  4419. END;
  4420. (* case 3d *)
  4421. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4422. Designate(expression,operand);
  4423. Dereference(operand,type.resolved,FALSE);
  4424. (*
  4425. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4426. *)
  4427. Pass((operand.tag));
  4428. (* case 3f *)
  4429. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4430. descriptorType := GetMathArrayDescriptorType(dim);
  4431. variable := GetTemporaryVariable(descriptorType, FALSE);
  4432. Symbol(variable,variableOp);
  4433. arrayDestinationTag := variableOp.op;
  4434. Designate(expression,operand);
  4435. IF operand.op.type.length >1 THEN (* vector register *)
  4436. variable2 := GetTemporaryVariable(type, FALSE);
  4437. Symbol(variable2, variable2Op);
  4438. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4439. Emit(Mov(position,tmp, operand.op));
  4440. ReleaseOperand(operand);
  4441. Symbol(variable2, operand);
  4442. END;
  4443. FOR i := 0 TO dim-1 DO
  4444. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4445. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4446. ReleaseOperand(tmpOperand);
  4447. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4448. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4449. ReleaseOperand(tmpOperand);
  4450. END;
  4451. dimOp := IntermediateCode.Immediate(addressType,dim);
  4452. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4453. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4454. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4455. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4456. baseType := SemanticChecker.ArrayBase(type,dim);
  4457. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4458. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4459. Pass((arrayDestinationTag));
  4460. ELSE HALT(100);
  4461. END;
  4462. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4463. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4464. (* case 4b *)
  4465. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4466. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4467. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4468. Symbol(variable,variableOp);
  4469. LoadValue(variableOp,system.addressType);
  4470. ELSE
  4471. descriptorType := GetMathArrayDescriptorType(dim);
  4472. variable := GetTemporaryVariable(descriptorType, FALSE);
  4473. Symbol(variable,variableOp);
  4474. END;
  4475. arrayDestinationTag := variableOp.op;
  4476. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4477. arrayDestinationDimension := 0;
  4478. Designate(expression,operand);
  4479. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4480. Symbol(variable,variableOp);
  4481. ELSE
  4482. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4483. Symbol(variable,variableOp);
  4484. MakeMemory(tmp,variableOp.op,addressType,0);
  4485. Emit(Mov(position,tmp,operand.tag));
  4486. ReleaseIntermediateOperand(tmp);
  4487. END;
  4488. Pass((variableOp.op));
  4489. ReleaseOperand(variableOp);
  4490. (* case 4a *)
  4491. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4492. i := 0;
  4493. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4494. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4495. INC(i);
  4496. END;
  4497. IF i = dim THEN
  4498. Designate(expression,operand);
  4499. arrayDestinationTag := operand.tag;
  4500. ELSE (* open-static *)
  4501. type := expression.type.resolved;
  4502. descriptorType := GetMathArrayDescriptorType(dim);
  4503. variable := GetTemporaryVariable(descriptorType, FALSE);
  4504. Symbol(variable,variableOp);
  4505. arrayDestinationTag := variableOp.op;
  4506. Designate(expression,operand);
  4507. FOR i := 0 TO dim-1 DO
  4508. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4509. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4510. ReleaseOperand(tmpOperand);
  4511. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4512. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4513. ReleaseOperand(tmpOperand);
  4514. END;
  4515. dimOp := IntermediateCode.Immediate(addressType,dim);
  4516. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4517. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4518. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4519. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4520. baseType := SemanticChecker.ArrayBase(type,dim);
  4521. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4522. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4523. END;
  4524. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4525. Symbol(variable,variableOp);
  4526. MakeMemory(tmp,variableOp.op,addressType,0);
  4527. Emit(Mov(position,tmp,arrayDestinationTag));
  4528. ReleaseIntermediateOperand(tmp);
  4529. Pass((variableOp.op));
  4530. ReleaseOperand(variableOp);
  4531. (* case 4d *)
  4532. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4533. Designate(expression,operand);
  4534. (*
  4535. Dereference(operand,type.resolved,FALSE);
  4536. *)
  4537. (*
  4538. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4539. *)
  4540. Pass((operand.op));
  4541. (* case 4f *)
  4542. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4543. descriptorType := GetMathArrayDescriptorType(dim);
  4544. variable := GetTemporaryVariable(descriptorType, FALSE);
  4545. Symbol(variable,variableOp);
  4546. arrayDestinationTag := variableOp.op;
  4547. Designate(expression,operand);
  4548. FOR i := 0 TO dim-1 DO
  4549. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4550. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4551. ReleaseOperand(tmpOperand);
  4552. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4553. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4554. ReleaseOperand(tmpOperand);
  4555. END;
  4556. dimOp := IntermediateCode.Immediate(addressType,dim);
  4557. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4558. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4559. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4560. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4561. baseType := SemanticChecker.ArrayBase(type,dim);
  4562. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4563. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4564. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4565. Symbol(variable,variableOp);
  4566. MakeMemory(tmp,variableOp.op,addressType,0);
  4567. Emit(Mov(position,tmp,arrayDestinationTag));
  4568. ReleaseIntermediateOperand(tmp);
  4569. Pass((variableOp.op));
  4570. ReleaseOperand(variableOp);
  4571. ELSE HALT(100);
  4572. END;
  4573. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4574. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4575. Designate(expression,operand);
  4576. IF operand.op.type.length > 1 THEN
  4577. Emit(Push(position, operand.op));
  4578. ELSE
  4579. size := system.SizeOf(type);
  4580. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4581. size := ToMemoryUnits(system,size);
  4582. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4583. arrayDestinationTag := sp;
  4584. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4585. END;
  4586. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4587. Designate(expression,operand);
  4588. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4589. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4590. Symbol(variable,variableOp);
  4591. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4592. Emit(Mov(position,tmp,operand.op));
  4593. Emit(Push(position,variableOp.op));
  4594. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4595. Pass((operand.op));
  4596. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4597. END;
  4598. ELSE HALT(200)
  4599. END;
  4600. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4601. IF parameter.kind = SyntaxTree.VarParameter THEN
  4602. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4603. Designate(expression, operand);
  4604. Pass((operand.op));
  4605. ELSE
  4606. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4607. Evaluate(expression, operand);
  4608. Pass((operand.extra)); (* step *)
  4609. Pass((operand.tag)); (* last *)
  4610. Pass((operand.op)) (* first *)
  4611. END
  4612. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4613. IF parameter.kind = SyntaxTree.VarParameter THEN
  4614. Designate(expression, operand);
  4615. Pass((operand.op));
  4616. ELSE
  4617. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4618. Evaluate(expression, operand);
  4619. Pass((operand.tag)); (* real part *)
  4620. Pass((operand.op)) (* imaginary part *)
  4621. END
  4622. ELSE
  4623. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4624. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4625. Designate(expression,operand);
  4626. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4627. (* stack allocation *)
  4628. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4629. (*! parameter alignment to be discussed ... *)
  4630. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4631. size := type(SyntaxTree.StringType).length;
  4632. END;
  4633. IF backend.cooperative & parameter.NeedsTrace() THEN
  4634. dst := NewRegisterOperand (addressType);
  4635. Emit(Mov(position,dst, sp));
  4636. IntermediateCode.InitImmediate(null, byteType, 0);
  4637. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4638. ReleaseIntermediateOperand(dst);
  4639. SaveRegisters();ReleaseUsedRegisters(saved);
  4640. (* register dst has been freed before SaveRegisters already *)
  4641. CallAssignMethod(dst, operand.op, parameter.type);
  4642. RestoreRegisters(saved);
  4643. END;
  4644. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4645. ELSIF IsOpenArray(parameter.type) THEN
  4646. Designate(expression,operand);
  4647. baseReg := operand.tag;
  4648. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4649. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4650. END;
  4651. Pass((operand.op)); (* address of the array *)
  4652. ELSIF IsDelegate(parameter.type) THEN
  4653. Evaluate(expression,operand);
  4654. IF backend.cooperative & parameter.NeedsTrace() THEN
  4655. Emit(Push(position, nil));
  4656. dst := NewRegisterOperand (addressType);
  4657. Emit(Mov(position,dst, sp));
  4658. ReleaseIntermediateOperand(dst);
  4659. SaveRegisters();ReleaseUsedRegisters(saved);
  4660. Emit(Push(position, dst));
  4661. (* register dst has been freed before SaveRegisters already *)
  4662. Emit(Push(position, operand.tag));
  4663. CallThis(position,"GarbageCollector","Assign",2);
  4664. RestoreRegisters(saved);
  4665. ELSE
  4666. Pass((operand.tag));
  4667. END;
  4668. Pass((operand.op));
  4669. ELSE
  4670. Evaluate(expression,operand);
  4671. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4672. Emit(Push(position, nil));
  4673. dst := NewRegisterOperand (addressType);
  4674. Emit(Mov(position,dst, sp));
  4675. ReleaseIntermediateOperand(dst);
  4676. SaveRegisters();ReleaseUsedRegisters(saved);
  4677. Emit(Push(position, dst));
  4678. (* register dst has been freed before SaveRegisters already *)
  4679. Emit(Push(position, operand.op));
  4680. CallThis(position,"GarbageCollector","Assign",2);
  4681. RestoreRegisters(saved);
  4682. ELSE
  4683. Pass((operand.op));
  4684. END;
  4685. END;
  4686. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4687. Evaluate(expression,operand);
  4688. Pass((operand.op));
  4689. ELSE (* var parameter *)
  4690. Designate(expression,operand);
  4691. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4692. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4693. Pass((operand.tag));
  4694. END;
  4695. END;
  4696. Pass((operand.op));
  4697. END;
  4698. END;
  4699. (* TODO: needed? *)
  4700. arrayDestinationTag := oldArrayDestinationTag;
  4701. arrayDestinationDimension := oldArrayDestinationDimension;
  4702. IF needsParameterBackup THEN
  4703. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4704. ReuseCopy(parameterBackup, operand.op)
  4705. END;
  4706. ReleaseOperand(operand);
  4707. IF Trace THEN TraceExit("PushParameter") END;
  4708. END PushParameter;
  4709. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4710. VAR prevConditional: BOOLEAN;
  4711. BEGIN
  4712. prevConditional := conditional;
  4713. conditional := FALSE;
  4714. IF (x.result # NIL) & ( x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4715. Expression(x.result); (* use register *)
  4716. END;
  4717. Statement(x.statement);
  4718. conditional := prevConditional;
  4719. IF x.result # NIL THEN Expression(x.result) END;
  4720. IF (x.result # NIL) & (x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4721. ReleaseIntermediateOperand(result.op);
  4722. END;
  4723. END VisitStatementDesignator;
  4724. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4725. VAR
  4726. procedure: SyntaxTree.Procedure;
  4727. procedureType: SyntaxTree.ProcedureType;
  4728. wasInline: BOOLEAN;
  4729. actualParameters: SyntaxTree.ExpressionList;
  4730. formalParameter: SyntaxTree.Parameter;
  4731. actualParameter: SyntaxTree.Expression;
  4732. i: LONGINT;
  4733. localVariable: SyntaxTree.Variable;
  4734. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4735. src, dest: Operand;
  4736. prevInlineExit : Label;
  4737. prevMapper: SymbolMapper;
  4738. tooComplex: BOOLEAN;
  4739. resultDesignator: SyntaxTree.Expression;
  4740. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4741. BEGIN
  4742. IF e = NIL THEN RETURN TRUE
  4743. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4744. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4745. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4746. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4747. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4748. ELSE RETURN FALSE
  4749. END;
  4750. END SimpleExpression;
  4751. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4752. BEGIN
  4753. RETURN checker.CanPassInRegister(type)
  4754. END FitsInRegister;
  4755. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4756. VAR
  4757. variable: SyntaxTree.Variable;
  4758. variableDesignator: SyntaxTree.Designator;
  4759. BEGIN
  4760. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4761. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4762. variableDesignator.SetType(type);
  4763. RETURN variableDesignator
  4764. END GetTemp;
  4765. BEGIN
  4766. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4767. wasInline := currentIsInline;
  4768. prevInlineExit := currentInlineExit;
  4769. prevMapper := currentMapper;
  4770. currentInlineExit := NewLabel();
  4771. tooComplex := FALSE;
  4772. NEW(currentMapper);
  4773. currentIsInline := TRUE;
  4774. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4775. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4776. formalParameter := procedureType.firstParameter;
  4777. actualParameters := x.parameters;
  4778. i := 0;
  4779. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4780. actualParameter := actualParameters.GetExpression(i);
  4781. IF actualParameter.resolved # NIL THEN
  4782. actualParameter := actualParameter.resolved
  4783. END;
  4784. (*
  4785. if expression is simple and can be passed immediately
  4786. or if type fits in register then we can proceed
  4787. otherwise we escape to ordinary procedure call.
  4788. *)
  4789. (* cases where the expression can be mapped identically *)
  4790. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4791. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4792. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4793. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4794. (*
  4795. Assign(variableDesignator, actualParameter);
  4796. *)
  4797. Evaluate(actualParameter, src);
  4798. Designate(variableDesignator, dest);
  4799. Emit(Mov(x.position, dest.op, src.op));
  4800. ReleaseOperand(dest);
  4801. ReleaseOperand(src);
  4802. (* the src operand should now have been completely released ! *)
  4803. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4804. ELSE tooComplex := TRUE
  4805. END;
  4806. (*
  4807. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4808. currentMapper.Add(formalParameter, actualParameter, NIL);
  4809. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4810. variableDesignator := GetTemp(system.addressType, FALSE);
  4811. Designate(actualParameter, src);
  4812. Designate(variableDesignator, dest);
  4813. IntermediateCode.MakeMemory(dest.op,addressType);
  4814. Emit(Mov(x.position, dest.op, src.op));
  4815. ReleaseOperand(dest);
  4816. IF src.tag.mode # IntermediateCode.Undefined THEN
  4817. tagDesignator := GetTemp(system.addressType, FALSE);
  4818. Designate(tagDesignator, dest);
  4819. IntermediateCode.MakeMemory(dest.op,addressType);
  4820. Emit(Mov(x.position, dest.op, src.op));
  4821. END;
  4822. ReleaseOperand(dest); ReleaseOperand(src);
  4823. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4824. END;
  4825. *)
  4826. formalParameter := formalParameter.nextParameter;
  4827. INC(i);
  4828. END;
  4829. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4830. IF resultDesignator # NIL THEN
  4831. returnDesignator := resultDesignator
  4832. ELSE
  4833. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4834. END;
  4835. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4836. END;
  4837. localVariable := procedure.procedureScope.firstVariable;
  4838. WHILE ~tooComplex & (localVariable # NIL) DO
  4839. variableDesignator := GetTemp(localVariable.type, FALSE);
  4840. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4841. localVariable := localVariable.nextVariable;
  4842. END;
  4843. IF ~tooComplex THEN
  4844. VisitStatementBlock(procedure.procedureScope.body);
  4845. SetLabel(currentInlineExit);
  4846. IF procedureType.returnType # NIL THEN
  4847. Designate(returnDesignator, result);
  4848. IF conditional THEN
  4849. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4850. ValueToCondition(result)
  4851. END;
  4852. END;
  4853. END;
  4854. currentMapper := prevMapper;
  4855. currentInlineExit := prevInlineExit;
  4856. currentIsInline := wasInline;
  4857. RETURN ~tooComplex
  4858. END InlineProcedureCall;
  4859. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4860. VAR
  4861. parameters: SyntaxTree.ExpressionList;
  4862. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4863. designator: SyntaxTree.Designator;
  4864. procedureType: SyntaxTree.ProcedureType;
  4865. formalParameter: SyntaxTree.Parameter;
  4866. operand, returnValue: Operand;
  4867. reg, size, mask, dest: IntermediateCode.Operand;
  4868. saved: RegisterEntry;
  4869. symbol: SyntaxTree.Symbol;
  4870. variable: SyntaxTree.Variable;
  4871. i, parametersSize, returnTypeSize : LONGINT;
  4872. structuredReturnType: BOOLEAN;
  4873. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4874. tempVariableDesignator: SyntaxTree.Designator;
  4875. gap, alignment: LONGINT; (*fld*)
  4876. (* TODO: remove unnecessary backup variables *)
  4877. oldResult: Operand;
  4878. oldCurrentScope: SyntaxTree.Scope;
  4879. oldArrayDestinationTag: IntermediateCode.Operand;
  4880. oldArrayDestinationDimension: LONGINT;
  4881. oldConstantDeclaration: SyntaxTree.Symbol;
  4882. oldDestination: IntermediateCode.Operand;
  4883. oldCurrentLoop: Label;
  4884. oldConditional: BOOLEAN;
  4885. oldTrueLabel, oldFalseLabel: Label;
  4886. oldLocked: BOOLEAN;
  4887. usedRegisters,oldUsedRegisters: RegisterEntry;
  4888. return: IntermediateCode.Operand;
  4889. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4890. arg: IntermediateCode.Operand;
  4891. dummy: IntermediateCode.Operand;
  4892. recordType: SyntaxTree.RecordType;
  4893. operatorSelectionProcedureOperand: Operand;
  4894. operatorSelectionProcedure: SyntaxTree.Procedure;
  4895. fingerPrint: SyntaxTree.FingerPrint;
  4896. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4897. identifierNumber: LONGINT;
  4898. parameterRegister: Backend.Registers;
  4899. parameterRegisters: LONGINT;
  4900. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4901. procedure: SyntaxTree.Procedure;
  4902. PROCEDURE BackupGlobalState;
  4903. BEGIN
  4904. oldResult := result;
  4905. oldCurrentScope := currentScope;
  4906. oldArrayDestinationTag := arrayDestinationTag;
  4907. oldArrayDestinationDimension := arrayDestinationDimension;
  4908. oldConstantDeclaration := constantDeclaration;
  4909. oldDestination := destination;
  4910. oldCurrentLoop := currentLoop;
  4911. oldConditional := conditional;
  4912. oldTrueLabel := trueLabel;
  4913. oldFalseLabel := falseLabel;
  4914. oldLocked := locked;
  4915. oldUsedRegisters := usedRegisters
  4916. END BackupGlobalState;
  4917. PROCEDURE RestoreGlobalState;
  4918. BEGIN
  4919. result := oldResult;
  4920. currentScope := oldCurrentScope;
  4921. arrayDestinationTag := oldArrayDestinationTag;
  4922. arrayDestinationDimension := oldArrayDestinationDimension;
  4923. constantDeclaration := oldConstantDeclaration;
  4924. destination := oldDestination;
  4925. currentLoop := oldCurrentLoop;
  4926. conditional := oldConditional;
  4927. trueLabel := oldTrueLabel;
  4928. falseLabel := oldFalseLabel;
  4929. locked := oldLocked;
  4930. usedRegisters := oldUsedRegisters
  4931. END RestoreGlobalState;
  4932. (** do preparations before parameter push for array-structured object types (ASOTs):
  4933. if ASOT is passed as VAR parameter:
  4934. - allocate temporary variable of math array type
  4935. - copy contents of ASOT to be passed to temporary variable
  4936. - use temporary variable as the actual parameter instead
  4937. - create and store a write-back call in a list (an index operator call that writes the contents of the temp. variable back into the ASOT)
  4938. **)
  4939. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4940. VAR
  4941. expression: SyntaxTree.Expression;
  4942. BEGIN
  4943. IF actualParameter IS SyntaxTree.Designator THEN
  4944. designator := actualParameter(SyntaxTree.Designator);
  4945. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4946. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4947. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4948. ASSERT(checker # NIL);
  4949. checker.SetCurrentScope(currentScope);
  4950. (* allocate temporary variable *)
  4951. ASSERT(actualParameter.type # NIL);
  4952. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4953. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4954. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4955. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4956. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4957. ASSERT(tempVariableDesignator.type # NIL);
  4958. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4959. (* copy math array stored in actual parameter to temporary variable *)
  4960. BackupGlobalState;
  4961. AssignMathArray(tempVariableDesignator, actualParameter);
  4962. RestoreGlobalState;
  4963. (* use temporary variable as actual parameter instead of the original one *)
  4964. actualParameter := tempVariableDesignator;
  4965. (* create write-back call and store it in linked list *)
  4966. (* create new list entry *)
  4967. IF firstWriteBackCall = NIL THEN
  4968. NEW(firstWriteBackCall);
  4969. currentWriteBackCall := firstWriteBackCall
  4970. ELSE
  4971. ASSERT(currentWriteBackCall # NIL);
  4972. NEW(currentWriteBackCall.next);
  4973. currentWriteBackCall := currentWriteBackCall.next
  4974. END;
  4975. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4976. ASSERT(expression.type = NIL);
  4977. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4978. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4979. (* prepare writeback for any other "normal" indexer *)
  4980. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4981. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4982. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4983. Assign(tempVariableDesignator, actualParameter);
  4984. actualParameter := tempVariableDesignator;
  4985. IF firstWriteBackCall = NIL THEN
  4986. NEW(firstWriteBackCall);
  4987. currentWriteBackCall := firstWriteBackCall
  4988. ELSE
  4989. ASSERT(currentWriteBackCall # NIL);
  4990. NEW(currentWriteBackCall.next);
  4991. currentWriteBackCall := currentWriteBackCall.next
  4992. END;
  4993. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  4994. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4995. END
  4996. END
  4997. END PrepareParameter;
  4998. BEGIN
  4999. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5000. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5001. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5002. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5003. IF InlineProcedureCall(x) THEN
  5004. RETURN
  5005. ELSE
  5006. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5007. END
  5008. END;
  5009. END;
  5010. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5011. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5012. dest := destination; destination := emptyOperand;
  5013. SaveRegisters();ReleaseUsedRegisters(saved);
  5014. parameters := x.parameters;
  5015. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5016. (* an operator is called *)
  5017. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5018. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5019. (* check if a dynamic operator call should be performed *)
  5020. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5021. ELSE
  5022. isCallOfDynamicOperator := FALSE
  5023. END;
  5024. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5025. Emit(Push(position, ap));
  5026. END;
  5027. alignment := procedureType.stackAlignment;
  5028. IF alignment > 1 THEN
  5029. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5030. Emit(Mov(position,reg, sp));
  5031. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5032. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5033. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5034. Emit(And(position,sp, sp, mask));
  5035. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5036. Emit(Push(position,reg));
  5037. (*
  5038. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5039. Emit(Mov(position,mem,reg));
  5040. *)
  5041. ReleaseIntermediateOperand(reg);
  5042. END;
  5043. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5044. (* align stack to 16-byte boundary *)
  5045. IntermediateCode.InitImmediate(mask,addressType,-16);
  5046. Emit(And(position,sp, sp, mask));
  5047. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5048. IF gap # 0 THEN
  5049. IntermediateCode.InitImmediate(size,addressType,gap);
  5050. Emit(Sub(position,sp,sp,size))
  5051. END;
  5052. END;
  5053. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5054. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5055. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5056. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5057. ELSE
  5058. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5059. END;
  5060. Evaluate(x.left, operand);
  5061. IF symbol IS SyntaxTree.Procedure THEN
  5062. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5063. Emit(Push(position,operand.tag));
  5064. ELSIF (procedureType.isDelegate) THEN
  5065. Emit(Push(position,operand.tag));
  5066. END;
  5067. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5068. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5069. Emit(Push(position,operand.tag));
  5070. END;
  5071. ELSE HALT(200);
  5072. END;
  5073. ReleaseIntermediateOperand(operand.tag);
  5074. operand.tag := emptyOperand;
  5075. (* determine if a structured return type is needed *)
  5076. structuredReturnType := StructuredReturnType(procedureType);
  5077. IF structuredReturnType THEN
  5078. IF resultDesignator # NIL THEN
  5079. d := resultDesignator;
  5080. ELSE
  5081. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5082. variable.SetUntraced(procedureType.hasUntracedReturn);
  5083. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5084. d.SetType(variable.type);
  5085. END;
  5086. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5087. Designate(d,returnValue);
  5088. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5089. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5090. Emit(Push(position,size));
  5091. Emit(Push(position,returnValue.op));
  5092. ReleaseOperand(returnValue);
  5093. ELSE
  5094. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5095. END;
  5096. END;
  5097. firstWriteBackCall := NIL; (* reset write-back call list *)
  5098. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5099. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5100. IF parameterRegister = NIL THEN parameterRegisters := 0
  5101. ELSE parameterRegisters := LEN(parameterRegister)
  5102. END;
  5103. passByRegister := parameterRegisters > 0;
  5104. registerNumber := 0;
  5105. formalParameter := procedureType.lastParameter;
  5106. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5107. actualParameter := parameters.GetExpression(i);
  5108. PrepareParameter(actualParameter, formalParameter);
  5109. IF passByRegister & (i < parameterRegisters) THEN
  5110. IF ~PassInRegister(formalParameter) THEN
  5111. Error(actualParameter.position,"cannot be passed by register")
  5112. ELSE
  5113. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5114. END;
  5115. INC(registerNumber);
  5116. ELSE
  5117. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5118. END;
  5119. formalParameter := formalParameter.prevParameter;
  5120. END;
  5121. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5122. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5123. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5124. END;
  5125. ELSE
  5126. hasDynamicOperands := FALSE;
  5127. formalParameter := procedureType.firstParameter;
  5128. FOR i := 0 TO parameters.Length() - 1 DO
  5129. actualParameter := parameters.GetExpression(i);
  5130. IF formalParameter # NIL THEN (* TENTATIVE *)
  5131. PrepareParameter(actualParameter, formalParameter);
  5132. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5133. ASSERT(i < 2);
  5134. hasDynamicOperands := TRUE;
  5135. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5136. ELSE
  5137. IF passByRegister & (registerNumber > 0) THEN
  5138. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5139. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5140. END;
  5141. passByRegister := FALSE;
  5142. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5143. END;
  5144. formalParameter := formalParameter.nextParameter;
  5145. END;
  5146. END;
  5147. END;
  5148. IF symbol IS SyntaxTree.Procedure THEN
  5149. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5150. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5151. Emit(Push(position,reg));
  5152. ReleaseIntermediateOperand(reg);
  5153. END;
  5154. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5155. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5156. parametersSize := ParametersSize(system,procedureType,FALSE);
  5157. END;
  5158. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5159. (*
  5160. dynamic operator overloading:
  5161. first push parameters, regularly:
  5162. [self]
  5163. par1
  5164. par2
  5165. then push parameters for GetOperator
  5166. identifier
  5167. ptr1
  5168. tag
  5169. ptr2
  5170. tag
  5171. call GetOperatorRuntimeProc
  5172. call Operator
  5173. *)
  5174. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5175. (* push ID *)
  5176. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5177. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5178. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5179. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5180. formalParameter := procedureType.firstParameter;
  5181. FOR i := 0 TO parameters.Length() - 1 DO
  5182. IF formalParameter.access # SyntaxTree.Hidden THEN
  5183. ASSERT(i < 2);
  5184. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5185. (* push pointer *)
  5186. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5187. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5188. (* add dereference *)
  5189. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5190. (*! better: do refer to stack above than using parameter backups !!*)
  5191. ReleaseIntermediateOperand(parameterBackups[i]);
  5192. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5193. END;
  5194. Emit(Push(position,parameterBackups[i]));
  5195. ReleaseIntermediateOperand(parameterBackups[i]);
  5196. (* push typetag *)
  5197. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5198. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5199. arg := TypeDescriptorAdr(recordType);
  5200. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5201. Emit(Push(position,arg));
  5202. ELSE
  5203. (* push 'NonPointer' *)
  5204. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5205. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5206. (* push fingerprint *)
  5207. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5208. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5209. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5210. END
  5211. END;
  5212. formalParameter := formalParameter.nextParameter
  5213. END;
  5214. (* for unary operators: complete the information for the second parameter *)
  5215. IF procedureType.numberParameters < 2 THEN
  5216. (* push 'NonPointer' *)
  5217. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5218. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5219. (* push 'NoType' *)
  5220. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5221. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5222. END;
  5223. (* call operator selection procedure *)
  5224. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5225. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5226. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5227. ReleaseOperand(operatorSelectionProcedureOperand);
  5228. (* use the address that the operator selection procedure returned as the target address of the call *)
  5229. InitOperand(operand, ModeValue);
  5230. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5231. Emit(Result(position,operand.op))
  5232. END
  5233. END;
  5234. ReleaseParameterRegisters();
  5235. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5236. Emit(Call(position,operand.op,0));
  5237. ELSE
  5238. Emit(Call(position,operand.op,parametersSize));
  5239. END;
  5240. ReleaseOperand(operand);
  5241. IF procedureType.noReturn THEN
  5242. EmitTrap(position,NoReturnTrap);
  5243. END;
  5244. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5245. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5246. Emit(Result(position,return));
  5247. END;
  5248. (* === return parameter space === *)
  5249. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5250. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5251. IF parametersSize < 32 THEN
  5252. (* allocated space for all parameter registers *)
  5253. parametersSize := 32
  5254. END;
  5255. size := IntermediateCode.Immediate(addressType,parametersSize);
  5256. Emit(Add(position,sp,sp,size))
  5257. END;
  5258. IF SysvABI(procedureType.callingConvention) THEN
  5259. IF passByRegister THEN
  5260. IF parameters.Length() > parameterRegisters THEN
  5261. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5262. ELSE
  5263. parametersSize := 0
  5264. END;
  5265. ELSE
  5266. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5267. INC( parametersSize, (-parametersSize) MOD 16 )
  5268. END;
  5269. IF parametersSize > 0 THEN
  5270. size := IntermediateCode.Immediate(addressType,parametersSize);
  5271. Emit(Add(position,sp,sp,size))
  5272. END;
  5273. END;
  5274. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5275. IF structuredReturnType THEN
  5276. (* stack pointer rewinding done by callee
  5277. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5278. Emit(Add(position,sp,sp,size));
  5279. *)
  5280. RestoreRegisters(saved);
  5281. InitOperand(result,ModeReference);
  5282. Symbol(variable,result);
  5283. ELSE
  5284. RestoreRegisters(saved);
  5285. InitOperand(result,ModeValue);
  5286. result.op := return;
  5287. END;
  5288. END;
  5289. IF alignment > 1 THEN
  5290. Emit(Pop(position,sp));
  5291. END;
  5292. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5293. Emit(Pop(position, ap));
  5294. END;
  5295. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5296. ValueToCondition(result);
  5297. END;
  5298. destination := dest;
  5299. (* perform all write-back calls in the list *)
  5300. BackupGlobalState;
  5301. currentWriteBackCall := firstWriteBackCall;
  5302. WHILE currentWriteBackCall # NIL DO
  5303. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5304. currentWriteBackCall := currentWriteBackCall.next
  5305. END;
  5306. RestoreGlobalState;
  5307. (* TENATIVE *)
  5308. (*
  5309. IF isOperatorCall THEN
  5310. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5311. END;
  5312. *)
  5313. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5314. END VisitProcedureCallDesignator;
  5315. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5316. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5317. td: SyntaxTree.Symbol;
  5318. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5319. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5320. BEGIN
  5321. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5322. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5323. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5324. variable.SetType(system.anyType);
  5325. scope.AddVariable(variable);
  5326. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5327. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5328. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5329. typeDeclaration.SetScope(module.module.moduleScope);
  5330. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5331. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5332. END;
  5333. RETURN hiddenPointerType;
  5334. END GetHiddenPointerType;
  5335. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5336. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5337. BEGIN
  5338. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5339. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5340. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5341. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5342. scope.AddVariable(variable);
  5343. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5344. variable.SetType(system.anyType);
  5345. scope.AddVariable(variable);
  5346. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5347. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5348. typeDeclaration.SetDeclaredType(delegatePointerType);
  5349. typeDeclaration.SetScope(module.module.moduleScope);
  5350. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5351. delegatePointerType.SetState(SyntaxTree.Resolved);
  5352. END;
  5353. RETURN delegatePointerType
  5354. END GetDelegateType;
  5355. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5356. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5357. such as in VAR a: RECORD ... END;
  5358. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5359. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5360. *)
  5361. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5362. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5363. BEGIN (* no code emission *)
  5364. source := NIL;
  5365. x := x.resolved;
  5366. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5367. x := GetHiddenPointerType();
  5368. ELSIF IsDelegate(x) THEN
  5369. x := GetDelegateType();
  5370. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5371. ELSE HALT(200);
  5372. END;
  5373. td := x.typeDeclaration;
  5374. IF td = NIL THEN
  5375. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5376. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5377. ASSERT(td # NIL);
  5378. END;
  5379. IF newObjectFile THEN
  5380. GetCodeSectionNameForSymbol(td,name);
  5381. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5382. meta.CheckTypeDeclaration(x);
  5383. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5384. ELSE
  5385. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5386. END;
  5387. IF backend.cooperative OR meta.simple THEN
  5388. offset := 0;
  5389. ELSE
  5390. IF x IS SyntaxTree.CellType THEN
  5391. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5392. IF baseRecord = NIL THEN
  5393. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5394. ELSE
  5395. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5396. END;
  5397. ELSE
  5398. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5399. END;
  5400. END;
  5401. ELSE
  5402. offset := 0;
  5403. source := module.allSections.FindBySymbol(td); (*TODO*)
  5404. IF source = NIL THEN
  5405. null := 0;
  5406. GetCodeSectionNameForSymbol(td,name);
  5407. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5408. Basic.SuffixSegmentedName (name, module.module.name);
  5409. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5410. IntermediateCode.InitImmediate(op,addressType,0);
  5411. source(IntermediateCode.Section).Emit(Data(position,op));
  5412. source.SetReferenced(FALSE)
  5413. ELSE
  5414. name := source.name;
  5415. END;
  5416. END;
  5417. RETURN td
  5418. END GetBackendType;
  5419. BEGIN
  5420. (*td := t.typeDeclaration;*)
  5421. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5422. (*
  5423. IF t IS SyntaxTree.PointerType THEN
  5424. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5425. ELSE
  5426. source := GetBackendType(t);
  5427. END;
  5428. *)
  5429. IF newObjectFile THEN
  5430. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5431. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5432. IntermediateCode.SetOffset(res,offset);
  5433. ELSE
  5434. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5435. END;
  5436. (*
  5437. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5438. make memory should be used when tag is used, not earlier
  5439. *)
  5440. RETURN res
  5441. END TypeDescriptorAdr;
  5442. (*
  5443. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5444. VAR result: IntermediateCode.Operand;
  5445. BEGIN
  5446. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5447. operand.tag := TypeDescriptorAdr(operand.type);
  5448. IntermediateCode.MakeMemory(operand.tag,addressType);
  5449. UseIntermediateOperand(operand.tag);
  5450. END;
  5451. END MakeTypeTag;
  5452. *)
  5453. PROCEDURE ProfilerInit;
  5454. VAR reg: IntermediateCode.Operand;
  5455. BEGIN
  5456. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5457. Emit(Call(position,reg,0));
  5458. END ProfilerInit;
  5459. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5460. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5461. BEGIN
  5462. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5463. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5464. THEN
  5465. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5466. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5467. Emit(Push(position,reg));
  5468. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5469. Emit(Push(position,reg));
  5470. StaticCallOperand(result,procedure);
  5471. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5472. ReleaseOperand(result);
  5473. END;
  5474. END ProfilerEnterExit;
  5475. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5476. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5477. BEGIN
  5478. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5479. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5480. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5481. profileInit.Emit(Push(position,reg));
  5482. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5483. profileInit.Emit(Push(position,reg));
  5484. NEW(string, LEN(name)); COPY(name, string^);
  5485. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5486. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5487. sv.SetType(type);
  5488. Designate(sv,result);
  5489. profileInit.Emit(Push(position,result.tag));
  5490. profileInit.Emit(Push(position,result.op));
  5491. StaticCallOperand(result,procedure);
  5492. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5493. ReleaseOperand(result);
  5494. END;
  5495. END ProfilerAddProcedure;
  5496. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5497. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5498. BEGIN
  5499. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5500. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5501. profileInit.Emit(Push(position,reg));
  5502. profileInitPatchPosition := profileInit.pc;
  5503. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5504. NEW(string, LEN(name)); COPY(name, string^);
  5505. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5506. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5507. sv.SetType(type);
  5508. Designate(sv,result);
  5509. profileInit.Emit(Push(position,result.tag));
  5510. profileInit.Emit(Push(position,result.op));
  5511. StaticCallOperand(result,procedure);
  5512. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5513. ReleaseOperand(result);
  5514. END;
  5515. END ProfilerAddModule;
  5516. PROCEDURE ProfilerPatchInit;
  5517. VAR reg: IntermediateCode.Operand;
  5518. BEGIN
  5519. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5520. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5521. EmitLeave(profileInit,position,NIL,0);
  5522. profileInit.Emit(Exit(position,0,0,0));
  5523. END ProfilerPatchInit;
  5524. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5525. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5526. VAR
  5527. id: LONGINT;
  5528. leftType, rightType: SyntaxTree.Type;
  5529. procedureType: SyntaxTree.ProcedureType;
  5530. runtimeProcedure: SyntaxTree.Procedure;
  5531. runtimeProcedureOperand, operatorOperand: Operand;
  5532. kind: SET;
  5533. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5534. VAR
  5535. arg: IntermediateCode.Operand;
  5536. recordType: SyntaxTree.RecordType;
  5537. fingerPrint: SyntaxTree.FingerPrint;
  5538. BEGIN
  5539. IF type = NIL THEN
  5540. (* no type: push 'NoType' *)
  5541. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5542. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5543. ELSIF IsStrictlyPointerToRecord(type) THEN
  5544. (* pointer to record type: push typetag *)
  5545. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5546. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5547. arg := TypeDescriptorAdr(recordType);
  5548. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5549. ELSE
  5550. (* non-pointer to record type: push fingerprint *)
  5551. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5552. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5553. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5554. END;
  5555. operatorInitializationCodeSection.Emit(Push(position,arg))
  5556. END PushTypeInfo;
  5557. BEGIN
  5558. ASSERT(operatorInitializationCodeSection # NIL);
  5559. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5560. procedureType := operator.type(SyntaxTree.ProcedureType);
  5561. (* determine types *)
  5562. leftType := procedureType.firstParameter.type;
  5563. IF procedureType.numberParameters = 2 THEN
  5564. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5565. rightType := procedureType.firstParameter.nextParameter.type;
  5566. ELSE
  5567. rightType := NIL
  5568. END;
  5569. (* determine operator kind *)
  5570. IF IsStrictlyPointerToRecord(leftType) THEN
  5571. kind := {LhsIsPointer}
  5572. ELSE
  5573. kind := {}
  5574. END;
  5575. IF IsStrictlyPointerToRecord(rightType) THEN
  5576. kind := kind + {RhsIsPointer}
  5577. END;
  5578. IF kind # {} THEN (* TODO: to be removed later on *)
  5579. (* at least one of the types is a pointer to record *)
  5580. (* emit a code that registers this specific operator in the runtime *)
  5581. dump := operatorInitializationCodeSection.comments;
  5582. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5583. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5584. (* push ID *)
  5585. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5586. id := Global.GetSymbol(module.module.case, operator.name);
  5587. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5588. (* push kind *)
  5589. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5590. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5591. (* push type infos *)
  5592. PushTypeInfo(leftType);
  5593. PushTypeInfo(rightType);
  5594. (* push operator address *)
  5595. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5596. StaticCallOperand(operatorOperand, operator);
  5597. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5598. ReleaseOperand(operatorOperand);
  5599. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5600. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5601. ReleaseOperand(runtimeProcedureOperand)
  5602. END
  5603. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5604. END
  5605. END RegisterDynamicOperator;
  5606. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5607. VAR
  5608. traceModule: SyntaxTree.Module;
  5609. procedure: SyntaxTree.Procedure;
  5610. procedureVariable: SyntaxTree.Variable;
  5611. s,msg: Basic.MessageString;
  5612. res: Operand;
  5613. i: LONGINT;
  5614. sv: SyntaxTree.StringValue;
  5615. type: SyntaxTree.Type;
  5616. recordType: SyntaxTree.RecordType;
  5617. printout: Printout.Printer;
  5618. stringWriter: Streams.StringWriter;
  5619. expression: SyntaxTree.Expression;
  5620. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5621. BEGIN
  5622. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5623. IF procedure = NIL THEN
  5624. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5625. END;
  5626. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5627. s := "procedure ";
  5628. Strings.Append(s,backend.traceModuleName);
  5629. Strings.Append(s,".");
  5630. Strings.Append(s,procedureName);
  5631. Strings.Append(s," not present");
  5632. Error(position,s);
  5633. RETURN FALSE
  5634. ELSE
  5635. RETURN TRUE
  5636. END;
  5637. END GetProcedure;
  5638. PROCEDURE CallProcedure;
  5639. VAR size: LONGINT;
  5640. BEGIN
  5641. IF procedure # NIL THEN
  5642. StaticCallOperand(result,procedure);
  5643. size := ProcedureParametersSize(system,procedure);
  5644. ELSE
  5645. Symbol(procedureVariable, result);
  5646. LoadValue(result, procedureVariable.type.resolved);
  5647. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5648. END;
  5649. Emit(Call(position,result.op,size));
  5650. END CallProcedure;
  5651. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5652. VAR res: Operand; string: SyntaxTree.String;
  5653. BEGIN
  5654. IF GetProcedure("String") THEN
  5655. NEW(string, LEN(s)); COPY(s, string^);
  5656. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5657. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5658. sv.SetType(type);
  5659. Designate(sv,res);
  5660. Emit(Push(position,res.tag));
  5661. Emit(Push(position,res.op));
  5662. ReleaseOperand(res);
  5663. CallProcedure;
  5664. END;
  5665. END String;
  5666. PROCEDURE Integer(op: IntermediateCode.Operand);
  5667. BEGIN
  5668. IF GetProcedure("Int") THEN
  5669. Emit(Push(position,op));
  5670. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5671. CallProcedure;
  5672. END;
  5673. END Integer;
  5674. PROCEDURE Float(op: IntermediateCode.Operand);
  5675. BEGIN
  5676. IF GetProcedure("HIntHex") THEN
  5677. Emit(Push(position,op));
  5678. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5679. CallProcedure;
  5680. END;
  5681. END Float;
  5682. PROCEDURE Set(op: IntermediateCode.Operand);
  5683. BEGIN
  5684. IF GetProcedure("Set") THEN
  5685. Emit(Push(position,op));
  5686. (*
  5687. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5688. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5689. *)
  5690. CallProcedure;
  5691. END;
  5692. END Set;
  5693. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5694. BEGIN
  5695. IF GetProcedure("Boolean") THEN
  5696. Emit(Push(position,op));
  5697. CallProcedure;
  5698. END;
  5699. END Boolean;
  5700. PROCEDURE Char(op: IntermediateCode.Operand);
  5701. BEGIN
  5702. IF GetProcedure("Char") THEN
  5703. Emit(Push(position,op));
  5704. CallProcedure;
  5705. END;
  5706. END Char;
  5707. PROCEDURE Address(op: IntermediateCode.Operand);
  5708. BEGIN
  5709. IF GetProcedure("Address") THEN
  5710. Emit(Push(position,op));
  5711. CallProcedure;
  5712. END;
  5713. END Address;
  5714. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5715. VAR len: IntermediateCode.Operand;
  5716. BEGIN
  5717. IF GetProcedure("String") THEN
  5718. len := GetArrayLength(type, op.tag);
  5719. Emit(Push(position,len));
  5720. ReleaseIntermediateOperand(len);
  5721. Emit(Push(position,op.op));
  5722. CallProcedure;
  5723. END;
  5724. END StringOperand;
  5725. PROCEDURE Ln;
  5726. BEGIN
  5727. IF GetProcedure("Ln") THEN
  5728. CallProcedure;
  5729. END;
  5730. END Ln;
  5731. BEGIN
  5732. IF backend.traceModuleName = "" THEN RETURN END;
  5733. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5734. IF GetProcedure("Enter") THEN
  5735. CallProcedure
  5736. END;
  5737. NEW(stringWriter,LEN(s));
  5738. FOR i := 0 TO x.Length()-1 DO
  5739. msg := "";
  5740. expression := x.GetExpression(i);
  5741. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5742. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5743. ELSE
  5744. Global.GetModuleName(module.module, s);
  5745. END;
  5746. IF i = 0 THEN
  5747. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5748. stringWriter.String(":");
  5749. END;
  5750. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5751. IF ~(expression IS SyntaxTree.StringValue) THEN
  5752. printout.Expression(expression);
  5753. stringWriter.Get(s);
  5754. Strings.Append(msg,s);
  5755. Strings.Append(msg,"= ");
  5756. ELSE stringWriter.Get(s); (* remove from string writer *)
  5757. Strings.Append(msg, s);
  5758. END;
  5759. String(msg);
  5760. IF SemanticChecker.IsStringType(expression.type) THEN
  5761. Designate(expression,res);
  5762. StringOperand(res, expression.type);
  5763. ELSE
  5764. Evaluate(expression,res);
  5765. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5766. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5767. Convert(res.op,int64);
  5768. END;
  5769. Integer(res.op);
  5770. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5771. Boolean(res.op);
  5772. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5773. Set(res.op);
  5774. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5775. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5776. Convert(res.op,float64);
  5777. END;
  5778. Float(res.op);
  5779. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5780. Char(res.op);
  5781. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5782. Address(res.op);String("H");
  5783. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5784. Address(res.op);String("H");
  5785. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5786. Address(res.op);String("H");
  5787. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5788. Address(res.op);String("H");
  5789. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5790. String("NIL");
  5791. ELSE HALT(200);
  5792. END;
  5793. END;
  5794. ReleaseOperand(res);
  5795. String("; ");
  5796. END;
  5797. IF GetProcedure("Exit") THEN
  5798. CallProcedure
  5799. ELSE
  5800. Ln;
  5801. END;
  5802. END;
  5803. END SystemTrace;
  5804. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5805. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5806. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5807. BEGIN
  5808. type := type.resolved;
  5809. IF type IS SyntaxTree.RecordType THEN
  5810. WITH type: SyntaxTree.RecordType DO
  5811. baseType := type.baseType;
  5812. IF baseType # NIL THEN
  5813. baseType := baseType.resolved;
  5814. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5815. InitFields(baseType,adr,offset);
  5816. END;
  5817. variable := type.recordScope.firstVariable;
  5818. WHILE variable # NIL DO
  5819. IF variable.initializer # NIL THEN
  5820. Evaluate(variable.initializer,initializerOp);
  5821. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5822. Emit(Mov(position,mem,initializerOp.op));
  5823. ReleaseOperand(initializerOp);
  5824. ReleaseIntermediateOperand(mem);
  5825. END;
  5826. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5827. variable := variable.nextVariable
  5828. END;
  5829. END;
  5830. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5831. WITH type: SyntaxTree.ArrayType DO
  5832. IF type.form = SyntaxTree.Static THEN
  5833. baseType := type.arrayBase;
  5834. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5835. FOR i := 0 TO type.staticLength-1 DO
  5836. InitFields(baseType,adr,offset+i*size);
  5837. END;
  5838. END;
  5839. END;
  5840. ELSIF type IS SyntaxTree.MathArrayType THEN
  5841. WITH type: SyntaxTree.MathArrayType DO
  5842. IF type.form = SyntaxTree.Open THEN
  5843. dim := DynamicDim(type);
  5844. imm := IntermediateCode.Immediate(addressType,dim);
  5845. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5846. baseType := SemanticChecker.ArrayBase(type,dim);
  5847. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5848. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5849. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5850. ReleaseIntermediateOperand(imm);
  5851. (* flags remain empty (=0) for open array *)
  5852. ELSIF type.form = SyntaxTree.Static THEN
  5853. baseType := type.arrayBase;
  5854. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5855. ASSERT(type.staticLength < 1024*1024*1024);
  5856. FOR i := 0 TO type.staticLength-1 DO
  5857. InitFields(baseType,adr,offset+i*size);
  5858. END;
  5859. END;
  5860. END;
  5861. END;
  5862. END InitFields;
  5863. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5864. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5865. BEGIN
  5866. type := variable.type.resolved;
  5867. IF (type IS SyntaxTree.MathArrayType) THEN
  5868. WITH type: SyntaxTree.MathArrayType DO
  5869. IF type.form = SyntaxTree.Open THEN
  5870. Symbol(variable,operand);
  5871. InitFields(type, operand.tag,0);
  5872. ELSIF type.form = SyntaxTree.Tensor THEN
  5873. Symbol(variable, operand);
  5874. MakeMemory(tmp,operand.op,addressType,0);
  5875. ReleaseOperand(operand);
  5876. Emit(Mov(position,tmp, nil ) );
  5877. ReleaseIntermediateOperand(tmp);
  5878. END;
  5879. END;
  5880. ELSE
  5881. Symbol(variable,operand);
  5882. IF variable.initializer # NIL THEN
  5883. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5884. reference.SetType(variable.type.resolved);
  5885. reference.SetAssignable(TRUE);
  5886. Assign(reference,variable.initializer);
  5887. END;
  5888. InitFields(type, operand.op,0);
  5889. ReleaseOperand(operand);
  5890. END;
  5891. END InitVariable;
  5892. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5893. VAR end: Label;
  5894. BEGIN
  5895. IF type.form = SyntaxTree.Tensor THEN
  5896. InitOperand(result,ModeValue);
  5897. ReuseCopy(result.op,base);
  5898. end := NewLabel();
  5899. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5900. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5901. SetLabel(end);
  5902. Convert(result.op,int32);
  5903. ELSE
  5904. InitOperand(result,ModeValue);
  5905. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5906. END
  5907. END MathArrayDim;
  5908. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5909. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5910. BEGIN
  5911. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5912. MakeMemory(mem,base,addressType,offset);
  5913. Emit(Mov(position,mem,value));
  5914. ReleaseIntermediateOperand(mem);
  5915. END PutMathArrayField;
  5916. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5917. VAR mem: IntermediateCode.Operand;
  5918. BEGIN
  5919. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5920. MakeMemory(mem,base,addressType,offset);
  5921. Emit(Mov(position,mem,value));
  5922. ReleaseIntermediateOperand(mem);
  5923. END PutMathArrayFieldOffset;
  5924. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5925. BEGIN
  5926. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5927. MakeMemory(value,base,addressType,offset);
  5928. END GetMathArrayField;
  5929. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5930. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5931. BEGIN
  5932. IF incr THEN
  5933. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5934. ELSE
  5935. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5936. END;
  5937. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5938. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5939. ELSE
  5940. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5941. Emit(Mov(position,reg,dim));
  5942. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5943. Emit(Add(position,reg,reg,base));
  5944. MakeMemory(mem, reg, addressType, offset);
  5945. ReleaseIntermediateOperand(reg);
  5946. Emit(Mov(position,mem,value));
  5947. ReleaseIntermediateOperand(mem);
  5948. END;
  5949. END PutMathArrayLenOrIncr;
  5950. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5951. BEGIN
  5952. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5953. END PutMathArrayLength;
  5954. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5955. BEGIN
  5956. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5957. END PutMathArrayIncrement;
  5958. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5959. BEGIN
  5960. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5961. END GetMathArrayIncrement;
  5962. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5963. BEGIN
  5964. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5965. END GetMathArrayLength;
  5966. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5967. VAR dimOp: IntermediateCode.Operand;
  5968. BEGIN
  5969. dimOp := IntermediateCode.Immediate(int32, dim);
  5970. GetMathArrayLength(type, operand, dimOp, check, result);
  5971. END GetMathArrayLengthAt;
  5972. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5973. VAR dimOp: IntermediateCode.Operand;
  5974. BEGIN
  5975. dimOp := IntermediateCode.Immediate(int32, dim);
  5976. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5977. END GetMathArrayIncrementAt;
  5978. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5979. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5980. offset: LONGINT;
  5981. BEGIN
  5982. IF increment THEN
  5983. offset := MathIncrOffset;
  5984. ELSE
  5985. offset := MathLenOffset;
  5986. END;
  5987. INC(offset,operand.dimOffset*2);
  5988. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5989. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5990. END;
  5991. (* static dimension *)
  5992. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5993. IF check & (type.form = SyntaxTree.Tensor) THEN
  5994. DimensionCheck(operand.tag,dim,BrltL);
  5995. END;
  5996. val := SHORT(dim.intValue);
  5997. IF type.form # SyntaxTree.Tensor THEN
  5998. t := SemanticChecker.ArrayBase(type,val);
  5999. type := t.resolved(SyntaxTree.MathArrayType);
  6000. IF type.form = SyntaxTree.Static THEN
  6001. IF increment THEN
  6002. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6003. ELSE
  6004. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6005. END;
  6006. InitOperand(result,ModeValue);
  6007. result.op := res;
  6008. RETURN;
  6009. END;
  6010. END;
  6011. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6012. MakeMemory(res,operand.tag,addressType,offset);
  6013. (*
  6014. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6015. *)
  6016. InitOperand(result,ModeValue);
  6017. result.op := res;
  6018. ELSE
  6019. Convert(dim,addressType);
  6020. IF check THEN
  6021. IF type.form = SyntaxTree.Tensor THEN
  6022. DimensionCheck(operand.tag,dim,BrltL);
  6023. ELSIF isUnchecked THEN (* do nothing *)
  6024. ELSE
  6025. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6026. END;
  6027. END;
  6028. end := NewLabel(); next := NIL;
  6029. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6030. Emit(Mov(position,res,dim));
  6031. Convert(res,int32);
  6032. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6033. WHILE t IS SyntaxTree.MathArrayType DO
  6034. type := t(SyntaxTree.MathArrayType);
  6035. IF type.form = SyntaxTree.Static THEN
  6036. imm := IntermediateCode.Immediate(int32,val);
  6037. next := NewLabel();
  6038. BrneL(next,imm,res);
  6039. IF increment THEN
  6040. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6041. ELSE
  6042. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6043. END;
  6044. Emit(MovReplace(position,res,imm));
  6045. BrL(end);
  6046. ELSE hasDynamicPart := TRUE;
  6047. END;
  6048. t := type.arrayBase.resolved;
  6049. val := val + 1;
  6050. IF next # NIL THEN SetLabel(next) END;
  6051. END;
  6052. IF hasDynamicPart THEN
  6053. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6054. Emit(Mov(position,res2,dim));
  6055. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6056. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6057. Emit(Add(position,res2,res2,imm));
  6058. Emit(Add(position,res2,res2,operand.tag));
  6059. IntermediateCode.MakeMemory(res2,int32);
  6060. Emit(MovReplace(position,res,res2));
  6061. ReleaseIntermediateOperand(res2);
  6062. END;
  6063. SetLabel(end);
  6064. Convert(res,int32);
  6065. InitOperand(result,ModeValue);
  6066. result.op := res;
  6067. END;
  6068. END MathArrayLenOrIncr;
  6069. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6070. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6071. offset: LONGINT;
  6072. BEGIN
  6073. offset := operand.dimOffset+DynamicDim(type)-1;
  6074. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6075. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6076. val := SHORT(dim.intValue);
  6077. t := SemanticChecker.ArrayBase(type,val);
  6078. type := t.resolved(SyntaxTree.ArrayType);
  6079. IF type.form = SyntaxTree.Static THEN
  6080. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6081. ELSE
  6082. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6083. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6084. END;
  6085. UseIntermediateOperand(res);
  6086. InitOperand(result,ModeValue);
  6087. result.op := res;
  6088. ELSE
  6089. Convert(dim,addressType);
  6090. IF ~isUnchecked THEN
  6091. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6092. END;
  6093. end := NewLabel(); next := NIL;
  6094. (* ReuseCopy(dim,res); *)
  6095. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6096. Emit(Mov(position,res,dim));
  6097. Convert(res,int32);
  6098. Convert(res,int32);
  6099. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6100. WHILE t IS SyntaxTree.ArrayType DO
  6101. type := t(SyntaxTree.ArrayType);
  6102. IF type.form = SyntaxTree.Static THEN
  6103. imm := IntermediateCode.Immediate(int32,val);
  6104. next := NewLabel();
  6105. BrneL(next,imm,res);
  6106. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6107. Emit(MovReplace(position,res,imm));
  6108. BrL(end);
  6109. ELSE hasDynamicPart := TRUE;
  6110. END;
  6111. t := type.arrayBase.resolved;
  6112. val := val + 1;
  6113. IF next # NIL THEN SetLabel(next) END;
  6114. END;
  6115. IF hasDynamicPart THEN
  6116. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6117. Convert(res2,addressType);
  6118. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6119. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6120. Emit(Sub(position,res2,imm,res2));
  6121. Emit(Add(position,res2,res2,operand.tag));
  6122. IntermediateCode.MakeMemory(res2,int32);
  6123. Emit(MovReplace(position,res,res2));
  6124. ReleaseIntermediateOperand(res2);
  6125. END;
  6126. SetLabel(end);
  6127. Convert(res,int32);
  6128. InitOperand(result,ModeValue);
  6129. result.op := res;
  6130. END;
  6131. END ArrayLen;
  6132. (**
  6133. create a temporary variable in current scope
  6134. **)
  6135. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6136. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6137. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6138. BEGIN
  6139. IF ~register THEN
  6140. variable := temporaries.GetFreeVariable(type, index);
  6141. ELSE
  6142. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6143. END;
  6144. scope := currentScope;
  6145. IF variable = NIL THEN
  6146. COPY("@hiddenIRVar",string);
  6147. Basic.AppendNumber(string,index);
  6148. name := SyntaxTree.NewIdentifier(string);
  6149. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6150. variable.SetType(type);
  6151. variable.SetAccess(SyntaxTree.Hidden);
  6152. IF ~register THEN
  6153. temporaries.AddVariable(variable);
  6154. IF scope.lastVariable # NIL THEN
  6155. offset := scope.lastVariable.offsetInBits;
  6156. ELSE
  6157. offset := 0;
  6158. END;
  6159. DEC(offset,system.SizeOf(variable.type));
  6160. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6161. variable(SyntaxTree.Variable).SetOffset(offset);
  6162. scope.AddVariable(variable(SyntaxTree.Variable));
  6163. scope.EnterSymbol(variable, duplicate);
  6164. ASSERT(~duplicate);
  6165. InitVariable(variable(SyntaxTree.Variable));
  6166. ELSE
  6167. variable.SetUseRegister(TRUE);
  6168. variable(SyntaxTree.Variable).SetOffset(0);
  6169. END;
  6170. ELSE
  6171. InitVariable(variable(SyntaxTree.Variable));
  6172. (*
  6173. ASSERT(variable.type.resolved = type.resolved)
  6174. *)
  6175. END;
  6176. RETURN variable(SyntaxTree.Variable)
  6177. END GetTemporaryVariable;
  6178. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6179. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6180. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6181. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6182. i: LONGINT; duplicate: BOOLEAN;
  6183. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6184. VAR variable: SyntaxTree.Variable;
  6185. BEGIN
  6186. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6187. variable.SetType(type);
  6188. recordScope.AddVariable(variable);
  6189. END AddVariable;
  6190. BEGIN
  6191. name := "@ArrayDescriptor";
  6192. Basic.AppendNumber(name,dimensions);
  6193. identifier := SyntaxTree.NewIdentifier(name);
  6194. parentScope := module.module.moduleScope;
  6195. symbol := parentScope.FindSymbol(identifier);
  6196. IF symbol # NIL THEN
  6197. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6198. type := typeDeclaration.declaredType;
  6199. ELSE
  6200. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6201. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6202. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6203. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6204. recordType.SetTypeDeclaration(typeDeclaration);
  6205. recordType.SetState(SyntaxTree.Resolved);
  6206. typeDeclaration.SetDeclaredType(recordType);
  6207. AddVariable("@ptr",system.anyType);
  6208. AddVariable("@adr",system.addressType);
  6209. AddVariable("@flags",system.addressType);
  6210. AddVariable("@dim",system.addressType);
  6211. AddVariable("@elementSize",system.addressType);
  6212. FOR i := 0 TO dimensions-1 DO
  6213. name := "@len";
  6214. Basic.AppendNumber(name,i);
  6215. AddVariable(name,system.addressType);
  6216. name := "@incr";
  6217. Basic.AppendNumber(name,i);
  6218. AddVariable(name,system.addressType);
  6219. END;
  6220. parentScope.AddTypeDeclaration(typeDeclaration);
  6221. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6222. ASSERT(~duplicate);
  6223. type := recordType;
  6224. END;
  6225. RETURN type
  6226. END GetMathArrayDescriptorType;
  6227. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6228. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6229. BEGIN
  6230. type := GetMathArrayDescriptorType(dimensions);
  6231. Emit(Push(position,op.op));
  6232. (* push type descriptor *)
  6233. reg := TypeDescriptorAdr(type);
  6234. IF ~newObjectFile THEN
  6235. IntermediateCode.MakeMemory(reg,addressType);
  6236. END;
  6237. Emit(Push(position,reg));
  6238. ReleaseIntermediateOperand(reg);
  6239. (* push realtime flag: false by default *)
  6240. Emit(Push(position,false));
  6241. CallThis(position,"Heaps","NewRec",3);
  6242. END NewMathArrayDescriptor;
  6243. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6244. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6245. BEGIN
  6246. NEW(string, LEN(s)); COPY(s, string^);
  6247. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6248. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6249. sv.SetType(type);
  6250. Designate(sv,res);
  6251. Emit(Push(position,res.tag));
  6252. Emit(Push(position,res.op));
  6253. ReleaseOperand(res);
  6254. END PushConstString;
  6255. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6256. BEGIN
  6257. IF b THEN
  6258. Emit(Push(Basic.invalidPosition, true));
  6259. ELSE
  6260. Emit(Push(Basic.invalidPosition, false));
  6261. END;
  6262. END PushConstBoolean;
  6263. PROCEDURE PushConstSet(v: SET);
  6264. VAR value: SyntaxTree.Value; op: Operand;
  6265. BEGIN
  6266. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6267. value.SetType(system.setType);
  6268. Evaluate(value, op);
  6269. Emit(Push(Basic.invalidPosition, op.op));
  6270. ReleaseOperand(op);
  6271. END PushConstSet;
  6272. PROCEDURE PushConstInteger(v: LONGINT);
  6273. VAR value: SyntaxTree.Value; op: Operand;
  6274. BEGIN
  6275. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6276. value.SetType(system.longintType);
  6277. Evaluate(value, op);
  6278. Emit(Push(Basic.invalidPosition, op.op));
  6279. ReleaseOperand(op);
  6280. END PushConstInteger;
  6281. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6282. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6283. section: IntermediateCode.Section;
  6284. BEGIN
  6285. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6286. Global.GetSymbolSegmentedName(symbol, name);
  6287. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6288. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6289. procedure.SetScope(moduleScope);
  6290. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6291. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6292. procedure.SetAccess(SyntaxTree.Hidden);
  6293. currentScope := procedureScope;
  6294. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6295. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6296. RETURN section;
  6297. END OpenInitializer;
  6298. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6299. BEGIN
  6300. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6301. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6302. section := prev;
  6303. END CloseInitializer;
  6304. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6305. VAR name: SyntaxTree.IdentifierString;
  6306. variable: SyntaxTree.Variable;
  6307. parameter: SyntaxTree.Parameter;
  6308. type: SyntaxTree.Type;
  6309. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6310. BEGIN
  6311. InitOperand(op,ModeReference);
  6312. op.op := fp;
  6313. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6314. Dereference(op, x, FALSE);
  6315. result := op;
  6316. Symbol(symbol, op);
  6317. END Field;
  6318. PROCEDURE Direction(direction: LONGINT): SET;
  6319. BEGIN
  6320. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6321. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6322. ELSE HALT(100);
  6323. END;
  6324. END Direction;
  6325. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6326. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6327. BEGIN
  6328. Field(port, op);
  6329. ToMemory(op.op,addressType,0);
  6330. Emit(Push(Basic.invalidPosition, op.op));
  6331. ReleaseOperand(op);
  6332. Basic.GetString(modifier.identifier, name);
  6333. PushConstString(name);
  6334. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6335. ASSERT(SemanticChecker.IsStringType(value.type));
  6336. Designate(value, op);
  6337. Emit(Push(modifier.position, op.tag));
  6338. Emit(Push(modifier.position, op.op));
  6339. ReleaseOperand(op);
  6340. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6341. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6342. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6343. Evaluate(value, op);
  6344. Emit(Push(modifier.position, op.op));
  6345. ReleaseOperand(op);
  6346. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6347. ELSE
  6348. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6349. END;
  6350. END AddPortProperty;
  6351. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6352. VAR modifier: SyntaxTree.Modifier;
  6353. BEGIN
  6354. modifier := parameter.modifiers;
  6355. WHILE modifier # NIL DO
  6356. AddPortProperty(parameter,modifier, modifier.expression);
  6357. modifier := modifier.nextModifier;
  6358. END;
  6359. END AddPortProperties;
  6360. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6361. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6362. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6363. PROCEDURE PushLens(type: SyntaxTree.Type);
  6364. BEGIN
  6365. IF IsSemiDynamicArray(type) THEN
  6366. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6367. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6368. Emit(Push(Basic.invalidPosition, op.op));
  6369. ReleaseOperand(op);
  6370. INC(dim);
  6371. ELSIF IsStaticArray(type) THEN
  6372. len := len * type(SyntaxTree.ArrayType).staticLength;
  6373. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6374. INC(dim);
  6375. ELSE
  6376. baseType := type;
  6377. END;
  6378. END PushLens;
  6379. BEGIN
  6380. (* cell *)
  6381. IF parameter.type IS SyntaxTree.ArrayType THEN
  6382. type := parameter.type;
  6383. dim := 0;
  6384. len := 1;
  6385. PushLens(type);
  6386. portType := baseType.resolved(SyntaxTree.PortType);
  6387. ELSE
  6388. portType := parameter.type(SyntaxTree.PortType);
  6389. END;
  6390. PushSelfPointer();
  6391. (* port / array of ports *)
  6392. IF IsStaticArray(type) THEN
  6393. PushConstInteger(len);
  6394. END;
  6395. Field(parameter, op);
  6396. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6397. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6398. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6399. Designate(d, op);*)
  6400. Emit(Push(Basic.invalidPosition, op.op));
  6401. ReleaseOperand(op);
  6402. (* name *)
  6403. PushConstString(name);
  6404. (* inout *)
  6405. PushConstSet(Direction(portType.direction));
  6406. (* width *)
  6407. PushConstInteger(portType.sizeInBits);
  6408. IF parameter.type IS SyntaxTree.PortType THEN
  6409. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6410. AddPortProperties(parameter);
  6411. ELSIF IsStaticArray(type)THEN
  6412. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6413. ELSIF IsSemiDynamicArray(type) THEN
  6414. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6415. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6416. Emit(Add(position,reg, sp, size));
  6417. (* dim *)
  6418. PushConstInteger(dim);
  6419. (* len array *)
  6420. Emit(Push(position, reg));
  6421. ReleaseIntermediateOperand(reg);
  6422. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6423. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6424. Emit(Add(position, sp,sp, size));
  6425. ELSE
  6426. HALT(100);
  6427. END;
  6428. END Parameter;
  6429. BEGIN
  6430. IF backend.cellsAreObjects THEN
  6431. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6432. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6433. END;
  6434. parameter := x.firstParameter;
  6435. WHILE (parameter # NIL) DO
  6436. type := parameter.type.resolved;
  6437. WHILE (type IS SyntaxTree.ArrayType) DO
  6438. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6439. END;
  6440. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6441. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6442. Parameter(name,parameter);
  6443. END;
  6444. parameter := parameter.nextParameter;
  6445. END;
  6446. ELSE HALT(200)
  6447. END;
  6448. END AddPorts;
  6449. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6450. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6451. BEGIN
  6452. Symbol(cell,op);
  6453. ToMemory(op.op,addressType,0);
  6454. Emit(Push(position,op.op));
  6455. ReleaseOperand(op);
  6456. property.GetName(name);
  6457. (* does not work when inheritance is used:
  6458. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6459. *)
  6460. PushConstString(name);
  6461. IF (value # NIL) THEN
  6462. ASSERT(
  6463. SemanticChecker.IsStringType(property.type)
  6464. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6465. OR (property.type.resolved IS SyntaxTree.FloatType)
  6466. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6467. OR (property.type.resolved IS SyntaxTree.SetType)
  6468. );
  6469. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6470. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6471. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6472. Designate(d, op);
  6473. IF SemanticChecker.IsStringType(property.type) THEN
  6474. Emit(Push(Basic.invalidPosition, op.tag))
  6475. END;
  6476. Emit(Push(Basic.invalidPosition, op.op));
  6477. ReleaseOperand(op);
  6478. END;
  6479. IF value = NIL THEN
  6480. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6481. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6482. ASSERT(SemanticChecker.IsStringType(value.type));
  6483. Designate(value, op);
  6484. PushString(op, value.type);
  6485. ReleaseOperand(op);
  6486. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6487. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6488. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6489. Evaluate(value, op);
  6490. Emit(Push(property.position, op.op));
  6491. ReleaseOperand(op);
  6492. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6493. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6494. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6495. Evaluate(value, op);
  6496. Emit(Push(property.position, op.op));
  6497. ReleaseOperand(op);
  6498. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6499. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6500. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6501. Evaluate(value, op);
  6502. Emit(Push(property.position, op.op));
  6503. ReleaseOperand(op);
  6504. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6505. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6506. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6507. Evaluate(value, op);
  6508. Emit(Push(property.position, op.op));
  6509. ReleaseOperand(op);
  6510. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6511. ELSE
  6512. HALT(200);
  6513. END;
  6514. END AddProperty;
  6515. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6516. VAR symbol: SyntaxTree.Symbol;
  6517. BEGIN
  6518. WHILE modifier # NIL DO
  6519. symbol := cellType.FindProperty(modifier.identifier);
  6520. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6521. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6522. modifier := modifier.nextModifier;
  6523. END;
  6524. END AddModifiers;
  6525. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6526. VAR last: SyntaxTree.Modifier;
  6527. BEGIN
  6528. IF to = NIL THEN
  6529. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6530. ELSE
  6531. last := to;
  6532. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6533. last := last.nextModifier;
  6534. END;
  6535. IF last.identifier # this.identifier THEN
  6536. ASSERT(last.nextModifier = NIL);
  6537. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6538. END;
  6539. END;
  6540. END AppendModifier;
  6541. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6542. BEGIN
  6543. WHILE this # NIL DO
  6544. AppendModifier(to, this);
  6545. this := this.nextModifier;
  6546. END;
  6547. END AppendModifiers;
  6548. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6549. VAR base: SyntaxTree.Type;
  6550. BEGIN
  6551. AppendModifiers(to, c.modifiers);
  6552. base := c.GetBaseValueType();
  6553. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6554. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6555. END;
  6556. END AppendCellTypeModifiers;
  6557. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6558. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6559. BEGIN
  6560. Basic.GetString(modifier.identifier, name);
  6561. PushConstString(name);
  6562. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6563. ASSERT(SemanticChecker.IsStringType(value.type));
  6564. Designate(value, op);
  6565. PushString(op, value.type);
  6566. ReleaseOperand(op);
  6567. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6568. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6569. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6570. Evaluate(value, op);
  6571. Emit(Push(modifier.position, op.op));
  6572. ReleaseOperand(op);
  6573. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6574. ELSE
  6575. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6576. END;
  6577. END AddPortProperty;
  6578. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6579. BEGIN
  6580. WHILE modifier # NIL DO
  6581. AddPortProperty(modifier, modifier.expression);
  6582. modifier := modifier.nextModifier;
  6583. END;
  6584. END AddPortProperties;
  6585. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6586. VAR op: Operand;
  6587. BEGIN
  6588. Evaluate(p, op);
  6589. Emit(Push(p.position, op.op));
  6590. ReleaseOperand(op);
  6591. IF p IS SyntaxTree.Designator THEN
  6592. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6593. END;
  6594. END PushPort;
  6595. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6596. VAR tmp: IntermediateCode.Operand;
  6597. BEGIN
  6598. actualType := actualType.resolved;
  6599. IF actualType IS SyntaxTree.StringType THEN
  6600. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6601. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6602. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6603. ELSE
  6604. tmp := op.tag;
  6605. IntermediateCode.MakeMemory(tmp,addressType);
  6606. Emit(Push(position,tmp));
  6607. END;
  6608. Emit(Push(position,op.op))
  6609. END PushString;
  6610. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6611. VAR
  6612. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6613. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6614. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6615. tmp:IntermediateCode.Operand;
  6616. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6617. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6618. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6619. dest: IntermediateCode.Operand;
  6620. staticLength: LONGINT; itype: IntermediateCode.Type;
  6621. convert,isTensor: BOOLEAN;
  6622. recordType: SyntaxTree.RecordType;
  6623. baseType: SyntaxTree.Type;
  6624. flags: SET;
  6625. left: SyntaxTree.Expression;
  6626. call: SyntaxTree.Designator;
  6627. procedure: SyntaxTree.Procedure;
  6628. temporaryVariable: SyntaxTree.Variable;
  6629. dummy: IntermediateCode.Operand;
  6630. customBuiltin: SyntaxTree.CustomBuiltin;
  6631. isVarPar: ARRAY 3 OF BOOLEAN;
  6632. callsection: Sections.Section;
  6633. segmentedName: Basic.SegmentedName;
  6634. needsTrace: BOOLEAN;
  6635. n: ARRAY 256 OF CHAR;
  6636. modifier: SyntaxTree.Modifier;
  6637. previous, init: IntermediateCode.Section;
  6638. prevScope: SyntaxTree.Scope;
  6639. firstPar: LONGINT;
  6640. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6641. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6642. priority: IntermediateCode.Operand;
  6643. op,callop: Operand;
  6644. BEGIN
  6645. IF type = NIL THEN RETURN END;
  6646. type := type.resolved;
  6647. IF type IS SyntaxTree.PointerType THEN
  6648. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6649. END;
  6650. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6651. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6652. recordScope := type(SyntaxTree.RecordType).recordScope;
  6653. IF recordScope.bodyProcedure # NIL THEN
  6654. procedure := recordScope.bodyProcedure;
  6655. body := procedure.procedureScope.body;
  6656. Emit(Push(position,self));
  6657. IF body.isActive THEN
  6658. StaticCallOperand(callop,procedure);
  6659. Emit(Push(position,callop.op));
  6660. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6661. Convert(priority,sizeType);
  6662. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6663. END;
  6664. Emit(Push(position,priority));
  6665. ReleaseIntermediateOperand(priority);
  6666. IF backend.cooperative THEN
  6667. Emit(Push(position,self));
  6668. CallThis(position,"Activities","Create",3)
  6669. ELSE
  6670. flags := 0;
  6671. IF body.isSafe THEN
  6672. flags := 1;
  6673. END;
  6674. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6675. Emit(Push(position,self));
  6676. CallThis(position,"Objects","CreateProcess",4)
  6677. END;
  6678. ELSE
  6679. Emit(Push(position,self));
  6680. StaticCallOperand(callop,procedure);
  6681. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6682. END;
  6683. Emit(Pop(position,self));
  6684. END;
  6685. END CallBodies;
  6686. PROCEDURE PushTD(type: SyntaxTree.Type);
  6687. VAR op: IntermediateCode.Operand;
  6688. BEGIN
  6689. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6690. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6691. ELSE
  6692. IF type.resolved IS SyntaxTree.PointerType THEN
  6693. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6694. END;
  6695. op := TypeDescriptorAdr(type.resolved);
  6696. IF ~newObjectFile THEN
  6697. IntermediateCode.MakeMemory(op,addressType);
  6698. END;
  6699. Emit(Push(position,op));
  6700. END
  6701. END PushTD;
  6702. BEGIN
  6703. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6704. dest := destination; destination := emptyOperand;
  6705. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6706. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6707. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6708. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6709. CASE x.id OF
  6710. (* ---- COPY ----- *)
  6711. |Global.Copy:
  6712. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6713. (* ---- EXCL, INCL----- *)
  6714. |Global.Excl,Global.Incl:
  6715. Evaluate(p0,s0);
  6716. Evaluate(p1,s1);
  6717. Convert(s1.op,setType);
  6718. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6719. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6720. END;
  6721. ReuseCopy(res,s0.op);
  6722. ReleaseOperand(s0);
  6723. Reuse1(tmp,s1.op);
  6724. ReleaseOperand(s1);
  6725. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6726. IF x.id = Global.Excl THEN
  6727. Emit(Not(position,tmp,tmp));
  6728. Emit(And(position,res,res,tmp));
  6729. ELSE
  6730. Emit(Or(position,res,res,tmp));
  6731. END;
  6732. ReleaseIntermediateOperand(tmp);
  6733. Designate(p0,s0);
  6734. ToMemory(s0.op,setType,0);
  6735. Emit(Mov(position,s0.op,res));
  6736. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6737. (* ---- DISPOSE ----- *)
  6738. |Global.Dispose:
  6739. Designate(p0,s0);
  6740. Emit(Push(position,s0.op));
  6741. ReleaseOperand(s0);
  6742. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6743. (* ---- GETPROCEDURE ----- *)
  6744. |Global.GetProcedure:
  6745. Designate(p0,s0);
  6746. PushString(s0,p0.type);
  6747. Designate(p1,s1);
  6748. PushString(s1,p1.type);
  6749. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6750. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6751. ELSE PushTD(procedureType.firstParameter.type)
  6752. END;
  6753. PushTD(procedureType.returnType);
  6754. Designate(p2,s2);
  6755. Emit(Push(position,s2.op));
  6756. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6757. CallThis(position,"Modules","GetProcedure", 7);
  6758. (* ---- ASH, LSH, ROT ----- *)
  6759. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6760. Evaluate(p0,s0);
  6761. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6762. (* make unsigned arguments in order to produced a logical shift *)
  6763. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6764. convert:= TRUE;
  6765. itype := s0.op.type;
  6766. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6767. Convert(s0.op,itype);
  6768. ELSE
  6769. convert := FALSE;
  6770. END;
  6771. END;
  6772. Evaluate(p1,s1);
  6773. IF IsIntegerConstant(p1,hint) THEN
  6774. ReuseCopy(reg,s0.op);
  6775. IF hint > 0 THEN
  6776. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6777. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6778. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6779. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6780. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6781. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6782. END;
  6783. ELSIF hint < 0 THEN
  6784. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6785. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6786. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6787. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6788. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6789. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6790. END;
  6791. END;
  6792. ReleaseOperand(s0); ReleaseOperand(s1);
  6793. ELSE
  6794. exit := NewLabel();
  6795. end := NewLabel();
  6796. ReuseCopy(reg,s0.op);
  6797. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6798. Reuse1(tmp,s1.op);
  6799. Emit(Neg(position,tmp,s1.op));
  6800. Convert(tmp,s1.op.type);
  6801. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6802. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6803. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6804. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6805. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6806. END;
  6807. ReleaseIntermediateOperand(tmp);
  6808. BrL(end);
  6809. SetLabel(exit);
  6810. ReuseCopy(tmp,s1.op);
  6811. Convert(tmp,s1.op.type);
  6812. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6813. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6814. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6815. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6816. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6817. END;
  6818. ReleaseIntermediateOperand(tmp);
  6819. SetLabel(end);
  6820. ReleaseOperand(s0); ReleaseOperand(s1);
  6821. END;
  6822. InitOperand(result,ModeValue);
  6823. IF convert THEN
  6824. itype := reg.type;
  6825. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6826. Convert(reg,itype);
  6827. END;
  6828. result.op := reg;
  6829. (* ---- CAP ----- *)
  6830. |Global.Cap:
  6831. Evaluate(p0,result);
  6832. ReuseCopy(reg,result.op);
  6833. ReleaseIntermediateOperand(result.op);
  6834. ignore := NewLabel();
  6835. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6836. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6837. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6838. SetLabel(ignore);
  6839. result.op := reg;
  6840. (* ---- CHR ----- *)
  6841. |Global.Chr, Global.Chr32:
  6842. Evaluate(p0,result);
  6843. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6844. |Global.Entier, Global.EntierH:
  6845. Evaluate(p0,result);
  6846. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6847. (* ---- MIN and MAX ----- *)
  6848. |Global.Max,Global.Min:
  6849. Evaluate(p0,s0);
  6850. Evaluate(p1,s1);
  6851. Reuse2(res,s0.op,s1.op);
  6852. else := NewLabel();
  6853. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6854. ELSE BrltL(else,s1.op,s0.op) END;
  6855. Emit(Mov(position,res,s0.op));
  6856. ReleaseOperand(s0);
  6857. end := NewLabel();
  6858. BrL(end);
  6859. SetLabel(else);
  6860. Emit(MovReplace(position,res,s1.op));
  6861. SetLabel(end);
  6862. ReleaseOperand(s1);
  6863. InitOperand(result,ModeValue);
  6864. result.op := res;
  6865. (* ---- ODD ----- *)
  6866. |Global.Odd:
  6867. IF ~conditional THEN
  6868. ConditionToValue(x)
  6869. ELSE
  6870. Evaluate(p0,result);
  6871. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6872. Reuse1(res,result.op);
  6873. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6874. ReleaseIntermediateOperand(result.op);
  6875. result.op := res;
  6876. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6877. ReleaseOperand(result);
  6878. BrL(falseLabel);
  6879. END;
  6880. (* ---- ORD ----- *)
  6881. |Global.Ord, Global.Ord32:
  6882. Evaluate(p0,result);
  6883. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6884. (* ---- SHORT, LONG ----- *)
  6885. |Global.Short, Global.Long:
  6886. Evaluate(p0,result);
  6887. IF x.type IS SyntaxTree.ComplexType THEN
  6888. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6889. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6890. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6891. ELSE
  6892. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6893. END
  6894. (* ---- HALT, SYSTEM.HALT----- *)
  6895. |Global.Halt, Global.systemHalt:
  6896. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6897. EmitTrap (position, val);
  6898. (* ---- ASSERT ----- *)
  6899. |Global.Assert:
  6900. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6901. trueL := NewLabel();
  6902. falseL := NewLabel();
  6903. Condition(p0,trueL,falseL);
  6904. IF p1 = NIL THEN val := AssertTrap
  6905. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6906. END;
  6907. SetLabel(falseL);
  6908. EmitTrap(position,val);
  6909. SetLabel(trueL);
  6910. END;
  6911. (*
  6912. Emit(TrapC(result.op,val);
  6913. *)
  6914. (* ---- INC, DEC----- *)
  6915. |Global.Inc,Global.Dec:
  6916. Expression(p0); adr := result.op;
  6917. LoadValue(result,p0.type); l := result;
  6918. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6919. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6920. END;
  6921. IF x.id = Global.Inc THEN
  6922. Emit(Add(position,l.op,l.op,r.op));
  6923. ELSE
  6924. Emit(Sub(position,l.op,l.op,r.op));
  6925. END;
  6926. ReleaseOperand(l); ReleaseOperand(r);
  6927. (* ---- LEN ----- *)
  6928. |Global.Len: (* dynamic length, static length done by checker *)
  6929. Designate(p0,operand);
  6930. IF p1 = NIL THEN
  6931. InitOperand(l,ModeValue);
  6932. l.op := IntermediateCode.Immediate(int32,0);
  6933. ELSE
  6934. Evaluate(p1,l);
  6935. END;
  6936. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6937. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6938. Dereference(operand, p0.type.resolved, FALSE);
  6939. END;
  6940. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6941. ReleaseOperand(operand); ReleaseOperand(l);
  6942. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6943. ASSERT(p1 # NIL);
  6944. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6945. Dereference(operand,p0.type.resolved,FALSE);
  6946. END;
  6947. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6948. ReleaseOperand(operand); ReleaseOperand(l);
  6949. ELSE HALT(100);
  6950. END;
  6951. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6952. (* ---- FIRST ---- *)
  6953. |Global.First:
  6954. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6955. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6956. ELSE
  6957. Designate(p0, result)
  6958. END
  6959. (* ---- LAST ---- *)
  6960. |Global.Last:
  6961. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6962. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6963. ELSE
  6964. Designate(p0, result);
  6965. (* make sure result.op is a register *)
  6966. tmp := result.op;
  6967. ReuseCopy(result.op, result.op);
  6968. ReleaseIntermediateOperand(tmp);
  6969. (* add offset to result.op *)
  6970. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6971. END
  6972. (* ---- STEP ---- *)
  6973. |Global.Step:
  6974. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6975. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6976. ELSE
  6977. Designate(p0, result);
  6978. (* make sure result.op is a register *)
  6979. tmp := result.op;
  6980. ReuseCopy(result.op, result.op);
  6981. ReleaseIntermediateOperand(tmp);
  6982. (* add offset to result.op *)
  6983. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6984. END
  6985. (* ---- RE ---- *)
  6986. |Global.Re:
  6987. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6988. Designate(p0, result)
  6989. ELSE
  6990. Evaluate(p0, result)
  6991. END
  6992. (* ---- IM ---- *)
  6993. |Global.Im:
  6994. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6995. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6996. Designate(p0, result);
  6997. (* make sure result.op is a register *)
  6998. tmp := result.op;
  6999. ReuseCopy(result.op, result.op);
  7000. ReleaseIntermediateOperand(tmp);
  7001. (* add offset to result.op *)
  7002. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7003. (* ---- ABS ----- *)
  7004. |Global.Abs:
  7005. Evaluate(p0,operand);
  7006. type := p0.type.resolved;
  7007. InitOperand(result,ModeValue);
  7008. Reuse1a(result.op,operand.op,dest);
  7009. Emit(Abs(position,result.op,operand.op));
  7010. ReleaseOperand(operand);
  7011. (* ---- WAIT ----- *)
  7012. |Global.Wait:
  7013. Evaluate(p0,operand);
  7014. Emit(Push(position,operand.op));
  7015. ReleaseOperand(operand);
  7016. CallThis(position,"Activities","Wait", 1);
  7017. (* ---- NEW ----- *)
  7018. |Global.New:
  7019. (*! the following code is only correct for "standard" Oberon calling convention *)
  7020. IF x.type # NIL THEN
  7021. type := x.type.resolved;
  7022. firstPar := 0;
  7023. ELSE
  7024. type := p0.type.resolved;
  7025. firstPar := 1;
  7026. END;
  7027. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7028. THEN
  7029. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7030. IF backend.cooperative THEN
  7031. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7032. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7033. IF recordType.isObject THEN
  7034. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7035. IF recordType.IsActive() THEN
  7036. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7037. END;
  7038. IF recordType.IsProtected() THEN
  7039. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7040. END;
  7041. ELSE
  7042. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7043. END;
  7044. END;
  7045. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7046. CallThis(position,"Runtime","New", 1);
  7047. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7048. Emit(Result(position, pointer));
  7049. exit := NewLabel();
  7050. BreqL(exit,pointer,nil);
  7051. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7052. GetRecordTypeName (recordType,name);
  7053. IF ~recordType.isObject THEN
  7054. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7055. END;
  7056. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7057. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7058. IF recordType.isObject THEN
  7059. IF recordType.IsProtected() THEN
  7060. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7061. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7062. END;
  7063. IF recordType.IsActive() THEN
  7064. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7065. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7066. END;
  7067. END;
  7068. END;
  7069. (* initialize fields *)
  7070. IF type(SyntaxTree.PointerType).isPlain THEN
  7071. size := 0;
  7072. ELSIF recordType.isObject THEN
  7073. size := BaseObjectTypeSize;
  7074. ELSE
  7075. size := BaseRecordTypeSize;
  7076. END;
  7077. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7078. (* call initializer *)
  7079. constructor := GetConstructor(recordType);
  7080. IF constructor # NIL THEN
  7081. (*! should be unified with ProcedureCallDesignator *)
  7082. Emit(Push(position,pointer));
  7083. ReleaseIntermediateOperand(pointer);
  7084. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7085. FOR i := firstPar TO x.parameters.Length()-1 DO
  7086. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7087. formalParameter := formalParameter.nextParameter;
  7088. END;
  7089. (* static call of the constructor *)
  7090. GetCodeSectionNameForSymbol(constructor,name);
  7091. ASSERT(~constructor.isInline);
  7092. IF constructor.scope.ownerModule # module.module THEN
  7093. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7094. ELSE
  7095. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7096. END;
  7097. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7098. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7099. Emit(Pop(position,pointer));
  7100. END;
  7101. (* call bodies *)
  7102. CallBodies(pointer,type);
  7103. SetLabel(exit);
  7104. needsTrace := p0.NeedsTrace();
  7105. IF needsTrace THEN ModifyAssignments(true) END;
  7106. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7107. Emit(Push(position, pointer));
  7108. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7109. Emit(Pop(position, pointer));
  7110. END;
  7111. Designate(p0,l);
  7112. IF needsTrace THEN
  7113. CallAssignPointer(l.op, pointer);
  7114. ELSE
  7115. ToMemory(l.op,addressType,0);
  7116. Emit(Mov(position,l.op,pointer));
  7117. END;
  7118. ReleaseIntermediateOperand(pointer);
  7119. ReleaseOperand(l);
  7120. IF needsTrace THEN ModifyAssignments(false) END;
  7121. ELSE (* not cooperative backend *)
  7122. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7123. IF temporaryVariable # NIL THEN
  7124. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7125. ELSE
  7126. Designate(p0,l);
  7127. END;
  7128. (* l.op contains address of pointer to record *)
  7129. Emit(Push(position,l.op)); (* address for use after syscall *)
  7130. Emit(Push(position,l.op));
  7131. ReleaseOperand(l);
  7132. (* push type descriptor *)
  7133. reg := TypeDescriptorAdr(recordType);
  7134. IF ~newObjectFile THEN
  7135. IntermediateCode.MakeMemory(reg,addressType);
  7136. END;
  7137. Emit(Push(position,reg));
  7138. ReleaseIntermediateOperand(reg);
  7139. (* push realtime flag *)
  7140. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7141. ELSE Emit(Push(position,false));
  7142. END;
  7143. CallThis(position,"Heaps","NewRec", 3);
  7144. (* check allocation success, if not successful then do not call initializers and bodies *)
  7145. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7146. Emit(Pop(position,pointer));
  7147. MakeMemory(reg,pointer,addressType,0);
  7148. ReleaseIntermediateOperand(pointer);
  7149. pointer := reg;
  7150. exit := NewLabel();
  7151. BreqL(exit,pointer,nil);
  7152. Emit(Push(position,pointer));
  7153. (* initialize fields *)
  7154. InitFields(recordType, pointer,0);
  7155. (* call initializer *)
  7156. constructor := GetConstructor(recordType);
  7157. IF constructor # NIL THEN
  7158. (*! should be unified with ProcedureCallDesignator *)
  7159. Emit(Push(position,pointer));
  7160. ReleaseIntermediateOperand(pointer);
  7161. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7162. FOR i := firstPar TO x.parameters.Length()-1 DO
  7163. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7164. formalParameter := formalParameter.nextParameter;
  7165. END;
  7166. (* static call of the constructor *)
  7167. GetCodeSectionNameForSymbol(constructor,name);
  7168. ASSERT(~constructor.isInline);
  7169. IF constructor.scope.ownerModule # module.module THEN
  7170. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7171. ELSE
  7172. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7173. END;
  7174. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7175. ELSE
  7176. ReleaseIntermediateOperand(pointer);
  7177. END;
  7178. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7179. Emit(Pop(position,pointer));
  7180. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7181. Designate(p0,l);
  7182. ToMemory(l.op,addressType,0);
  7183. Emit(Mov(position,l.op,pointer));
  7184. ReleaseOperand(l);
  7185. result.tag := emptyOperand;
  7186. ELSIF (x.type # NIL) THEN
  7187. result := l; (* temporary variable is the result of NEW Type() *)
  7188. END;
  7189. (* call bodies *)
  7190. CallBodies(pointer,type);
  7191. ReleaseIntermediateOperand(pointer);
  7192. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7193. end := NewLabel();
  7194. BrL(end);
  7195. SetLabel(exit);
  7196. Designate(p0,l);
  7197. ToMemory(l.op,addressType,0);
  7198. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7199. ReleaseOperand(l);
  7200. SetLabel(end);
  7201. ELSE
  7202. SetLabel(exit);
  7203. END;
  7204. END;
  7205. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7206. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7207. dim := 0;
  7208. IntermediateCode.InitOperand(reg);
  7209. IF p1 # NIL THEN
  7210. FOR i := firstPar TO x.parameters.Length()-1 DO
  7211. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7212. parameter := x.parameters.GetExpression(i);
  7213. Evaluate(parameter,r);
  7214. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7215. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7216. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7217. END;
  7218. Emit(Push(position,r.op));
  7219. IF i=1 THEN
  7220. CopyInt(reg,r.op);
  7221. ELSE
  7222. MulInt(reg, reg, r.op);
  7223. END;
  7224. ReleaseOperand(r);
  7225. INC(dim);
  7226. END;
  7227. Convert(reg,addressType);
  7228. ELSE
  7229. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7230. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7231. END;
  7232. openDim := dim;
  7233. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7234. IF backend.cooperative THEN
  7235. size := ToMemoryUnits(system,system.SizeOf(type));
  7236. WHILE type IS SyntaxTree.ArrayType DO
  7237. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7238. END;
  7239. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7240. IF (size # 1) THEN
  7241. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7242. END;
  7243. Emit(Push(position,reg));
  7244. size := ToMemoryUnits(system,system.SizeOf(type));
  7245. IF (size # 1) THEN
  7246. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7247. END;
  7248. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  7249. Emit(Push(position,reg));
  7250. ReleaseIntermediateOperand(reg);
  7251. CallThis(position,"Runtime","New", 1);
  7252. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7253. Emit(Result(position, pointer));
  7254. exit := NewLabel();
  7255. else := NewLabel();
  7256. BreqL(else,pointer,nil);
  7257. IF ~type.hasPointers THEN
  7258. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7259. ELSIF type IS SyntaxTree.RecordType THEN
  7260. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7261. ELSIF type IS SyntaxTree.ProcedureType THEN
  7262. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7263. ELSE
  7264. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7265. END;
  7266. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7267. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7268. 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))));
  7269. IF type IS SyntaxTree.RecordType THEN
  7270. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7271. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7272. ELSE
  7273. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7274. END;
  7275. i := openDim;
  7276. WHILE i > 0 DO
  7277. DEC (i);
  7278. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7279. END;
  7280. needsTrace := p0.NeedsTrace();
  7281. IF needsTrace THEN ModifyAssignments(true) END;
  7282. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7283. Emit(Push(position, pointer));
  7284. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7285. Emit(Pop(position, pointer));
  7286. END;
  7287. Designate(p0,l);
  7288. IF needsTrace THEN
  7289. CallAssignPointer(l.op, pointer);
  7290. ModifyAssignments(false);
  7291. ELSE
  7292. ToMemory(l.op,addressType,0);
  7293. Emit(Mov(position,l.op,pointer));
  7294. END;
  7295. ReleaseIntermediateOperand(pointer);
  7296. ReleaseOperand(l);
  7297. BrL(exit);
  7298. SetLabel(else);
  7299. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7300. Designate(p0,l);
  7301. IF needsTrace THEN
  7302. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7303. ELSE
  7304. ToMemory(l.op,addressType,0);
  7305. Emit(Mov(position,l.op,pointer));
  7306. END;
  7307. ReleaseOperand(l);
  7308. SetLabel(exit);
  7309. ELSE
  7310. (*! the following code is only correct for "standard" Oberon calling convention *)
  7311. IF SemanticChecker.ContainsPointer(type) THEN
  7312. IF type IS SyntaxTree.ArrayType THEN
  7313. staticLength := 1;
  7314. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7315. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7316. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7317. END;
  7318. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7319. MulInt(reg,reg,tmp);
  7320. END;
  7321. Designate(p0,l);
  7322. IF openDim > 0 THEN
  7323. Emit(Push(position,l.op)); (* address for use after syscall *)
  7324. END;
  7325. Emit(Push(position,l.op)); (* address *)
  7326. ReleaseOperand(l);
  7327. tmp := TypeDescriptorAdr(type);
  7328. IF ~newObjectFile THEN
  7329. IntermediateCode.MakeMemory(tmp,addressType);
  7330. END;
  7331. Emit(Push(position,tmp)); (* type descriptor *)
  7332. ReleaseIntermediateOperand(tmp);
  7333. Emit(Push(position,reg)); (* number Elements *)
  7334. ReleaseIntermediateOperand(reg);
  7335. tmp := IntermediateCode.Immediate(addressType,dim);
  7336. Emit(Push(position,tmp)); (* dimensions *)
  7337. (* push realtime flag *)
  7338. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7339. ELSE Emit(Push(position,false));
  7340. END;
  7341. CallThis(position,"Heaps","NewArr",5)
  7342. ELSE
  7343. size := ToMemoryUnits(system,system.SizeOf(type));
  7344. IF (size # 1) THEN
  7345. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7346. (*
  7347. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7348. *)
  7349. END;
  7350. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7351. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7352. AddInt(reg, reg, tmp);
  7353. (*
  7354. Emit(Add(position,reg,reg,tmp));
  7355. *)
  7356. Designate(p0,l);
  7357. IF openDim >0 THEN
  7358. Emit(Push(position,l.op)); (* address for use after syscall *)
  7359. END;
  7360. Emit(Push(position,l.op)); (* address for syscall *)
  7361. ReleaseOperand(l); (* pointer address *)
  7362. Emit(Push(position,reg)); (* size *)
  7363. ReleaseIntermediateOperand(reg);
  7364. (* push realtime flag *)
  7365. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7366. ELSE Emit(Push(position,false));
  7367. END;
  7368. CallThis(position,"Heaps","NewSys", 3)
  7369. END;
  7370. IF openDim > 0 THEN
  7371. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7372. Emit(Pop(position,adr));
  7373. ToMemory(adr,addressType,0);
  7374. ReuseCopy(tmp,adr);
  7375. ReleaseIntermediateOperand(adr);
  7376. adr := tmp;
  7377. else := NewLabel();
  7378. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7379. i := openDim-1;
  7380. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7381. WHILE (i >= 0) DO
  7382. Emit(Pop(position,reg));
  7383. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7384. Emit(Mov(position,res,reg));
  7385. DEC(i);
  7386. END;
  7387. ReleaseIntermediateOperand(adr);
  7388. ReleaseIntermediateOperand(reg);
  7389. exit := NewLabel();
  7390. BrL(exit);
  7391. SetLabel(else);
  7392. (* else part: array could not be allocated *)
  7393. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7394. Emit(Add(position,sp,sp,tmp));
  7395. SetLabel(exit);
  7396. END;
  7397. END;
  7398. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7399. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7400. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7401. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7402. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7403. left.SetType(procedure.type);
  7404. formalParameter := procedureType.firstParameter;
  7405. (* push array to allocate *)
  7406. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7407. formalParameter :=formalParameter.nextParameter;
  7408. (* push length array *)
  7409. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7410. (* push size *)
  7411. type := t0;
  7412. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7413. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7414. END;
  7415. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7416. Emit(Push(position,tmp));
  7417. (* *)
  7418. IF SemanticChecker.ContainsPointer(type) THEN
  7419. tmp := TypeDescriptorAdr(type);
  7420. IF ~newObjectFile THEN
  7421. IntermediateCode.MakeMemory(tmp,addressType);
  7422. END;
  7423. ELSE
  7424. tmp := IntermediateCode.Immediate(addressType, 0);
  7425. END;
  7426. Emit(Push(position,tmp)); (* type descriptor *)
  7427. StaticCallOperand(result,procedure);
  7428. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7429. ReleaseOperand(result);
  7430. END;
  7431. (*
  7432. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7433. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7434. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7435. *)
  7436. ELSE
  7437. dim := 0;
  7438. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7439. (* generate geometry descriptor *)
  7440. Designate(p0,l);
  7441. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7442. ReleaseOperand(l);
  7443. isTensor := TRUE;
  7444. ELSE
  7445. isTensor := FALSE;
  7446. END;
  7447. FOR i := firstPar TO x.parameters.Length()-1 DO
  7448. IF ~isTensor THEN
  7449. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7450. END;
  7451. parameter := x.parameters.GetExpression(i);
  7452. Evaluate(parameter,r);
  7453. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7454. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7455. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7456. END;
  7457. Emit(Push(position,r.op));
  7458. IF i=1 THEN
  7459. CopyInt(reg, r.op);
  7460. ELSE
  7461. MulInt(reg, reg, r.op);
  7462. END;
  7463. ReleaseOperand(r);
  7464. INC(dim);
  7465. END;
  7466. Convert(reg,addressType);
  7467. openDim := dim;
  7468. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7469. (*! the following code is only correct for "standard" Oberon calling convention *)
  7470. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7471. t := type;
  7472. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7473. staticLength := 1;
  7474. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7475. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7476. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7477. END;
  7478. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7479. MulInt(reg,reg,tmp);
  7480. END;
  7481. Designate(p0,l);
  7482. IF isTensor THEN
  7483. Dereference(l,type,FALSE);
  7484. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7485. END;
  7486. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7487. Emit(Push(position,l.tag)); (* address *)
  7488. ReleaseOperand(l);
  7489. tmp := TypeDescriptorAdr(t);
  7490. IF ~newObjectFile THEN
  7491. IntermediateCode.MakeMemory(tmp,addressType);
  7492. END;
  7493. Emit(Push(position,tmp)); (* type descriptor *)
  7494. ReleaseIntermediateOperand(tmp);
  7495. Emit(Push(position,reg)); (* number Elements *)
  7496. ReleaseIntermediateOperand(reg);
  7497. tmp := IntermediateCode.Immediate(addressType,0);
  7498. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7499. (* push realtime flag: false by default *)
  7500. Emit(Push(position,false));
  7501. CallThis(position,"Heaps","NewArr",5);
  7502. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7503. Emit(Pop(position,adr));
  7504. GetMathArrayField(tmp,adr,MathPtrOffset);
  7505. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7506. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7507. PutMathArrayField(adr,reg,MathAdrOffset);
  7508. ReleaseIntermediateOperand(tmp);
  7509. ReleaseIntermediateOperand(reg);
  7510. ELSE
  7511. IF isTensor THEN
  7512. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7513. ELSE
  7514. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7515. END;
  7516. IF (size # 1) THEN
  7517. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7518. END;
  7519. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7520. AddInt(reg,reg,tmp);
  7521. Designate(p0,l);
  7522. IF isTensor THEN
  7523. Dereference(l,type,FALSE);
  7524. END;
  7525. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7526. Emit(Push(position,l.tag)); (* address for syscall *)
  7527. ReleaseOperand(l); (* pointer address *)
  7528. Emit(Push(position,reg)); (* size *)
  7529. ReleaseIntermediateOperand(reg);
  7530. (* push realtime flag: false by default *)
  7531. Emit(Push(position,false));
  7532. CallThis(position,"Heaps","NewSys",3);
  7533. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7534. Emit(Pop(position,adr));
  7535. GetMathArrayField(tmp,adr,MathPtrOffset);
  7536. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7537. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7538. PutMathArrayField(adr,reg,MathAdrOffset);
  7539. ReleaseIntermediateOperand(tmp);
  7540. ReleaseIntermediateOperand(reg);
  7541. END;
  7542. flags := {};
  7543. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7544. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7545. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7546. PutMathArrayField(adr,tmp,MathDimOffset);
  7547. else := NewLabel();
  7548. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7549. i := openDim-1;
  7550. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7551. IF isTensor THEN
  7552. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7553. ELSE
  7554. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7555. END;
  7556. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7557. WHILE (i >= 0) DO
  7558. Emit(Pop(position,reg));
  7559. PutMathArrayLength(adr,reg,i);
  7560. PutMathArrayIncrement(adr,tmp,i);
  7561. IF i > 0 THEN
  7562. IF i=openDim-1 THEN
  7563. CopyInt(tmp,tmp);
  7564. END;
  7565. MulInt(tmp,tmp,reg);
  7566. END;
  7567. DEC(i);
  7568. END;
  7569. ReleaseIntermediateOperand(adr);
  7570. ReleaseIntermediateOperand(reg);
  7571. ReleaseIntermediateOperand(tmp);
  7572. exit := NewLabel();
  7573. BrL(exit);
  7574. SetLabel(else);
  7575. (* else part: array could not be allocated *)
  7576. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7577. Emit(Add(position,sp,sp,tmp));
  7578. SetLabel(exit);
  7579. END;
  7580. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7581. THEN
  7582. IF ~backend.cellsAreObjects THEN RETURN END;
  7583. IF InCellScope(currentScope) THEN
  7584. PushSelfPointer()
  7585. ELSE
  7586. Emit(Push(position, nil));
  7587. END;
  7588. (* push temp address *)
  7589. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7590. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7591. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7592. (* l.op contains address of pointer to record *)
  7593. Emit(Push(position,l.op)); (* address for use after syscall *)
  7594. ReleaseOperand(l);
  7595. (* push type descriptor *)
  7596. reg := TypeDescriptorAdr(baseType);
  7597. IF ~newObjectFile THEN
  7598. IntermediateCode.MakeMemory(reg,addressType);
  7599. END;
  7600. Emit(Push(position,reg));
  7601. ReleaseIntermediateOperand(reg);
  7602. (* push name *)
  7603. (*Global.GetSymbolName(p0, n);*)
  7604. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7605. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7606. ELSE
  7607. Global.GetModuleName(module.module, n);
  7608. END;
  7609. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7610. (*type.typeDeclaration.GetName(n);*)
  7611. PushConstString(n);
  7612. (* push cellnet boolean *)
  7613. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7614. (* push engine boolean *)
  7615. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7616. (* allocate *)
  7617. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7618. (* add capabilities *)
  7619. modifier := p0(SyntaxTree.Designator).modifiers;
  7620. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7621. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7622. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7623. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7624. END;
  7625. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7626. (*
  7627. modifier := baseType(SyntaxTree.CellType).modifiers;
  7628. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7629. modifier := p0(SyntaxTree.Designator).modifiers;
  7630. *)
  7631. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7632. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7633. (* l.op contains address of pointer to record *)
  7634. ToMemory(l.op,addressType,0);
  7635. (* l.op contains value of pointer to record *)
  7636. Emit(Push(position,l.op)); (* address for use after syscall *)
  7637. ReleaseOperand(l);
  7638. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7639. prevScope := currentScope;
  7640. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7641. previous := section;
  7642. section := init;
  7643. (* add ports *)
  7644. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7645. CloseInitializer(previous);
  7646. currentScope := prevScope;
  7647. Symbol(temporaryVariable,l);
  7648. ToMemory(l.op,addressType,0);
  7649. Emit(Push(position,l.op));
  7650. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7651. (*
  7652. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7653. IF constructor # NIL THEN
  7654. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7655. FOR i := 1 TO x.parameters.Length()-1 DO
  7656. p := x.parameters.GetExpression(i);
  7657. Global.GetSymbolName(parameter,name);
  7658. Evaluate(p, value);
  7659. ASSERT(value.type # NIL);
  7660. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7661. par := instance.AddParameter(name);
  7662. par.SetInteger(value.integer);
  7663. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7664. par := instance.AddParameter(name);
  7665. par.SetBoolean(value.boolean);
  7666. ELSE Error(x.position,NotYetImplemented)
  7667. END;
  7668. parameter := parameter.nextParameter
  7669. END;
  7670. END;
  7671. *)
  7672. (* call initializer *)
  7673. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7674. IF constructor # NIL THEN
  7675. (*! should be unified with ProcedureCallDesignator *)
  7676. IF backend.cellsAreObjects THEN
  7677. Symbol(temporaryVariable,l);
  7678. ToMemory(l.op,addressType,0);
  7679. Emit(Push(position,l.op));
  7680. ReleaseOperand(l);
  7681. END;
  7682. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7683. FOR i := firstPar TO x.parameters.Length()-1 DO
  7684. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7685. formalParameter := formalParameter.nextParameter;
  7686. END;
  7687. (* static call of the constructor *)
  7688. Global.GetSymbolSegmentedName(constructor,name);
  7689. ASSERT(~constructor.isInline);
  7690. IF constructor.scope.ownerModule # module.module THEN
  7691. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7692. ELSE
  7693. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7694. END;
  7695. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7696. (*ELSE
  7697. ReleaseIntermediateOperand(pointer);*)
  7698. END;
  7699. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7700. ToMemory(l.op, addressType, 0);
  7701. Designate(p0,s0);
  7702. ToMemory(s0.op,addressType,0);
  7703. Emit(Mov(position,s0.op,l.op));
  7704. ReleaseOperand(l);
  7705. ReleaseOperand(s0);
  7706. result.tag := emptyOperand;
  7707. (* start *)
  7708. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7709. (* push cell *)
  7710. Symbol(temporaryVariable, l);
  7711. ToMemory(l.op,addressType,0);
  7712. Emit(Push(Basic.invalidPosition,l.op));
  7713. (* push delegate *)
  7714. Emit(Push(Basic.invalidPosition,l.op));
  7715. ReleaseOperand(l);
  7716. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7717. Emit(Push(position, s1.op));
  7718. ReleaseOperand(s1);
  7719. CallThis(position,"ActiveCellsRuntime","Start",3);
  7720. END;
  7721. (*IF temporaryVariable # NIL THEN
  7722. end := NewLabel();
  7723. BrL(end);
  7724. SetLabel(exit);
  7725. Designate(p0,l);
  7726. ToMemory(l.op,addressType,0);
  7727. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7728. ReleaseOperand(l);
  7729. SetLabel(end);
  7730. ELSE
  7731. SetLabel(exit);
  7732. END;
  7733. *)
  7734. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7735. ELSE (* no pointer to record, no pointer to array *)
  7736. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7737. (* ignore new statement *)
  7738. Warning(p0.position, "cannot run on final hardware");
  7739. ELSE
  7740. HALT(200);
  7741. END;
  7742. END;
  7743. (* ---- ADDRESSOF----- *)
  7744. |Global.systemAdr:
  7745. Designate(p0,s0);
  7746. s0.mode := ModeValue;
  7747. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7748. ReleaseIntermediateOperand(s0.op);
  7749. s0.op := s0.tag;
  7750. IntermediateCode.InitOperand(s0.tag);
  7751. END;
  7752. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7753. result := s0;
  7754. (* ---- BIT ----- *)
  7755. |Global.systemBit:
  7756. Evaluate(p0,s0);
  7757. ToMemory(s0.op,addressType,0);
  7758. ReuseCopy(res,s0.op);
  7759. ReleaseOperand(s0);
  7760. Evaluate(p1,s1);
  7761. Emit(Ror(position,res,res,s1.op));
  7762. ReleaseOperand(s1);
  7763. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7764. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7765. IF ~conditional THEN
  7766. InitOperand(result,ModeValue); result.op := res;
  7767. ELSE
  7768. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7769. BrL(falseLabel);
  7770. ReleaseIntermediateOperand(res);
  7771. END;
  7772. (* --- MSK ----*)
  7773. |Global.systemMsk:
  7774. Evaluate(p0,s0);
  7775. Evaluate(p1,s1);
  7776. ReuseCopy(res,s0.op);
  7777. ReleaseOperand(s0);
  7778. Emit(And(position,res,res,s1.op));
  7779. ReleaseOperand(s1);
  7780. InitOperand(result,ModeValue);
  7781. result.op := res;
  7782. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7783. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7784. Evaluate(p0,s0);
  7785. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7786. ReleaseOperand(s0);
  7787. InitOperand(result,ModeValue);
  7788. result.op := res;
  7789. (* ---- SYSTEM.GetStackPointer ----- *)
  7790. |Global.systemGetStackPointer:
  7791. InitOperand(result,ModeValue);
  7792. result.op := sp;
  7793. (* ---- SYSTEM.GetFramePointer ----- *)
  7794. |Global.systemGetFramePointer:
  7795. InitOperand(result,ModeValue);
  7796. result.op := fp;
  7797. (* ---- SYSTEM.GetActivity ----- *)
  7798. |Global.systemGetActivity:
  7799. ASSERT(backend.cooperative);
  7800. InitOperand(result,ModeValue);
  7801. result.op := ap;
  7802. (* ---- SYSTEM.SetStackPointer ----- *)
  7803. |Global.systemSetStackPointer:
  7804. Evaluate(p0,s0); (* *)
  7805. Emit(Mov(position,sp,s0.op));
  7806. ReleaseOperand(s0);
  7807. (* ---- SYSTEM.SetFramePointer ----- *)
  7808. |Global.systemSetFramePointer:
  7809. Evaluate(p0,s0); (* *)
  7810. Emit(Mov(position,fp,s0.op));
  7811. ReleaseOperand(s0);
  7812. (* ---- SYSTEM.Activity ----- *)
  7813. |Global.systemSetActivity:
  7814. ASSERT(backend.cooperative);
  7815. Evaluate(p0,s0); (* *)
  7816. Emit(Mov(position,ap,s0.op));
  7817. ReleaseOperand(s0);
  7818. (* ---- SYSTEM.VAL ----- *)
  7819. |Global.systemVal:
  7820. Expression(p1);
  7821. s1 := result;
  7822. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7823. IF s1.mode = ModeReference THEN
  7824. (* nothing to be done if not record type, just take over new type *)
  7825. IF (type IS SyntaxTree.RecordType) THEN
  7826. ReleaseIntermediateOperand(s1.tag);
  7827. s1.tag := TypeDescriptorAdr(type);
  7828. IF ~newObjectFile THEN
  7829. IntermediateCode.MakeMemory(s1.tag,addressType);
  7830. END;
  7831. UseIntermediateOperand(s1.tag);
  7832. END;
  7833. result := s1;
  7834. ELSE (* copy over result to different type, may not use convert *)
  7835. itype := IntermediateCode.GetType(system,type);
  7836. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7837. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7838. Emit(Mov(position,s0.op,s1.op));
  7839. ReleaseOperand(s1);
  7840. InitOperand(result,ModeValue);
  7841. result.op := s0.op;
  7842. ELSE (* different size, must convert *)
  7843. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7844. Convert(s1.op, IntermediateCode.GetType(system,type));
  7845. result := s1;
  7846. END;
  7847. END;
  7848. (* ---- SYSTEM.GET ----- *)
  7849. |Global.systemGet:
  7850. Evaluate(p0,s0); (* adr *)
  7851. Designate(p1,s1); (* variable *)
  7852. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7853. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7854. Emit(Mov(position,s1.op,s0.op));
  7855. ReleaseOperand(s1);
  7856. ReleaseOperand(s0);
  7857. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7858. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7859. Evaluate(p0,s0); (* *)
  7860. Evaluate(p1,s1); (* variable *)
  7861. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7862. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7863. (* real part *)
  7864. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7865. Emit(Mov(position,res, s1.op));
  7866. ReleaseIntermediateOperand(res);
  7867. (* imaginary part *)
  7868. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7869. Emit(Mov(position,res, s1.tag));
  7870. ReleaseIntermediateOperand(res);
  7871. ReleaseOperand(s1);
  7872. ReleaseOperand(s0);
  7873. ELSE
  7874. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7875. ReleaseOperand(s0);
  7876. Emit(Mov(position,res,s1.op));
  7877. ReleaseIntermediateOperand(res);
  7878. ReleaseOperand(s1);
  7879. END;
  7880. (* ---- SYSTEM.MOVE ----- *)
  7881. |Global.systemMove:
  7882. Evaluate(p0,s0);
  7883. Evaluate(p1,s1);
  7884. Evaluate(p2,s2);
  7885. Emit(Copy(position,s1.op,s0.op,s2.op));
  7886. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7887. (* ---- SYSTEM.NEW ----- *)
  7888. |Global.systemNew:
  7889. Designate(p0,s0);
  7890. Emit(Push(position,s0.op));
  7891. ReleaseOperand(s0);
  7892. Evaluate(p1,s1);
  7893. Emit(Push(position,s1.op));
  7894. ReleaseOperand(s1);
  7895. (* push realtime flag: false by default *)
  7896. Emit(Push(position,false));
  7897. CallThis(position,"Heaps","NewSys",3);
  7898. (* ---- SYSTEM.CALL ----- *)
  7899. |Global.systemRef:
  7900. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7901. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7902. s0.mode := ModeValue;
  7903. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7904. result := s0
  7905. (* ---- INCR ----- *)
  7906. |Global.Incr:
  7907. Designate(p0,operand);
  7908. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7909. Dereference(operand,p0.type.resolved,FALSE);
  7910. END;
  7911. ASSERT(p1 # NIL);
  7912. Evaluate(p1,l);
  7913. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7914. ReleaseOperand(operand); ReleaseOperand(l);
  7915. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7916. (* ---- SUM ----- *)
  7917. |Global.Sum: HALT(200);
  7918. (* ---- ALL ----- *)
  7919. |Global.All: HALT(200);
  7920. (* ---- CAS ----- *)
  7921. |Global.Cas:
  7922. needsTrace := p0.NeedsTrace();
  7923. IF needsTrace THEN ModifyAssignments(true) END;
  7924. Designate(p0,s0);
  7925. Evaluate(p1,s1);
  7926. Evaluate(p2,s2);
  7927. IF needsTrace THEN
  7928. Emit(Push(position, s0.op));
  7929. Emit(Push(position, s1.op));
  7930. Emit(Push(position, s2.op));
  7931. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7932. ELSE
  7933. Emit(Cas(position,s0.op,s1.op,s2.op));
  7934. END;
  7935. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7936. IF needsTrace THEN ModifyAssignments(false) END;
  7937. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7938. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7939. IF conditional THEN
  7940. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7941. BrL(falseLabel);
  7942. ReleaseIntermediateOperand(res);
  7943. END;
  7944. (* ---- DIM ----- *)
  7945. |Global.Dim:
  7946. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7947. Designate(p0,s0);
  7948. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7949. Dereference(s0,p0.type.resolved,FALSE);
  7950. END;
  7951. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7952. ReleaseOperand(s0);
  7953. (* ---- RESHAPE ----- *)
  7954. |Global.Reshape:
  7955. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7956. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7957. left.SetType(procedure.type);
  7958. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7959. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7960. END;
  7961. (* ---- SYSTEM.TYPECODE ----- *)
  7962. |Global.systemTypeCode:
  7963. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7964. IF type.resolved IS SyntaxTree.PointerType THEN
  7965. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7966. END;
  7967. result.op := TypeDescriptorAdr(type);
  7968. IF ~newObjectFile THEN
  7969. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7970. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7971. END;
  7972. result.mode := ModeValue;
  7973. (* ---- SYSTEM.TRACE ----- *)
  7974. |Global.systemTrace:
  7975. SystemTrace(x.parameters, x.position);
  7976. (* ----- CONNECT ------*)
  7977. |Global.Connect:
  7978. IF backend.cellsAreObjects THEN
  7979. PushPort(p0);
  7980. PushPort(p1);
  7981. IF p2 # NIL THEN
  7982. Evaluate(p2, s2);
  7983. Emit(Push(p2.position, s2.op));
  7984. ReleaseOperand(s2);
  7985. ELSE
  7986. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7987. END;
  7988. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7989. ELSE
  7990. Warning(x.position, "cannot run on final hardware");
  7991. END;
  7992. (* ----- DELEGATE ------*)
  7993. |Global.Delegate:
  7994. IF backend.cellsAreObjects THEN
  7995. PushPort(p0);
  7996. PushPort(p1);
  7997. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7998. ELSE
  7999. Warning(x.position, "cannot run on final hardware");
  8000. END;
  8001. (* ----- SEND ------*)
  8002. |Global.Send:
  8003. Evaluate(p0,s0);
  8004. Evaluate(p1,s1);
  8005. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8006. Emit(Push(position,s0.op));
  8007. Emit(Push(position,s1.op));
  8008. (*
  8009. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8010. *)
  8011. IF ~backend.cellsAreObjects THEN
  8012. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8013. END;
  8014. ReleaseOperand(s0);
  8015. ReleaseOperand(s1);
  8016. IF backend.cellsAreObjects THEN
  8017. CallThis(position,"ActiveCellsRuntime","Send",2);
  8018. ELSE
  8019. CallThis(position,ChannelModuleName,"Send",2);
  8020. END;
  8021. (* ----- RECEIVE ------*)
  8022. |Global.Receive:
  8023. Evaluate(p0,s0);
  8024. Emit(Push(position,s0.op));
  8025. Designate(p1,s1);
  8026. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8027. Emit(Push(position,s1.op));
  8028. IF p2 # NIL THEN
  8029. Designate(p2,s2);
  8030. Emit(Push(position,s2.op));
  8031. END;
  8032. (*
  8033. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8034. *)
  8035. IF ~backend.cellsAreObjects THEN
  8036. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8037. END;
  8038. ReleaseOperand(s0);
  8039. ReleaseOperand(s1);
  8040. ReleaseOperand(s2);
  8041. IF backend.cellsAreObjects THEN
  8042. IF p2 = NIL THEN
  8043. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8044. ELSE
  8045. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8046. END;
  8047. ELSE
  8048. IF p2 = NIL THEN
  8049. CallThis(position,ChannelModuleName,"Receive",2)
  8050. ELSE
  8051. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8052. END;
  8053. END;
  8054. | Global.systemSpecial:
  8055. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8056. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8057. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8058. (* determine if parameters are of the VAR kind *)
  8059. ASSERT(x.parameters.Length() <= 3);
  8060. formalParameter := procedureType.firstParameter;
  8061. FOR i := 0 TO x.parameters.Length() - 1 DO
  8062. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8063. formalParameter := formalParameter.nextParameter
  8064. END;
  8065. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8066. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8067. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8068. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8069. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8070. IF procedureType.returnType # NIL THEN
  8071. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8072. Emit(Result(position, res));
  8073. (*InitOperand(result,ModeValue);
  8074. result.op := res;
  8075. *)
  8076. IF ~conditional THEN
  8077. InitOperand(result,ModeValue); result.op := res;
  8078. ELSE
  8079. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8080. BrL(falseLabel);
  8081. ReleaseIntermediateOperand(res);
  8082. END;
  8083. END
  8084. ELSE (* function not yet implemented *)
  8085. Error(position,"not yet implemented");
  8086. END;
  8087. destination := dest;
  8088. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8089. END VisitBuiltinCallDesignator;
  8090. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8091. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8092. BEGIN
  8093. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8094. dest := destination; destination := emptyOperand;
  8095. Expression(x.left);
  8096. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8097. ELSIF isUnchecked THEN (* no check *)
  8098. ELSE
  8099. trueL := NewLabel();
  8100. falseL := NewLabel();
  8101. IF IsPointerToRecord(x.left.type,recordType) THEN
  8102. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8103. Emit(Mov(position,tag, result.op));
  8104. IF result.mode # ModeValue THEN
  8105. ptr := tag;
  8106. IntermediateCode.MakeMemory(ptr,addressType);
  8107. Emit(Mov(position,tag, ptr));
  8108. END;
  8109. IF ~backend.cooperative THEN
  8110. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8111. END;
  8112. IntermediateCode.MakeMemory(tag,addressType);
  8113. ELSE
  8114. tag := result.tag;
  8115. UseIntermediateOperand(tag);
  8116. END;
  8117. TypeTest(tag,x.type,trueL,falseL);
  8118. ReleaseIntermediateOperand(tag);
  8119. SetLabel(falseL);
  8120. EmitTrap(position,TypeCheckTrap);
  8121. SetLabel(trueL);
  8122. END;
  8123. destination := dest;
  8124. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8125. END VisitTypeGuardDesignator;
  8126. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8127. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8128. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8129. VAR label: Label; pc: LONGINT;
  8130. BEGIN
  8131. IF backend.cooperative & ~isUnchecked THEN
  8132. pc := section.pc;
  8133. label := NewLabel();
  8134. BrneL(label, operand.op, nil);
  8135. EmitTrap(position, NilPointerTrap);
  8136. SetLabel(label);
  8137. INC(statCoopNilCheck, section.pc - pc);
  8138. END;
  8139. END NilCheck;
  8140. BEGIN
  8141. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8142. ReuseCopy(dereferenced,operand.op);
  8143. ReleaseOperand(operand);
  8144. operand.mode := ModeReference;
  8145. operand.op := dereferenced;
  8146. operand.tag := dereferenced;
  8147. UseIntermediateOperand(operand.tag);
  8148. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8149. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8150. ReleaseIntermediateOperand(operand.tag);
  8151. operand.tag := TypeDescriptorAdr(type);
  8152. IF ~newObjectFile THEN
  8153. IntermediateCode.MakeMemory(operand.tag,addressType);
  8154. END;
  8155. ELSE
  8156. IF ~backend.cooperative THEN
  8157. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8158. END;
  8159. IntermediateCode.MakeMemory(operand.tag,addressType);
  8160. END;
  8161. NilCheck(operand.op);
  8162. ELSIF type IS SyntaxTree.ArrayType THEN
  8163. IF isUnsafe THEN
  8164. NilCheck(operand.op);
  8165. ReleaseIntermediateOperand(operand.tag);
  8166. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8167. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8168. ELSE
  8169. operand.tag := emptyOperand;
  8170. END;
  8171. ELSE
  8172. NilCheck(operand.op);
  8173. IF backend.cooperative THEN
  8174. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8175. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8176. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8177. ELSE
  8178. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8179. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8180. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8181. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8182. END;
  8183. END;
  8184. ELSIF type IS SyntaxTree.MathArrayType THEN
  8185. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8186. IntermediateCode.MakeMemory(operand.op,addressType);
  8187. ELSE HALT(100);
  8188. END;
  8189. END Dereference;
  8190. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8191. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8192. BEGIN
  8193. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8194. dest := destination; destination := emptyOperand;
  8195. Evaluate(x.left,d);
  8196. type := x.type.resolved;
  8197. prevIsUnchecked := isUnchecked;
  8198. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8199. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8200. END;
  8201. Dereference(d,type,IsUnsafePointer(x.left.type));
  8202. isUnchecked := prevIsUnchecked;
  8203. result := d;
  8204. 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
  8205. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8206. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8207. END;
  8208. END;
  8209. destination := dest;
  8210. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8211. END VisitDereferenceDesignator;
  8212. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8213. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8214. BEGIN
  8215. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8216. dest := destination; destination := emptyOperand;
  8217. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8218. tag := result.op;
  8219. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8220. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8221. StaticCallOperand(result,procedure.super);
  8222. ReleaseIntermediateOperand(result.tag);
  8223. UseIntermediateOperand(tag); (* necessary ? *)
  8224. result.tag := tag;
  8225. destination := dest;
  8226. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8227. END VisitSupercallDesignator;
  8228. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8229. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8230. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8231. name: Basic.SegmentedName;
  8232. procedure: SyntaxTree.Procedure;
  8233. procedureType: SyntaxTree.ProcedureType;
  8234. BEGIN
  8235. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8236. dest := destination; destination := emptyOperand;
  8237. scope := currentScope;
  8238. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8239. scope := scope.outerScope;
  8240. END;
  8241. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8242. IF newObjectFile THEN
  8243. moduleSection := meta.ModuleSection();
  8244. IF backend.cooperative THEN
  8245. moduleOffset := 0;
  8246. ELSE
  8247. moduleOffset := moduleSection.pc;
  8248. END;
  8249. result.mode := ModeValue;
  8250. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8251. ELSE
  8252. Symbol(moduleSelf,result);
  8253. IntermediateCode.MakeMemory(result.op,addressType);
  8254. END
  8255. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8256. result.mode := ModeValue;
  8257. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8258. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8259. ELSE
  8260. GetBaseRegister(basereg,currentScope,scope);
  8261. InitOperand(result,ModeReference);
  8262. result.op := basereg;
  8263. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8264. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8265. parametersSize := ProcedureParametersSize(system,procedure);
  8266. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8267. IF backend.cooperative THEN
  8268. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8269. END;
  8270. (* tag must be loaded when dereferencing SELF pointer *)
  8271. END;
  8272. destination := dest;
  8273. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8274. END VisitSelfDesignator;
  8275. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8276. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8277. BEGIN
  8278. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8279. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8280. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8281. parameter := procedureType.returnParameter;
  8282. VisitParameter(parameter);
  8283. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8284. END VisitResultDesignator;
  8285. (** values *)
  8286. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8287. BEGIN
  8288. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8289. IF conditional THEN
  8290. IF x.value THEN BrL(trueLabel)
  8291. ELSE BrL(falseLabel)
  8292. END;
  8293. ELSE
  8294. InitOperand(result,ModeValue);
  8295. IF x.value THEN result.op := true ELSE result.op := false END;
  8296. END;
  8297. END VisitBooleanValue;
  8298. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8299. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8300. BEGIN
  8301. Global.GetModuleSegmentedName(module.module, name);
  8302. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8303. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8304. RETURN section
  8305. END GetDataSection;
  8306. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8307. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8308. BEGIN
  8309. type := vop.type;
  8310. data := GetDataSection();
  8311. pc := EnterImmediate(data,vop);
  8312. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8313. IntermediateCode.MakeMemory(vop, type);
  8314. END GetImmediateMem;
  8315. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8316. BEGIN
  8317. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8318. InitOperand(result,ModeValue);
  8319. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8320. IF ~supportedImmediate(result.op) &~inData THEN
  8321. GetImmediateMem(result.op)
  8322. END;
  8323. END VisitIntegerValue;
  8324. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8325. BEGIN
  8326. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8327. InitOperand(result,ModeValue);
  8328. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8329. END VisitCharacterValue;
  8330. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8331. BEGIN
  8332. IF Trace THEN TraceEnter("VisitSetValue") END;
  8333. InitOperand(result,ModeValue);
  8334. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8335. END VisitSetValue;
  8336. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8337. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8338. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8339. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8340. BEGIN
  8341. numberElements := x.elements.Length();
  8342. FOR i := 0 TO numberElements-1 DO
  8343. expression := x.elements.GetExpression(i);
  8344. IF expression IS SyntaxTree.MathArrayExpression THEN
  8345. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8346. ELSE
  8347. inData := TRUE;
  8348. Evaluate(expression,op);
  8349. irv.Emit(Data(position,op.op));
  8350. inData := FALSE;
  8351. ReleaseOperand(op);
  8352. END;
  8353. END;
  8354. END RecursiveData;
  8355. BEGIN
  8356. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8357. IF ~TryConstantDeclaration() THEN
  8358. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8359. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8360. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8361. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8362. ELSE
  8363. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8364. END;
  8365. RecursiveData(x.array);
  8366. InitOperand(result,ModeReference);
  8367. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8368. END
  8369. END VisitMathArrayValue;
  8370. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8371. VAR constant: Sections.Section;
  8372. BEGIN
  8373. IF constantDeclaration = NIL THEN
  8374. RETURN FALSE
  8375. ELSE
  8376. (* Is a constant in this module: did we generate it already? *)
  8377. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8378. IF constant # NIL THEN
  8379. InitOperand(result,ModeReference);
  8380. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8381. RETURN TRUE;
  8382. END;
  8383. END;
  8384. RETURN FALSE
  8385. END TryConstantDeclaration;
  8386. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8387. BEGIN
  8388. constantDeclaration := x;
  8389. x.value.resolved.Accept(SELF);
  8390. END VisitConstant;
  8391. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8392. BEGIN
  8393. IF Trace THEN TraceEnter("VisitRealValue") END;
  8394. InitOperand(result,ModeValue);
  8395. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8396. END VisitRealValue;
  8397. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8398. VAR
  8399. componentType: SyntaxTree.Type;
  8400. BEGIN
  8401. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8402. ASSERT(x.type IS SyntaxTree.ComplexType);
  8403. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8404. InitOperand(result,ModeValue);
  8405. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8406. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8407. END VisitComplexValue;
  8408. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8409. VAR i: LONGINT; name: Basic.SegmentedName;
  8410. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8411. BEGIN
  8412. IF Trace THEN TraceEnter("VisitStringValue") END;
  8413. IF ~TryConstantDeclaration() THEN
  8414. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8415. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8416. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8417. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8418. ELSE
  8419. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8420. END;
  8421. FOR i := 0 TO x.length-1 DO
  8422. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8423. irv.Emit(Data(position,op));
  8424. END;
  8425. InitOperand(result,ModeReference);
  8426. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8427. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8428. END
  8429. END VisitStringValue;
  8430. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8431. BEGIN
  8432. IF Trace THEN TraceEnter("VisitNilValue") END;
  8433. InitOperand(result,ModeValue);
  8434. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8435. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8436. END VisitNilValue;
  8437. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8438. BEGIN
  8439. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8440. InitOperand(result,ModeValue);
  8441. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8442. END VisitEnumerationValue;
  8443. (** symbols *)
  8444. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8445. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8446. END VisitImport;
  8447. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8448. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8449. BEGIN
  8450. IF scope # baseScope THEN
  8451. (* left := [fp+8] *)
  8452. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8453. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8454. ReuseCopy(left,right);
  8455. ReleaseIntermediateOperand(right);
  8456. scope := scope.outerScope; DEC(level);
  8457. (* { left := [left+8] } *)
  8458. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8459. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8460. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8461. Emit(Mov(position,left,right));
  8462. scope := scope.outerScope; DEC(level);
  8463. END;
  8464. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8465. result := left;
  8466. ELSE
  8467. result := fp;
  8468. END;
  8469. END GetBaseRegister;
  8470. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8471. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8472. BEGIN
  8473. IF Trace THEN TraceEnter("VisitVariable"); END;
  8474. type := x.type.resolved;
  8475. IF (x.useRegister) THEN
  8476. InitOperand(result, ModeValue);
  8477. IF x.registerNumber < 0 THEN
  8478. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8479. reg := x.registerNumber;
  8480. ELSE
  8481. reg := registerUsageCount.Map(x.registerNumber);
  8482. UseRegister(reg);
  8483. END;
  8484. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8485. ELSIF x.externalName # NIL THEN
  8486. InitOperand(result,ModeReference);
  8487. Basic.ToSegmentedName(x.externalName^, name);
  8488. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8489. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8490. InitOperand(result,ModeReference);
  8491. GetBaseRegister(result.op,currentScope,x.scope);
  8492. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8493. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8494. InitOperand(result,ModeReference);
  8495. GetCodeSectionNameForSymbol(x,name);
  8496. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8497. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8498. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8499. InitOperand(result,ModeReference);
  8500. GetCodeSectionNameForSymbol(x,name);
  8501. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8502. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8503. ELSE (* field, left designator must have been emitted *)
  8504. ASSERT(result.mode = ModeReference);
  8505. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8506. ReuseCopy(temp,result.op);
  8507. ReleaseIntermediateOperand(result.op);
  8508. result.op := temp;
  8509. END;
  8510. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8511. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8512. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8513. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8514. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8515. END;
  8516. END;
  8517. END;
  8518. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8519. ValueToCondition(result);
  8520. ELSIF type IS SyntaxTree.ProcedureType THEN
  8521. ReleaseIntermediateOperand(result.tag);
  8522. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8523. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8524. UseIntermediateOperand(result.tag);
  8525. ELSE
  8526. result.tag := nil; (* nil *)
  8527. END;
  8528. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8529. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8530. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8531. ReleaseIntermediateOperand(result.tag);
  8532. Global.GetSymbolSegmentedName(x,name);
  8533. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8534. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8535. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8536. ELSE
  8537. END;
  8538. ELSE
  8539. ReleaseIntermediateOperand(result.tag);
  8540. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8541. END;
  8542. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8543. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8544. ReleaseIntermediateOperand(result.tag);
  8545. result.tag := result.op;
  8546. UseIntermediateOperand(result.tag);
  8547. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8548. IntermediateCode.MakeMemory(result.op,addressType);
  8549. END;
  8550. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8551. ReleaseIntermediateOperand(result.tag);
  8552. result.tag := TypeDescriptorAdr(type);
  8553. IF ~newObjectFile THEN
  8554. IntermediateCode.MakeMemory(result.tag,addressType);
  8555. END;
  8556. UseIntermediateOperand(result.tag);
  8557. (* tag for pointer type computed not here but during dereferencing *)
  8558. END;
  8559. IF Trace THEN TraceExit("VisitVariable") END;
  8560. END VisitVariable;
  8561. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8562. BEGIN
  8563. VisitVariable(property);
  8564. END VisitProperty;
  8565. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8566. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8567. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8568. BEGIN
  8569. type := x.type.resolved;
  8570. IF Trace THEN TraceEnter("VisitParameter") END;
  8571. IF x.ownerType IS SyntaxTree.CellType THEN
  8572. ptype := x.type.resolved;
  8573. IF backend.cellsAreObjects THEN
  8574. ASSERT(result.mode = ModeReference);
  8575. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8576. ReuseCopy(temp,result.op);
  8577. ReleaseIntermediateOperand(result.op);
  8578. result.op := temp;
  8579. END;
  8580. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8581. RETURN;
  8582. ELSE
  8583. InitOperand(result,ModeReference);
  8584. GetCodeSectionNameForSymbol(x,name);
  8585. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8586. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8587. RETURN;
  8588. END;
  8589. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8590. InitOperand(result,ModeReference);
  8591. GetCodeSectionNameForSymbol(x,name);
  8592. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8593. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8594. RETURN
  8595. ELSE
  8596. GetBaseRegister(basereg,currentScope,x.scope);
  8597. InitOperand(result,ModeReference);
  8598. result.op := basereg;
  8599. END;
  8600. IF IsOpenArray(type) THEN
  8601. result.tag := basereg;
  8602. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8603. IntermediateCode.MakeMemory(result.op,addressType);
  8604. IF Global.IsOberonProcedure(x.ownerType) THEN
  8605. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8606. UseIntermediateOperand(result.tag);
  8607. ELSE
  8608. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8609. END;
  8610. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8611. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8612. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8613. ELSIF IsStaticArray(type) THEN
  8614. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8615. IntermediateCode.MakeMemory(result.op,addressType);
  8616. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8617. ELSIF type IS SyntaxTree.MathArrayType THEN
  8618. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8619. WITH type: SyntaxTree.MathArrayType DO
  8620. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8621. IF type.form = SyntaxTree.Tensor THEN
  8622. ELSIF type.form = SyntaxTree.Open THEN
  8623. result.tag := result.op;
  8624. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8625. IntermediateCode.MakeMemory(result.op,addressType);
  8626. UseIntermediateOperand(result.tag);
  8627. ELSIF type.form = SyntaxTree.Static THEN
  8628. IF x.kind = SyntaxTree.ConstParameter THEN
  8629. IntermediateCode.MakeMemory(result.op,addressType);
  8630. END;
  8631. ELSE HALT(100)
  8632. END;
  8633. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8634. IF type.form = SyntaxTree.Tensor THEN
  8635. ToMemory(result.op,addressType,0);
  8636. ELSIF type.form = SyntaxTree.Open THEN
  8637. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8638. ReuseCopy(result.tag, mem);
  8639. ReleaseIntermediateOperand(mem);
  8640. ReleaseIntermediateOperand(result.op);
  8641. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8642. ELSIF type.form = SyntaxTree.Static THEN
  8643. IntermediateCode.MakeMemory(result.op,addressType);
  8644. ELSE HALT(100)
  8645. END;
  8646. ELSE HALT(100)
  8647. END;
  8648. END;
  8649. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8650. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8651. IntermediateCode.MakeMemory(result.op,addressType);
  8652. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8653. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8654. END;
  8655. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8656. ValueToCondition(result);
  8657. ELSIF type IS SyntaxTree.ProcedureType THEN
  8658. ReleaseIntermediateOperand(result.tag);
  8659. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8660. IF x.kind = SyntaxTree.VarParameter THEN
  8661. ReuseCopy(result.tag,result.op);
  8662. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8663. IntermediateCode.MakeMemory(result.tag,addressType);
  8664. ELSE
  8665. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8666. UseIntermediateOperand(result.tag);
  8667. END;
  8668. ELSE
  8669. result.tag := nil;
  8670. END;
  8671. (* tag for pointer type computed not here but during dereferencing *)
  8672. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8673. ReleaseIntermediateOperand(result.tag);
  8674. result.tag := basereg;
  8675. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8676. IntermediateCode.MakeMemory(result.tag,addressType);
  8677. UseIntermediateOperand(result.tag);
  8678. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8679. ReleaseIntermediateOperand(result.tag);
  8680. result.tag := TypeDescriptorAdr(type);
  8681. IF ~newObjectFile THEN
  8682. IntermediateCode.MakeMemory(result.tag,addressType);
  8683. END;
  8684. UseIntermediateOperand(result.tag);
  8685. END;
  8686. IF Trace THEN TraceExit("VisitParameter") END;
  8687. END VisitParameter;
  8688. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8689. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8690. BEGIN
  8691. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8692. (* left.p: left already emitted *)
  8693. tag := result.op; (* value of pointer to left *)
  8694. (* get type desc *)
  8695. tmp := result.tag;
  8696. IntermediateCode.MakeMemory(tmp,addressType);
  8697. (* get method adr *)
  8698. Reuse1(reg,tmp);
  8699. ReleaseIntermediateOperand(tmp);
  8700. IF backend.cooperative THEN
  8701. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8702. WHILE recordType.baseType # NIL DO
  8703. recordType := recordType.GetBaseRecord ();
  8704. END;
  8705. GetRecordTypeName (recordType,name);
  8706. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8707. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8708. offset := 0;
  8709. ELSE
  8710. offset := 2;
  8711. END;
  8712. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8713. ELSE
  8714. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8715. END;
  8716. InitOperand(operand,ModeReference);
  8717. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8718. operand.op := reg;
  8719. operand.tag := tag;
  8720. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8721. END DynamicCallOperand;
  8722. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8723. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8724. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8725. BEGIN
  8726. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8727. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8728. tag := operand.op;
  8729. ReleaseIntermediateOperand(operand.tag);
  8730. ELSE tag := nil
  8731. END;
  8732. IF x.isInline THEN
  8733. sectionType := Sections.InlineCodeSection;
  8734. ELSE
  8735. sectionType := Sections.CodeSection;
  8736. END;
  8737. IF x.externalName # NIL THEN
  8738. Basic.ToSegmentedName(x.externalName^, name);
  8739. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8740. ELSE
  8741. GetCodeSectionNameForSymbol(x, name);
  8742. IF (x.scope.ownerModule = module.module) THEN
  8743. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8744. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8745. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8746. IF source.pc = 0 THEN (* no code yet *)
  8747. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8748. END;
  8749. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8750. bits := x.procedureScope.body.code.inlineCode;
  8751. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8752. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8753. binary.CopyBits(bits, 0, bits.GetSize());
  8754. source.SetResolved(binary);
  8755. ELSE
  8756. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8757. END;
  8758. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8759. END;
  8760. InitOperand(operand,ModeValue);
  8761. operand.op := reg;
  8762. operand.tag := tag;
  8763. IF Trace THEN TraceExit("StaticCallOperand") END;
  8764. END StaticCallOperand;
  8765. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8766. (* handle expressions of the form designator.procedure or procedure *)
  8767. BEGIN
  8768. IF Trace THEN TraceEnter("VisitProcedure") END;
  8769. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8770. DynamicCallOperand(result,x);
  8771. ELSIF x.isInline THEN
  8772. StaticCallOperand(result,x);
  8773. ELSE
  8774. StaticCallOperand(result,x);
  8775. END;
  8776. IF Trace THEN TraceExit("VisitProcedure") END;
  8777. END VisitProcedure;
  8778. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8779. BEGIN
  8780. VisitProcedure(x);
  8781. END VisitOperator;
  8782. (** statements *)
  8783. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8784. BEGIN
  8785. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8786. Expression(x.call);
  8787. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8788. ReleaseOperand(result)
  8789. END;
  8790. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8791. END VisitProcedureCallStatement;
  8792. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8793. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8794. leftBase, rightBase: SyntaxTree.Type;
  8795. procedureName,s: SyntaxTree.IdentifierString;
  8796. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8797. size: LONGINT; rightKind: LONGINT;
  8798. dummy: IntermediateCode.Operand;
  8799. CONST moduleName = "FoxArrayBase";
  8800. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8801. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8802. BEGIN
  8803. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8804. IF base IS SyntaxTree.MathArrayType THEN
  8805. base := OpenArray(base(SyntaxTree.MathArrayType));
  8806. END;
  8807. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8808. result.SetArrayBase(base);
  8809. RETURN result
  8810. END OpenArray;
  8811. BEGIN
  8812. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8813. SaveRegisters();ReleaseUsedRegisters(saved);
  8814. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8815. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8816. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8817. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8818. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8819. IF leftType.form = SyntaxTree.Tensor THEN
  8820. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8821. ELSIF leftType.form = SyntaxTree.Open THEN
  8822. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8823. ELSIF leftType.form = SyntaxTree.Static THEN
  8824. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8825. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8826. END;
  8827. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8828. IF procedure = NIL THEN
  8829. s := "Instruction not supported on target, emulation procedure ";
  8830. Strings.Append(s,moduleName);
  8831. Strings.Append(s,".");
  8832. Strings.Append(s,procedureName);
  8833. Strings.Append(s," not present");
  8834. Error(position,s);
  8835. ELSE
  8836. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8837. parameter.SetType(leftType);
  8838. parameter.SetAccess(SyntaxTree.Internal);
  8839. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8840. parameter.SetKind(rightKind);
  8841. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8842. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8843. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8844. StaticCallOperand(result,procedure);
  8845. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8846. ReleaseOperand(result);
  8847. END;
  8848. RestoreRegisters(saved);
  8849. END;
  8850. END AssignMathArray;
  8851. VAR modifyAssignmentCounter := 0: LONGINT;
  8852. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8853. VAR processor,mem,dst: IntermediateCode.Operand;
  8854. BEGIN
  8855. IF value.intValue = true.intValue THEN
  8856. INC(modifyAssignmentCounter);
  8857. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8858. modifyAssignmentsPC := section.pc;
  8859. ELSE
  8860. DEC(modifyAssignmentCounter);
  8861. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8862. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8863. END;
  8864. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8865. dst := NewRegisterOperand (addressType);
  8866. Emit(Mov(position,dst, processor));
  8867. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8868. Emit(Mov(position,mem, value));
  8869. ReleaseIntermediateOperand(dst);
  8870. END ModifyAssignments;
  8871. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8872. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8873. BEGIN
  8874. type := left.type.resolved;
  8875. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8876. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8877. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8878. IF procedureType.returnParameter = parameter THEN
  8879. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8880. END;
  8881. END;
  8882. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8883. END CopySize;
  8884. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8885. VAR
  8886. leftO, rightO: Operand;
  8887. mem, sizeOp: IntermediateCode.Operand;
  8888. leftType, rightType, componentType: SyntaxTree.Type;
  8889. size: LONGINT;
  8890. parameters: SyntaxTree.ExpressionList;
  8891. procedure: SyntaxTree.Procedure;
  8892. call: SyntaxTree.ProcedureCallDesignator;
  8893. designator: SyntaxTree.Designator;
  8894. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8895. VAR procedureType: SyntaxTree.ProcedureType;
  8896. BEGIN
  8897. IF ReturnedAsParameter(right.type) THEN
  8898. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8899. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8900. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8901. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8902. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8903. IF right.id = Global.Reshape THEN RETURN TRUE
  8904. END;
  8905. END;
  8906. END;
  8907. END;
  8908. RETURN FALSE
  8909. END CanPassAsResultParameter;
  8910. BEGIN
  8911. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8912. leftType := left.type.resolved; rightType:= right.type.resolved;
  8913. IF backend.cooperative & left.NeedsTrace() THEN
  8914. ModifyAssignments(true);
  8915. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8916. Designate(right, rightO);
  8917. Designate(left, leftO);
  8918. ASSERT(leftO.mode = ModeReference);
  8919. TransferToRegister(leftO.op, leftO.op);
  8920. TransferToRegister(rightO.op, rightO.op);
  8921. Emit(Push(position, leftO.op));
  8922. Emit(Push(position, rightO.op));
  8923. CallAssignMethod(leftO.op, rightO.op, left.type);
  8924. Emit(Pop(position, rightO.op));
  8925. Emit(Pop(position, leftO.op));
  8926. sizeOp := CopySize(left);
  8927. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8928. ReleaseOperand(leftO);
  8929. ReleaseOperand(rightO);
  8930. ELSE
  8931. Evaluate(right,rightO);
  8932. Designate(left,leftO);
  8933. ASSERT(leftO.mode = ModeReference);
  8934. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8935. (* copy procedure address first *)
  8936. MakeMemory(mem,leftO.op,addressType,0);
  8937. Emit(Mov(position,mem,rightO.op));
  8938. ReleaseIntermediateOperand(mem);
  8939. (* copy pointer address *)
  8940. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8941. CallAssignPointer(leftO.tag, rightO.tag);
  8942. ELSE
  8943. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8944. CallAssignPointer(leftO.op, rightO.op);
  8945. END;
  8946. ReleaseOperand(leftO);
  8947. ReleaseOperand(rightO);
  8948. END;
  8949. ModifyAssignments(false);
  8950. RETURN;
  8951. END;
  8952. IF CanPassAsResultParameter(right) THEN
  8953. procedureResultDesignator := left(SyntaxTree.Designator);
  8954. Expression(right);
  8955. procedureResultDesignator := NIL;
  8956. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8957. (* left <-- ALIAS OF right: zerocopy *)
  8958. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8959. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8960. designator.SetType(procedure.type);
  8961. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8962. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8963. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8964. END;
  8965. ELSIF leftType IS SyntaxTree.RangeType THEN
  8966. (* LHS is of array range type *)
  8967. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8968. Evaluate(right, rightO);
  8969. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8970. (* first *)
  8971. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8972. Emit(Mov(position,mem, rightO.op));
  8973. ReleaseIntermediateOperand(mem);
  8974. (* last *)
  8975. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8976. Emit(Mov(position,mem, rightO.tag));
  8977. ReleaseIntermediateOperand(mem);
  8978. (* step *)
  8979. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8980. Emit(Mov(position,mem, rightO.extra));
  8981. ReleaseIntermediateOperand(mem);
  8982. ReleaseOperand(rightO);
  8983. ReleaseOperand(leftO)
  8984. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8985. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8986. Evaluate(right, rightO);
  8987. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8988. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8989. (* real part *)
  8990. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8991. Emit(Mov(position,mem, rightO.op));
  8992. ReleaseIntermediateOperand(mem);
  8993. (* imaginary part *)
  8994. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8995. Emit(Mov(position,mem, rightO.tag));
  8996. ReleaseIntermediateOperand(mem);
  8997. ReleaseOperand(rightO);
  8998. ReleaseOperand(leftO)
  8999. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9000. OR (leftType IS SyntaxTree.PortType) THEN
  9001. (* rightO := leftO;*)
  9002. Evaluate(right,rightO);
  9003. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9004. Designate(left,leftO);
  9005. IF leftO.mode = ModeReference THEN
  9006. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9007. destination := mem;
  9008. ELSE
  9009. destination := leftO.op;
  9010. END;
  9011. ReleaseOperand(leftO);
  9012. IF destination.mode # IntermediateCode.Undefined THEN
  9013. Emit(Mov(position,destination,rightO.op));
  9014. END;
  9015. ReleaseOperand(rightO);
  9016. ReleaseIntermediateOperand(mem);
  9017. IntermediateCode.InitOperand(destination);
  9018. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9019. Evaluate(right,rightO);
  9020. Designate(left,leftO);
  9021. MakeMemory(mem,leftO.op,addressType,0);
  9022. Emit(Mov(position,mem,rightO.op));
  9023. ReleaseIntermediateOperand(mem);
  9024. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9025. (* delegate *)
  9026. (*
  9027. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9028. *)
  9029. Emit(Mov(position,leftO.tag,rightO.tag));
  9030. END;
  9031. ReleaseOperand(leftO);
  9032. ReleaseOperand(rightO);
  9033. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9034. Designate(right,rightO);
  9035. Designate(left,leftO);
  9036. sizeOp := CopySize(left);
  9037. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9038. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9039. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9040. IF (rightType IS SyntaxTree.StringType) THEN
  9041. CopyString(left,right);
  9042. 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
  9043. Designate(right,rightO);
  9044. Designate(left,leftO);
  9045. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9046. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9047. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9048. ELSE
  9049. HALT(201)
  9050. END;
  9051. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9052. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9053. Designate(right,rightO);
  9054. Designate(left,leftO);
  9055. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9056. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9057. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9058. END;
  9059. AssignMathArray(left,right);
  9060. ELSE
  9061. HALT(200);
  9062. END;
  9063. END Assign;
  9064. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9065. BEGIN
  9066. IF Trace THEN TraceEnter("VisitAssignment") END;
  9067. Assign(x.left,x.right);
  9068. IF Trace THEN TraceExit("VisitAssignment") END;
  9069. END VisitAssignment;
  9070. PROCEDURE EmitCooperativeSwitch;
  9071. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9072. BEGIN
  9073. ASSERT (cooperativeSwitches);
  9074. pc := section.pc;
  9075. IF lastSwitchPC = section.pc THEN RETURN END;
  9076. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9077. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9078. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9079. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9080. INC(statCoopSwitch, section.pc - pc);
  9081. END EmitCooperativeSwitch;
  9082. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9083. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9084. BEGIN
  9085. p0 := communication.left; p1 := communication.right;
  9086. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9087. Evaluate(p0,s0);
  9088. Evaluate(p1,s1);
  9089. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9090. Emit(Push(position,s0.op));
  9091. Emit(Push(position,s1.op));
  9092. (*
  9093. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9094. *)
  9095. IF ~backend.cellsAreObjects THEN
  9096. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9097. END;
  9098. ReleaseOperand(s0);
  9099. ReleaseOperand(s1);
  9100. IF backend.cellsAreObjects THEN
  9101. CallThis(position,"ActiveCellsRuntime","Send",2);
  9102. ELSE
  9103. CallThis(position,ChannelModuleName,"Send",2);
  9104. END;
  9105. (* ----- RECEIVE ------*)
  9106. ELSE
  9107. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9108. tmp := p0; p0 := p1; p1 := tmp;
  9109. END;
  9110. Evaluate(p0,s0);
  9111. Emit(Push(position,s0.op));
  9112. Designate(p1,s1);
  9113. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9114. Emit(Push(position,s1.op));
  9115. (*
  9116. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9117. *)
  9118. IF ~backend.cellsAreObjects THEN
  9119. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9120. END;
  9121. ReleaseOperand(s0);
  9122. ReleaseOperand(s1);
  9123. IF backend.cellsAreObjects THEN
  9124. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9125. ELSE
  9126. CallThis(position,ChannelModuleName,"Receive",2)
  9127. END;
  9128. END;
  9129. END VisitCommunicationStatement;
  9130. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9131. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9132. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9133. VAR true, false: Label; condition, value: BOOLEAN;
  9134. BEGIN
  9135. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9136. IF condition THEN
  9137. true := NewLabel();
  9138. false := NewLabel();
  9139. Condition(if.condition,true,false);
  9140. SetLabel(true);
  9141. StatementSequence(if.statements);
  9142. BrL(end);
  9143. SetLabel(false);
  9144. ELSE
  9145. IF value THEN (* always true *)
  9146. escape := TRUE;
  9147. StatementSequence(if.statements);
  9148. (* no branch necessary -- rest skipped *)
  9149. END;
  9150. END;
  9151. END IfPart;
  9152. BEGIN
  9153. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9154. end := NewLabel();
  9155. elsifs := x.ElsifParts();
  9156. IfPart(x.ifPart);
  9157. FOR i := 0 TO elsifs-1 DO
  9158. IF ~escape THEN
  9159. elsif := x.GetElsifPart(i);
  9160. IfPart(elsif);
  9161. END;
  9162. END;
  9163. IF (x.elsePart # NIL) & ~escape THEN
  9164. StatementSequence(x.elsePart);
  9165. END;
  9166. SetLabel(end);
  9167. IF Trace THEN TraceExit("VisitIfStatement") END;
  9168. END VisitIfStatement;
  9169. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9170. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9171. BEGIN
  9172. (*IF x.variable.type.resolved = x.type.resolved THEN
  9173. (* always true, do nothing *)
  9174. ELSE*)
  9175. Designate(x.variable,res);
  9176. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9177. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9178. END;
  9179. trueL := NewLabel();
  9180. TypeTest(res.tag,x.type,trueL,falseL);
  9181. ReleaseOperand(res);
  9182. SetLabel(trueL);
  9183. StatementSequence(x.statements);
  9184. BrL(endL);
  9185. END WithPart;
  9186. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9187. VAR endL,falseL: Label;i: LONGINT;
  9188. BEGIN
  9189. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9190. endL := NewLabel();
  9191. FOR i := 0 TO x.WithParts()-1 DO
  9192. falseL := NewLabel();
  9193. WithPart(x.GetWithPart(i),falseL,endL);
  9194. SetLabel(falseL);
  9195. END;
  9196. IF x.elsePart = NIL THEN
  9197. IF ~isUnchecked THEN
  9198. EmitTrap(position,WithTrap);
  9199. END;
  9200. ELSE
  9201. StatementSequence(x.elsePart)
  9202. END;
  9203. SetLabel(endL);
  9204. IF Trace THEN TraceExit("VisitWithStatement") END;
  9205. END VisitWithStatement;
  9206. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9207. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9208. out,else: Label; label: Label;
  9209. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9210. symbol: SyntaxTree.Symbol;
  9211. BEGIN
  9212. (*! split case statement into if-elsif statements for large case label lists *)
  9213. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9214. size := x.max-x.min+1;
  9215. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9216. END;
  9217. Evaluate(x.variable,var);
  9218. ReuseCopy(tmp,var.op);
  9219. ReleaseIntermediateOperand(var.op);
  9220. var.op := tmp;
  9221. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9222. Convert(var.op,addressType);
  9223. size := x.max-x.min+1;
  9224. else := NewLabel();
  9225. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9226. (*
  9227. UniqueId(name,module.module,"case",caseId);
  9228. *)
  9229. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9230. Global.GetModuleSegmentedName(module.module, name);
  9231. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9232. symbol := SyntaxTree.NewSymbol(name[1]);
  9233. symbol.SetScope(moduleScope);
  9234. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9235. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9236. section.isCaseTable := TRUE;
  9237. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9238. ReuseCopy(res,var.op);
  9239. ReleaseOperand(var);
  9240. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9241. Emit(Add(position,res,res,jmp));
  9242. IntermediateCode.MakeMemory(res,addressType);
  9243. Emit(Br(position,res));
  9244. ReleaseIntermediateOperand(res);
  9245. out := NewLabel();
  9246. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9247. part := x.GetCasePart(i);
  9248. constant := part.firstConstant;
  9249. label := NewLabel();
  9250. SetLabel(label);
  9251. WHILE(constant # NIL) DO (* case labels for this case part *)
  9252. FOR j := constant.min TO constant.max DO
  9253. fixups[j-x.min] := label;
  9254. END;
  9255. constant := constant.next;
  9256. END;
  9257. StatementSequence(part.statements);
  9258. BrL(out);
  9259. END;
  9260. SetLabel(else);
  9261. FOR i := 0 TO size-1 DO
  9262. IF fixups[i] = NIL THEN
  9263. fixups[i] := else;
  9264. END;
  9265. END;
  9266. IF x.elsePart # NIL THEN
  9267. StatementSequence(x.elsePart);
  9268. ELSIF ~isUnchecked THEN
  9269. EmitTrap(position,CaseTrap);
  9270. END;
  9271. SetLabel(out);
  9272. FOR i := 0 TO size-1 DO
  9273. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9274. section.Emit(Data(position,op));
  9275. END;
  9276. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9277. END VisitCaseStatement;
  9278. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9279. VAR start: Label; true,false: Label;
  9280. BEGIN
  9281. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9282. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9283. start := NewLabel();
  9284. true := NewLabel();
  9285. false := NewLabel();
  9286. SetLabel(start);
  9287. Condition(x.condition,true,false);
  9288. SetLabel(true);
  9289. StatementSequence(x.statements);
  9290. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9291. BrL(start);
  9292. SetLabel(false);
  9293. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9294. END VisitWhileStatement;
  9295. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9296. VAR false,true: Label;
  9297. BEGIN
  9298. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9299. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9300. true := NewLabel();
  9301. false := NewLabel();
  9302. SetLabel(false);
  9303. StatementSequence(x.statements);
  9304. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9305. Condition(x.condition,true,false);
  9306. SetLabel(true);
  9307. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9308. END VisitRepeatStatement;
  9309. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9310. VAR
  9311. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9312. temporaryVariable: SyntaxTree.Variable;
  9313. temporaryVariableDesignator : SyntaxTree.Designator;
  9314. BEGIN
  9315. IF Trace THEN TraceEnter("VisitForStatement") END;
  9316. true := NewLabel();
  9317. false := NewLabel();
  9318. start := NewLabel();
  9319. Assign(x.variable,x.from);
  9320. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9321. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9322. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9323. Assign(temporaryVariableDesignator,x.to);
  9324. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9325. IF by > 0 THEN
  9326. cmp := Scanner.LessEqual
  9327. ELSE
  9328. cmp := Scanner.GreaterEqual
  9329. END;
  9330. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9331. binary.SetType(system.booleanType);
  9332. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9333. SetLabel(start);
  9334. Condition(binary,true,false);
  9335. SetLabel(true);
  9336. StatementSequence(x.statements);
  9337. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9338. binary.SetType(x.variable.type);
  9339. Assign(x.variable,binary);
  9340. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9341. BrL(start);
  9342. SetLabel(false);
  9343. IF Trace THEN TraceExit("VisitForStatement") END;
  9344. END VisitForStatement;
  9345. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9346. VAR prevLoop: Label;
  9347. BEGIN
  9348. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9349. prevLoop := currentLoop;
  9350. currentLoop := NewLabel();
  9351. StatementSequence(x.statements);
  9352. SetLabel(currentLoop);
  9353. currentLoop := prevLoop;
  9354. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9355. END VisitExitableBlock;
  9356. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9357. VAR prevLoop,start: Label;
  9358. BEGIN
  9359. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9360. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9361. start := NewLabel();
  9362. prevLoop := currentLoop;
  9363. SetLabel(start);
  9364. currentLoop := NewLabel();
  9365. StatementSequence(x.statements);
  9366. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9367. BrL(start);
  9368. SetLabel(currentLoop);
  9369. currentLoop := prevLoop;
  9370. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9371. END VisitLoopStatement;
  9372. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9373. VAR outer: SyntaxTree.Statement;
  9374. BEGIN
  9375. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9376. IF locked THEN (* r if we jump out of an exclusive block *)
  9377. outer := x.outer;
  9378. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9379. outer := outer.outer;
  9380. END;
  9381. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9382. Lock(FALSE);
  9383. END;
  9384. END;
  9385. BrL(currentLoop);
  9386. IF Trace THEN TraceExit("VisitExitStatement") END;
  9387. END VisitExitStatement;
  9388. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9389. VAR
  9390. expression, parameterDesignator: SyntaxTree.Expression;
  9391. type, componentType: SyntaxTree.Type;
  9392. res, right: Operand;
  9393. left, mem, reg: IntermediateCode.Operand;
  9394. parameter: SyntaxTree.Parameter;
  9395. procedure: SyntaxTree.Procedure;
  9396. procedureType: SyntaxTree.ProcedureType;
  9397. returnTypeOffset: LONGINT;
  9398. delegate: BOOLEAN;
  9399. map: SymbolMap;
  9400. cc, parametersSize: LONGINT;
  9401. BEGIN
  9402. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9403. expression := x.returnValue;
  9404. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9405. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9406. IF currentIsInline THEN
  9407. IF expression # NIL THEN
  9408. map := currentMapper.Get(NIL);
  9409. IF map # NIL THEN
  9410. Assign(map.to, expression);
  9411. ELSE
  9412. Evaluate(expression,res);
  9413. Emit(Return(position,res.op));
  9414. ReleaseOperand(res);
  9415. END;
  9416. END;
  9417. BrL(currentInlineExit);
  9418. RETURN;
  9419. END;
  9420. IF expression # NIL THEN
  9421. type := expression.type.resolved;
  9422. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9423. IF locked THEN Lock(FALSE) END;
  9424. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9425. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9426. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9427. (* return without structured return parameter *)
  9428. Evaluate(expression,res);
  9429. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9430. IF locked OR profile THEN
  9431. Emit(Push(position,res.op));
  9432. IF delegate THEN HALT(200) END;
  9433. ReleaseOperand(res);
  9434. IF locked THEN Lock(FALSE) END;
  9435. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9436. reg := NewRegisterOperand(res.op.type);
  9437. Emit(Pop(position,reg));
  9438. Emit(Return(position,reg));
  9439. ReleaseIntermediateOperand(reg);
  9440. ELSE
  9441. Emit(Return(position,res.op));
  9442. ReleaseOperand(res);
  9443. END;
  9444. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9445. THEN
  9446. (* return using structured return parameter *)
  9447. 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)
  9448. OR SemanticChecker.IsPointerType(type));
  9449. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9450. parameter :=procedureType.returnParameter;
  9451. ASSERT(parameter # NIL);
  9452. returnTypeOffset := parameter.offsetInBits;
  9453. (*
  9454. IF parameter# NIL THEN
  9455. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9456. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9457. ELSE
  9458. returnTypeOffset := system.offsetFirstParameter
  9459. END;
  9460. *)
  9461. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9462. IF type IS SyntaxTree.RangeType THEN
  9463. (* array range type *)
  9464. Evaluate(expression, right);
  9465. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9466. Emit(Mov(position,mem, right.op)); (* first *)
  9467. ReleaseIntermediateOperand(mem);
  9468. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9469. Emit(Mov(position,mem, right.tag)); (* last *)
  9470. ReleaseIntermediateOperand(mem);
  9471. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9472. Emit(Mov(position,mem, right.extra)); (* step *)
  9473. ReleaseIntermediateOperand(mem);
  9474. ReleaseOperand(right);
  9475. ELSIF type IS SyntaxTree.ComplexType THEN
  9476. Evaluate(expression, right);
  9477. componentType := type(SyntaxTree.ComplexType).componentType;
  9478. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9479. Emit(Mov(position,mem, right.op)); (* real part *)
  9480. ReleaseIntermediateOperand(mem);
  9481. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9482. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9483. ReleaseIntermediateOperand(mem);
  9484. ReleaseOperand(right);
  9485. ELSE (* covers cases: pointer / record / array *)
  9486. parameter := procedureType.returnParameter;
  9487. checker.SetCurrentScope(currentScope);
  9488. ASSERT(parameter # NIL);
  9489. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9490. Assign(parameterDesignator,expression);
  9491. END;
  9492. ReleaseIntermediateOperand(left);
  9493. IF locked THEN Lock(FALSE) END;
  9494. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9495. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9496. parameter := procedureType.returnParameter;
  9497. checker.SetCurrentScope(currentScope);
  9498. IF parameter = NIL THEN
  9499. Error(procedure.position, "structured return of parameter of procedure not found");
  9500. ELSE
  9501. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9502. Assign(parameterDesignator,expression);
  9503. END;
  9504. IF locked THEN Lock(FALSE) END;
  9505. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9506. ELSE
  9507. HALT(200);
  9508. END;
  9509. ELSE
  9510. IF locked THEN Lock(FALSE) END;
  9511. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9512. END;
  9513. IF backend.cooperative THEN
  9514. BrL(exitLabel);
  9515. ELSE
  9516. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9517. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9518. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9519. ELSE
  9520. parametersSize := 0;
  9521. END;
  9522. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9523. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9524. END;
  9525. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9526. END VisitReturnStatement;
  9527. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9528. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9529. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9530. statements: SyntaxTree.StatementSequence;
  9531. name, suffix: SyntaxTree.IdentifierString;
  9532. BEGIN
  9533. Strings.IntToStr(awaitProcCounter,suffix);
  9534. Strings.Concat("@AwaitProcedure",suffix,name);
  9535. identifier := SyntaxTree.NewIdentifier(name);
  9536. INC(awaitProcCounter);
  9537. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9538. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9539. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9540. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9541. procedure.SetAccess(SyntaxTree.Hidden);
  9542. procedure.SetScope(currentScope);
  9543. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9544. procedureType.SetReturnType(system.booleanType);
  9545. procedure.SetType(procedureType);
  9546. body := SyntaxTree.NewBody(x.position,procedureScope);
  9547. procedureScope.SetBody(body);
  9548. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9549. returnStatement.SetReturnValue(x.condition);
  9550. statements := SyntaxTree.NewStatementSequence();
  9551. statements.AddStatement(returnStatement);
  9552. body.SetStatementSequence(statements);
  9553. currentScope.AddProcedure(procedure);
  9554. RETURN procedure
  9555. END MakeAwaitProcedure;
  9556. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9557. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9558. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9559. BEGIN
  9560. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9561. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9562. IF backend.cooperative THEN
  9563. start := NewLabel();
  9564. true := NewLabel();
  9565. false := NewLabel();
  9566. SetLabel(start);
  9567. Condition(x.condition,true,false);
  9568. SetLabel(false);
  9569. PushSelfPointer();
  9570. CallThis(position,"ExclusiveBlocks","Await",1);
  9571. BrL(start);
  9572. SetLabel(true);
  9573. PushSelfPointer();
  9574. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9575. ELSE
  9576. proc := MakeAwaitProcedure(x);
  9577. Emit(Push(position,fp));
  9578. GetCodeSectionNameForSymbol(proc,name);
  9579. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9580. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9581. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9582. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9583. Emit(Result(position,res));
  9584. (*
  9585. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9586. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9587. *)
  9588. InitOperand(result,ModeValue);
  9589. result.op := res;
  9590. label := NewLabel();
  9591. BreqL(label, result.op, SELF.true);
  9592. ReleaseOperand(result);
  9593. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9594. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9595. Emit(Push(position,res));
  9596. Emit(Push(position,fp));
  9597. PushSelfPointer();
  9598. Emit(Push(position,nil));
  9599. CallThis(position,"Objects","Await",4);
  9600. SetLabel(label);
  9601. END;
  9602. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9603. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9604. END VisitAwaitStatement;
  9605. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9606. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9607. BEGIN
  9608. FOR i := 0 TO x.Length() - 1 DO
  9609. statement := x.GetStatement( i );
  9610. Statement(statement);
  9611. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9612. END;
  9613. END StatementSequence;
  9614. PROCEDURE PushSelfPointer;
  9615. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9616. procedureType: SyntaxTree.ProcedureType;
  9617. BEGIN
  9618. scope := currentScope;
  9619. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9620. scope := scope.outerScope;
  9621. END;
  9622. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9623. IF ~newObjectFile THEN
  9624. Symbol(moduleSelf,op);
  9625. IntermediateCode.MakeMemory(op.op,addressType);
  9626. ELSE
  9627. moduleSection := meta.ModuleSection();
  9628. IF backend.cooperative THEN
  9629. moduleOffset := 0;
  9630. ELSE
  9631. moduleOffset := moduleSection.pc;
  9632. END;
  9633. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9634. END;
  9635. ELSE
  9636. GetBaseRegister(op.op,currentScope,scope);
  9637. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9638. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9639. parametersSize := ProcedureParametersSize(system,procedure);
  9640. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9641. IF backend.cooperative THEN
  9642. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9643. END;
  9644. IntermediateCode.MakeMemory(op.op,addressType);
  9645. END;
  9646. Emit(Push(position,op.op));
  9647. ReleaseOperand(op);
  9648. END PushSelfPointer;
  9649. PROCEDURE Lock(lock: BOOLEAN);
  9650. BEGIN
  9651. IF Trace THEN TraceEnter("Lock") END;
  9652. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9653. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9654. ASSERT(modifyAssignmentCounter = 0);
  9655. IF dump # NIL THEN
  9656. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9657. dump.Ln;dump.Update;
  9658. END;
  9659. PushSelfPointer;
  9660. IF backend.cooperative THEN
  9661. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9662. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9663. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9664. END;
  9665. ELSE
  9666. Emit(Push(position,true));
  9667. IF lock THEN CallThis(position,"Objects","Lock",2)
  9668. ELSE CallThis(position,"Objects","Unlock",2);
  9669. END;
  9670. END;
  9671. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9672. IF Trace THEN TraceExit("Lock") END;
  9673. END Lock;
  9674. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9675. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9676. BEGIN
  9677. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9678. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9679. previouslyUnchecked := isUnchecked;
  9680. isUnchecked := isUnchecked OR x.isUnchecked;
  9681. previouslyCooperativeSwitches := cooperativeSwitches;
  9682. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9683. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9684. IF x.statements # NIL THEN
  9685. StatementSequence(x.statements);
  9686. END;
  9687. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9688. isUnchecked := previouslyUnchecked;
  9689. cooperativeSwitches := previouslyCooperativeSwitches;
  9690. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9691. END VisitStatementBlock;
  9692. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9693. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9694. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9695. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9696. procedure: SyntaxTree.Procedure;
  9697. map: SymbolMap;
  9698. cc, parametersSize: LONGINT;
  9699. BEGIN
  9700. scope := currentScope;
  9701. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9702. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9703. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9704. return := emptyOperand;
  9705. IF Trace THEN TraceEnter("VisitCode") END;
  9706. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9707. NEW(in, x.inRules.Length());
  9708. FOR i := 0 TO LEN(in)-1 DO
  9709. statement := x.inRules.GetStatement(i);
  9710. WITH statement: SyntaxTree.Assignment DO
  9711. Evaluate(statement.right, operand);
  9712. result := operand.op;
  9713. NEW(str, 64);
  9714. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9715. in[i] := result; IntermediateCode.SetString(in[i], str);
  9716. ReleaseIntermediateOperand(operand.tag);
  9717. END;
  9718. END;
  9719. ELSE in := NIL
  9720. END;
  9721. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9722. NEW(out, x.outRules.Length());
  9723. FOR i := 0 TO LEN(out)-1 DO
  9724. statement := x.outRules.GetStatement(i);
  9725. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9726. WITH statement: SyntaxTree.Assignment DO
  9727. Designate(statement.left, operand);
  9728. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9729. NEW(str, 64);
  9730. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9731. out[i] := result; IntermediateCode.SetString(out[i], str);
  9732. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9733. |statement: SyntaxTree.ReturnStatement DO
  9734. NEW(str, 64);
  9735. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9736. IF currentIsInline THEN
  9737. map := currentMapper.Get(NIL);
  9738. Designate(map.to, operand);
  9739. IF map.isAddress THEN
  9740. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9741. ELSE
  9742. result := operand.op;
  9743. END;
  9744. (*! only if it does not fit into register
  9745. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9746. *)
  9747. (*Evaluate(map.to, operand);*)
  9748. out[i] := result;
  9749. ELSE
  9750. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9751. END;
  9752. IntermediateCode.SetString(out[i], str);
  9753. ReleaseIntermediateOperand(operand.tag);
  9754. return := out[i];
  9755. ELSE
  9756. END;
  9757. END;
  9758. ELSE out := NIL
  9759. END;
  9760. Emit(Asm(x.position,x.sourceCode, in, out));
  9761. IF in # NIL THEN
  9762. FOR i := 0 TO LEN(in)-1 DO
  9763. ReleaseIntermediateOperand(in[i]);
  9764. END;
  9765. END;
  9766. IF out # NIL THEN
  9767. FOR i := 0 TO LEN(out)-1 DO
  9768. WITH statement: SyntaxTree.Assignment DO
  9769. ReleaseIntermediateOperand(out[i]);
  9770. |statement: SyntaxTree.ReturnStatement DO
  9771. (* release happens below *)
  9772. ELSE
  9773. END;
  9774. statement := x.outRules.GetStatement(i);
  9775. END;
  9776. END;
  9777. IF return.mode # IntermediateCode.Undefined THEN
  9778. IF currentIsInline THEN
  9779. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9780. Symbol(procedureType.returnParameter, par);
  9781. MakeMemory(mem, par.op, return.type, 0);
  9782. ReleaseOperand(par);
  9783. Emit(Mov(position, mem, return));
  9784. ReleaseIntermediateOperand(mem);
  9785. ELSE
  9786. Emit(Return(position,return));
  9787. END;
  9788. ReleaseIntermediateOperand(return);
  9789. IF currentIsInline THEN RETURN END;
  9790. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9791. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9792. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9793. ELSE
  9794. parametersSize := 0;
  9795. END;
  9796. EmitLeave(section, position,NIL, cc);
  9797. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9798. END;
  9799. IF Trace THEN TraceExit("VisitCode") END;
  9800. END VisitCode;
  9801. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9802. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9803. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9804. const, call: IntermediateCode.Operand;
  9805. parameterDesignator: SyntaxTree.Expression;
  9806. saved: RegisterEntry;
  9807. name: Basic.SegmentedName;
  9808. BEGIN
  9809. IF Trace THEN TraceEnter("ParameterCopies") END;
  9810. parameter := x.firstParameter;
  9811. WHILE parameter # NIL DO
  9812. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9813. type := parameter.type.resolved;
  9814. IF IsOpenArray(type) THEN
  9815. VisitParameter(parameter);
  9816. op := result;
  9817. IF backend.cooperative & parameter.NeedsTrace() THEN
  9818. length := GetArrayLength(type, op.tag);
  9819. size := NewRegisterOperand(addressType);
  9820. base := ArrayBaseType(type);
  9821. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9822. Emit(Mul(position, size, length, const));
  9823. dst := NewRegisterOperand (addressType);
  9824. Emit(Mov(position, dst, size));
  9825. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9826. Emit(Sub(position,dst,sp,dst));
  9827. Emit(And(position,dst,dst,const));
  9828. Emit(Mov(position,sp,dst));
  9829. par := fp;
  9830. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9831. IntermediateCode.InitImmediate(null, byteType, 0);
  9832. Emit(Fill(position, dst, size, null));
  9833. ReleaseIntermediateOperand(dst);
  9834. ReleaseIntermediateOperand(length);
  9835. SaveRegisters();ReleaseUsedRegisters(saved);
  9836. (* register dst has been freed before SaveRegisters already *)
  9837. base := ArrayBaseType(type);
  9838. (* assign method of open array *)
  9839. IF base.IsRecordType() THEN
  9840. Emit (Push(position, length));
  9841. Emit (Push(position, dst));
  9842. Emit (Push(position, op.op));
  9843. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9844. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9845. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9846. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9847. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9848. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9849. Emit (Push(position,length));
  9850. Emit (Push(position, dst));
  9851. Emit (Push(position, length));
  9852. Emit (Push(position, op.op));
  9853. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9854. ELSE
  9855. Emit (Push(position, length));
  9856. Emit (Push(position, dst));
  9857. Emit (Push(position, length));
  9858. Emit (Push(position, op.op));
  9859. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9860. ASSERT(ArrayBaseType(type).IsPointer());
  9861. END;
  9862. RestoreRegisters(saved);
  9863. ELSE
  9864. temp := GetDynamicSize(type,op.tag);
  9865. ReuseCopy(size,temp);
  9866. ReleaseIntermediateOperand(temp);
  9867. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9868. Emit(Sub(position,size,sp,size));
  9869. Emit(And(position,size,size,const));
  9870. Emit(Mov(position,sp,size));
  9871. par := fp;
  9872. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9873. ReleaseIntermediateOperand(size);
  9874. size := GetDynamicSize(type,op.tag);
  9875. END;
  9876. Emit(Copy(position,sp,op.op,size));
  9877. ReleaseIntermediateOperand(size);
  9878. ReleaseOperand(op);
  9879. IntermediateCode.MakeMemory(par,addressType);
  9880. Emit(Mov(position,par,sp));
  9881. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9882. checker.SetCurrentScope(currentScope);
  9883. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9884. Assign(parameterDesignator,parameterDesignator);
  9885. END;
  9886. END;
  9887. parameter := parameter.nextParameter;
  9888. END;
  9889. IF Trace THEN TraceExit("ParameterCopies") END;
  9890. END ParameterCopies;
  9891. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9892. VAR x: SyntaxTree.Variable;
  9893. BEGIN
  9894. x := scope.firstVariable;
  9895. WHILE x # NIL DO
  9896. InitVariable(x);
  9897. x := x.nextVariable;
  9898. END;
  9899. END InitVariables;
  9900. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9901. BEGIN
  9902. IF (symbol # NIL) THEN
  9903. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9904. ELSE
  9905. RETURN 0
  9906. END;
  9907. END GetFingerprint;
  9908. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9909. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9910. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9911. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9912. name: Basic.SegmentedName;
  9913. offset: LONGINT;
  9914. BEGIN
  9915. IF Trace THEN TraceEnter("Body") END;
  9916. ReleaseUsedRegisters(saved); (* just in case ... *)
  9917. section := ir;
  9918. exitLabel := NewLabel ();
  9919. IF moduleBody THEN moduleBodySection := section END;
  9920. IF ir.comments # NIL THEN
  9921. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9922. commentPrintout.SingleStatement(TRUE);
  9923. dump := ir.comments;
  9924. ELSE
  9925. commentPrintout := NIL;
  9926. dump := NIL;
  9927. END;
  9928. prevScope := currentScope;
  9929. currentScope := scope;
  9930. lastSwitchPC := 0;
  9931. cooperativeSwitches := backend.cooperative;
  9932. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9933. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9934. IF x # NIL THEN
  9935. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9936. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9937. IF moduleBody THEN
  9938. ProfilerInit();
  9939. ELSE
  9940. Basic.SegmentedNameToString(ir.name, string);
  9941. ProfilerAddProcedure(numberProcedures,string);
  9942. ProfilerEnterExit(numberProcedures,TRUE);
  9943. END;
  9944. END;
  9945. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9946. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9947. END;
  9948. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9949. IF moduleBody & ~newObjectFile THEN
  9950. InitVariables(moduleScope)
  9951. END;
  9952. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9953. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9954. IF procedure = cellScope.bodyProcedure THEN
  9955. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9956. StaticCallOperand(op, cellScope.constructor);
  9957. Emit(Call(position,op.op,0));
  9958. END;
  9959. END;
  9960. END;
  9961. ParameterCopies(procedureType);
  9962. InitVariables(scope);
  9963. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9964. GetCodeSectionNameForSymbol(procedure, name);
  9965. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9966. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9967. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9968. IntermediateCode.SetOffset(right,offset); (* tag *)
  9969. IntermediateCode.InitMemory(left,addressType,fp,0);
  9970. Emit(Mov(position, left, right));
  9971. END;
  9972. IF x.code = NIL THEN
  9973. VisitStatementBlock(x);
  9974. ELSE
  9975. VisitCode(x.code)
  9976. END;
  9977. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9978. ir.SetFinally(ir.pc);
  9979. StatementSequence(x.finally)
  9980. END;
  9981. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9982. IF ~backend.cooperative THEN
  9983. ProfilerEnterExit(numberProcedures,FALSE);
  9984. END;
  9985. INC(numberProcedures);
  9986. END;
  9987. END;
  9988. IF backend.cooperative THEN
  9989. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9990. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9991. END;
  9992. IF x # NIL THEN
  9993. SELF.position := x.position;
  9994. END;
  9995. CheckRegistersFree();
  9996. ASSERT(modifyAssignmentCounter = 0);
  9997. currentScope := prevScope;
  9998. IF Trace THEN TraceExit("Body") END;
  9999. END Body;
  10000. END ImplementationVisitor;
  10001. MetaDataGenerator=OBJECT
  10002. VAR
  10003. implementationVisitor: ImplementationVisitor;
  10004. declarationVisitor: DeclarationVisitor;
  10005. module: Sections.Module;
  10006. moduleName: ARRAY 128 OF CHAR;
  10007. moduleNamePool: Basic.HashTableInt;
  10008. moduleNamePoolSection: IntermediateCode.Section;
  10009. modulePointerSection: IntermediateCode.Section;
  10010. modulePointerSizePC: LONGINT;
  10011. modulePointerSectionOffset: LONGINT;
  10012. modulePointers: LONGINT;
  10013. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10014. RecordBaseOffset: LONGINT;
  10015. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10016. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10017. TypeTags: LONGINT; (* type extension level support *)
  10018. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10019. patchInfoPC: LONGINT;
  10020. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10021. BEGIN
  10022. IF implementationVisitor.backend.cooperative THEN
  10023. TypeTags := MAX(LONGINT);
  10024. BaseTypesTableOffset := 0;
  10025. MethodTableOffset := 2;
  10026. TypeRecordBaseOffset := 0;
  10027. RecordBaseOffset := 0;
  10028. ELSIF simple THEN
  10029. TypeTags := 3; (* only 3 extensions allowed *)
  10030. BaseTypesTableOffset := 1;
  10031. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10032. TypeRecordBaseOffset := 0;
  10033. RecordBaseOffset := 1;
  10034. ELSE
  10035. TypeTags := 16;
  10036. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10037. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10038. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10039. RecordBaseOffset := 8;
  10040. END;
  10041. SELF.simple := simple;
  10042. SELF.implementationVisitor := implementationVisitor;
  10043. SELF.declarationVisitor := declarationVisitor;
  10044. implementationVisitor.meta := SELF;
  10045. declarationVisitor.meta := SELF;
  10046. END InitMetaDataGenerator;
  10047. PROCEDURE SetModule(module: Sections.Module);
  10048. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10049. BEGIN
  10050. SELF.module := module;
  10051. Global.GetModuleName(module.module,moduleName);
  10052. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10053. NEW(moduleNamePool, 32);
  10054. (*! require GC protection *)
  10055. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10056. name := "Heaps.AnyPtr";
  10057. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10058. (* set base pointer *)
  10059. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10060. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10061. modulePointers := 0;
  10062. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10063. AddPointer(moduleNamePoolSection, namePoolOffset);
  10064. END;
  10065. END SetModule;
  10066. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10067. BEGIN
  10068. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10069. INC(modulePointers);
  10070. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10071. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10072. END AddPointer;
  10073. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10074. BEGIN
  10075. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10076. END GetTypeRecordBaseOffset;
  10077. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10078. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10079. BEGIN
  10080. INC(dataAdrOffset,6);
  10081. Info(section,"headerAdr");
  10082. Address(section,0);
  10083. Info(section,"typeDesc");
  10084. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10085. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10086. NamedSymbol(section, name, symbol, 0, offset);
  10087. Info(section,"mark: LONGINT;");
  10088. Longint(section,-1);
  10089. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10090. Info(section,"dataAdr-: ADDRESS");
  10091. Symbol(section,section, dataAdrOffset,0);
  10092. Info(section,"size-: SIZE");
  10093. Address(section,0);
  10094. Info(section,"nextRealtime: HeapBlock;");
  10095. Address(section,0);
  10096. END HeapBlock;
  10097. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10098. VAR i: LONGINT;
  10099. BEGIN
  10100. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10101. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10102. Info(section,"count*: LONGINT");
  10103. Longint(section,0);
  10104. Info(section,"locked*: BOOLEAN");
  10105. Longint(section,0);
  10106. Info(section,"awaitingLock*: ProcessQueue");
  10107. Address(section,0);
  10108. Address(section,0);
  10109. Info(section,"awaitingCond*: ProcessQueue");
  10110. Address(section,0);
  10111. Address(section,0);
  10112. Info(section,"lockedBy*: ANY");
  10113. Address(section,0);
  10114. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10115. Longint(section,1);
  10116. FOR i := 2 TO 6 DO
  10117. Longint(section,0);
  10118. END;
  10119. Info(section,"lock*: ANY");
  10120. Address(section,0);
  10121. END ProtectedHeapBlock;
  10122. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10123. BEGIN
  10124. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10125. END Info;
  10126. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10127. VAR op: IntermediateCode.Operand;
  10128. BEGIN
  10129. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10130. section.Emit(Data(Basic.invalidPosition,op));
  10131. END Address;
  10132. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10133. VAR op: IntermediateCode.Operand;
  10134. BEGIN
  10135. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10136. section.Emit(Data(Basic.invalidPosition,op));
  10137. END Size;
  10138. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10139. VAR op: IntermediateCode.Operand;
  10140. BEGIN
  10141. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10142. section.Emit(Data(Basic.invalidPosition,op));
  10143. END Set;
  10144. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10145. VAR op: IntermediateCode.Operand;
  10146. BEGIN
  10147. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10148. section.Emit(Data(Basic.invalidPosition,op));
  10149. END Longint;
  10150. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10151. VAR op,noOperand: IntermediateCode.Operand;
  10152. BEGIN
  10153. IntermediateCode.InitOperand(noOperand);
  10154. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10155. section.PatchOperands(pc,op,noOperand,noOperand);
  10156. END PatchAddress;
  10157. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10158. VAR op,noOperand: IntermediateCode.Operand;
  10159. BEGIN
  10160. IntermediateCode.InitOperand(noOperand);
  10161. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10162. section.PatchOperands(pc,op,noOperand,noOperand);
  10163. END PatchSize;
  10164. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10165. VAR op,noOperand: IntermediateCode.Operand;
  10166. BEGIN
  10167. IntermediateCode.InitOperand(noOperand);
  10168. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10169. section.PatchOperands(pc,op,noOperand,noOperand);
  10170. END PatchLongint;
  10171. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10172. VAR op, noOperand: IntermediateCode.Operand;
  10173. BEGIN
  10174. IntermediateCode.InitOperand(noOperand);
  10175. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10176. section.PatchOperands(pc,op,noOperand,noOperand);
  10177. END PatchSymbol;
  10178. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10179. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10180. BEGIN
  10181. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10182. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10183. section.Emit(Data(Basic.invalidPosition,op));
  10184. END Boolean;
  10185. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10186. VAR op: IntermediateCode.Operand;
  10187. BEGIN
  10188. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10189. section.Emit(Data(Basic.invalidPosition,op));
  10190. END Char;
  10191. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10192. VAR op: IntermediateCode.Operand;
  10193. BEGIN
  10194. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10195. section.Emit(Data(Basic.invalidPosition,op));
  10196. END Integer;
  10197. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10198. VAR i: LONGINT;
  10199. BEGIN
  10200. Info(section,str);
  10201. i := 0;
  10202. WHILE(str[i] # 0X) DO
  10203. Char(section,str[i]);
  10204. INC(i);
  10205. END;
  10206. Char(section,0X);
  10207. END String;
  10208. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10209. VAR s: Basic.SectionName;
  10210. BEGIN
  10211. StringPool.GetString(str, s);
  10212. String(section, s);
  10213. END String0;
  10214. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10215. VAR op: IntermediateCode.Operand;
  10216. BEGIN
  10217. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10218. IntermediateCode.SetOffset(op,realOffset);
  10219. section.Emit(Data(Basic.invalidPosition,op));
  10220. END NamedSymbol;
  10221. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10222. VAR op: IntermediateCode.Operand;
  10223. BEGIN
  10224. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10225. IntermediateCode.SetOffset(op,realOffset);
  10226. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10227. END NamedSymbolAt;
  10228. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10229. BEGIN
  10230. IF symbol= NIL THEN
  10231. Address( section, realOffset);
  10232. ASSERT(virtualOffset = 0);
  10233. ELSE
  10234. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10235. END;
  10236. END Symbol;
  10237. (* OutPointers delivers
  10238. {pointerOffset}
  10239. *)
  10240. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10241. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10242. BEGIN
  10243. type := type.resolved;
  10244. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10245. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10246. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10247. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10248. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10249. ELSIF type IS SyntaxTree.PointerType THEN
  10250. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10251. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10252. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10253. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10254. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10255. ELSIF (type IS SyntaxTree.RecordType) THEN
  10256. (* never treat a record like a pointer, even if the pointer field is set! *)
  10257. WITH type: SyntaxTree.RecordType DO
  10258. base := type.GetBaseRecord();
  10259. IF base # NIL THEN
  10260. Pointers(offset,symbol,section, base,numberPointers);
  10261. END;
  10262. variable := type.recordScope.firstVariable;
  10263. WHILE(variable # NIL) DO
  10264. IF ~(variable.untraced) THEN
  10265. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10266. END;
  10267. variable := variable.nextVariable;
  10268. END;
  10269. END;
  10270. ELSIF (type IS SyntaxTree.CellType) THEN
  10271. WITH type: SyntaxTree.CellType DO
  10272. base := type.GetBaseRecord();
  10273. IF base # NIL THEN
  10274. Pointers(offset,symbol,section, base,numberPointers);
  10275. END;
  10276. variable := type.cellScope.firstVariable;
  10277. WHILE(variable # NIL) DO
  10278. IF ~(variable.untraced) THEN
  10279. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10280. END;
  10281. variable := variable.nextVariable;
  10282. END;
  10283. property := type.firstProperty;
  10284. WHILE(property # NIL) DO
  10285. IF ~(property.untraced) THEN
  10286. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10287. END;
  10288. property := property.nextProperty;
  10289. END;
  10290. parameter := type.firstParameter;
  10291. WHILE(parameter # NIL) DO
  10292. IF ~(parameter.untraced) THEN
  10293. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10294. END;
  10295. parameter := parameter.nextParameter;
  10296. END;
  10297. END;
  10298. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10299. WITH type: SyntaxTree.ArrayType DO
  10300. IF type.form= SyntaxTree.Static THEN
  10301. n := type.staticLength;
  10302. base := type.arrayBase.resolved;
  10303. WHILE(base IS SyntaxTree.ArrayType) DO
  10304. type := base(SyntaxTree.ArrayType);
  10305. n := n* type.staticLength;
  10306. base := type.arrayBase.resolved;
  10307. END;
  10308. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10309. IF SemanticChecker.ContainsPointer(base) THEN
  10310. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10311. FOR i := 0 TO n-1 DO
  10312. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10313. END;
  10314. END;
  10315. ELSE
  10316. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10317. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10318. END;
  10319. END;
  10320. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10321. WITH type: SyntaxTree.MathArrayType DO
  10322. IF type.form = SyntaxTree.Static THEN
  10323. n := type.staticLength;
  10324. base := type.arrayBase.resolved;
  10325. WHILE(base IS SyntaxTree.MathArrayType) DO
  10326. type := base(SyntaxTree.MathArrayType);
  10327. n := n* type.staticLength;
  10328. base := type.arrayBase.resolved;
  10329. END;
  10330. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10331. IF SemanticChecker.ContainsPointer(base) THEN
  10332. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10333. FOR i := 0 TO n-1 DO
  10334. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10335. END;
  10336. END;
  10337. ELSE
  10338. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10339. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10340. END
  10341. END;
  10342. (* ELSE no pointers in type *)
  10343. END;
  10344. END Pointers;
  10345. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10346. VAR position,i: LONGINT; ch: CHAR;
  10347. BEGIN
  10348. IF pool.Has(index) THEN
  10349. RETURN pool.GetInt(index)
  10350. ELSE
  10351. position := source.pc;
  10352. pool.PutInt(index, position);
  10353. Info(source, name);
  10354. i := 0;
  10355. REPEAT
  10356. ch := name[i]; INC(i);
  10357. Char( source, ch);
  10358. UNTIL ch = 0X;
  10359. END;
  10360. RETURN position;
  10361. END EnterDynamicName;
  10362. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10363. VAR name: Basic.SectionName; position: LONGINT;
  10364. BEGIN
  10365. IF pool.Has(index) THEN
  10366. RETURN pool.GetInt(index)
  10367. ELSE
  10368. StringPool.GetString(index, name);
  10369. position := EnterDynamicName(source,name,index, pool);
  10370. END;
  10371. RETURN position;
  10372. END DynamicName;
  10373. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10374. VAR section: IntermediateCode.Section;
  10375. BEGIN
  10376. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10377. IF implementationVisitor.backend.cooperative THEN
  10378. Info(section, "TypeDescriptor");
  10379. Basic.ToSegmentedName("BaseTypes.Array", name);
  10380. NamedSymbol(section, name,NIL, 0, 0);
  10381. BasePointer(section);
  10382. offset := 0;
  10383. ELSE
  10384. HeapBlock(mName,typeName,section,2);
  10385. Info(section, "HeapBlock");
  10386. (*
  10387. Symbol(section,section,2,0);
  10388. *)
  10389. Address(section,0); (* empty such that GC does not go on traversing *)
  10390. Info(section, "TypeDescriptor");
  10391. Address(section,0);
  10392. offset := section.pc;
  10393. END;
  10394. RETURN section
  10395. END NamedBlock;
  10396. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10397. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10398. BEGIN
  10399. COPY(moduleName,name);
  10400. Strings.Append(name,suffix);
  10401. Basic.ToSegmentedName(name, pooledName);
  10402. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10403. END Block;
  10404. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10405. VAR name: Basic.SegmentedName;
  10406. BEGIN
  10407. Info(source,"ArrayHeader");
  10408. IF implementationVisitor.backend.cooperative THEN
  10409. sizePC := source.pc;
  10410. Address(source,0);
  10411. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10412. IF baseType # "" THEN
  10413. Basic.ToSegmentedName(baseType, name);
  10414. NamedSymbol(source, name,NIL, 0, 0);
  10415. ELSE
  10416. Address(source,0);
  10417. END;
  10418. Address(source,0);
  10419. ELSE
  10420. Address(source,0);
  10421. Address(source,0);
  10422. (* first pointer for GC *)
  10423. IF hasPointer THEN
  10424. (* points to first element in the array, this is NOT the base type descriptor *)
  10425. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10426. ELSE
  10427. Address(source,0);
  10428. END;
  10429. sizePC := source.pc;
  10430. Address(source,0);
  10431. Info(source,"array data");
  10432. END;
  10433. END ArrayBlock;
  10434. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10435. BEGIN
  10436. IF implementationVisitor.backend.cooperative THEN
  10437. PatchSize(section, pc, size);
  10438. PatchSize(section, pc + 3, size);
  10439. ELSE
  10440. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10441. PatchSize(section, pc, size);
  10442. END;
  10443. END PatchArray;
  10444. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10445. VAR
  10446. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10447. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10448. poolMap: Basic.HashTableInt;
  10449. namePool: IntermediateCode.Section;
  10450. namePoolOffset: LONGINT;
  10451. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10452. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10453. BEGIN
  10454. Basic.SegmentedNameToString(s1.name,n1);
  10455. Basic.SegmentedNameToString(s2.name,n2);
  10456. index := 0;
  10457. ch1 := n1[index];
  10458. ch2 := n2[index];
  10459. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10460. INC(index);
  10461. ch1 := n1[index];
  10462. ch2 := n2[index];
  10463. END;
  10464. RETURN ch1 < ch2;
  10465. END Compare;
  10466. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10467. VAR
  10468. i, j: LONGINT;
  10469. x, t: Sections.Section;
  10470. BEGIN
  10471. IF lo < hi THEN
  10472. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10473. WHILE i <= j DO
  10474. WHILE Compare(list[i], x) DO INC(i) END;
  10475. WHILE Compare(x, list[j]) DO DEC(j) END;
  10476. IF i <= j THEN
  10477. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10478. INC(i); DEC(j)
  10479. END
  10480. END;
  10481. IF lo < j THEN QuickSort(list, lo, j) END;
  10482. IF i < hi THEN QuickSort(list, i, hi) END
  10483. END;
  10484. END QuickSort;
  10485. (*
  10486. ExportDesc* = RECORD
  10487. fp*: ADDRESS;
  10488. name* {UNTRACED}: DynamicName;
  10489. adr*: ADDRESS;
  10490. exports*: LONGINT;
  10491. dsc* {UNTRACED}: ExportArray
  10492. END;
  10493. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10494. *)
  10495. PROCEDURE ExportDesc2(
  10496. source: IntermediateCode.Section;
  10497. namePool: IntermediateCode.Section;
  10498. fingerPrinter: FingerPrinter.FingerPrinter;
  10499. symbol: Sections.Section;
  10500. name: StringPool.Index;
  10501. VAR patchAdr: LONGINT
  10502. ): BOOLEAN;
  10503. VAR fingerPrint: SyntaxTree.FingerPrint;
  10504. BEGIN
  10505. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10506. & (symbol.type # Sections.InlineCodeSection)
  10507. THEN
  10508. *)
  10509. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10510. & (symbol.type # Sections.InlineCodeSection))
  10511. THEN
  10512. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10513. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10514. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10515. Longint(source,fingerPrint.shallow);
  10516. ELSE
  10517. Longint(source, 0);
  10518. END;
  10519. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10520. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10521. Symbol(source, symbol,0,0);
  10522. patchAdr := source.pc;
  10523. Longint(source, 0);
  10524. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10525. Address(source,0);
  10526. END;
  10527. RETURN TRUE
  10528. ELSE
  10529. RETURN FALSE
  10530. END;
  10531. END ExportDesc2;
  10532. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10533. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10534. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10535. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10536. nextPatch: LONGINT;
  10537. TYPE
  10538. Scope = RECORD
  10539. elements: LONGINT;
  10540. gelements: LONGINT;
  10541. section: IntermediateCode.Section;
  10542. patchAdr: LONGINT;
  10543. arraySizePC: LONGINT;
  10544. beginPC: LONGINT; (* current scope start pc *)
  10545. END;
  10546. BEGIN
  10547. Basic.InitSegmentedName(prev);
  10548. olevel := -1;
  10549. scopes[0].section := source;
  10550. scopes[0].arraySizePC := MIN(LONGINT);
  10551. FOR s := 0 TO LEN(sections)-1 DO
  10552. symbol := sections[s];
  10553. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10554. this := sections[s].name;
  10555. level := 0;
  10556. WHILE (this[level] > 0) DO
  10557. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10558. INC(level);
  10559. END;
  10560. WHILE level < olevel DO
  10561. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10562. IF olevel > 0 THEN
  10563. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10564. nextPatch := scopes[olevel-1].patchAdr+1;
  10565. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10566. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10567. END;
  10568. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10569. DEC(olevel);
  10570. END;
  10571. IF (this[level] > 0) THEN
  10572. IF level > olevel THEN
  10573. (*TRACE("opening",level); *)
  10574. IF scopes[level].section = NIL THEN
  10575. arrayName := ".@ExportArray";
  10576. Strings.AppendInt(arrayName, level);
  10577. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10578. AddPointer(scopes[level].section,offset);
  10579. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10580. END;
  10581. scopes[level].beginPC := scopes[level].section.pc;
  10582. olevel := level;
  10583. scopes[olevel].elements := 0;
  10584. END;
  10585. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10586. sym := sections[s];
  10587. ELSE
  10588. sym := NIL;
  10589. END;
  10590. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10591. THEN
  10592. INC(scopes[olevel].elements);
  10593. END;
  10594. (* enter string in scope *)
  10595. INC(level);
  10596. END;
  10597. END;
  10598. Basic.SegmentedNameToString(this, name);
  10599. prev := this;
  10600. END;
  10601. END;
  10602. WHILE 0 <= olevel DO
  10603. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10604. IF olevel > 0 THEN
  10605. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10606. nextPatch := scopes[olevel-1].patchAdr+1;
  10607. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10608. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10609. END;
  10610. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10611. DEC(olevel);
  10612. END;
  10613. level := 0;
  10614. WHILE (level < LEN(scopes)) DO
  10615. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10616. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10617. END;
  10618. INC(level);
  10619. END;
  10620. END Export;
  10621. BEGIN
  10622. NEW(fingerPrinter, module.system);
  10623. NEW(poolMap, 64);
  10624. (* 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 *)
  10625. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10626. NEW(sectionArray, module.allSections.Length());
  10627. FOR i := 0 TO module.allSections.Length() - 1 DO
  10628. section := module.allSections.GetSection(i);
  10629. sectionArray[i] := section;
  10630. END;
  10631. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10632. Export(sectionArray^);
  10633. END ExportDesc;
  10634. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10635. VAR
  10636. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10637. BEGIN
  10638. Info(source, "exception table offsets array descriptor");
  10639. size := 0;
  10640. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10641. Info(source, "exception table content");
  10642. FOR i := 0 TO module.allSections.Length() - 1 DO
  10643. p := module.allSections.GetSection(i);
  10644. IF p.type = Sections.CodeSection THEN
  10645. finallyPC := p(IntermediateCode.Section).finally;
  10646. IF finallyPC>=0 THEN
  10647. Symbol( source, p, 0,0);
  10648. Symbol( source, p, finallyPC, 0);
  10649. Symbol( source, p, finallyPC,0);
  10650. INC(size);
  10651. END;
  10652. END
  10653. END;
  10654. PatchArray(source,sizePC,size);
  10655. END ExceptionArray;
  10656. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10657. VAR i: LONGINT; ch: CHAR;
  10658. BEGIN
  10659. i := 0;
  10660. REPEAT
  10661. ch := name[i]; INC(i);
  10662. Char( section, ch);
  10663. UNTIL ch = 0X;
  10664. WHILE i < 32 DO
  10665. Char( section, 0X); INC(i);
  10666. END;
  10667. END Name;
  10668. PROCEDURE References(section: IntermediateCode.Section);
  10669. CONST
  10670. sfTypeNone = 0X;
  10671. sfTypeCHAR = 01X;
  10672. sfTypeCHAR8 = 02X;
  10673. sfTypeCHAR16 = 03X;
  10674. sfTypeCHAR32 = 04X;
  10675. sfTypeRANGE = 05X;
  10676. sfTypeSHORTINT = 06X;
  10677. sfTypeINTEGER = 07X;
  10678. sfTypeLONGINT = 08X;
  10679. sfTypeHUGEINT = 09X;
  10680. sfTypeWORD = 0AX;
  10681. sfTypeLONGWORD = 0BX;
  10682. sfTypeSIGNED8 = 0CX;
  10683. sfTypeSIGNED16 = 0DX;
  10684. sfTypeSIGNED32 = 0EX;
  10685. sfTypeSIGNED64 = 0FX;
  10686. sfTypeUNSIGNED8 = 10X;
  10687. sfTypeUNSIGNED16 = 11X;
  10688. sfTypeUNSIGNED32 = 12X;
  10689. sfTypeUNSIGNED64 = 13X;
  10690. sfTypeREAL = 14X;
  10691. sfTypeLONGREAL = 15X;
  10692. sfTypeCOMPLEX = 16X;
  10693. sfTypeLONGCOMPLEX = 17X;
  10694. sfTypeBOOLEAN = 18X;
  10695. sfTypeSET = 19X;
  10696. sfTypeANY = 1AX;
  10697. sfTypeOBJECT = 1BX;
  10698. sfTypeBYTE = 1CX;
  10699. sfTypeADDRESS = 1DX;
  10700. sfTypeSIZE = 1EX;
  10701. sfTypeIndirect = 1FX;
  10702. sfTypeRecord = 20X;
  10703. sfTypePointerToRecord = 21X;
  10704. sfTypePointerToArray = 22X;
  10705. sfTypeOpenArray = 23X;
  10706. sfTypeStaticArray = 24X;
  10707. sfTypeDynamicArray = 25X;
  10708. sfTypeMathStaticArray = 26X;
  10709. sfTypeMathOpenArray = 27X;
  10710. sfTypeMathTensor = 28X;
  10711. sfTypeDelegate = 29X;
  10712. sfTypeENUM = 2AX;
  10713. sfTypeCELL = 2BX;
  10714. sfTypePORT = 2CX;
  10715. sfIN = 0X;
  10716. sfOUT = 1X;
  10717. flagDelegate = 0;
  10718. flagConstructor = 1;
  10719. (* variable / parameter addressing modes *)
  10720. sfAbsolute = 0X; (* global vars *)
  10721. sfRelative = 1X; (* variables, value parameters *)
  10722. sfIndirect = 2X; (* var parameters *)
  10723. sfScopeBegin = 0F0X;
  10724. sfScopeEnd = 0F1X;
  10725. sfProcedure = 0F2X;
  10726. sfVariable = 0F3X;
  10727. sfTypeDeclaration = 0F4X;
  10728. sfModule = 0FFX;
  10729. RefInfo = TRUE;
  10730. VAR
  10731. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10732. indirectTypes: Basic.HashTable;
  10733. PROCEDURE CurrentIndex(): SIZE;
  10734. VAR i: LONGINT;
  10735. BEGIN
  10736. FOR i := startPC TO section.pc -1 DO
  10737. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10738. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10739. END;
  10740. startPC := section.pc;
  10741. RETURN lastOffset;
  10742. END CurrentIndex;
  10743. (*
  10744. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10745. Module = sfModule prevSymbol:SIZE name:String Scope.
  10746. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10747. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10748. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10749. Type =
  10750. sfTypePointerToRecord
  10751. | sfTypePointerToArray Type
  10752. | sfTypeOpenArray Type
  10753. | sfTypeDynamicArray Type
  10754. | sfTypeStaticArray length:SIZE Type
  10755. | sfTypeMathOpenArray Type
  10756. | sfTypeMathStaticArray length:SIZE Type
  10757. | sfTypeMathTensor Type
  10758. | sfTypeRecord tdAdr:ADDRESS
  10759. | sfTypeDelegate {Parameter} return:Type
  10760. | sfTypePort (sfIN | sfOUT)
  10761. | sfTypeBOOLEAN
  10762. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10763. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10764. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10765. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10766. | sfTypeWORD | sfTypeLONGWORD
  10767. | sfTypeREAL | sfTypeLONGREAL
  10768. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10769. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10770. | sfTypeIndirect offset:SIZE.
  10771. *)
  10772. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10773. VAR offset: SIZE;
  10774. BEGIN
  10775. IF indirectTypes.Has(type) THEN
  10776. offset := indirectTypes.GetInt(type);
  10777. Char(section, sfTypeIndirect);
  10778. Size(section, offset);
  10779. RETURN TRUE;
  10780. ELSE
  10781. indirectTypes.PutInt(type, CurrentIndex());
  10782. RETURN FALSE;
  10783. END;
  10784. END Indirect;
  10785. PROCEDURE NType(type: SyntaxTree.Type);
  10786. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10787. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10788. BEGIN
  10789. IF type = NIL THEN
  10790. Char(section, sfTypeNone)
  10791. ELSE
  10792. type := type.resolved;
  10793. size := type.sizeInBits;
  10794. WITH type:SyntaxTree.PointerType DO
  10795. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10796. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10797. Char(section, sfTypePointerToRecord);
  10798. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10799. ELSE
  10800. IF RefInfo THEN Info(section,"PointerToArray") END;
  10801. Char(section, sfTypePointerToArray);
  10802. NType(type.pointerBase);
  10803. END;
  10804. | type: SyntaxTree.ArrayType DO
  10805. IF ~Indirect(type) THEN
  10806. IF type.form = SyntaxTree.Open THEN
  10807. IF RefInfo THEN Info(section,"OpenArray") END;
  10808. Char(section, sfTypeOpenArray);
  10809. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10810. IF RefInfo THEN Info(section,"DynamicArray") END;
  10811. Char(section, sfTypeDynamicArray);
  10812. ELSIF type.form = SyntaxTree.Static THEN
  10813. IF RefInfo THEN Info(section,"StaticArray") END;
  10814. Char(section, sfTypeStaticArray);
  10815. Size(section, type.staticLength);
  10816. ELSE
  10817. HALT(100);
  10818. END;
  10819. NType(type.arrayBase);
  10820. END;
  10821. | type: SyntaxTree.MathArrayType DO
  10822. IF ~Indirect(type) THEN
  10823. IF type.form = SyntaxTree.Open THEN
  10824. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10825. Char(section, sfTypeMathOpenArray);
  10826. ELSIF type.form = SyntaxTree.Static THEN
  10827. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10828. Char(section, sfTypeMathStaticArray);
  10829. Size(section, type.staticLength);
  10830. ELSIF type.form = SyntaxTree.Tensor THEN
  10831. IF RefInfo THEN Info(section,"MathTensor") END;
  10832. Char(section, sfTypeMathTensor);
  10833. ELSE
  10834. HALT(100);
  10835. END;
  10836. NType(type.arrayBase);
  10837. END;
  10838. | type: SyntaxTree.RecordType DO
  10839. IF ~Indirect(type) THEN
  10840. IF type.pointerType # NIL (* OBJECT *) THEN
  10841. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10842. Char(section, sfTypePointerToRecord)
  10843. ELSE
  10844. IF RefInfo THEN Info(section,"Record") END;
  10845. Char(section, sfTypeRecord);
  10846. td := type.typeDeclaration;
  10847. IF RefInfo THEN Info(section,"TD") END;
  10848. IF (td # NIL) THEN
  10849. Global.GetSymbolSegmentedName(td,segmentedName);
  10850. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10851. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10852. ELSE
  10853. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10854. END;
  10855. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10856. Symbol(section, tir, 0, offset);
  10857. ELSE
  10858. Address(section, 0);
  10859. END;
  10860. END;
  10861. END;
  10862. | type: SyntaxTree.CellType DO
  10863. IF ~Indirect(type) THEN
  10864. IF RefInfo THEN Info(section,"Record") END;
  10865. Char(section, sfTypeRecord);
  10866. td := type.typeDeclaration;
  10867. IF RefInfo THEN Info(section,"TD") END;
  10868. IF (td # NIL) THEN
  10869. Global.GetSymbolSegmentedName(td,segmentedName);
  10870. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10871. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10872. ELSE
  10873. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10874. END;
  10875. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10876. Symbol(section, tir, 0, offset);
  10877. ELSE
  10878. Address(section, 0);
  10879. END;
  10880. END;
  10881. | type: SyntaxTree.PortType DO
  10882. Char(section, sfTypePORT);
  10883. IF type.direction = SyntaxTree.OutPort THEN
  10884. Char(section, sfOUT)
  10885. ELSE
  10886. Char(section, sfIN)
  10887. END;
  10888. | type: SyntaxTree.ProcedureType DO
  10889. IF ~Indirect(type) THEN
  10890. Char(section, sfTypeDelegate);
  10891. parameter := type.firstParameter;
  10892. WHILE(parameter # NIL) DO
  10893. NParameter(parameter, -1);
  10894. parameter := parameter.nextParameter;
  10895. END;
  10896. NType(type.returnType);
  10897. END;
  10898. | type:SyntaxTree.EnumerationType DO
  10899. Char(section, sfTypeENUM);
  10900. | type: SyntaxTree.BasicType DO
  10901. WITH type: SyntaxTree.BooleanType DO
  10902. IF RefInfo THEN Info(section,"Boolean") END;
  10903. Char(section, sfTypeBOOLEAN);
  10904. | type: SyntaxTree.CharacterType DO
  10905. IF type = module.system.characterType THEN
  10906. IF RefInfo THEN Info(section,"CHAR") END;
  10907. Char(section, sfTypeCHAR);
  10908. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10909. IF RefInfo THEN Info(section,"CHAR8") END;
  10910. Char(section, sfTypeCHAR8)
  10911. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10912. IF RefInfo THEN Info(section,"CHAR16") END;
  10913. Char(section, sfTypeCHAR16);
  10914. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10915. IF RefInfo THEN Info(section,"CHAR32") END;
  10916. Char(section, sfTypeCHAR32);
  10917. ELSE
  10918. HALT(100);
  10919. END;
  10920. |type: SyntaxTree.IntegerType DO
  10921. IF type(SyntaxTree.IntegerType).signed THEN
  10922. IF (type = module.system.shortintType) THEN
  10923. IF RefInfo THEN Info(section,"SHORTINT") END;
  10924. Char(section, sfTypeSHORTINT)
  10925. ELSIF (type = module.system.integerType) THEN
  10926. IF RefInfo THEN Info(section,"INTEGER") END;
  10927. Char(section, sfTypeINTEGER)
  10928. ELSIF (type = module.system.longintType) THEN
  10929. IF RefInfo THEN Info(section,"LONGINT") END;
  10930. Char(section, sfTypeLONGINT)
  10931. ELSIF (type = module.system.hugeintType) THEN
  10932. IF RefInfo THEN Info(section,"HUGEINT") END;
  10933. Char(section, sfTypeHUGEINT)
  10934. ELSIF (type = module.system.wordType) THEN
  10935. IF RefInfo THEN Info(section,"WORD") END;
  10936. Char(section, sfTypeWORD)
  10937. ELSIF (type = module.system.longWordType) THEN
  10938. IF RefInfo THEN Info(section,"LONGWORD") END;
  10939. Char(section, sfTypeLONGWORD);
  10940. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10941. IF RefInfo THEN Info(section,"SIGNED8") END;
  10942. Char(section, sfTypeSIGNED8)
  10943. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10944. IF RefInfo THEN Info(section,"SIGNED16") END;
  10945. Char(section, sfTypeSIGNED16)
  10946. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10947. IF RefInfo THEN Info(section,"SIGNED32") END;
  10948. Char(section, sfTypeSIGNED32)
  10949. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10950. IF RefInfo THEN Info(section,"SIGNED64") END;
  10951. Char(section, sfTypeSIGNED64)
  10952. ELSE
  10953. HALT(100);
  10954. END
  10955. ELSE (* unsigned *)
  10956. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10957. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10958. Char(section, sfTypeUNSIGNED8)
  10959. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10960. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10961. Char(section, sfTypeUNSIGNED16)
  10962. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10963. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10964. Char(section, sfTypeUNSIGNED32)
  10965. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10966. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10967. Char(section, sfTypeUNSIGNED64)
  10968. ELSE
  10969. HALT(100)
  10970. END
  10971. END;
  10972. | type: SyntaxTree.FloatType DO
  10973. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10974. IF RefInfo THEN Info(section,"REAL") END;
  10975. Char(section, sfTypeREAL);
  10976. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  10977. IF RefInfo THEN Info(section,"LONGREAL") END;
  10978. Char(section, sfTypeLONGREAL);
  10979. ELSE
  10980. HALT(100);
  10981. END;
  10982. |type: SyntaxTree.ComplexType DO
  10983. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  10984. IF RefInfo THEN Info(section,"COMPLEX") END;
  10985. Char(section, sfTypeCOMPLEX);
  10986. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  10987. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  10988. Char(section, sfTypeLONGCOMPLEX);
  10989. ELSE
  10990. HALT(100);
  10991. END;
  10992. |type:SyntaxTree.SetType DO
  10993. IF RefInfo THEN Info(section,"SET") END;
  10994. Char(section, sfTypeSET);
  10995. |type:SyntaxTree.AnyType DO
  10996. IF RefInfo THEN Info(section,"ANY") END;
  10997. Char(section, sfTypeANY);
  10998. |type:SyntaxTree.ObjectType DO
  10999. IF RefInfo THEN Info(section,"OBJECT") END;
  11000. Char(section, sfTypeOBJECT);
  11001. |type:SyntaxTree.ByteType DO
  11002. IF RefInfo THEN Info(section,"BYTE") END;
  11003. Char(section, sfTypeBYTE);
  11004. |type:SyntaxTree.RangeType DO
  11005. IF RefInfo THEN Info(section,"RANGE") END;
  11006. Char(section, sfTypeRANGE)
  11007. |type:SyntaxTree.AddressType DO
  11008. IF RefInfo THEN Info(section,"ADDRESS") END;
  11009. Char(section, sfTypeADDRESS)
  11010. |type:SyntaxTree.SizeType DO
  11011. IF RefInfo THEN Info(section,"SIZE") END;
  11012. Char(section, sfTypeSIZE)
  11013. ELSE
  11014. HALT(100)
  11015. END;
  11016. ELSE HALT(101);
  11017. END;
  11018. END;
  11019. END NType;
  11020. (*
  11021. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11022. *)
  11023. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11024. VAR pos: LONGINT; type: SyntaxTree.Type;
  11025. BEGIN
  11026. IF RefInfo THEN Info(section, "Parameter") END;
  11027. Char(section, sfVariable);
  11028. Size(section, procOffset);
  11029. String0(section, parameter.name);
  11030. type := parameter.type.resolved;
  11031. IF parameter.kind = SyntaxTree.VarParameter THEN
  11032. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11033. ELSE Char(section, sfIndirect)
  11034. END;
  11035. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11036. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11037. Char(section, sfIndirect);
  11038. ELSE
  11039. Char(section, sfRelative);
  11040. END;
  11041. ELSE
  11042. Char(section, sfRelative);
  11043. END;
  11044. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11045. NType(parameter.type);
  11046. END NParameter;
  11047. (*
  11048. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11049. *)
  11050. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11051. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11052. name: Basic.SegmentedName; flags: SET;
  11053. BEGIN
  11054. IF RefInfo THEN Info(section, "Procedure") END;
  11055. pos := CurrentIndex();
  11056. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11057. Char(section, sfProcedure);
  11058. Size(section, scopeOffset);
  11059. String0(section,procedure.name);
  11060. s := module.allSections.FindBySymbol(procedure);
  11061. Symbol(section,s,0,0); (* start *)
  11062. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11063. flags := {};
  11064. IF procedureType.isDelegate THEN
  11065. INCL(flags, flagDelegate);
  11066. END;
  11067. IF procedure.isConstructor THEN
  11068. INCL(flags, flagConstructor);
  11069. END;
  11070. Set(section, flags);
  11071. Global.GetSymbolSegmentedName(procedure,name);
  11072. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11073. IF RefInfo THEN Info(section, "Parameters") END;
  11074. parameter := procedureType.firstParameter;
  11075. WHILE(parameter # NIL) DO
  11076. NParameter(parameter, pos);
  11077. parameter := parameter.nextParameter;
  11078. END;
  11079. IF RefInfo THEN Info(section, "ReturnType") END;
  11080. NType(procedureType.returnType);
  11081. NScope(procedure.procedureScope, pos);
  11082. END NProcedure;
  11083. (*
  11084. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11085. *)
  11086. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11087. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11088. BEGIN
  11089. IF RefInfo THEN Info(section, "Variable") END;
  11090. pos := CurrentIndex();
  11091. Char(section, sfVariable);
  11092. Size(section, scopeOffset);
  11093. String0(section, variable.name);
  11094. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11095. Char(section, sfAbsolute);
  11096. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11097. NamedSymbol(section, sn,variable, 0,0);
  11098. ELSE
  11099. Char(section, sfRelative);
  11100. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11101. END;
  11102. NType(variable.type);
  11103. s := module.allSections.FindBySymbol(variable);
  11104. END NVariable;
  11105. (*
  11106. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11107. *)
  11108. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11109. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11110. BEGIN
  11111. IF typeDeclaration = NIL THEN RETURN END;
  11112. pos := CurrentIndex();
  11113. s := module.allSections.FindBySymbol(typeDeclaration);
  11114. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11115. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11116. Char(section, sfTypeDeclaration);
  11117. Size(section, scopeOffset);
  11118. String0(section, typeDeclaration.name);
  11119. declared := typeDeclaration.declaredType.resolved;
  11120. IF (declared IS SyntaxTree.PointerType) THEN
  11121. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11122. END;
  11123. WITH declared: SyntaxTree.RecordType DO
  11124. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11125. Symbol(section, s, 0, offset);
  11126. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11127. Basic.AppendToSegmentedName(name,".@Info");
  11128. s := module.allSections.FindByName(name);
  11129. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11130. NScope(declared.recordScope, pos);
  11131. |declared: SyntaxTree.CellType DO
  11132. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11133. Symbol(section, s, 0, offset);
  11134. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11135. Basic.AppendToSegmentedName(name,".@Info");
  11136. s := module.allSections.FindByName(name);
  11137. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11138. NScope(declared.cellScope, pos);
  11139. ELSE
  11140. Address(section, 0);
  11141. END;
  11142. END NTypeDeclaration;
  11143. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11144. VAR pos: LONGINT;
  11145. BEGIN
  11146. pos := CurrentIndex();
  11147. Char(section,sfModule);
  11148. Size(section, prevSymbol);
  11149. String0(section, module.name);
  11150. NScope(module.moduleScope, pos);
  11151. END NModule;
  11152. (*
  11153. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11154. *)
  11155. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11156. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11157. BEGIN
  11158. IF scope = NIL THEN RETURN END;
  11159. IF RefInfo THEN Info(section, "Scope") END;
  11160. Char(section, sfScopeBegin);
  11161. variable := scope.firstVariable;
  11162. WHILE (variable # NIL) DO
  11163. NVariable(variable, prevSymbol);
  11164. variable := variable.nextVariable;
  11165. END;
  11166. WITH scope: SyntaxTree.ModuleScope DO
  11167. bodyProcedure := scope.bodyProcedure;
  11168. |scope: SyntaxTree.RecordScope DO
  11169. bodyProcedure := scope.bodyProcedure;
  11170. ELSE
  11171. bodyProcedure := NIL;
  11172. END;
  11173. IF bodyProcedure # NIL THEN
  11174. NProcedure(bodyProcedure, prevSymbol)
  11175. END;
  11176. procedure := scope.firstProcedure;
  11177. WHILE procedure # NIL DO
  11178. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11179. procedure := procedure.nextProcedure;
  11180. END;
  11181. typeDeclaration := scope.firstTypeDeclaration;
  11182. WHILE typeDeclaration # NIL DO
  11183. NTypeDeclaration(typeDeclaration, prevSymbol);
  11184. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11185. END;
  11186. Char(section, sfScopeEnd); (* scope ends *)
  11187. END NScope;
  11188. BEGIN
  11189. NEW(indirectTypes, 32);
  11190. ArrayBlock(section,sizePC,"", FALSE);
  11191. startPC := section.pc;
  11192. NModule(module.module, -1);
  11193. PatchArray(section,sizePC,CurrentIndex());
  11194. END References;
  11195. (*
  11196. Command* = RECORD
  11197. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11198. name*: Name; (* name of the procedure *)
  11199. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11200. entryAdr* : ADDRESS; (* entry address of procedure *)
  11201. END;
  11202. *)
  11203. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11204. VAR
  11205. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11206. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11207. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11208. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11209. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11210. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11211. BEGIN
  11212. RETURN
  11213. (type = NIL) OR
  11214. (type.resolved IS SyntaxTree.RecordType) OR
  11215. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11216. (type.resolved IS SyntaxTree.AnyType);
  11217. END TypeAllowed;
  11218. BEGIN
  11219. numberParameters := procedureType.numberParameters;
  11220. RETURN
  11221. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11222. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11223. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11224. END GetProcedureAllowed;
  11225. PROCEDURE WriteType(type : SyntaxTree.Type);
  11226. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11227. name: Basic.SegmentedName; offset: LONGINT;
  11228. BEGIN
  11229. IF type = NIL THEN
  11230. Address(source,0);
  11231. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11232. Address(source,1);
  11233. ELSE
  11234. type := type.resolved;
  11235. IF type IS SyntaxTree.PointerType THEN
  11236. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11237. END;
  11238. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11239. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11240. name[0] := typeDeclaration.name; name[1] := -1;
  11241. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11242. ASSERT(section # NIL);
  11243. ELSE
  11244. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11245. (* TODO *)
  11246. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11247. END;
  11248. IF implementationVisitor.backend.cooperative THEN
  11249. offset := 0;
  11250. ELSE
  11251. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11252. END;
  11253. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11254. END;
  11255. END WriteType;
  11256. BEGIN
  11257. Info(source, "command array descriptor");
  11258. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11259. numberCommands := 0;
  11260. Info(source, "command array content");
  11261. FOR i := 0 TO module.allSections.Length() - 1 DO
  11262. p := module.allSections.GetSection(i);
  11263. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11264. procedure := p.symbol(SyntaxTree.Procedure);
  11265. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11266. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11267. procedure.GetName(name);
  11268. Name(source,name);
  11269. numberParameters := procedureType.numberParameters;
  11270. (* offset of type of first parameter *)
  11271. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11272. ELSE WriteType(procedureType.firstParameter.type)
  11273. END;
  11274. (* offset of type of return parameter *)
  11275. WriteType(procedureType.returnType);
  11276. (* command name *)
  11277. (* command code offset *)
  11278. Symbol(source,p,0,0);
  11279. INC(numberCommands);
  11280. IF Trace THEN
  11281. D.Ln;
  11282. END;
  11283. END;
  11284. END
  11285. END;
  11286. PatchArray(source,sizePC,numberCommands);
  11287. END CommandArray;
  11288. (* to prevent from double import of different module aliases *)
  11289. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11290. VAR i: SyntaxTree.Import;
  11291. BEGIN
  11292. i := module.module.moduleScope.firstImport;
  11293. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11294. i := i.nextImport;
  11295. END;
  11296. RETURN i = import
  11297. END IsFirstDirectOccurence;
  11298. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11299. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11300. BEGIN
  11301. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11302. ArrayBlock(source,pc,"", FALSE);
  11303. Info(source, "import module array data");
  11304. IF implementationVisitor.backend.cooperative THEN
  11305. offset := 0;
  11306. ELSE
  11307. IF module.system.addressType.sizeInBits = 64 THEN
  11308. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11309. ELSE
  11310. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11311. END;
  11312. END;
  11313. import := module.module.moduleScope.firstImport;
  11314. numberImports := 0;
  11315. WHILE import # NIL DO
  11316. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11317. Global.GetModuleSegmentedName(import.module,name);
  11318. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11319. NamedSymbol(source, name, NIL, 0, offset);
  11320. INC(numberImports);
  11321. END;
  11322. import := import.nextImport
  11323. END;
  11324. PatchArray(source,pc,numberImports);
  11325. END ImportsArray;
  11326. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11327. VAR
  11328. p: Sections.Section; sizePC, size, i: LONGINT;
  11329. BEGIN
  11330. Info(source, "Type info section");
  11331. size := 0;
  11332. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11333. FOR i := 0 TO module.allSections.Length() - 1 DO
  11334. p := module.allSections.GetSection(i);
  11335. WITH p: IntermediateCode.Section DO
  11336. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11337. Symbol(source,p,0,0);
  11338. INC(size);
  11339. END;
  11340. END
  11341. END;
  11342. PatchArray(source,sizePC,size);
  11343. END TypeInfoSection;
  11344. (*
  11345. ProcTableEntry* = RECORD
  11346. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11347. noPtr*: LONGINT;
  11348. END;
  11349. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11350. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11351. *)
  11352. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11353. VAR
  11354. numberPointers: SIZE;
  11355. procedure: SyntaxTree.Procedure;
  11356. BEGIN
  11357. Info(section,"pcFrom");
  11358. Symbol(section,procedureSection,0,0);
  11359. Info(section,"pcTo");
  11360. Symbol(section, procedureSection, procedureSection.pc, 0);
  11361. Info(section,"pointer to offsets array");
  11362. Symbol(section, section,section.pc+1,0);
  11363. Info(section,"offsets array");
  11364. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11365. PointerArray(section, procedure.procedureScope, numberPointers);
  11366. END ProcedureDescriptor;
  11367. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11368. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11369. BEGIN
  11370. name := procedureSection.name;
  11371. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11372. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11373. ProcedureDescriptor(dest, procedureSection);
  11374. Symbol(section, dest, offset, 0);
  11375. END ProcedureDescriptorPointer;
  11376. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11377. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11378. BEGIN
  11379. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11380. numberProcs := 0;
  11381. FOR i := 0 TO module.allSections.Length() - 1 DO
  11382. destination := module.allSections.GetSection(i);
  11383. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11384. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11385. INC(numberProcs);
  11386. END
  11387. END;
  11388. PatchArray(section, sizePC, numberProcs);
  11389. END ProcedureDescriptorArray;
  11390. (*
  11391. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11392. VAR
  11393. next*: Module; (** once a module is published, all fields are read-only *)
  11394. name*: Name;
  11395. init, published: BOOLEAN;
  11396. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11397. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11398. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11399. command*: POINTER TO ARRAY OF Command;
  11400. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11401. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11402. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11403. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11404. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11405. data*, code*: Bytes;
  11406. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11407. export*: ExportDesc;
  11408. term*: TerminationHandler;
  11409. exTable*: ExceptionTable;
  11410. noProcs*: LONGINT;
  11411. firstProc*: ADDRESS; <- done by loader
  11412. maxPtrs*: LONGINT;
  11413. crc*: LONGINT;
  11414. *)
  11415. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11416. BEGIN
  11417. Info(section, "cycle");
  11418. Size(section,0);
  11419. Info(section, "references");
  11420. Size(section,0);
  11421. Info(section, "nextMarked");
  11422. Address(section,0);
  11423. Info(section, "nextWatched");
  11424. Address(section,0);
  11425. END BasePointer;
  11426. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11427. BEGIN
  11428. BasePointer(section);
  11429. Info(section, "action");
  11430. Address(section,0);
  11431. Info(section, "monitor");
  11432. Address(section,0);
  11433. END BaseObject;
  11434. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11435. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11436. pooledName: Basic.SegmentedName;
  11437. symbol: SyntaxTree.Symbol;
  11438. BEGIN
  11439. Global.GetModuleName(module.module,name);
  11440. Strings.Append(name,".@Module.@Descriptor");
  11441. Basic.ToSegmentedName(name, pooledName);
  11442. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11443. Symbol(section,descriptorSection,0,0);
  11444. Info(descriptorSection, "descriptor");
  11445. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11446. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11447. Address(descriptorSection,0);
  11448. Global.GetModuleName(module.module,name);
  11449. Strings.Append(name,".@Trace");
  11450. Basic.ToSegmentedName(name, pooledName);
  11451. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11452. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11453. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11454. END ModuleDescriptor;
  11455. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11456. VAR name: ARRAY 128 OF CHAR;
  11457. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11458. symbol: SyntaxTree.Symbol;
  11459. BEGIN
  11460. Global.GetModuleName(module.module,name);
  11461. Strings.Append(name,".@Module");
  11462. Basic.ToSegmentedName(name, pooledName);
  11463. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11464. moduleSection.SetExported(TRUE);
  11465. IF moduleSection.pc = 0 THEN
  11466. IF implementationVisitor.backend.cooperative THEN
  11467. Info(moduleSection, "descriptor");
  11468. ModuleDescriptor(moduleSection);
  11469. BaseObject(moduleSection);
  11470. implementationVisitor.CreateTraceModuleMethod(module.module);
  11471. ELSE
  11472. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11473. Info(moduleSection, "HeapBlock");
  11474. Symbol(moduleSection,moduleSection,2,0);
  11475. Info(moduleSection, "TypeDescriptor");
  11476. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11477. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11478. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11479. END;
  11480. END;
  11481. RETURN moduleSection;
  11482. END ModuleSection;
  11483. PROCEDURE NewModuleInfo();
  11484. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11485. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11486. sectionName: Basic.SectionName;
  11487. CONST MPO=-40000000H;
  11488. BEGIN
  11489. (*
  11490. TypeDesc* = POINTER TO RECORD
  11491. descSize: LONGINT;
  11492. sentinel: LONGINT; (* = MPO-4 *)
  11493. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11494. flags*: SET;
  11495. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11496. name*: Name;
  11497. END;
  11498. *)
  11499. Global.GetSymbolSegmentedName(module.module,name);
  11500. Basic.AppendToSegmentedName(name,".@Info");
  11501. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11502. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11503. Address(source,MPO-4);
  11504. Info(source, "type tag pointer");
  11505. Address( source,0);
  11506. Info(source, "type flags");
  11507. flags := {};
  11508. Set( source, flags);
  11509. Info(source, "pointer to module");
  11510. moduleSection := ModuleSection();
  11511. Symbol( source, moduleSection, moduleSection.pc,0);
  11512. Info(source, "type name");
  11513. i := 0;
  11514. sectionName := "@Self";
  11515. (*
  11516. Global.GetSymbolSegmentedName(td,name);
  11517. Basic.SegmentedNameToString(name, sectionName);
  11518. *)
  11519. Name(source,sectionName);
  11520. source.SetReferenced(FALSE);
  11521. patchInfoPC := source.pc;
  11522. Size(source, 0);
  11523. END NewModuleInfo;
  11524. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11525. VAR
  11526. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11527. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11528. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11529. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11530. referenceSectionOffset : LONGINT;
  11531. name: Basic.SegmentedName; offset: LONGINT;
  11532. BEGIN
  11533. NewModuleInfo();
  11534. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11535. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11536. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11537. ImportsArray(importSection);
  11538. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11539. CommandArray(commandsSection);
  11540. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11541. ExceptionArray(exceptionSection);
  11542. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11543. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11544. TypeInfoSection(typeInfoSection);
  11545. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11546. References(referenceSection);
  11547. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11548. ProcedureDescriptorArray(procTableSection, numberProcs);
  11549. name := "Heaps.AnyPtr";
  11550. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11551. (* set base pointer *)
  11552. NamedSymbolAt(procTableSection, procTableSectionOffset -1, name, NIL, 0, offset);
  11553. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11554. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11555. moduleSection := ModuleSection();
  11556. Info(moduleSection, "nextRoot*: RootObject");
  11557. Address(moduleSection,0);
  11558. Info(moduleSection, "next*: Module");
  11559. Address(moduleSection,0);
  11560. Info(moduleSection, "name*: Name");
  11561. Name(moduleSection,moduleName);
  11562. Info(moduleSection, "init, published: BOOLEAN");
  11563. Boolean(moduleSection,FALSE);
  11564. Boolean(moduleSection,FALSE);
  11565. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11566. Boolean(moduleSection,FALSE);
  11567. Boolean(moduleSection,FALSE);
  11568. Info(moduleSection, "refcnt*: LONGINT");
  11569. Longint(moduleSection,0);
  11570. Info(moduleSection, "sb*: ADDRESS");
  11571. Address(moduleSection,0);
  11572. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11573. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11574. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11575. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11576. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11577. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11578. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11579. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11580. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11581. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11582. Info(moduleSection, "procTable*: ProcTable");
  11583. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11584. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11585. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11586. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11587. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11588. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11589. Info(moduleSection, "export*: ExportDesc");
  11590. ExportDesc(moduleSection);
  11591. Info(moduleSection, "term*: TerminationHandler");
  11592. Address(moduleSection,0);
  11593. Info(moduleSection, "exTable*: ExceptionTable");
  11594. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11595. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11596. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11597. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11598. Info(moduleSection, "crc*: LONGINT");
  11599. Longint(moduleSection, 0); (*! must be implemented *)
  11600. Info(moduleSection, "body*: ADDRESS");
  11601. Symbol(moduleSection, bodyProc, 0,0);
  11602. IF implementationVisitor.backend.cooperative THEN
  11603. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11604. Info(moduleSection, "monitor.owner");
  11605. Address(moduleSection,0);
  11606. Info(moduleSection, "monitor.nestingLevel");
  11607. Address(moduleSection,0);
  11608. Info(moduleSection, "monitor.blockedQueue");
  11609. Address(moduleSection,0); Address(moduleSection,0);
  11610. Info(moduleSection, "monitor.waitingQueue");
  11611. Address(moduleSection,0); Address(moduleSection,0);
  11612. Info(moduleSection, "monitor.waitingSentinel");
  11613. Address(moduleSection,0);
  11614. END;
  11615. END Module;
  11616. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11617. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11618. BEGIN
  11619. ArrayBlock(source,pc,"",FALSE);
  11620. Info(source, "pointer offsets array data");
  11621. IF scope IS SyntaxTree.RecordScope THEN
  11622. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11623. ELSIF scope IS SyntaxTree.CellScope THEN
  11624. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11625. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11626. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11627. WHILE variable # NIL DO
  11628. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11629. symbol := module.allSections.FindBySymbol(variable);
  11630. ASSERT(symbol # NIL);
  11631. Pointers(0,symbol, source,variable.type,numberPointers);
  11632. END;
  11633. variable := variable.nextVariable;
  11634. END;
  11635. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11636. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11637. WHILE parameter # NIL DO
  11638. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11639. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11640. END;
  11641. parameter := parameter.nextParameter;
  11642. END;
  11643. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11644. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11645. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11646. IF implementationVisitor.backend.preciseGC THEN
  11647. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11648. END;
  11649. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11650. END;
  11651. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11652. WHILE(variable # NIL) DO
  11653. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11654. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11655. END;
  11656. variable := variable.nextVariable
  11657. END;
  11658. END;
  11659. PatchArray(source,pc,numberPointers);
  11660. END PointerArray;
  11661. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11662. VAR
  11663. name: Basic.SegmentedName;
  11664. section: IntermediateCode.Section;
  11665. BEGIN
  11666. ASSERT(implementationVisitor.newObjectFile);
  11667. Global.GetSymbolSegmentedName(symbol,name);
  11668. Basic.AppendToSegmentedName(name,suffix);
  11669. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11670. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11671. Info(section, "HeapBlock");
  11672. Address(section,0); (* empty such that GC does not go on traversing *)
  11673. Info(section, suffix);
  11674. Address(section,0);
  11675. pc := section.pc;
  11676. RETURN section;
  11677. END SymbolSection;
  11678. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11679. VAR recordType: SyntaxTree.RecordType;
  11680. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11681. section: Sections.Section; cellType: SyntaxTree.CellType;
  11682. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11683. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11684. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11685. sectionName: Basic.SectionName;
  11686. CONST MPO=-40000000H;
  11687. BEGIN
  11688. (*
  11689. TypeDesc* = POINTER TO RECORD
  11690. descSize: LONGINT;
  11691. sentinel: LONGINT; (* = MPO-4 *)
  11692. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11693. flags*: SET;
  11694. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11695. name*: Name;
  11696. END;
  11697. *)
  11698. (* source := module.sections.FindByName(...) *)
  11699. Global.GetSymbolSegmentedName(td,name);
  11700. Basic.AppendToSegmentedName(name,".@Info");
  11701. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11702. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11703. Address(source,MPO-4);
  11704. Info(source, "type tag pointer");
  11705. Symbol( source, tag, offset, 0);
  11706. Info(source, "type flags");
  11707. flags := {};
  11708. IF isProtected THEN INCL(flags,31) END;
  11709. Set( source, flags);
  11710. Info(source, "pointer to module");
  11711. moduleSection := ModuleSection();
  11712. Symbol( source, moduleSection, moduleSection.pc,0);
  11713. Info(source, "type name");
  11714. i := 0;
  11715. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11716. (*
  11717. Global.GetSymbolSegmentedName(td,name);
  11718. Basic.SegmentedNameToString(name, sectionName);
  11719. *)
  11720. Name(source,sectionName);
  11721. source.SetReferenced(FALSE);
  11722. Size(source, 0);
  11723. RETURN source;
  11724. END NewTypeDescriptorInfo;
  11725. PROCEDURE NewTypeDescriptor;
  11726. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11727. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11728. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11729. numberPointers: LONGINT; padding, i: LONGINT;
  11730. CONST MPO=-40000000H;
  11731. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11732. VAR i: LONGINT;
  11733. PROCEDURE Td(record: SyntaxTree.RecordType);
  11734. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11735. BEGIN
  11736. IF record # NIL THEN
  11737. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11738. baseTD := record.typeDeclaration;
  11739. Global.GetSymbolSegmentedName(baseTD,name);
  11740. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11741. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11742. ELSE
  11743. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11744. END;
  11745. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11746. Symbol(source, tir, 0, offset);
  11747. IF reverse THEN Td(record.GetBaseRecord()) END;
  11748. END;
  11749. END Td;
  11750. BEGIN
  11751. Info(source, "tag table");
  11752. baseRecord := recordType;
  11753. i := 0;
  11754. WHILE baseRecord # NIL DO
  11755. INC(i);
  11756. baseRecord := baseRecord.GetBaseRecord();
  11757. END;
  11758. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11759. IF ~reverse THEN Td(recordType) END;
  11760. WHILE i < size DO
  11761. Address(source,0);
  11762. INC(i);
  11763. END;
  11764. IF reverse THEN Td(recordType) END;
  11765. END TdTable;
  11766. PROCEDURE MethodTable(reverse: BOOLEAN);
  11767. VAR i,methods: LONGINT;
  11768. BEGIN
  11769. Info(source, "method table");
  11770. IF recordType # NIL THEN
  11771. methods := recordType.recordScope.numberMethods;
  11772. IF reverse THEN
  11773. FOR i := methods-1 TO 0 BY -1 DO
  11774. procedure := recordType.recordScope.FindMethod(i);
  11775. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11776. NamedSymbol(source, name,procedure, 0,0);
  11777. END;
  11778. ELSE
  11779. FOR i := 0 TO methods-1 DO
  11780. procedure := recordType.recordScope.FindMethod(i);
  11781. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11782. NamedSymbol(source, name,procedure, 0,0);
  11783. END;
  11784. END;
  11785. END;
  11786. END MethodTable;
  11787. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11788. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11789. BEGIN
  11790. Info(source, "method table");
  11791. baseRecord := recordType;
  11792. WHILE baseRecord.baseType # NIL DO
  11793. baseRecord := baseRecord.GetBaseRecord ();
  11794. END;
  11795. GetRecordTypeName (baseRecord, name);
  11796. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11797. IF name = stackFrame THEN
  11798. start := 0;
  11799. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11800. baseRecord := recordType;
  11801. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11802. baseRecord := baseRecord.GetBaseRecord ();
  11803. END;
  11804. IF baseRecord # NIL THEN
  11805. GetRecordTypeName (baseRecord, name);
  11806. IF pointer & ~baseRecord.isObject THEN
  11807. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11808. END;
  11809. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11810. ELSIF recordType.isObject THEN
  11811. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11812. ELSIF pointer THEN
  11813. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11814. ELSE
  11815. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11816. END;
  11817. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11818. Symbol(source, tir, 0, 0);
  11819. start := 0;
  11820. baseRecord := recordType;
  11821. WHILE (baseRecord # NIL) DO
  11822. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11823. baseRecord := baseRecord.GetBaseRecord ();
  11824. END;
  11825. ELSE
  11826. (* explicit trace method: *)
  11827. procedure := recordType.recordScope.FindMethod(0);
  11828. IF ~procedure.isFinalizer THEN
  11829. Global.GetSymbolSegmentedName(procedure, name);
  11830. NamedSymbol(source, name,procedure, 0,0);
  11831. END;
  11832. start := 1;
  11833. END;
  11834. IF (name # stackFrame) & recordType.isObject THEN
  11835. baseRecord := recordType;
  11836. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11837. baseRecord := baseRecord.GetBaseRecord ();
  11838. END;
  11839. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11840. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11841. ELSE
  11842. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11843. END;
  11844. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11845. Symbol(source, tir, 0, 0);
  11846. END;
  11847. methods := recordType.recordScope.numberMethods;
  11848. FOR i := start TO methods-1 DO
  11849. procedure := recordType.recordScope.FindMethod(i);
  11850. IF ~procedure.isFinalizer THEN
  11851. Global.GetSymbolSegmentedName(procedure, name);
  11852. NamedSymbol(source, name,procedure, 0,0);
  11853. END;
  11854. END;
  11855. END CooperativeMethodTable;
  11856. BEGIN
  11857. Global.GetSymbolSegmentedName(td,name);
  11858. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11859. source.SetExported(IsExported(td));
  11860. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11861. IF implementationVisitor.backend.cooperative THEN
  11862. base := NIL;
  11863. baseRecord := recordType.GetBaseRecord();
  11864. IF baseRecord # NIL THEN
  11865. baseTD := baseRecord.typeDeclaration;
  11866. END;
  11867. IF ~recordType.isObject THEN
  11868. Info(source, "parent");
  11869. IF baseRecord # NIL THEN
  11870. Global.GetSymbolSegmentedName(baseTD,name);
  11871. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11872. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11873. ELSE
  11874. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11875. END;
  11876. Symbol(source, tir, 0, 0);
  11877. ELSE
  11878. Address(source,0);
  11879. END;
  11880. Info(source, "record size");
  11881. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11882. source.SetReferenced(FALSE);
  11883. CooperativeMethodTable(FALSE);
  11884. base := source;
  11885. Global.GetSymbolSegmentedName(td,name);
  11886. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11887. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11888. source.SetExported(IsExported(td));
  11889. source.SetReferenced(FALSE);
  11890. END;
  11891. Info(source, "parent");
  11892. IF baseRecord # NIL THEN
  11893. Global.GetSymbolSegmentedName(baseTD,name);
  11894. sym := baseTD;
  11895. IF ~recordType.isObject THEN
  11896. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11897. sym := NIL;
  11898. END;
  11899. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11900. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11901. ELSE
  11902. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11903. END;
  11904. Symbol(source, tir, 0, 0);
  11905. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11906. Address(source,0);
  11907. ELSE
  11908. IF recordType.isObject THEN
  11909. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11910. ELSE
  11911. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11912. END;
  11913. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11914. Symbol(source, tir, 0, 0);
  11915. END;
  11916. Info(source, "base record descriptor");
  11917. Symbol(source, base, 0, 0);
  11918. CooperativeMethodTable(TRUE);
  11919. source.SetReferenced(FALSE);
  11920. IF recordType.hasPointers THEN
  11921. IF ~HasExplicitTraceMethod (recordType) THEN
  11922. implementationVisitor.CreateTraceMethod(recordType);
  11923. END;
  11924. implementationVisitor.CreateResetProcedure(recordType);
  11925. implementationVisitor.CreateAssignProcedure(recordType);
  11926. END;
  11927. ELSIF ~simple THEN
  11928. (*
  11929. MethodEnd = MPO
  11930. ---
  11931. methods (# methods)
  11932. ---
  11933. tags (16)
  11934. ---
  11935. TypeDesc = TypeInfoAdr
  11936. ---
  11937. td adr ---> rec size
  11938. ----
  11939. pointer offsets
  11940. ----
  11941. (padding)
  11942. -----
  11943. empty [2 addresses aligned]
  11944. empty
  11945. empty
  11946. numPtrs
  11947. ---
  11948. pointer offsets
  11949. ---
  11950. *)
  11951. Info(source, "MethodEnd = MPO");
  11952. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11953. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11954. MethodTable(TRUE);
  11955. TdTable(TypeTags, TRUE);
  11956. Info(source, "type descriptor info pointer");
  11957. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11958. IF (cellType # NIL) THEN
  11959. IF cellType.sizeInBits < 0 THEN
  11960. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11961. END;
  11962. Info(source, "cell size");
  11963. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11964. ELSE
  11965. Info(source, "record size");
  11966. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11967. END;
  11968. Info(source, "pointer offsets pointer");
  11969. padding := 1- source.pc MOD 2;
  11970. Symbol(source, source, source.pc+1+padding,0);
  11971. IF padding >0 THEN
  11972. Info(source, "padding");
  11973. FOR i := 1 TO padding DO Address(source,0) END;
  11974. END;
  11975. IF cellType # NIL THEN
  11976. PointerArray(source, cellType.cellScope, numberPointers);
  11977. ELSE
  11978. PointerArray(source, recordType.recordScope, numberPointers);
  11979. END;
  11980. ELSE
  11981. (*
  11982. simple:
  11983. td adr --> size
  11984. tag(1)
  11985. tag(2)
  11986. tag(3)
  11987. methods ->
  11988. *)
  11989. Info(source, "record size");
  11990. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11991. TdTable(TypeTags, FALSE);
  11992. MethodTable(FALSE);
  11993. source.SetReferenced(FALSE);
  11994. END;
  11995. END NewTypeDescriptor;
  11996. BEGIN
  11997. x := x.resolved;
  11998. IF (x IS SyntaxTree.PointerType) THEN
  11999. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12000. END;
  12001. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12002. recordType := x(SyntaxTree.RecordType);
  12003. td := x.typeDeclaration;
  12004. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12005. ASSERT(td # NIL);
  12006. section := module.allSections.FindBySymbol(td); (* TODO *)
  12007. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12008. IF implementationVisitor.newObjectFile THEN
  12009. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12010. NewTypeDescriptor
  12011. END;
  12012. ELSE
  12013. (* data section in intermediate code *)
  12014. Global.GetSymbolSegmentedName(td,name);
  12015. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12016. Basic.SuffixSegmentedName (name, module.module.name);
  12017. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12018. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12019. tir.Emit(Data(Basic.invalidPosition,op));
  12020. END;
  12021. END;
  12022. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12023. cellType := x(SyntaxTree.CellType);
  12024. td := x.typeDeclaration;
  12025. section := module.allSections.FindBySymbol(td); (* TODO *)
  12026. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12027. IF implementationVisitor.newObjectFile THEN
  12028. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12029. NewTypeDescriptor
  12030. END;
  12031. END;
  12032. END;
  12033. END
  12034. END CheckTypeDeclaration
  12035. END MetaDataGenerator;
  12036. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12037. VAR
  12038. trace-: BOOLEAN;
  12039. traceString-: SyntaxTree.IdentifierString;
  12040. traceModuleName-: SyntaxTree.IdentifierString;
  12041. newObjectFile-: BOOLEAN;
  12042. profile-: BOOLEAN;
  12043. noRuntimeChecks: BOOLEAN;
  12044. simpleMetaData-: BOOLEAN;
  12045. noAsserts: BOOLEAN;
  12046. optimize-: BOOLEAN;
  12047. cooperative-: BOOLEAN;
  12048. preregisterStatic-: BOOLEAN;
  12049. dump-: Basic.Writer;
  12050. cellsAreObjects: BOOLEAN;
  12051. preciseGC: BOOLEAN;
  12052. PROCEDURE &InitIntermediateBackend*;
  12053. BEGIN
  12054. simpleMetaData := FALSE;
  12055. newObjectFile := FALSE;
  12056. InitBackend;
  12057. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12058. SetTraceModuleName(DefaultTraceModuleName);
  12059. END InitIntermediateBackend;
  12060. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12061. VAR
  12062. declarationVisitor: DeclarationVisitor;
  12063. implementationVisitor: ImplementationVisitor;
  12064. module: Sections.Module;
  12065. name, platformName: SyntaxTree.IdentifierString;
  12066. meta: MetaDataGenerator;
  12067. BEGIN
  12068. ResetError;
  12069. Global.GetSymbolName(x,name);
  12070. NEW(module,x,system); (* backend structures *)
  12071. Global.GetModuleName(x, name);
  12072. module.SetModuleName(name);
  12073. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12074. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12075. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12076. declarationVisitor.Module(x,module);
  12077. IF newObjectFile & ~meta.simple THEN
  12078. meta.Module(implementationVisitor.moduleBodySection);
  12079. END;
  12080. GetDescription(platformName);
  12081. module.SetPlatformName(platformName);
  12082. RETURN module
  12083. END GenerateIntermediate;
  12084. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12085. BEGIN RETURN TRUE
  12086. END SupportedImmediate;
  12087. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12088. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12089. END ProcessSyntaxTreeModule;
  12090. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12091. VAR
  12092. result: Sections.Module;
  12093. traceName: Basic.MessageString;
  12094. BEGIN
  12095. ASSERT(intermediateCodeModule IS Sections.Module);
  12096. result := intermediateCodeModule(Sections.Module);
  12097. IF trace THEN
  12098. traceName := "intermediate code trace: ";
  12099. Strings.Append(traceName,traceString);
  12100. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12101. IF (traceString="") OR (traceString="*") THEN
  12102. result.Dump(dump);
  12103. dump.Update
  12104. ELSE
  12105. Sections.DumpFiltered(dump, result, traceString);
  12106. END
  12107. END;
  12108. RETURN result
  12109. END ProcessIntermediateCodeModule;
  12110. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12111. BEGIN instructionSet := "Intermediate";
  12112. END GetDescription;
  12113. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12114. BEGIN
  12115. SELF.newObjectFile := newObjectFile;
  12116. SELF.simpleMetaData := simpleMetaData;
  12117. END SetNewObjectFile;
  12118. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12119. BEGIN COPY(name, traceModuleName)
  12120. END SetTraceModuleName;
  12121. PROCEDURE DefineOptions(options: Options.Options);
  12122. BEGIN
  12123. DefineOptions^(options);
  12124. options.Add(0X,"trace",Options.String);
  12125. options.Add(0X,"runtime",Options.String);
  12126. options.Add(0X,"newObjectFile",Options.Flag);
  12127. options.Add(0X,"traceModule",Options.String);
  12128. options.Add(0X,"profile",Options.Flag);
  12129. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12130. options.Add(0X,"noAsserts",Options.Flag);
  12131. options.Add(0X,"metaData",Options.String);
  12132. options.Add('o',"optimize", Options.Flag);
  12133. options.Add(0X,"preregisterStatic", Options.Flag);
  12134. options.Add(0X,"cellsAreObjects", Options.Flag);
  12135. options.Add(0X,"preciseGC", Options.Flag);
  12136. END DefineOptions;
  12137. PROCEDURE GetOptions(options: Options.Options);
  12138. VAR name,string: SyntaxTree.IdentifierString;
  12139. BEGIN
  12140. GetOptions^(options);
  12141. trace := options.GetString("trace",traceString);
  12142. profile := options.GetFlag("profile");
  12143. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12144. noAsserts := options.GetFlag("noAsserts");
  12145. cooperative := options.GetFlag("cooperative");
  12146. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12147. IF cooperative THEN
  12148. SetRuntimeModuleName("CPU") END
  12149. END;
  12150. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12151. simpleMetaData := TRUE
  12152. END;
  12153. IF options.GetString("metaData",string) THEN
  12154. IF string = "simple" THEN simpleMetaData := TRUE
  12155. ELSIF string ="full" THEN simpleMetaData := FALSE
  12156. END;
  12157. END;
  12158. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12159. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12160. optimize := options.GetFlag("optimize");
  12161. preregisterStatic := options.GetFlag("preregisterStatic");
  12162. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12163. preciseGC := options.GetFlag("preciseGC");
  12164. END GetOptions;
  12165. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12166. BEGIN RETURN SymbolFileFormat.Get()
  12167. END DefaultSymbolFileFormat;
  12168. END IntermediateBackend;
  12169. (* ----------------------------------- register allocation ------------------------------------- *)
  12170. (* register mapping scheme
  12171. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12172. spill offset
  12173. part(n) --> ticket <--> physical register
  12174. spill offset
  12175. *)
  12176. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12177. emptyOperand: IntermediateCode.Operand;
  12178. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12179. statCoopResetVariables: LONGINT;
  12180. statCoopModifyAssignments: LONGINT;
  12181. modifyAssignmentsPC : LONGINT;
  12182. statCoopNilCheck: LONGINT;
  12183. statCoopSwitch: LONGINT;
  12184. statCoopAssignProcedure: LONGINT;
  12185. statCoopTraceMethod: LONGINT;
  12186. statCoopResetProcedure: LONGINT;
  12187. statCoopTraceModule: LONGINT;
  12188. PROCEDURE ResetStatistics*;
  12189. BEGIN
  12190. statCoopResetVariables := 0;
  12191. statCoopModifyAssignments := 0;
  12192. statCoopNilCheck:= 0;
  12193. statCoopSwitch:= 0;
  12194. statCoopAssignProcedure:= 0;
  12195. statCoopTraceMethod:= 0;
  12196. statCoopResetProcedure:= 0;
  12197. statCoopTraceModule:= 0;
  12198. END ResetStatistics;
  12199. PROCEDURE Statistics*;
  12200. BEGIN
  12201. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12202. TRACE(statCoopNilCheck, statCoopSwitch);
  12203. TRACE(statCoopAssignProcedure,
  12204. statCoopTraceMethod,
  12205. statCoopResetProcedure,
  12206. statCoopTraceModule)
  12207. END Statistics;
  12208. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12209. VAR h: LONGINT;
  12210. BEGIN
  12211. WHILE b # 0 DO
  12212. h := a MOD b;
  12213. a := b;
  12214. b := h;
  12215. END;
  12216. RETURN a
  12217. END GCD;
  12218. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12219. BEGIN
  12220. RETURN a*b DIV GCD(a,b)
  12221. END SCM;
  12222. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12223. BEGIN
  12224. (*TRACE(a,b);*)
  12225. IF a = 0 THEN RETURN b
  12226. ELSIF b = 0 THEN RETURN a
  12227. ELSE RETURN SCM(a,b)
  12228. END;
  12229. END CommonAlignment;
  12230. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12231. BEGIN
  12232. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12233. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12234. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12235. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12236. 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)
  12237. END
  12238. END PassBySingleReference;
  12239. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12240. BEGIN
  12241. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12242. END PassInRegister;
  12243. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12244. VAR new: RegisterEntry;
  12245. BEGIN
  12246. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12247. IF queue = NIL THEN
  12248. queue := new
  12249. ELSE
  12250. new.next := queue;
  12251. IF queue#NIL THEN queue.prev := new END;
  12252. queue := new
  12253. END;
  12254. END AddRegisterEntry;
  12255. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12256. VAR this: RegisterEntry;
  12257. BEGIN
  12258. this := queue;
  12259. WHILE (this # NIL) & (this.register # register) DO
  12260. this := this.next;
  12261. END;
  12262. IF this = NIL THEN
  12263. RETURN FALSE
  12264. END;
  12265. ASSERT(this # NIL);
  12266. IF this = queue THEN queue := queue.next END;
  12267. IF this.prev # NIL THEN this.prev.next := this.next END;
  12268. IF this.next # NIL THEN this.next.prev := this.prev END;
  12269. RETURN TRUE
  12270. END RemoveRegisterEntry;
  12271. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12272. BEGIN ASSERT(cond);
  12273. END Assert;
  12274. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12275. BEGIN
  12276. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12277. END ReusableRegister;
  12278. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12279. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12280. BEGIN
  12281. procedure := moduleScope.bodyProcedure;
  12282. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12283. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12284. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12285. procedure.SetScope(moduleScope);
  12286. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12287. procedure.SetAccess(SyntaxTree.Hidden);
  12288. moduleScope.SetBodyProcedure(procedure);
  12289. moduleScope.AddProcedure(procedure);
  12290. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12291. END;
  12292. END EnsureBodyProcedure;
  12293. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12294. VAR import: SyntaxTree.Import;
  12295. selfName: SyntaxTree.IdentifierString;
  12296. module: SyntaxTree.Module;
  12297. BEGIN
  12298. scope.ownerModule.GetName(selfName);
  12299. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12300. module := scope.ownerModule
  12301. ELSE
  12302. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12303. IF import = NIL THEN
  12304. RETURN NIL
  12305. ELSIF import.module = NIL THEN
  12306. RETURN NIL
  12307. ELSE module := import.module
  12308. END;
  12309. END;
  12310. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12311. END GetSymbol;
  12312. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12313. BEGIN
  12314. op.mode := mode;
  12315. IntermediateCode.InitOperand(op.op);
  12316. IntermediateCode.InitOperand(op.tag);
  12317. IntermediateCode.InitOperand(op.extra);
  12318. op.dimOffset := 0;
  12319. END InitOperand;
  12320. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12321. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12322. BEGIN RETURN IntermediateCode.GetType(system, type)
  12323. END GetType;
  12324. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12325. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12326. BEGIN
  12327. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12328. (*
  12329. UniqueId(name,module,name,adr);
  12330. *)
  12331. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12332. constant.SetValue(value);
  12333. constant.SetAccess(SyntaxTree.Hidden);
  12334. module.moduleScope.AddConstant(constant);
  12335. constant.SetScope(module.moduleScope);
  12336. RETURN constant
  12337. END BuildConstant;
  12338. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12339. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12340. BEGIN
  12341. variable := scope.firstVariable;
  12342. WHILE variable # NIL DO
  12343. IF variable.NeedsTrace() THEN
  12344. RETURN TRUE;
  12345. END;
  12346. variable := variable.nextVariable;
  12347. END;
  12348. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12349. WHILE parameter # NIL DO
  12350. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12351. RETURN TRUE;
  12352. END;
  12353. parameter := parameter.nextParameter;
  12354. END;
  12355. RETURN FALSE;
  12356. END HasPointers;
  12357. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12358. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12359. END IsVariableParameter;
  12360. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12361. VAR parameter: SyntaxTree.Parameter;
  12362. BEGIN
  12363. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12364. WHILE parameter # NIL DO
  12365. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12366. IF parameter.movable THEN RETURN TRUE END;
  12367. parameter := parameter.nextParameter;
  12368. END;
  12369. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12370. END HasVariableParameters;
  12371. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12372. BEGIN
  12373. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12374. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12375. END HasExplicitTraceMethod;
  12376. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12377. BEGIN
  12378. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12379. IF (expression IS SyntaxTree.IntegerValue) THEN
  12380. val := expression(SyntaxTree.IntegerValue).value;
  12381. RETURN TRUE
  12382. ELSE
  12383. RETURN FALSE
  12384. END;
  12385. END IsIntegerConstant;
  12386. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12387. BEGIN
  12388. IF val <= 0 THEN RETURN FALSE END;
  12389. exp := 0;
  12390. WHILE ~ODD(val) DO
  12391. val := val DIV 2;
  12392. INC(exp)
  12393. END;
  12394. RETURN val = 1
  12395. END PowerOf2;
  12396. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12397. VAR procedure: SyntaxTree.Procedure;
  12398. BEGIN
  12399. procedure := record.recordScope.constructor;
  12400. IF procedure = NIL THEN
  12401. record := record.GetBaseRecord();
  12402. IF record # NIL THEN
  12403. procedure := GetConstructor(record)
  12404. END;
  12405. END;
  12406. RETURN procedure;
  12407. END GetConstructor;
  12408. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12409. BEGIN
  12410. value := SHORT(op.intValue);
  12411. RETURN op.mode = IntermediateCode.ModeImmediate;
  12412. END IsIntegerImmediate;
  12413. (** whether a type strictily is a pointer to record or object type
  12414. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12415. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12416. BEGIN
  12417. IF type = NIL THEN
  12418. RETURN FALSE
  12419. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12420. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12421. ELSE
  12422. RETURN FALSE
  12423. END
  12424. END IsStrictlyPointerToRecord;
  12425. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12426. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12427. END IsUnsafePointer;
  12428. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12429. BEGIN type := type.resolved;
  12430. IF type IS SyntaxTree.PointerType THEN
  12431. type := type(SyntaxTree.PointerType).pointerBase;
  12432. type := type.resolved;
  12433. IF type IS SyntaxTree.RecordType THEN
  12434. recordType := type(SyntaxTree.RecordType);
  12435. RETURN TRUE
  12436. ELSE
  12437. RETURN FALSE
  12438. END
  12439. ELSIF type IS SyntaxTree.RecordType THEN
  12440. recordType := type(SyntaxTree.RecordType);
  12441. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12442. ELSIF type IS SyntaxTree.ObjectType THEN
  12443. RETURN TRUE
  12444. ELSIF type IS SyntaxTree.AnyType THEN
  12445. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12446. ELSE
  12447. RETURN FALSE
  12448. END;
  12449. END IsPointerToRecord;
  12450. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12451. BEGIN
  12452. type := type.resolved;
  12453. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12454. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12455. END IsArrayOfSystemByte;
  12456. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12457. BEGIN
  12458. IF type = NIL THEN RETURN FALSE END;
  12459. type := type.resolved;
  12460. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12461. END IsOpenArray;
  12462. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12463. BEGIN
  12464. IF type = NIL THEN RETURN FALSE END;
  12465. type := type.resolved;
  12466. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12467. END IsSemiDynamicArray;
  12468. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12469. BEGIN
  12470. IF type = NIL THEN RETURN FALSE END;
  12471. type := type.resolved;
  12472. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12473. END IsStaticArray;
  12474. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12475. VAR size: LONGINT;
  12476. BEGIN
  12477. size := 1;
  12478. WHILE (IsStaticArray(type)) DO
  12479. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12480. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12481. END;
  12482. RETURN size;
  12483. END StaticArrayNumElements;
  12484. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12485. BEGIN
  12486. WHILE (IsStaticArray(type)) DO
  12487. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12488. END;
  12489. RETURN type;
  12490. END StaticArrayBaseType;
  12491. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12492. BEGIN
  12493. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12494. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12495. END;
  12496. RETURN type;
  12497. END ArrayBaseType;
  12498. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12499. BEGIN
  12500. IF type = NIL THEN RETURN FALSE END;
  12501. type := type.resolved;
  12502. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12503. END IsDelegate;
  12504. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12505. VAR i: LONGINT;
  12506. BEGIN
  12507. i := 0; type := type.resolved;
  12508. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12509. INC(i);
  12510. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12511. END;
  12512. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12513. INC(i);
  12514. type := type(SyntaxTree.MathArrayType).arrayBase;
  12515. IF type # NIL THEN type := type.resolved END;
  12516. END;
  12517. RETURN i
  12518. END DynamicDim;
  12519. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12520. BEGIN
  12521. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12522. type := type(SyntaxTree.ArrayType).arrayBase;
  12523. END;
  12524. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12525. type := type(SyntaxTree.MathArrayType).arrayBase;
  12526. END;
  12527. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12528. END StaticSize;
  12529. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12530. BEGIN
  12531. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12532. END IsImmediate;
  12533. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12534. BEGIN
  12535. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12536. END IsAddress;
  12537. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12538. BEGIN
  12539. RETURN (x.mode = IntermediateCode.ModeRegister);
  12540. END IsRegister;
  12541. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12542. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12543. BEGIN
  12544. typeDeclaration := recordType.typeDeclaration;
  12545. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12546. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12547. END GetRecordTypeName;
  12548. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12549. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12550. BEGIN
  12551. parSize := 0;
  12552. IF StructuredReturnType(procedureType) THEN
  12553. parameter := procedureType.returnParameter;
  12554. INC(parSize,system.SizeOfParameter(parameter));
  12555. parSize := parSize + (-parSize) MOD system.addressSize;
  12556. END;
  12557. parameter :=procedureType.lastParameter;
  12558. WHILE (parameter # NIL) DO
  12559. INC(parSize,system.SizeOfParameter(parameter));
  12560. parSize := parSize + (-parSize) MOD system.addressSize;
  12561. parameter := parameter.prevParameter;
  12562. END;
  12563. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  12564. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12565. RETURN ToMemoryUnits(system,parSize)
  12566. END ParametersSize;
  12567. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  12568. BEGIN
  12569. IF type = NIL THEN RETURN FALSE
  12570. ELSE
  12571. type := type.resolved;
  12572. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  12573. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  12574. END
  12575. END ReturnedAsParameter;
  12576. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  12577. BEGIN
  12578. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  12579. END StructuredReturnType;
  12580. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12581. BEGIN
  12582. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12583. END IsNested;
  12584. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12585. BEGIN
  12586. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12587. scope := scope.outerScope;
  12588. END;
  12589. RETURN scope # NIL;
  12590. END InCellScope;
  12591. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12592. BEGIN
  12593. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12594. RETURN 0
  12595. ELSE
  12596. *)
  12597. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12598. (*END;*)
  12599. END ProcedureParametersSize;
  12600. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12601. VAR dataUnit: LONGINT;
  12602. BEGIN dataUnit := system.dataUnit;
  12603. ASSERT(size MOD system.dataUnit = 0);
  12604. RETURN size DIV system.dataUnit
  12605. END ToMemoryUnits;
  12606. PROCEDURE Get*(): Backend.Backend;
  12607. VAR backend: IntermediateBackend;
  12608. BEGIN NEW(backend); RETURN backend
  12609. END Get;
  12610. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12611. VAR instruction: IntermediateCode.Instruction;
  12612. BEGIN
  12613. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12614. RETURN instruction
  12615. END Nop;
  12616. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12617. VAR instruction: IntermediateCode.Instruction;
  12618. BEGIN
  12619. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12620. RETURN instruction
  12621. END Mov;
  12622. (* like Mov but ensures that no new register will be allocated for dest *)
  12623. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12624. VAR instruction: IntermediateCode.Instruction;
  12625. BEGIN
  12626. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12627. RETURN instruction
  12628. END MovReplace;
  12629. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12630. VAR instruction: IntermediateCode.Instruction;
  12631. BEGIN
  12632. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12633. RETURN instruction
  12634. END Conv;
  12635. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12636. BEGIN
  12637. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12638. END SysvABI;
  12639. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12640. BEGIN
  12641. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12642. END SysvABIorWINAPI;
  12643. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12644. VAR instruction: IntermediateCode.Instruction;
  12645. BEGIN
  12646. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12647. RETURN instruction
  12648. END Call;
  12649. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12650. VAR op1, op2, op3: IntermediateCode.Operand;
  12651. VAR instruction: IntermediateCode.Instruction;
  12652. BEGIN
  12653. IntermediateCode.InitNumber(op1,pcOffset);
  12654. IntermediateCode.InitNumber(op2,callingConvention);
  12655. IntermediateCode.InitNumber(op3,unwind);
  12656. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12657. RETURN instruction
  12658. END Exit;
  12659. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12660. VAR instruction: IntermediateCode.Instruction;
  12661. BEGIN
  12662. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12663. RETURN instruction
  12664. END Return;
  12665. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12666. VAR instruction: IntermediateCode.Instruction;
  12667. BEGIN
  12668. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12669. RETURN instruction
  12670. END Result;
  12671. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12672. VAR op1: IntermediateCode.Operand;
  12673. VAR instruction: IntermediateCode.Instruction;
  12674. BEGIN
  12675. IntermediateCode.InitNumber(op1,nr);
  12676. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12677. RETURN instruction
  12678. END Trap;
  12679. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12680. VAR instruction: IntermediateCode.Instruction;
  12681. BEGIN
  12682. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12683. RETURN instruction
  12684. END Br;
  12685. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12686. VAR instruction: IntermediateCode.Instruction;
  12687. BEGIN
  12688. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12689. RETURN instruction
  12690. END Breq;
  12691. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12692. VAR instruction: IntermediateCode.Instruction;
  12693. BEGIN
  12694. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12695. RETURN instruction
  12696. END Brne;
  12697. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12698. VAR instruction: IntermediateCode.Instruction;
  12699. BEGIN
  12700. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12701. RETURN instruction
  12702. END Brge;
  12703. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12704. VAR instruction: IntermediateCode.Instruction;
  12705. BEGIN
  12706. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12707. RETURN instruction
  12708. END Brlt;
  12709. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12710. VAR instruction: IntermediateCode.Instruction;
  12711. BEGIN
  12712. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12713. RETURN instruction
  12714. END Pop;
  12715. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12716. VAR instruction: IntermediateCode.Instruction;
  12717. BEGIN
  12718. ASSERT(op.mode # IntermediateCode.Undefined);
  12719. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12720. RETURN instruction
  12721. END Push;
  12722. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12723. VAR instruction: IntermediateCode.Instruction;
  12724. BEGIN
  12725. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12726. RETURN instruction
  12727. END Neg;
  12728. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12729. VAR instruction: IntermediateCode.Instruction;
  12730. BEGIN
  12731. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12732. RETURN instruction
  12733. END Not;
  12734. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12735. VAR instruction: IntermediateCode.Instruction;
  12736. BEGIN
  12737. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12738. RETURN instruction
  12739. END Abs;
  12740. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12741. VAR instruction: IntermediateCode.Instruction;
  12742. BEGIN
  12743. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12744. ASSERT(~IsImmediate(instruction.op1));
  12745. RETURN instruction
  12746. END Mul;
  12747. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12748. VAR instruction: IntermediateCode.Instruction;
  12749. BEGIN
  12750. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12751. RETURN instruction
  12752. END Div;
  12753. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12754. VAR instruction: IntermediateCode.Instruction;
  12755. BEGIN
  12756. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12757. RETURN instruction
  12758. END Mod;
  12759. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12760. VAR instruction: IntermediateCode.Instruction;
  12761. BEGIN
  12762. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12763. RETURN instruction
  12764. END Sub;
  12765. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12766. VAR instruction: IntermediateCode.Instruction;
  12767. BEGIN
  12768. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12769. RETURN instruction
  12770. END Add;
  12771. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12772. VAR instruction: IntermediateCode.Instruction;
  12773. BEGIN
  12774. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12775. RETURN instruction
  12776. END And;
  12777. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12778. VAR instruction: IntermediateCode.Instruction;
  12779. BEGIN
  12780. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12781. RETURN instruction
  12782. END Or;
  12783. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12784. VAR instruction: IntermediateCode.Instruction;
  12785. BEGIN
  12786. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12787. RETURN instruction
  12788. END Xor;
  12789. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12790. VAR instruction: IntermediateCode.Instruction;
  12791. BEGIN
  12792. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12793. RETURN instruction
  12794. END Shl;
  12795. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12796. VAR instruction: IntermediateCode.Instruction;
  12797. BEGIN
  12798. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12799. RETURN instruction
  12800. END Shr;
  12801. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12802. VAR instruction: IntermediateCode.Instruction;
  12803. BEGIN
  12804. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12805. RETURN instruction
  12806. END Rol;
  12807. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12808. VAR instruction: IntermediateCode.Instruction;
  12809. BEGIN
  12810. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12811. RETURN instruction
  12812. END Ror;
  12813. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12814. VAR instruction: IntermediateCode.Instruction;
  12815. BEGIN
  12816. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12817. RETURN instruction
  12818. END Cas;
  12819. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12820. VAR instruction: IntermediateCode.Instruction;
  12821. BEGIN
  12822. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12823. RETURN instruction
  12824. END Copy;
  12825. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12826. VAR instruction: IntermediateCode.Instruction;
  12827. BEGIN
  12828. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12829. RETURN instruction
  12830. END Fill;
  12831. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12832. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12833. BEGIN
  12834. string := IntermediateCode.String(s);
  12835. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12836. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12837. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12838. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12839. RETURN instruction
  12840. END Asm;
  12841. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12842. VAR instruction: IntermediateCode.Instruction;
  12843. BEGIN
  12844. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12845. RETURN instruction
  12846. END Data;
  12847. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12848. VAR instruction: IntermediateCode.Instruction;
  12849. BEGIN
  12850. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12851. IntermediateCode.SetSubType(instruction, subtype);
  12852. RETURN instruction
  12853. END SpecialInstruction;
  12854. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12855. VAR op1: IntermediateCode.Operand;
  12856. VAR instruction: IntermediateCode.Instruction;
  12857. BEGIN
  12858. (*! generate a warning if size exceeds a certain limit *)
  12859. (*
  12860. ASSERT(bytes < 1000000); (* sanity check *)
  12861. *)
  12862. ASSERT(0 <= units); (* sanity check *)
  12863. IntermediateCode.InitNumber(op1,units);
  12864. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12865. RETURN instruction
  12866. END Reserve;
  12867. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12868. VAR op1: IntermediateCode.Operand;
  12869. VAR instruction: IntermediateCode.Instruction;
  12870. BEGIN
  12871. IntermediateCode.InitNumber(op1,position.start);
  12872. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12873. RETURN instruction
  12874. END LabelInstruction;
  12875. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12876. VAR pc: LONGINT;
  12877. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12878. BEGIN
  12879. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12880. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12881. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12882. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12883. IF instr.op1.type.form = IntermediateCode.Float THEN
  12884. RETURN instr.op1.floatValue = op.floatValue
  12885. ELSE
  12886. RETURN instr.op1.intValue = op.intValue
  12887. END;
  12888. END ProvidesValue;
  12889. BEGIN
  12890. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12891. pc := 0;
  12892. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12893. INC(pc);
  12894. END;
  12895. IF pc = data.pc THEN
  12896. data.Emit(Data(Basic.invalidPosition,vop));
  12897. END;
  12898. RETURN pc
  12899. END EnterImmediate;
  12900. PROCEDURE Init;
  12901. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12902. BEGIN
  12903. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12904. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12905. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12906. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12907. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12908. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12909. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12910. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12911. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12912. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12913. IntermediateCode.InitOperand(emptyOperand);
  12914. FOR i := 0 TO NumberSystemCalls-1 DO
  12915. name := "@SystemCall";
  12916. Basic.AppendNumber(name,i);
  12917. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12918. END;
  12919. END Init;
  12920. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12921. BEGIN
  12922. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12923. END IsExported;
  12924. BEGIN
  12925. Init;
  12926. END FoxIntermediateBackend.
  12927. Compiler.Compile FoxIntermediateBackend.Mod ~
  12928. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12929. # 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 ~
  12930. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12931. # 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 ~
  12932. FSTools.CloseFiles A2Z.exe ~
  12933. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12934. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12935. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12936. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12937. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12938. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12939. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12940. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12941. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12942. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12943. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod