FoxIntermediateBackend.Mod 518 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322
  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, ActiveCells := FoxActiveCells;
  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. suppressModuleRegistration=FALSE;
  53. NonPointer = -1; (* special pointer values *)
  54. NoType = 0; (* special type info values *)
  55. LhsIsPointer = 0; (* for the operator kind *)
  56. RhsIsPointer = 1;
  57. (* priority values, lower means higher priority *)
  58. EntryPriority=-4;
  59. FirstPriority=-3;
  60. InitPriority=-2;
  61. ExitPriority=-1;
  62. StrictChecks = FALSE;
  63. BasePointerTypeSize = 5;
  64. BaseArrayTypeSize = BasePointerTypeSize + 3;
  65. LengthOffset = BasePointerTypeSize + 0;
  66. DataOffset = BasePointerTypeSize + 1;
  67. DescriptorOffset = BasePointerTypeSize + 2;
  68. BaseRecordTypeSize = BasePointerTypeSize + 2;
  69. ActionOffset = BasePointerTypeSize + 0;
  70. MonitorOffset = BasePointerTypeSize + 1;
  71. BaseObjectTypeSize = BaseRecordTypeSize;
  72. ActionTypeSize = 3;
  73. MonitorTypeSize = 7;
  74. ProcessorOffset = BaseObjectTypeSize + 1;
  75. StackLimitOffset* = BaseObjectTypeSize + 3;
  76. QuantumOffset = BaseObjectTypeSize + 4;
  77. TYPE
  78. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  79. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  80. Operand = RECORD
  81. mode: SHORTINT;
  82. op: IntermediateCode.Operand;
  83. tag: IntermediateCode.Operand;
  84. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  85. dimOffset: LONGINT;
  86. END;
  87. Fixup= POINTER TO RECORD
  88. pc: LONGINT;
  89. nextFixup: Fixup;
  90. END;
  91. WriteBackCall = POINTER TO RECORD
  92. call: SyntaxTree.ProcedureCallDesignator;
  93. next: WriteBackCall;
  94. END;
  95. Label= OBJECT
  96. VAR
  97. fixups: Fixup;
  98. section: IntermediateCode.Section;
  99. pc: LONGINT;
  100. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  101. BEGIN
  102. SELF.section := section; pc := -1;
  103. END InitLabel;
  104. PROCEDURE Resolve(pc: LONGINT);
  105. VAR at: LONGINT;
  106. BEGIN
  107. SELF.pc := pc;
  108. WHILE(fixups # NIL) DO
  109. at := fixups.pc;
  110. section.PatchAddress(at,pc);
  111. fixups := fixups.nextFixup;
  112. END;
  113. END Resolve;
  114. PROCEDURE AddFixup(at: LONGINT);
  115. VAR fixup: Fixup;
  116. BEGIN
  117. ASSERT(pc=-1);
  118. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  119. END AddFixup;
  120. END Label;
  121. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  122. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  123. VAR
  124. backend: IntermediateBackend;
  125. implementationVisitor: ImplementationVisitor;
  126. meta: MetaDataGenerator;
  127. system: Global.System;
  128. currentScope: SyntaxTree.Scope;
  129. module: Sections.Module;
  130. moduleSelf: SyntaxTree.Variable;
  131. dump: BOOLEAN;
  132. forceModuleBody: BOOLEAN;
  133. addressType: IntermediateCode.Type;
  134. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  135. BEGIN
  136. currentScope := NIL; module := NIL; moduleSelf := NIL;
  137. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  138. SELF.dump := dump;
  139. SELF.backend := backend;
  140. SELF.forceModuleBody := forceModuleBody;
  141. addressType := IntermediateCode.GetType(system,system.addressType)
  142. END Init;
  143. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  144. BEGIN
  145. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  146. END Error;
  147. PROCEDURE Type(x: SyntaxTree.Type);
  148. BEGIN
  149. x.Accept(SELF);
  150. END Type;
  151. (** types **)
  152. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  153. BEGIN (* no code emission *) END VisitBasicType;
  154. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  155. BEGIN (* no code emission *) END VisitCharacterType;
  156. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  157. BEGIN (* no code emission *) END VisitIntegerType;
  158. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  159. BEGIN (* no code emission *) END VisitFloatType;
  160. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  161. BEGIN (* no code emission *) END VisitComplexType;
  162. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  163. VAR type: SyntaxTree.Type;
  164. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  165. type := x.resolved;
  166. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  167. meta.CheckTypeDeclaration(type);
  168. END;
  169. END VisitQualifiedType;
  170. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  171. BEGIN (* no code emission *) END VisitStringType;
  172. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  173. BEGIN (* no code emission *)
  174. END VisitArrayRangeType;
  175. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  176. BEGIN (* no code emission *) END VisitArrayType;
  177. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  178. BEGIN
  179. meta.CheckTypeDeclaration(x);
  180. END VisitMathArrayType;
  181. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  182. BEGIN
  183. meta.CheckTypeDeclaration(x);
  184. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  185. END VisitPointerType;
  186. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  187. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  188. BEGIN (* no code emission *)
  189. meta.CheckTypeDeclaration(x);
  190. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  191. IF x.pointerType.typeDeclaration # NIL THEN
  192. td := x.pointerType.typeDeclaration
  193. ELSE
  194. td := x.typeDeclaration
  195. END;
  196. Global.GetSymbolName(td,name);
  197. (* code section for object *)
  198. END;
  199. Scope(x.recordScope);
  200. END VisitRecordType;
  201. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  202. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  203. BEGIN
  204. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  205. WHILE (this # NIL) & (this.identifier# id) DO
  206. this := this.nextModifier;
  207. END;
  208. RETURN this # NIL
  209. END HasFlag;
  210. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  211. VAR name:Basic.SegmentedName; td: SyntaxTree.TypeDeclaration; type: SyntaxTree.Type; len,port,adr: LONGINT;
  212. parameter: SyntaxTree.Parameter; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  213. PROCEDURE CreatePortArray(type: SyntaxTree.Type; len: LONGINT);
  214. VAR i,len2: LONGINT; baseType: SyntaxTree.Type;
  215. BEGIN
  216. FOR i := 0 TO len-1 DO
  217. IF SemanticChecker.IsStaticArray(type, baseType, len2) THEN
  218. CreatePortArray(baseType, len2);
  219. ELSE
  220. IF backend.cellsAreObjects THEN
  221. adr := port
  222. ELSE
  223. (*! add check from ActiveCells2 *)
  224. adr := backend.activeCellsSpecification.GetPortAddress(port);
  225. END;
  226. IntermediateCode.InitImmediate(op,addressType,adr);
  227. symbol.Emit(Data(-1,op));
  228. INC(port);
  229. END;
  230. END;
  231. END CreatePortArray;
  232. BEGIN
  233. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  234. IF (x.cellScope.ownerModule = module.module) THEN
  235. td := x.typeDeclaration;
  236. Global.GetSymbolSegmentedName(td,name);
  237. (* code section for object *)
  238. END;
  239. port := 0;
  240. parameter := x.firstParameter;
  241. WHILE parameter # NIL DO
  242. type := parameter.type.resolved;
  243. IF type IS SyntaxTree.PortType THEN
  244. len := 1;
  245. INC(port);
  246. ELSIF SemanticChecker.IsStaticArray(type,type,len) OR SemanticChecker.IsDynamicArray(type, type) THEN
  247. IF backend.cellsAreObjects THEN
  248. IF IsStaticArray(parameter.type.resolved) THEN
  249. Error(parameter.position, "static arrays of ports are currently not implemented, please use a property (array property of port)");
  250. END;
  251. (* do nothing *)
  252. ELSE
  253. Global.GetSymbolSegmentedName(parameter,name);
  254. symbol := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,parameter,dump);
  255. CreatePortArray(type, len);
  256. (*
  257. WHILE len > 0 DO
  258. adr := backend.activeCellsSpecification.GetPortAddress(port);
  259. IntermediateCode.InitImmediate(op,addressType,adr);
  260. symbol.Emit(Data(-1,op));
  261. DEC(len); INC(port);
  262. END;
  263. *)
  264. END;
  265. ELSE
  266. Error(parameter.position,"should never happen, check semantic checker!");
  267. END;
  268. parameter := parameter.nextParameter;
  269. END;
  270. capabilities := {};
  271. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  272. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  273. backend.SetCapabilities(capabilities);
  274. Scope(x.cellScope);
  275. END VisitCellType;
  276. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  277. BEGIN (* no code emission *) END VisitProcedureType;
  278. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  279. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  280. END VisitEnumerationType;
  281. (* symbols *)
  282. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  283. BEGIN
  284. Procedure(x);
  285. END VisitProcedure;
  286. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  287. BEGIN
  288. Procedure(x);
  289. END VisitOperator;
  290. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  291. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section;
  292. BEGIN
  293. IF x.externalName # NIL THEN RETURN END;
  294. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  295. (* code section for variable *)
  296. Global.GetSymbolSegmentedName(x,name);
  297. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  298. irv.SetExported(IsExported(x));
  299. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  300. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  301. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))));
  302. meta.CheckTypeDeclaration(x.type);
  303. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  304. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  305. END;
  306. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  307. END VisitVariable;
  308. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  309. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; op: Operand;
  310. BEGIN
  311. ASSERT(currentScope.outerScope IS SyntaxTree.CellScope);
  312. (* code section for variable *)
  313. Global.GetSymbolSegmentedName(x,name);
  314. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  315. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  316. (*
  317. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  318. *)
  319. IF x.defaultValue = NIL THEN
  320. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))))
  321. ELSE
  322. implementationVisitor.inData := TRUE;
  323. implementationVisitor.Evaluate(x.defaultValue, op);
  324. irv.Emit(Data(x.position,op.op));
  325. implementationVisitor.inData := FALSE;
  326. END;
  327. meta.CheckTypeDeclaration(x.type);
  328. END VisitParameter;
  329. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  330. BEGIN
  331. Type(x.declaredType); (* => code in objects *)
  332. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  333. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  334. END;
  335. END VisitTypeDeclaration;
  336. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  337. BEGIN
  338. IF (SyntaxTree.Public * x.access # {}) THEN
  339. implementationVisitor.VisitConstant(x);
  340. END;
  341. END VisitConstant;
  342. PROCEDURE Scope(x: SyntaxTree.Scope);
  343. VAR procedure: SyntaxTree.Procedure;
  344. constant: SyntaxTree.Constant;
  345. variable: SyntaxTree.Variable;
  346. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  347. BEGIN
  348. prevScope := currentScope;
  349. currentScope := x;
  350. (* constants treated in implementation visitor *)
  351. typeDeclaration := x.firstTypeDeclaration;
  352. WHILE typeDeclaration # NIL DO
  353. VisitTypeDeclaration(typeDeclaration);
  354. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  355. END;
  356. variable := x.firstVariable;
  357. WHILE variable # NIL DO
  358. VisitVariable(variable);
  359. variable := variable.nextVariable;
  360. END;
  361. procedure := x.firstProcedure;
  362. WHILE procedure # NIL DO
  363. VisitProcedure(procedure);
  364. procedure := procedure.nextProcedure;
  365. END;
  366. constant := x.firstConstant;
  367. WHILE constant # NIL DO
  368. VisitConstant(constant);
  369. constant := constant.nextConstant;
  370. END;
  371. currentScope := prevScope;
  372. END Scope;
  373. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  374. VAR parameter: SyntaxTree.Parameter;
  375. BEGIN
  376. parameter := first;
  377. WHILE parameter # NIL DO
  378. VisitParameter(parameter);
  379. parameter := parameter.nextParameter;
  380. END;
  381. END Parameters;
  382. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  383. VAR scope: SyntaxTree.ProcedureScope;
  384. prevScope: SyntaxTree.Scope;
  385. inline, finalizer: BOOLEAN;
  386. procedureType: SyntaxTree.ProcedureType;
  387. pc: LONGINT;
  388. stackSize: LONGINT;
  389. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  390. null,size,src,dest,fp,res: IntermediateCode.Operand;
  391. cc: LONGINT;
  392. cellType: SyntaxTree.CellType;
  393. registerNumber: LONGINT;
  394. registerClass: IntermediateCode.RegisterClass;
  395. type: IntermediateCode.Type;
  396. formalParameter: SyntaxTree.Parameter;
  397. recordType: SyntaxTree.RecordType;
  398. isModuleBody: BOOLEAN;
  399. PROCEDURE Signature;
  400. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  401. BEGIN
  402. procedureType := x.type(SyntaxTree.ProcedureType);
  403. returnType := procedureType.returnType;
  404. IF returnType # NIL THEN
  405. meta.CheckTypeDeclaration(returnType)
  406. END;
  407. parameter := procedureType.firstParameter;
  408. WHILE parameter # NIL DO
  409. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  410. parameter := parameter.nextParameter;
  411. END;
  412. END Signature;
  413. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  414. VAR result: BOOLEAN;
  415. BEGIN
  416. result := FALSE;
  417. IF x = SyntaxTree.invalidExpression THEN
  418. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  419. result := TRUE;
  420. value := x.resolved(SyntaxTree.IntegerValue).value;
  421. ELSE
  422. Error(x.position,"expression is not an integer constant");
  423. END;
  424. RETURN result;
  425. END CheckIntegerValue;
  426. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  427. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  428. BEGIN
  429. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  430. WHILE (this # NIL) & (this.identifier # id) DO
  431. this := this.nextModifier;
  432. END;
  433. IF this # NIL THEN
  434. IF this.expression = NIL THEN
  435. Error(this.position,"expected expression value");
  436. ELSIF CheckIntegerValue(this.expression,value) THEN
  437. END;
  438. RETURN TRUE
  439. ELSE RETURN FALSE
  440. END;
  441. END HasValue;
  442. BEGIN
  443. IF x.externalName # NIL THEN RETURN END;
  444. (*
  445. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  446. *)
  447. (* code section for this procedure *)
  448. scope := x.procedureScope;
  449. prevScope := currentScope;
  450. currentScope := scope;
  451. procedureType := x.type(SyntaxTree.ProcedureType);
  452. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  453. implementationVisitor.temporaries.Init;
  454. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  455. IF (scope.body # NIL) & (x.isInline) THEN
  456. inline := TRUE;
  457. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  458. ir.SetExported(IsExported(x));
  459. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  460. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  461. IF backend.cellsAreObjects THEN
  462. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  463. ir.SetExported(IsExported(x));
  464. ELSE
  465. RETURN; (* cellnet cannot be compiled for final static hardware *)
  466. END;
  467. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  468. inline := FALSE;
  469. AddBodyCallStub(x);
  470. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  471. ir.SetExported(IsExported(x));
  472. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  473. inline := FALSE;
  474. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  475. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := ActiveCells.defaultDataMemorySize END;
  476. AddBodyCallStub(x);
  477. AddStackAllocation(x,stackSize);
  478. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  479. ir.SetExported(IsExported(x));
  480. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  481. inline := FALSE;
  482. Parameters(procedureType.firstParameter);
  483. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  484. ir.SetExported(IsExported(x));
  485. ELSE
  486. inline := FALSE;
  487. IF x.isEntry OR x.isExit THEN
  488. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  489. ir.SetExported(TRUE);
  490. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  491. ELSE
  492. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  493. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  494. END;
  495. END;
  496. cc := procedureType.callingConvention;
  497. IF scope.body # NIL THEN
  498. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  499. registerNumber := 0;
  500. IF ~inline THEN
  501. IF scope.lastVariable = NIL THEN
  502. stackSize := 0
  503. ELSE
  504. stackSize := scope.lastVariable.offsetInBits;
  505. IF stackSize <0 THEN stackSize := -stackSize END;
  506. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  507. END;
  508. (*
  509. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  510. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  511. *)
  512. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  513. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  514. END;
  515. pc := ir.pc-1;
  516. (*
  517. ir.Emit(Nop(position)); (* placeholder for fill *)
  518. *)
  519. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  520. formalParameter := procedureType.lastParameter;
  521. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  522. IF ~PassInRegister(formalParameter) THEN
  523. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  524. ELSE
  525. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  526. type := GetType(system, formalParameter.type);
  527. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  528. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  529. ir.Emit(Mov(-1,dest, src));
  530. implementationVisitor.ReleaseIntermediateOperand(src);
  531. INC(registerNumber);
  532. formalParameter := formalParameter.prevParameter;
  533. END;
  534. END;
  535. END;
  536. ir.EnterValidPAF;
  537. END;
  538. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  539. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  540. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  541. IF scope.lastVariable # NIL THEN
  542. stackSize := scope.lastVariable.offsetInBits;
  543. IF stackSize <0 THEN stackSize := -stackSize END;
  544. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  545. END;
  546. END;
  547. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  548. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  549. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  550. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  551. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  552. ir.EmitAt(pc,Push(size));
  553. implementationVisitor.StaticCallOperand(result,procedure);
  554. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  555. END;
  556. *)
  557. END;
  558. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  559. IF stackSize > 0 THEN
  560. IF (stackSize MOD system.addressSize = 0) THEN
  561. null := IntermediateCode.Immediate(addressType,0);
  562. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  563. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  564. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  565. ELSE
  566. null := IntermediateCode.Immediate(int8,0);
  567. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  568. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  569. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  570. END;
  571. (*! should potentially be called by backend -- enter might initialize
  572. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  573. *)
  574. END;
  575. ir.ExitValidPAF;
  576. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  577. finalizer := FALSE;
  578. IF backend.cooperative & x.isFinalizer THEN
  579. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  580. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  581. GetRecordTypeName(recordType,name);
  582. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  583. END;
  584. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  585. IF backend.cooperative THEN
  586. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  587. IF implementationVisitor.profile & ~isModuleBody THEN
  588. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  589. END;
  590. END;
  591. implementationVisitor.EmitLeave(ir, x.position,cc);
  592. IF finalizer THEN
  593. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  594. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  595. ir.Emit(Br(x.position,dest));
  596. ELSE
  597. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  598. END;
  599. ELSE
  600. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  601. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  602. END;
  603. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  604. IF backend.cooperative THEN
  605. IF HasPointers (scope) THEN
  606. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  607. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  608. ir.Emit(Result(x.position, res));
  609. ir.Emit(Push(x.position, res));
  610. implementationVisitor.ResetVariables(scope);
  611. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  612. ir.Emit(Pop(x.position, res));
  613. ir.Emit(Return(x.position, res));
  614. ELSE
  615. implementationVisitor.ResetVariables(scope);
  616. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  617. END;
  618. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  619. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  620. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  621. ir.Emit(Result(x.position, res));
  622. ir.Emit(Push(x.position, res));
  623. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  624. ir.Emit(Pop(x.position, res));
  625. ir.Emit(Return(x.position, res));
  626. ELSE
  627. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  628. END;
  629. END;
  630. implementationVisitor.EmitLeave(ir,x.position,cc);
  631. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  632. ELSE
  633. ir.Emit(Nop(x.position));
  634. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  635. implementationVisitor.EmitLeave(ir,x.position,cc);
  636. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  637. END;
  638. END;
  639. END
  640. END;
  641. ELSE (* force body for procedures *)
  642. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  643. ir.EnterValidPAF;
  644. implementationVisitor.usedRegisters := NIL;
  645. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  646. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  647. ir.ExitValidPAF;
  648. implementationVisitor.EmitLeave(ir,x.position,cc);
  649. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  650. END;
  651. Scope(scope);
  652. Signature;
  653. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  654. currentScope := prevScope;
  655. END Procedure;
  656. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  657. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  658. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  659. BEGIN
  660. ASSERT (bodyProcedure # NIL);
  661. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  662. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  663. procedure.SetScope(bodyProcedure.scope);
  664. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  665. procedure.SetAccess(SyntaxTree.Hidden);
  666. Global.GetSymbolSegmentedName (procedure,name);
  667. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  668. ir.SetExported(TRUE);
  669. ir.SetPriority(InitPriority);
  670. Global.GetSymbolSegmentedName (bodyProcedure,name);
  671. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  672. implementationVisitor.currentScope := module.module.moduleScope;
  673. implementationVisitor.section := ir;
  674. implementationVisitor.PushSelfPointer();
  675. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  676. ELSIF backend.preregisterStatic THEN
  677. implementationVisitor.currentScope := module.module.moduleScope;
  678. implementationVisitor.section := ir;
  679. implementationVisitor.PushSelfPointer();
  680. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  681. ELSE
  682. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  683. ir.Emit(Call(bodyProcedure.position,op, 0));
  684. END;
  685. END AddBodyCallStub;
  686. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  687. VAR name: Basic.SegmentedName;
  688. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  689. BEGIN
  690. Global.GetSymbolSegmentedName (symbol,name);
  691. Basic.RemoveSuffix(name);
  692. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  693. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  694. ir.SetExported(TRUE);
  695. ir.SetPriority(FirstPriority);
  696. IntermediateCode.InitImmediate(op,addressType,initStack);
  697. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  698. END AddStackAllocation;
  699. (** entry function to visit a complete module *)
  700. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  701. VAR
  702. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  703. hasDynamicOperatorDeclarations: BOOLEAN;
  704. operator: SyntaxTree.Operator;
  705. import: SyntaxTree.Import;
  706. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  707. BEGIN
  708. type := type.resolved;
  709. IF type IS SyntaxTree.RecordType THEN
  710. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  711. ELSIF (type IS SyntaxTree.ArrayType) THEN
  712. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  713. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  714. END;
  715. ELSIF type IS SyntaxTree.MathArrayType THEN
  716. WITH type: SyntaxTree.MathArrayType DO
  717. IF type.form = SyntaxTree.Open THEN
  718. RETURN TRUE
  719. ELSIF type.form = SyntaxTree.Static THEN
  720. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  721. END;
  722. END;
  723. END;
  724. RETURN FALSE
  725. END TypeNeedsInitialization;
  726. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  727. VAR variable: SyntaxTree.Variable;
  728. BEGIN
  729. variable := scope.firstVariable;
  730. WHILE variable # NIL DO
  731. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  732. IF variable.initializer # NIL THEN RETURN TRUE END;
  733. variable := variable.nextVariable;
  734. END;
  735. RETURN FALSE
  736. END ScopeNeedsInitialization;
  737. BEGIN
  738. ASSERT(x # NIL); ASSERT(module # NIL);
  739. SELF.module := module;
  740. (* add import names to the generated Sections.Module *)
  741. import := x.moduleScope.firstImport;
  742. WHILE import # NIL DO
  743. import.module.GetName(idstr);
  744. module.imports.AddName(idstr);
  745. import := import.nextImport
  746. END;
  747. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  748. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  749. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  750. moduleSelf.SetType(system.anyType);
  751. moduleSelf.SetScope(x.moduleScope);
  752. moduleSelf.SetUntraced(TRUE);
  753. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  754. ir.SetExported(TRUE);
  755. IntermediateCode.InitImmediate(op,addressType,0);
  756. ir.Emit(Data(-1,op));
  757. END;
  758. implementationVisitor.module := module;
  759. implementationVisitor.moduleScope := x.moduleScope;
  760. implementationVisitor.moduleSelf := moduleSelf;
  761. implementationVisitor.canBeLoaded := TRUE;
  762. meta.SetModule(module);
  763. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  764. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  765. END;
  766. IF backend.profile THEN
  767. EnsureBodyProcedure(x.moduleScope);
  768. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  769. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  770. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  771. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  772. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  773. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  774. Global.GetModuleName(module.module,idstr);
  775. implementationVisitor.ProfilerAddModule(idstr);
  776. implementationVisitor.numberProcedures := 0;
  777. END;
  778. implementationVisitor.profile := backend.profile;
  779. (* check if there is at least one dynamic operator locally defined *)
  780. hasDynamicOperatorDeclarations := FALSE;
  781. operator := x.moduleScope.firstOperator;
  782. WHILE operator # NIL DO
  783. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  784. operator := operator.nextOperator
  785. END;
  786. (* add operator initialization code section *)
  787. IF hasDynamicOperatorDeclarations THEN
  788. EnsureBodyProcedure(x.moduleScope);
  789. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  790. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  791. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  792. END;
  793. Scope(x.moduleScope);
  794. IF hasDynamicOperatorDeclarations THEN
  795. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  796. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  797. END;
  798. IF backend.profile THEN
  799. implementationVisitor.ProfilerPatchInit;
  800. END;
  801. END Module;
  802. END DeclarationVisitor;
  803. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  804. RegisterUsageCount*=OBJECT
  805. VAR used: UsedArray; count: LONGINT;
  806. PROCEDURE &Init;
  807. VAR i: LONGINT;
  808. BEGIN
  809. count := 0;
  810. IF used = NIL THEN NEW(used,64); END;
  811. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  812. END Init;
  813. PROCEDURE Grow;
  814. VAR new: UsedArray; size,i: LONGINT;
  815. BEGIN
  816. size := LEN(used)*2;
  817. NEW(new,size);
  818. FOR i := 0 TO LEN(used)-1 DO
  819. new[i].count := used[i].count;
  820. new[i].type := used[i].type;
  821. new[i].map := used[i].map
  822. END;
  823. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  824. used := new
  825. END Grow;
  826. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  827. BEGIN
  828. INC(count);
  829. IF count = LEN(used) THEN Grow END;
  830. used[count].type := type;
  831. used[count].class := class;
  832. used[count].map := count;
  833. RETURN count;
  834. END Next;
  835. PROCEDURE IncUse(register: LONGINT);
  836. BEGIN
  837. INC(used[register].count);
  838. (*
  839. IF (register = 1) & (count > 30) THEN
  840. D.TraceBack;
  841. END;
  842. *)
  843. END IncUse;
  844. PROCEDURE DecUse(register: LONGINT);
  845. BEGIN
  846. DEC(used[register].count);
  847. END DecUse;
  848. PROCEDURE Map(register: LONGINT): LONGINT;
  849. VAR map : LONGINT;
  850. BEGIN
  851. IF register > 0 THEN
  852. map := used[register].map;
  853. WHILE register # map DO register := map; map := used[register].map END;
  854. END;
  855. RETURN register
  856. END Map;
  857. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  858. BEGIN
  859. used[register].map:= to;
  860. used[to].count := used[register].count;
  861. used[register].count := 0;
  862. END Remap;
  863. PROCEDURE Use(register: LONGINT): LONGINT;
  864. BEGIN
  865. IF register< LEN(used) THEN
  866. RETURN used[register].count
  867. ELSE
  868. RETURN 0
  869. END
  870. END Use;
  871. END RegisterUsageCount;
  872. RegisterEntry = POINTER TO RECORD
  873. prev,next: RegisterEntry;
  874. register: LONGINT;
  875. registerClass: IntermediateCode.RegisterClass;
  876. type: IntermediateCode.Type;
  877. END;
  878. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  879. Variables = OBJECT (Basic.List)
  880. VAR
  881. inUse: VariableUse;
  882. registerIndex: LONGINT;
  883. PROCEDURE & Init;
  884. VAR i: LONGINT;
  885. BEGIN
  886. InitList(16);
  887. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  888. registerIndex := 1024;
  889. END Init;
  890. PROCEDURE GetUsage(VAR use: VariableUse);
  891. BEGIN
  892. use := inUse;
  893. END GetUsage;
  894. PROCEDURE SetUsage(CONST use: VariableUse);
  895. BEGIN
  896. inUse := use;
  897. END SetUsage;
  898. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  899. VAR any: ANY;
  900. BEGIN
  901. any := Get(i);;
  902. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  903. END GetVariable;
  904. PROCEDURE Occupy(pos: LONGINT);
  905. BEGIN
  906. INCL(inUse[pos DIV 32], pos MOD 32);
  907. END Occupy;
  908. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  909. BEGIN
  910. Occupy(Length());
  911. Add(v);
  912. END AddVariable;
  913. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  914. VAR var : SyntaxTree.Variable;
  915. BEGIN
  916. FOR pos := 0 TO Length()-1 DO
  917. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  918. var := GetVariable(pos);
  919. IF type.SameType(var.type) THEN
  920. Occupy(pos); RETURN var
  921. END;
  922. END;
  923. END;
  924. pos := Length();
  925. RETURN NIL
  926. END GetFreeVariable;
  927. END Variables;
  928. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  929. SymbolMapper = OBJECT
  930. VAR
  931. first: SymbolMap;
  932. PROCEDURE & Init;
  933. BEGIN
  934. first := NIL;
  935. END Init;
  936. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  937. VAR new: SymbolMap;
  938. BEGIN
  939. NEW(new); new.this := this; new.to := to; new.tag := tag;
  940. new.next := first; first := new;
  941. END Add;
  942. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  943. VAR s: SymbolMap;
  944. BEGIN
  945. s := first;
  946. WHILE (s # NIL) & (s.this # this) DO
  947. s := s.next
  948. END;
  949. RETURN s
  950. END Get;
  951. END SymbolMapper;
  952. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  953. VAR
  954. system: Global.System;
  955. section: IntermediateCode.Section;
  956. module: Sections.Module;
  957. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  958. awaitProcCounter, labelId, constId, caseId: LONGINT;
  959. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  960. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  961. checker: SemanticChecker.Checker;
  962. backend: IntermediateBackend;
  963. meta: MetaDataGenerator;
  964. position: LONGINT;
  965. moduleSelf: SyntaxTree.Variable;
  966. (* variables for hand over of variables / temporary state *)
  967. currentScope: SyntaxTree.Scope;
  968. constantDeclaration : SyntaxTree.Symbol;
  969. result: Operand; (* result of the most recent expression / statement *)
  970. destination: IntermediateCode.Operand;
  971. arrayDestinationTag: IntermediateCode.Operand;
  972. arrayDestinationDimension:LONGINT;
  973. currentLoop: Label; (* variable to hand over loop exit jump list *)
  974. conditional: BOOLEAN;
  975. trueLabel, falseLabel, exitLabel: Label;
  976. locked: BOOLEAN;
  977. (*
  978. usedRegisters: Registers;
  979. *)
  980. registerUsageCount: RegisterUsageCount;
  981. usedRegisters: RegisterEntry;
  982. (* useful operands and types *)
  983. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  984. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  985. commentPrintout: Printout.Printer;
  986. dump: Streams.Writer;
  987. tagsAvailable : BOOLEAN;
  988. supportedInstruction: SupportedInstructionProcedure;
  989. supportedImmediate: SupportedImmediateProcedure;
  990. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  991. emitLabels: BOOLEAN;
  992. runtimeModuleName : SyntaxTree.IdentifierString;
  993. newObjectFile: BOOLEAN;
  994. indexCounter: LONGINT;
  995. profile: BOOLEAN;
  996. profileId, profileInit: IntermediateCode.Section;
  997. profileInitPatchPosition: LONGINT;
  998. numberProcedures: LONGINT;
  999. procedureResultDesignator : SyntaxTree.Designator;
  1000. operatorInitializationCodeSection: IntermediateCode.Section;
  1001. fingerPrinter: FingerPrinter.FingerPrinter;
  1002. temporaries: Variables;
  1003. canBeLoaded : BOOLEAN;
  1004. currentIsInline: BOOLEAN;
  1005. currentMapper: SymbolMapper;
  1006. currentInlineExit: Label;
  1007. moduleBodySection: IntermediateCode.Section;
  1008. NeedDescriptor : BOOLEAN;
  1009. cooperativeSwitches: BOOLEAN;
  1010. lastSwitchPC: LONGINT;
  1011. isUnchecked: BOOLEAN;
  1012. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1013. newObjectFile: BOOLEAN);
  1014. BEGIN
  1015. SELF.system := system;
  1016. SELF.runtimeModuleName := runtime;
  1017. currentScope := NIL;
  1018. hiddenPointerType := NIL;
  1019. delegatePointerType := NIL;
  1020. awaitProcCounter := 0;
  1021. labelId := 0; constId := 0; labelId := 0;
  1022. SELF.checker := checker;
  1023. SELF.backend := backend;
  1024. position := Diagnostics.Invalid;
  1025. conditional := FALSE;
  1026. locked := FALSE;
  1027. InitOperand(result,ModeUndefined);
  1028. addressType := IntermediateCode.GetType(system,system.addressType);
  1029. setType := IntermediateCode.GetType(system,system.setType);
  1030. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1031. byteType := IntermediateCode.GetType(system, system.byteType);
  1032. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1033. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1034. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1035. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1036. nil := IntermediateCode.Immediate(addressType,0);
  1037. IntermediateCode.InitOperand(destination);
  1038. tagsAvailable := TRUE;
  1039. supportedInstruction := supportedInstructionProcedure;
  1040. supportedImmediate := supportedImmediateProcedure;
  1041. inData := FALSE;
  1042. SELF.emitLabels := emitLabels;
  1043. IntermediateCode.InitOperand(arrayDestinationTag);
  1044. bool := IntermediateCode.GetType(system,system.booleanType);
  1045. IntermediateCode.InitImmediate(false,bool,0);
  1046. IntermediateCode.InitImmediate(true,bool,1);
  1047. SELF.newObjectFile := newObjectFile;
  1048. indexCounter := 0;
  1049. NEW(registerUsageCount);
  1050. usedRegisters := NIL;
  1051. procedureResultDesignator := NIL;
  1052. NEW(fingerPrinter, system);
  1053. NEW(temporaries);
  1054. currentIsInline := FALSE;
  1055. NeedDescriptor := FALSE;
  1056. isUnchecked := backend.noRuntimeChecks;
  1057. END Init;
  1058. TYPE Context = RECORD
  1059. section: IntermediateCode.Section;
  1060. registerUsageCount: RegisterUsageCount;
  1061. usedRegisters: RegisterEntry;
  1062. END;
  1063. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1064. VAR context: Context;
  1065. BEGIN
  1066. context.section := section;
  1067. context.registerUsageCount := registerUsageCount;
  1068. context.usedRegisters := usedRegisters;
  1069. section := new;
  1070. NEW(registerUsageCount);
  1071. usedRegisters := NIL;
  1072. RETURN context;
  1073. END SwitchContext;
  1074. PROCEDURE ReturnToContext(context: Context);
  1075. BEGIN
  1076. section := context.section;
  1077. registerUsageCount := context.registerUsageCount;
  1078. usedRegisters := context.usedRegisters;
  1079. END ReturnToContext;
  1080. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1081. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1082. BEGIN
  1083. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1084. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1085. END;
  1086. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1087. section.SetExported(IsExported(syntaxTreeSymbol));
  1088. RETURN section
  1089. END NewSection;
  1090. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1091. VAR new: LONGINT;
  1092. BEGIN
  1093. new := registerUsageCount.Next(type,class);
  1094. UseRegister(new);
  1095. RETURN new
  1096. END AcquireRegister;
  1097. (** get the name for the code section that represens a certain symbol
  1098. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1099. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1100. VAR
  1101. operatorFingerPrint: SyntaxTree.FingerPrint;
  1102. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1103. BEGIN
  1104. Global.GetSymbolSegmentedName(symbol, name);
  1105. (* if the symbol is an operator, then append the fingerprint to the name *)
  1106. IF symbol IS SyntaxTree.Operator THEN
  1107. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1108. string := "[";
  1109. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1110. Strings.Append(string, operatorFingerPrintString);
  1111. Strings.Append(string, "]");
  1112. Basic.AppendToSegmentedName(name,string);
  1113. END
  1114. END GetCodeSectionNameForSymbol;
  1115. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1116. BEGIN
  1117. IF dump # NIL THEN
  1118. dump.String("enter "); dump.String(s); dump.Ln;
  1119. END;
  1120. END TraceEnter;
  1121. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1122. BEGIN
  1123. IF dump # NIL THEN
  1124. dump.String("exit "); dump.String(s); dump.Ln;
  1125. END;
  1126. END TraceExit;
  1127. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1128. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1129. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1130. VAR i: LONGINT;
  1131. BEGIN
  1132. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1133. IF op.rule # NIL THEN
  1134. FOR i := 0 TO LEN(op.rule)-1 DO
  1135. CheckRegister(op.rule[i])
  1136. END;
  1137. END;
  1138. END CheckRegister;
  1139. BEGIN
  1140. CheckRegister(instruction.op1);
  1141. CheckRegister(instruction.op2);
  1142. CheckRegister(instruction.op3);
  1143. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1144. ELSE section.Emit(instruction);
  1145. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1146. END;
  1147. END Emit;
  1148. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1149. BEGIN
  1150. IF backend.cooperative THEN
  1151. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1152. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1153. ELSE
  1154. Emit(Trap(position,trapNo));
  1155. END;
  1156. END EmitTrap;
  1157. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1158. VAR name: Basic.SegmentedName;
  1159. VAR op1, op2, reg: IntermediateCode.Operand;
  1160. VAR call, nocall: Label;
  1161. VAR parametersSize: LONGINT;
  1162. VAR prevSection: IntermediateCode.Section;
  1163. VAR prevDump: Streams.Writer;
  1164. BEGIN
  1165. ASSERT(~procedure.type(SyntaxTree.ProcedureType).noPAF);
  1166. prevSection := SELF.section;
  1167. SELF.section := section;
  1168. prevDump := dump;
  1169. dump := section.comments;
  1170. IF backend.hasLinkRegister THEN
  1171. Emit(Push(-1, lr));
  1172. END;
  1173. Emit(Push(-1,fp));
  1174. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1175. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1176. GetCodeSectionNameForSymbol(procedure, name);
  1177. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1178. ELSE
  1179. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1180. END;
  1181. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1182. Emit(Push(-1,op1));
  1183. Emit(Mov(-1,fp, sp));
  1184. IF (procedure.procedureScope.body = NIL) OR ~procedure.procedureScope.body.isUnchecked THEN
  1185. NEW(call, section);
  1186. NEW(nocall, section);
  1187. reg := NewRegisterOperand(addressType);
  1188. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1189. Emit(Sub(-1,reg, sp, op1));
  1190. BrltL(call, sp, reg);
  1191. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1192. BrgeL(nocall, sp, op2);
  1193. call.Resolve(section.pc);
  1194. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1195. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1196. Emit(Push(-1, op2));
  1197. Emit(Push(-1, reg));
  1198. ReleaseIntermediateOperand(reg);
  1199. CallThis(position, "Activities","ExpandStack",0);
  1200. Emit(Result(-1, sp));
  1201. nocall.Resolve(section.pc);
  1202. END;
  1203. ELSE
  1204. Emit(Mov(-1, fp, sp));
  1205. END;
  1206. Emit(Enter(-1, callconv, varSize));
  1207. SELF.section := prevSection;
  1208. dump := prevDump;
  1209. END EmitEnter;
  1210. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1211. VAR op1,op2: IntermediateCode.Operand;
  1212. VAR instruction: IntermediateCode.Instruction;
  1213. BEGIN
  1214. IntermediateCode.InitNumber(op1,callconv);
  1215. IntermediateCode.InitNumber(op2,varSize);
  1216. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1217. RETURN instruction
  1218. END Enter;
  1219. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1220. VAR op1: IntermediateCode.Operand;
  1221. VAR instruction: IntermediateCode.Instruction;
  1222. BEGIN
  1223. IntermediateCode.InitNumber(op1,callconv);
  1224. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1225. RETURN instruction
  1226. END Leave;
  1227. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1228. VAR prevSection: IntermediateCode.Section;
  1229. VAR op2: IntermediateCode.Operand;
  1230. BEGIN
  1231. prevSection := SELF.section;
  1232. SELF.section := section;
  1233. Emit(Leave(position, callconv));
  1234. IF backend.cooperative THEN
  1235. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1236. Emit(Add(position, sp, fp, op2));
  1237. ELSE
  1238. Emit(Mov(position, sp, fp));
  1239. END;
  1240. Emit(Pop(position, fp));
  1241. SELF.section := prevSection;
  1242. END EmitLeave;
  1243. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1244. VAR m: SymbolMap;
  1245. BEGIN
  1246. position := x.position;
  1247. IF currentIsInline THEN
  1248. m := currentMapper.Get(x);
  1249. IF m # NIL THEN
  1250. (*
  1251. Printout.Info("mapping from", x);
  1252. Printout.Info("mapping to ", m.to);
  1253. *)
  1254. m.to.Accept(SELF);
  1255. op := result;
  1256. IF m.tag # NIL THEN
  1257. ReleaseIntermediateOperand(result.tag);
  1258. m.tag.Accept(SELF);
  1259. op.tag := result.op;
  1260. ReleaseIntermediateOperand(result.tag);
  1261. END;
  1262. RETURN
  1263. END;
  1264. END;
  1265. x.Accept(SELF);
  1266. op := result;
  1267. END Symbol;
  1268. PROCEDURE Expression(x: SyntaxTree.Expression);
  1269. BEGIN
  1270. position := x.position;
  1271. constantDeclaration := NIL;
  1272. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1273. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1274. END;
  1275. IF x.resolved # NIL THEN
  1276. x.resolved.Accept(SELF)
  1277. ELSE
  1278. x.Accept(SELF)
  1279. END;
  1280. (* check this, was commented out in ActiveCells3 *)
  1281. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1282. Error(x.position, "unsupported modifier");
  1283. END;
  1284. END Expression;
  1285. (*
  1286. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1287. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1288. BEGIN
  1289. temporaries.GetUsage(current);
  1290. FOR i := 0 TO LEN(current)-1 DO
  1291. set := current[i] - previous[i];
  1292. IF set # {} THEN
  1293. FOR j := 0 TO MAX(SET)-1 DO
  1294. IF j IN set THEN
  1295. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1296. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1297. Symbol(variable, op);
  1298. MakeMemory(tmp,op.op,addressType,0);
  1299. ReleaseOperand(op);
  1300. Emit(Mov(position,tmp, nil ) );
  1301. ReleaseIntermediateOperand(tmp);
  1302. END;
  1303. END;
  1304. END;
  1305. END;
  1306. END;
  1307. END ResetUsedTemporaries;
  1308. *)
  1309. PROCEDURE Statement(x: SyntaxTree.Statement);
  1310. VAR use: VariableUse;
  1311. BEGIN
  1312. temporaries.GetUsage(use);
  1313. position := x.position;
  1314. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1315. IF commentPrintout # NIL THEN
  1316. commentPrintout.Statement(x);
  1317. dump.Ln;
  1318. (*dump.Update;*)
  1319. END;
  1320. x.Accept(SELF);
  1321. (*
  1322. CheckRegistersFree();
  1323. *)
  1324. (*ResetUsedTemporaries(use);*)
  1325. temporaries.SetUsage(use);
  1326. END Statement;
  1327. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1328. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1329. *)
  1330. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1331. BEGIN
  1332. IF op.mode = IntermediateCode.ModeMemory THEN
  1333. ReuseCopy(res,op);
  1334. ELSE
  1335. res := op;
  1336. UseIntermediateOperand(res);
  1337. END;
  1338. IntermediateCode.AddOffset(res,offset);
  1339. IntermediateCode.MakeMemory(res,type);
  1340. END MakeMemory;
  1341. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1342. VAR mem: IntermediateCode.Operand;
  1343. BEGIN
  1344. MakeMemory(mem,res,type,offset);
  1345. ReleaseIntermediateOperand(res);
  1346. res := mem;
  1347. END ToMemory;
  1348. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1349. VAR mem: IntermediateCode.Operand;
  1350. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1351. componentType: SyntaxTree.Type;
  1352. BEGIN
  1353. type := type.resolved;
  1354. IF operand.mode = ModeReference THEN
  1355. IF type IS SyntaxTree.RangeType THEN
  1356. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1357. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1358. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1359. ReleaseOperand(operand);
  1360. operand.op := firstOp;
  1361. operand.tag := lastOp;
  1362. operand.extra := stepOp;
  1363. ELSIF type IS SyntaxTree.ComplexType THEN
  1364. componentType := type(SyntaxTree.ComplexType).componentType;
  1365. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1366. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1367. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1368. ReleaseOperand(operand);
  1369. operand.op := firstOp;
  1370. operand.tag := lastOp
  1371. ELSE
  1372. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1373. ReleaseIntermediateOperand(operand.op);
  1374. operand.op := mem;
  1375. END;
  1376. operand.mode := ModeValue;
  1377. END;
  1378. ASSERT(operand.mode = ModeValue);
  1379. END LoadValue;
  1380. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1381. VAR prevConditional: BOOLEAN;
  1382. BEGIN
  1383. prevConditional := conditional;
  1384. conditional := FALSE;
  1385. InitOperand(result, ModeUndefined);
  1386. Expression(x);
  1387. op := result;
  1388. LoadValue(op,x.type.resolved);
  1389. conditional := prevConditional;
  1390. END Evaluate;
  1391. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1392. VAR prevConditional: BOOLEAN;
  1393. BEGIN
  1394. prevConditional := conditional;
  1395. conditional := FALSE;
  1396. InitOperand(result,ModeUndefined);
  1397. Expression(x);
  1398. op := result;
  1399. (*
  1400. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1401. *)
  1402. conditional := prevConditional;
  1403. END Designate;
  1404. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1405. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1406. BEGIN
  1407. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1408. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1409. conditional := TRUE;
  1410. trueLabel := trueL; falseLabel := falseL;
  1411. Expression(x);
  1412. trueL := trueLabel; falseL := falseLabel;
  1413. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1414. END Condition;
  1415. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1416. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1417. BEGIN
  1418. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1419. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1420. RETURN op
  1421. END NewRegisterOperand;
  1422. PROCEDURE UnuseRegister(register: LONGINT);
  1423. BEGIN
  1424. IF (register > 0) THEN
  1425. register := registerUsageCount.Map(register);
  1426. registerUsageCount.DecUse(register);
  1427. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1428. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1429. END;
  1430. IF registerUsageCount.Use(register)=0 THEN
  1431. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1432. Warning(position, "register cannot be removed");
  1433. END;
  1434. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1435. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1436. END;
  1437. ELSIF registerUsageCount.Use(register)<0 THEN
  1438. Warning(position, "register removed too often");
  1439. IF dump # NIL THEN
  1440. dump.String("register removed too often"); dump.Ln; dump.Update;
  1441. END;
  1442. D.TraceBack;
  1443. END;
  1444. END;
  1445. END UnuseRegister;
  1446. PROCEDURE UseRegister(register: LONGINT);
  1447. BEGIN
  1448. IF (register > 0) THEN
  1449. register := registerUsageCount.Map(register);
  1450. registerUsageCount.IncUse(register);
  1451. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1452. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1453. END;
  1454. IF registerUsageCount.Use(register)=1 THEN
  1455. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1456. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1457. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1458. END;
  1459. END;
  1460. END;
  1461. END UseRegister;
  1462. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1463. BEGIN
  1464. UnuseRegister(op.register)
  1465. END ReleaseIntermediateOperand;
  1466. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1467. BEGIN
  1468. UseRegister(op.register)
  1469. END UseIntermediateOperand;
  1470. PROCEDURE ReleaseOperand(CONST op: Operand);
  1471. BEGIN
  1472. UnuseRegister(op.op.register);
  1473. UnuseRegister(op.tag.register);
  1474. UnuseRegister(op.extra.register);
  1475. END ReleaseOperand;
  1476. (* save registers marked in array "markedRegisters" to the stack
  1477. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1478. *)
  1479. PROCEDURE SaveRegisters();
  1480. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1481. BEGIN
  1482. entry := usedRegisters;
  1483. WHILE entry # NIL DO
  1484. type := registerUsageCount.used[entry.register].type;
  1485. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1486. Emit(Push(position,op));
  1487. entry := entry.next;
  1488. END;
  1489. END SaveRegisters;
  1490. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1491. BEGIN
  1492. saved := usedRegisters;
  1493. usedRegisters := NIL;
  1494. END ReleaseUsedRegisters;
  1495. (** remove parameter registers from used queue *)
  1496. PROCEDURE ReleaseParameterRegisters;
  1497. VAR entry,prev,next: RegisterEntry;
  1498. BEGIN
  1499. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1500. WHILE entry # NIL DO
  1501. next := entry.next;
  1502. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1503. registerUsageCount.DecUse(entry.register);
  1504. ASSERT(registerUsageCount.Use(entry.register)=0);
  1505. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1506. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1507. END;
  1508. ELSIF prev = NIL THEN
  1509. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1510. ELSE
  1511. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1512. END;
  1513. entry := next;
  1514. END;
  1515. END ReleaseParameterRegisters;
  1516. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1517. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1518. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1519. BEGIN
  1520. entry := saved;
  1521. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1522. entry := prev;
  1523. WHILE entry # NIL DO
  1524. prev := entry.prev;
  1525. type := entry.type;
  1526. class := entry.registerClass;
  1527. IntermediateCode.InitRegister(op,type,class,entry.register);
  1528. (*
  1529. new := registerUsageCount.Next(type,class);
  1530. registerUsageCount.Remap(entry.register,new);
  1531. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1532. dump.String("remap register "); dump.Int(entry.register,1);
  1533. dump.String("to "); dump.Int(new,1);
  1534. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1535. END;
  1536. entry.register := new;
  1537. *)
  1538. Emit(Pop(position,op));
  1539. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1540. entry := prev;
  1541. END;
  1542. (*
  1543. usedRegisters := saved;
  1544. *)
  1545. END RestoreRegisters;
  1546. PROCEDURE CheckRegistersFree;
  1547. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1548. BEGIN
  1549. IF usedRegisters # NIL THEN
  1550. r := usedRegisters;
  1551. WHILE r # NIL DO
  1552. warning := "register ";
  1553. Strings.AppendInt(warning, r.register);
  1554. Strings.Append(warning, " not released.");
  1555. Warning(position,warning);
  1556. r := r .next;
  1557. END;
  1558. END;
  1559. FOR i := 0 TO registerUsageCount.count-1 DO
  1560. IF registerUsageCount.used[i].count < 0 THEN
  1561. warning := "register ";
  1562. Strings.AppendInt(warning, i);
  1563. Strings.Append(warning, " unused too often.");
  1564. Warning(position,warning);
  1565. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1566. warning := "register ";
  1567. Strings.AppendInt(warning, i);
  1568. Strings.Append(warning, " not unused often enough.");
  1569. Warning(position,warning);
  1570. END;
  1571. END;
  1572. END CheckRegistersFree;
  1573. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1574. Otherwise allocate a new register.
  1575. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1576. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1577. BEGIN
  1578. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1579. UseIntermediateOperand(result);
  1580. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1581. UseIntermediateOperand(result);
  1582. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1583. END;
  1584. END Reuse2;
  1585. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1586. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1587. If not then allocate a new register.
  1588. Does NOT necessarily keep the content of src1 or src2 in result!
  1589. *)
  1590. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1591. BEGIN
  1592. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1593. UseIntermediateOperand(result);
  1594. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1595. UseIntermediateOperand(result);
  1596. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1597. result := alternative; alternative := emptyOperand;
  1598. UseIntermediateOperand(result);
  1599. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1600. END;
  1601. END Reuse2a;
  1602. (* like reuse2 but only one source *)
  1603. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1604. BEGIN
  1605. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1606. UseIntermediateOperand(result);
  1607. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1608. END;
  1609. END Reuse1;
  1610. (* like reuse2a but only one source *)
  1611. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1612. BEGIN
  1613. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1614. UseIntermediateOperand(result);
  1615. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1616. UseIntermediateOperand(result);
  1617. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1618. END;
  1619. END Reuse1a;
  1620. (* like reuse1 but guarantees that content of src1 is in result *)
  1621. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1622. BEGIN
  1623. IF ReusableRegister(src1) THEN
  1624. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1625. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1626. UseIntermediateOperand(result);
  1627. ELSE
  1628. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1629. Emit(Mov(position,result,src1));
  1630. END
  1631. END ReuseCopy;
  1632. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1633. BEGIN
  1634. IF ReusableRegister(src) THEN
  1635. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1636. ELSE
  1637. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1638. Emit(Mov(position,result,src));
  1639. ReleaseIntermediateOperand(src);
  1640. END
  1641. END TransferToRegister;
  1642. (** labels and branches **)
  1643. PROCEDURE NewLabel(): Label;
  1644. VAR label: Label;
  1645. BEGIN
  1646. NEW(label,section); RETURN label;
  1647. END NewLabel;
  1648. PROCEDURE SetLabel(label: Label);
  1649. BEGIN label.Resolve(section.pc);
  1650. END SetLabel;
  1651. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1652. BEGIN
  1653. ASSERT(label # NIL);
  1654. IF label.pc < 0 THEN (* label not yet set *)
  1655. label.AddFixup(section.pc);
  1656. END;
  1657. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1658. END LabelOperand;
  1659. PROCEDURE BrL(label: Label);
  1660. BEGIN
  1661. Emit(Br(position,LabelOperand(label)));
  1662. END BrL;
  1663. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1664. BEGIN
  1665. Emit(Brge(position,LabelOperand(label),left,right));
  1666. END BrgeL;
  1667. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1668. BEGIN
  1669. Emit(Brlt(position,LabelOperand(label),left,right));
  1670. END BrltL;
  1671. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1672. BEGIN
  1673. Emit(Breq(position,LabelOperand(label),left,right));
  1674. END BreqL;
  1675. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1676. BEGIN
  1677. Emit(Brne(position,LabelOperand(label),left,right));
  1678. END BrneL;
  1679. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1680. VAR new: IntermediateCode.Operand;
  1681. BEGIN
  1682. IF Trace THEN TraceEnter("Convert") END;
  1683. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1684. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1685. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1686. & (operand.offset = 0)
  1687. THEN
  1688. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1689. Emit(Conv(position,new,operand));
  1690. ELSE
  1691. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1692. Emit(Conv(position,new,operand));
  1693. ReleaseIntermediateOperand(operand);
  1694. END;
  1695. operand := new;
  1696. 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
  1697. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1698. ELSE
  1699. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1700. Emit(Conv(position,new,operand));
  1701. ReleaseIntermediateOperand(operand);
  1702. operand := new;
  1703. END;
  1704. IF Trace THEN TraceExit("Convert") END;
  1705. END Convert;
  1706. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1707. VAR exit: Label;
  1708. BEGIN
  1709. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1710. exit := NewLabel();
  1711. br(exit,left,right);
  1712. EmitTrap(position,trapNo);
  1713. SetLabel(exit);
  1714. END TrapC;
  1715. (** expressions *)
  1716. (** emit necessary runtime check for set elements **)
  1717. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1718. VAR max: IntermediateCode.Operand;
  1719. BEGIN
  1720. IF isUnchecked THEN RETURN END;
  1721. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1722. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1723. TrapC(BrgeL, max, o, SetElementTrap);
  1724. END;
  1725. END CheckSetElement;
  1726. (** the set that a range represents **)
  1727. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1728. VAR
  1729. operand: Operand;
  1730. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1731. BEGIN
  1732. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1733. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1734. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1735. one := IntermediateCode.Immediate(setType, 1);
  1736. Evaluate(x, operand);
  1737. Convert(operand.op, setType);
  1738. Convert(operand.tag, setType);
  1739. CheckSetElement(operand.op);
  1740. CheckSetElement(operand.tag);
  1741. (* create mask for lower bound
  1742. i.e. shift 11111111 to the left by the value of the lower bound
  1743. *)
  1744. Reuse1(temp, operand.op);
  1745. Emit(Shl(position,temp, allBits, operand.op));
  1746. ReleaseIntermediateOperand(operand.op);
  1747. operand.op := temp;
  1748. (* create mask for upper bound
  1749. i.e. shift 11111111 to the right by the difference between the
  1750. upper bound and the maximum number of set elements
  1751. *)
  1752. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1753. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1754. Reuse1(temp, operand.tag);
  1755. ELSE
  1756. Reuse1(temp, operand.tag);
  1757. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1758. Emit(Sub(position,temp, size, operand.tag));
  1759. END;
  1760. Emit(Shr(position,temp, allBits, operand.tag));
  1761. ReleaseIntermediateOperand(operand.tag);
  1762. operand.tag := temp;
  1763. Reuse2(resultingSet, operand.op, operand.tag);
  1764. (* intersect the two masks *)
  1765. Emit(And(position,resultingSet, operand.op, operand.tag));
  1766. ReleaseOperand(operand);
  1767. RETURN resultingSet
  1768. END SetFromRange;
  1769. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1770. VAR
  1771. res, operand: Operand;
  1772. temp, one, noBits, dest: IntermediateCode.Operand;
  1773. expression: SyntaxTree.Expression;
  1774. i: LONGINT;
  1775. BEGIN
  1776. IF Trace THEN TraceEnter("VisitSet") END;
  1777. dest := destination;
  1778. destination := emptyOperand;
  1779. noBits := IntermediateCode.Immediate(setType, 0);
  1780. one := IntermediateCode.Immediate(setType, 1);
  1781. (* start off with the empty set *)
  1782. InitOperand(res, ModeValue);
  1783. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1784. Emit(Mov(position,res.op, noBits));
  1785. FOR i := 0 TO x.elements.Length() - 1 DO
  1786. expression := x.elements.GetExpression(i);
  1787. IF expression IS SyntaxTree.RangeExpression THEN
  1788. (* range of set elements *)
  1789. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1790. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1791. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1792. ReleaseIntermediateOperand(temp)
  1793. ELSE
  1794. (* singelton element *)
  1795. Evaluate(expression, operand);
  1796. Convert(operand.op, setType);
  1797. CheckSetElement(operand.op);
  1798. (* create subset containing single element *)
  1799. Reuse1(temp, operand.op);
  1800. Emit(Shl(position,temp, one, operand.op));
  1801. ReleaseOperand(operand);
  1802. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1803. ReleaseIntermediateOperand(temp);
  1804. END
  1805. END;
  1806. result := res;
  1807. destination := dest;
  1808. IF Trace THEN TraceExit("VisitSet") END;
  1809. END VisitSet;
  1810. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1811. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1812. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1813. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1814. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1815. element: SyntaxTree.IntegerValue;
  1816. BEGIN
  1817. numberElements := x.elements.Length();
  1818. expression := index.parameters.GetExpression(dim);
  1819. element := expression(SyntaxTree.IntegerValue);
  1820. FOR i := 0 TO numberElements-1 DO
  1821. expression := x.elements.GetExpression(i);
  1822. element.SetValue(i);
  1823. IF expression IS SyntaxTree.MathArrayExpression THEN
  1824. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1825. ELSE
  1826. Assign(index,expression);
  1827. END;
  1828. END;
  1829. END RecursiveAssignment;
  1830. BEGIN
  1831. variable := GetTemporaryVariable(x.type, FALSE);
  1832. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1833. designator.SetType(variable.type);
  1834. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1835. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1836. FOR i := 0 TO dim-1 DO
  1837. element := SyntaxTree.NewIntegerValue(x.position,0);
  1838. element.SetType(system.longintType);
  1839. index.parameters.AddExpression(element);
  1840. END;
  1841. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1842. RecursiveAssignment(x,0);
  1843. Expression(designator);
  1844. END VisitMathArrayExpression;
  1845. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1846. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1847. BEGIN
  1848. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1849. dest := destination; destination := emptyOperand;
  1850. IF x.operator = Scanner.Not THEN
  1851. IF conditional THEN
  1852. Condition(x.left,falseLabel,trueLabel)
  1853. ELSE
  1854. Evaluate(x.left,operand);
  1855. InitOperand(result,ModeValue);
  1856. Reuse1a(result.op,operand.op,dest);
  1857. Emit(Xor(position,result.op,operand.op,true));
  1858. ReleaseOperand(operand);
  1859. END;
  1860. ELSIF x.operator = Scanner.Minus THEN
  1861. Evaluate(x.left,operand);
  1862. InitOperand(result,ModeValue);
  1863. Reuse1a(result.op,operand.op,dest);
  1864. type := x.left.type.resolved;
  1865. IF type IS SyntaxTree.SetType THEN
  1866. Emit(Not(position,result.op,operand.op));
  1867. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1868. Reuse1(result.tag,operand.tag);
  1869. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1870. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1871. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1872. Emit(Neg(position,result.op,operand.op));
  1873. ELSE HALT(200)
  1874. END;
  1875. ReleaseOperand(operand);
  1876. ELSIF x.operator = Scanner.Address THEN
  1877. Designate(x.left,operand);
  1878. operand.mode := ModeValue;
  1879. t0 := x.left.type.resolved;
  1880. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1881. ReleaseIntermediateOperand(operand.op);
  1882. operand.op := operand.tag;
  1883. IntermediateCode.InitOperand(operand.tag);
  1884. END;
  1885. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1886. result := operand;
  1887. ELSE HALT(100)
  1888. END;
  1889. destination := dest;
  1890. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1891. END VisitUnaryExpression;
  1892. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1893. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1894. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1895. BEGIN
  1896. type := type.resolved;
  1897. originalType := type;
  1898. IF type IS SyntaxTree.PointerType THEN
  1899. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1900. END;
  1901. IF type IS SyntaxTree.ObjectType THEN
  1902. BrL(trueL);
  1903. ELSE
  1904. ASSERT(type IS SyntaxTree.RecordType);
  1905. (*
  1906. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1907. *)
  1908. ReuseCopy(left,tag);
  1909. right := TypeDescriptorAdr(type);
  1910. IF ~newObjectFile THEN
  1911. IntermediateCode.MakeMemory(right,addressType);
  1912. END;
  1913. IF backend.cooperative THEN
  1914. repeatL := NewLabel();
  1915. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1916. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1917. END;
  1918. SetLabel(repeatL);
  1919. BreqL(trueL,left,right);
  1920. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1921. BrneL(repeatL,left,nil);
  1922. ELSE
  1923. level := type(SyntaxTree.RecordType).Level();
  1924. (* get type desc tag of level relative to base tag *)
  1925. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1926. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1927. IntermediateCode.MakeMemory(left,addressType);
  1928. BreqL(trueL,left,right);
  1929. END;
  1930. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1931. BrL(falseL);
  1932. END;
  1933. END TypeTest;
  1934. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1935. BEGIN
  1936. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1937. IF dump # NIL THEN
  1938. dump.String(s); dump.Ln;
  1939. END;
  1940. END Error;
  1941. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1942. BEGIN
  1943. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1944. IF dump # NIL THEN
  1945. dump.String(s); dump.Ln; dump.Update;
  1946. END;
  1947. END Warning;
  1948. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1949. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1950. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1951. operand:Operand;
  1952. BEGIN
  1953. previousSection := section;
  1954. Global.GetModuleName(mod,name);
  1955. Strings.Append(name,".@Trace");
  1956. Basic.ToSegmentedName(name, pooledName);
  1957. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1958. IF dump # NIL THEN dump := section.comments END;
  1959. variable := mod.moduleScope.firstVariable;
  1960. WHILE variable # NIL DO
  1961. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1962. Symbol(variable, operand);
  1963. register := operand.op;
  1964. CallTraceMethod(register, variable.type);
  1965. ReleaseIntermediateOperand(register);
  1966. END;
  1967. variable := variable.nextVariable;
  1968. END;
  1969. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1970. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1971. Emit(Br(position,op));
  1972. INC(statCoopTraceModule, section.pc);
  1973. section := previousSection;
  1974. IF dump # NIL THEN dump := section.comments END;
  1975. END CreateTraceModuleMethod;
  1976. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1977. BEGIN
  1978. Emit (Push(position, dst));
  1979. Emit (Push(position, src));
  1980. CallThis(position,"GarbageCollector","Assign", 2);
  1981. END CallAssignPointer;
  1982. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1983. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1984. BEGIN
  1985. IF SemanticChecker.IsPointerType (type) THEN
  1986. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  1987. ELSIF type.IsRecordType() THEN
  1988. Emit (Push(position,dst));
  1989. Emit (Push(position,src));
  1990. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  1991. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1992. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1993. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  1994. ELSIF IsStaticArray(type) THEN
  1995. size := StaticArrayNumElements(type);
  1996. base := StaticArrayBaseType(type);
  1997. IF base.IsRecordType() THEN
  1998. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1999. Emit (Push(position, dst));
  2000. Emit (Push(position, src));
  2001. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2002. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2003. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2004. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2005. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2006. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2007. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2008. Emit (Push(position, dst));
  2009. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2010. Emit (Push(position, src));
  2011. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2012. ELSE
  2013. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2014. Emit (Push(position, dst));
  2015. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2016. Emit (Push(position, src));
  2017. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2018. ASSERT(StaticArrayBaseType(type).IsPointer());
  2019. END;
  2020. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2021. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2022. Emit (Push(position, dst));
  2023. Emit (Push(position, src));
  2024. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2025. ELSE HALT(100); (* missing ? *)
  2026. END;
  2027. END CallAssignMethod;
  2028. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2029. VAR name: Basic.SegmentedName;
  2030. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2031. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2032. context: Context;
  2033. BEGIN
  2034. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2035. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2036. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2037. GetRecordTypeName (recordType,name);
  2038. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2039. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2040. IF dump # NIL THEN dump := section.comments END;
  2041. variable := recordType.recordScope.firstVariable;
  2042. WHILE variable # NIL DO
  2043. IF variable.NeedsTrace() THEN
  2044. dst := NewRegisterOperand (addressType);
  2045. src := NewRegisterOperand (addressType);
  2046. Emit (Mov(position, dst, parameter1));
  2047. Emit (Mov(position, src, parameter2));
  2048. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2049. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2050. CallAssignMethod(dst, src, variable.type);
  2051. ReleaseIntermediateOperand(src);
  2052. ReleaseIntermediateOperand(dst);
  2053. END;
  2054. variable := variable.nextVariable;
  2055. END;
  2056. recordBase := recordType.GetBaseRecord();
  2057. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2058. GetRecordTypeName (recordBase,name);
  2059. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2060. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2061. Emit(Br(position,op));
  2062. ELSE
  2063. Emit(Exit(position,0,0));
  2064. END;
  2065. IF ~recordType.isObject THEN
  2066. GetRecordTypeName (recordType,name);
  2067. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2068. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2069. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2070. NEW(registerUsageCount);
  2071. usedRegisters := NIL;
  2072. dst := NewRegisterOperand (addressType);
  2073. src := NewRegisterOperand (addressType);
  2074. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2075. Emit(Mov(position, dst, parameter1));
  2076. Emit(Mov(position, src, parameter2));
  2077. label := NewLabel();
  2078. SetLabel(label);
  2079. Emit(Push(position, dst));
  2080. Emit(Push(position, src));
  2081. GetRecordTypeName (recordType,name);
  2082. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2083. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2084. Emit(Call(position, op, 0));
  2085. Emit(Pop(position, src));
  2086. Emit(Pop(position, dst));
  2087. Emit(Add(position, dst, dst, ofs));
  2088. Emit(Add(position, src, src, ofs));
  2089. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2090. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2091. Emit(Exit(position,0,0));
  2092. END;
  2093. INC(statCoopAssignProcedure, section.pc);
  2094. ReturnToContext(context);
  2095. IF dump # NIL THEN dump := section.comments END;
  2096. END CreateAssignProcedure;
  2097. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2098. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2099. BEGIN
  2100. IF IsUnsafePointer (type) THEN
  2101. skip := NewLabel();
  2102. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2103. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2104. BreqL (skip, op, nil);
  2105. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2106. SetLabel (skip);
  2107. ELSIF SemanticChecker.IsPointerType (type) THEN
  2108. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2109. CallThis(position,"GarbageCollector","Mark", 1);
  2110. ELSIF type.IsRecordType() THEN
  2111. Emit (Push(position,register));
  2112. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2113. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2114. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2115. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2116. ELSIF IsStaticArray(type) THEN
  2117. size := StaticArrayNumElements(type);
  2118. base := StaticArrayBaseType(type);
  2119. IF base.IsRecordType() THEN
  2120. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2121. Emit (Push(position, register));
  2122. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2123. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2124. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2125. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2126. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2127. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2128. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2129. Emit (Push(position, register));
  2130. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2131. ELSE
  2132. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2133. Emit (Push(position, register));
  2134. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2135. ASSERT(base.IsPointer());
  2136. END;
  2137. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2138. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2139. CallThis(position,"GarbageCollector","Mark", 1);
  2140. ELSE HALT(100); (* missing ? *)
  2141. END;
  2142. END CallTraceMethod;
  2143. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2144. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2145. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2146. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2147. BEGIN
  2148. previousSection := section;
  2149. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2150. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2151. GetRecordTypeName (recordType,name);
  2152. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2153. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2154. IF dump # NIL THEN dump := section.comments END;
  2155. variable := recordType.recordScope.firstVariable;
  2156. WHILE variable # NIL DO
  2157. IF variable.NeedsTrace() THEN
  2158. register := NewRegisterOperand (addressType);
  2159. Emit (Mov(position,register,parameter1));
  2160. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2161. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2162. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2163. END;
  2164. CallTraceMethod(register, variable.type);
  2165. ReleaseIntermediateOperand(register);
  2166. END;
  2167. variable := variable.nextVariable;
  2168. END;
  2169. recordBase := recordType.GetBaseRecord();
  2170. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2171. recordBase := recordBase.GetBaseRecord();
  2172. END;
  2173. IF recordBase # NIL THEN
  2174. GetRecordTypeName (recordBase,name);
  2175. IF HasExplicitTraceMethod (recordBase) THEN
  2176. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2177. ELSE
  2178. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2179. END;
  2180. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2181. Emit(Br(position,op));
  2182. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2183. Emit(Exit(position,0,0));
  2184. ELSE
  2185. IF recordType.isObject THEN
  2186. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2187. ELSE
  2188. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2189. END;
  2190. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2191. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2192. Emit(Br(position,op));
  2193. END;
  2194. IF ~recordType.isObject THEN
  2195. GetRecordTypeName (recordType,name);
  2196. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2197. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2198. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2199. NEW(registerUsageCount);
  2200. usedRegisters := NIL;
  2201. IF dump # NIL THEN dump := section.comments END;
  2202. register := NewRegisterOperand (addressType);
  2203. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2204. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2205. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2206. END;
  2207. Emit (Push(position,register));
  2208. GetRecordTypeName (recordType,name);
  2209. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2210. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2211. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2212. ReleaseIntermediateOperand(register);
  2213. Emit(Exit(position,0,0));
  2214. GetRecordTypeName (recordType,name);
  2215. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2216. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2217. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2218. NEW(registerUsageCount);
  2219. usedRegisters := NIL;
  2220. register := NewRegisterOperand (addressType);
  2221. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2222. Emit(Mov(position, register, parameter1));
  2223. label := NewLabel();
  2224. SetLabel(label);
  2225. Emit(Push(position, register));
  2226. GetRecordTypeName (recordType,name);
  2227. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2228. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2229. Emit(Call(position, op, 0));
  2230. Emit(Pop(position, register));
  2231. Emit(Add(position, register, register, ofs));
  2232. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2233. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2234. Emit(Exit(position,0,0));
  2235. END;
  2236. INC(statCoopTraceMethod, section.pc);
  2237. ReturnToContext(context);
  2238. IF dump # NIL THEN dump := section.comments END;
  2239. END CreateTraceMethod;
  2240. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2241. VAR name: Basic.SegmentedName;
  2242. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2243. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2244. context: Context;
  2245. BEGIN
  2246. IF recordType.isObject THEN RETURN END;
  2247. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2248. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2249. GetRecordTypeName (recordType,name);
  2250. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2251. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2252. IF dump # NIL THEN dump := section.comments END;
  2253. variable := recordType.recordScope.firstVariable;
  2254. WHILE variable # NIL DO
  2255. IF variable.NeedsTrace() THEN
  2256. dst := NewRegisterOperand (addressType);
  2257. Emit (Mov(position, dst, parameter1));
  2258. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2259. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2260. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2261. END;
  2262. CallResetProcedure(dst, nil, variable.type);
  2263. ReleaseIntermediateOperand(dst);
  2264. END;
  2265. variable := variable.nextVariable;
  2266. END;
  2267. recordBase := recordType.GetBaseRecord();
  2268. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2269. GetRecordTypeName (recordBase,name);
  2270. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2271. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2272. Emit(Br(position,op));
  2273. ELSE
  2274. Emit(Exit(position,0,0));
  2275. END;
  2276. GetRecordTypeName (recordType,name);
  2277. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2278. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2279. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2280. NEW(registerUsageCount);
  2281. usedRegisters := NIL;
  2282. dst := NewRegisterOperand (addressType);
  2283. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2284. Emit(Mov(position, dst, parameter1));
  2285. label := NewLabel();
  2286. SetLabel(label);
  2287. Emit(Push(position, dst));
  2288. GetRecordTypeName (recordType,name);
  2289. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2290. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2291. Emit(Call(position, op, 0));
  2292. Emit(Pop(position, dst));
  2293. Emit(Add(position, dst, dst, ofs));
  2294. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2295. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2296. Emit(Exit(position,0,0));
  2297. INC(statCoopResetProcedure, section.pc);
  2298. ReturnToContext(context);
  2299. IF dump # NIL THEN dump := section.comments END;
  2300. END CreateResetProcedure;
  2301. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2302. VAR name: Basic.SegmentedName; context: Context;
  2303. BEGIN
  2304. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2305. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2306. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2307. IF dump # NIL THEN dump := section.comments END;
  2308. Emit(Push(position,fp));
  2309. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2310. ResetVariables(scope);
  2311. Emit(Pop(position,fp));
  2312. Emit(Exit(position,0,0));
  2313. ReturnToContext(context);
  2314. IF dump # NIL THEN dump := section.comments END;
  2315. END CreateResetMethod;
  2316. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2317. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2318. BEGIN
  2319. IF SemanticChecker.IsPointerType (type) THEN
  2320. Emit (Push(position, dest));
  2321. CallThis(position,"GarbageCollector","Reset", 1);
  2322. ELSIF type.IsRecordType() THEN
  2323. Emit (Push(position, dest));
  2324. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2325. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2326. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2327. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2328. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2329. size := GetArrayLength(type, tag);
  2330. base := ArrayBaseType(type);
  2331. IF base.IsRecordType() THEN
  2332. Emit (Push(position, size));
  2333. Emit (Push(position, dest));
  2334. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2335. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2336. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2337. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2338. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2339. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2340. Emit (Push(position, size));
  2341. Emit (Push(position, dest));
  2342. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2343. ELSE
  2344. Emit (Push(position, size));
  2345. Emit (Push(position, dest));
  2346. CallThis(position,"GarbageCollector","ResetArray", 2);
  2347. ASSERT(ArrayBaseType(type).IsPointer());
  2348. END;
  2349. ReleaseIntermediateOperand(size);
  2350. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2351. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2352. Emit (Push(position, dest));
  2353. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2354. ELSE HALT(100); (* missing ? *)
  2355. END;
  2356. END CallResetProcedure;
  2357. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2358. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2359. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2360. VAR operand: Operand;
  2361. BEGIN
  2362. Symbol (symbol, operand);
  2363. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2364. ReleaseOperand(operand);
  2365. END Reset;
  2366. BEGIN
  2367. previousScope := currentScope;
  2368. currentScope := scope;
  2369. pc := section.pc;
  2370. variable := scope.firstVariable;
  2371. WHILE variable # NIL DO
  2372. IF variable.NeedsTrace() THEN
  2373. Reset (variable);
  2374. END;
  2375. variable := variable.nextVariable;
  2376. END;
  2377. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2378. WHILE parameter # NIL DO
  2379. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2380. Reset (parameter);
  2381. END;
  2382. parameter := parameter.nextParameter;
  2383. END;
  2384. INC(statCoopResetVariables, section.pc - pc);
  2385. currentScope := previousScope;
  2386. END ResetVariables;
  2387. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2388. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2389. VAR op: IntermediateCode.Operand; context: Context;
  2390. BEGIN
  2391. previousSection := section;
  2392. GetCodeSectionNameForSymbol(procedure, name);
  2393. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2394. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2395. IF dump # NIL THEN dump := section.comments END;
  2396. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2397. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2398. Emit(Data(position,op));
  2399. Emit(Data(position,nil));
  2400. IF HasPointers (procedure.procedureScope) THEN
  2401. GetCodeSectionNameForSymbol(procedure, name);
  2402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2403. ELSE
  2404. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2405. END;
  2406. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2407. Emit(Data(position,op));
  2408. ReturnToContext(context);
  2409. IF dump # NIL THEN dump := section.comments END;
  2410. END CreateProcedureDescriptor;
  2411. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2412. VAR import: SyntaxTree.Import;
  2413. s: Basic.MessageString;
  2414. selfName: SyntaxTree.IdentifierString;
  2415. BEGIN
  2416. moduleScope.ownerModule.GetName(selfName);
  2417. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2418. module := moduleScope.ownerModule
  2419. ELSE
  2420. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2421. IF import = NIL THEN
  2422. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2423. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2424. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2425. s := "Module ";
  2426. Strings.Append(s,moduleName);
  2427. Strings.Append(s," cannot be imported.");
  2428. IF force THEN
  2429. Error(position,s);
  2430. ELSIF canBeLoaded THEN
  2431. Strings.Append(s, "=> no dynamic linking.");
  2432. Warning(position, s);
  2433. canBeLoaded := FALSE;
  2434. END;
  2435. RETURN FALSE
  2436. ELSE
  2437. SELF.module.imports.AddName(moduleName)
  2438. END;
  2439. ELSIF import.module = NIL THEN (* already tried *)
  2440. RETURN FALSE
  2441. END;
  2442. module := import.module;
  2443. END;
  2444. RETURN TRUE
  2445. END AddImport;
  2446. (* needed for old binary object file format*)
  2447. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2448. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2449. BEGIN
  2450. IF AddImport(moduleName,module,TRUE) THEN
  2451. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2452. IF procedure = NIL THEN
  2453. s := "Instruction not supported on target, emulation procedure ";
  2454. Strings.Append(s,moduleName);
  2455. Strings.Append(s,".");
  2456. Strings.Append(s,procedureName);
  2457. Strings.Append(s," not present");
  2458. Error(position,s);
  2459. ELSE
  2460. StaticCallOperand(r,procedure);
  2461. ReleaseOperand(r);
  2462. fp := GetFingerprint(procedure);
  2463. END;
  2464. END;
  2465. END EnsureSymbol;
  2466. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2467. VAR exit: Label; trueL,falseL: Label;
  2468. BEGIN
  2469. trueL := NewLabel();
  2470. falseL := NewLabel();
  2471. exit := NewLabel();
  2472. Condition(x,trueL,falseL);
  2473. InitOperand(result,ModeValue);
  2474. SetLabel(trueL);
  2475. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2476. Emit(Mov(position,result.op,true));
  2477. BrL(exit);
  2478. SetLabel(falseL);
  2479. Emit(MovReplace(position,result.op,false));
  2480. SetLabel(exit);
  2481. END ConditionToValue;
  2482. PROCEDURE ValueToCondition(VAR op: Operand);
  2483. BEGIN
  2484. LoadValue(op,system.booleanType);
  2485. BrneL(trueLabel,op.op, false);
  2486. ReleaseOperand(op);
  2487. BrL(falseLabel);
  2488. END ValueToCondition;
  2489. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2490. VAR size: LONGINT;
  2491. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2492. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2493. BEGIN
  2494. ASSERT(type.form = SyntaxTree.Open);
  2495. baseType := type.arrayBase.resolved;
  2496. IF IsOpenArray(baseType) THEN
  2497. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2498. ELSE
  2499. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2500. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2501. END;
  2502. len := tag;
  2503. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2504. IntermediateCode.MakeMemory(len,addressType);
  2505. UseIntermediateOperand(len);
  2506. Reuse2(res,sizeOperand,len);
  2507. Emit(Mul(position,res,sizeOperand,len));
  2508. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2509. RETURN res
  2510. END GetArraySize;
  2511. BEGIN
  2512. type := type.resolved;
  2513. IF IsOpenArray(type) THEN
  2514. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2515. RETURN tag
  2516. ELSE
  2517. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2518. END;
  2519. ELSE
  2520. size := ToMemoryUnits(system,system.SizeOf(type));
  2521. RETURN IntermediateCode.Immediate(addressType,size)
  2522. END;
  2523. END GetDynamicSize;
  2524. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2525. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2526. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2527. BEGIN
  2528. ASSERT(type.form = SyntaxTree.Open);
  2529. baseType := type.arrayBase.resolved;
  2530. IF IsOpenArray(baseType) THEN
  2531. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2532. ELSE
  2533. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2534. END;
  2535. len := tag;
  2536. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2537. IntermediateCode.MakeMemory(len,addressType);
  2538. UseIntermediateOperand(len);
  2539. Reuse2(res,sizeOperand,len);
  2540. Emit(Mul(position,res,sizeOperand,len));
  2541. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2542. RETURN res
  2543. END GetLength;
  2544. BEGIN
  2545. type := type.resolved;
  2546. IF IsOpenArray(type) THEN
  2547. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2548. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2549. ELSE
  2550. RETURN IntermediateCode.Immediate(addressType,1)
  2551. END;
  2552. END GetArrayLength;
  2553. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2554. VAR result: IntermediateCode.Operand;
  2555. BEGIN
  2556. IF backend.cooperative THEN
  2557. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2558. ELSE
  2559. MakeMemory(result, tag, addressType, 0);
  2560. END;
  2561. RETURN result
  2562. END GetSizeFromTag;
  2563. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2564. VAR parameter: SyntaxTree.Parameter;
  2565. BEGIN
  2566. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2567. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2568. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2569. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2570. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2571. END;
  2572. RETURN GetDynamicSize(e.type, tag);
  2573. END GetArrayOfBytesSize;
  2574. (*
  2575. to find imported symbol. not needed ?
  2576. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2577. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2578. BEGIN
  2579. IF AddImport(moduleName,importedModule,FALSE) THEN
  2580. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2581. RETURN symbol
  2582. ELSE
  2583. RETURN NIL
  2584. END
  2585. END SymbolByName;
  2586. *)
  2587. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2588. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2589. BEGIN
  2590. IF AddImport(moduleName,runtimeModule,force) THEN
  2591. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2592. IF procedure = NIL THEN
  2593. s := "Procedure ";
  2594. Strings.Append(s,moduleName);
  2595. Strings.Append(s,".");
  2596. Strings.Append(s,procedureName);
  2597. Strings.Append(s," not present");
  2598. Error(position,s);
  2599. RETURN FALSE
  2600. ELSE
  2601. RETURN TRUE
  2602. END;
  2603. ELSE RETURN FALSE
  2604. END;
  2605. END GetRuntimeProcedure;
  2606. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2607. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2608. s: Basic.MessageString;
  2609. BEGIN
  2610. Basic.InitSegmentedName(name);
  2611. name[0] := Basic.MakeString(moduleName);
  2612. name[1] := Basic.MakeString(typeName);
  2613. name[2] := -1;
  2614. IF AddImport(moduleName,importedModule, FALSE) THEN
  2615. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2616. IF symbol = NIL THEN
  2617. s := "type ";
  2618. Strings.Append(s,moduleName);
  2619. Strings.Append(s,".");
  2620. Strings.Append(s,typeName);
  2621. Strings.Append(s," not present");
  2622. Error(position,s);
  2623. END;
  2624. ELSE symbol := NIL;
  2625. END;
  2626. IF importedModule = moduleScope.ownerModule THEN
  2627. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2628. ELSE
  2629. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2630. END;
  2631. RETURN symbol
  2632. END GetTypeDescriptor;
  2633. (* 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. *)
  2634. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2635. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2636. pooledName: Basic.SegmentedName; size: LONGINT;
  2637. BEGIN
  2638. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2639. StaticCallOperand(result,procedure);
  2640. IF numberParameters < 0 THEN
  2641. size := ProcedureParametersSize(system,procedure);
  2642. ELSE
  2643. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2644. IF size # ProcedureParametersSize(system,procedure) THEN
  2645. Error(position,"runtime call parameter count mismatch");
  2646. END;
  2647. END;
  2648. Emit(Call(position, result.op, size));
  2649. ReleaseOperand(result);
  2650. ELSE (* only static linking possible *)
  2651. Basic.InitSegmentedName(pooledName);
  2652. pooledName[0] := Basic.MakeString(moduleName);
  2653. pooledName[1] := Basic.MakeString(procedureName);
  2654. pooledName[2] := -1;
  2655. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2656. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2657. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2658. END;
  2659. END CallThis;
  2660. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2661. VAR
  2662. left,right: Operand;
  2663. leftSize, rightSize: IntermediateCode.Operand;
  2664. saved: RegisterEntry;
  2665. reg: IntermediateCode.Operand;
  2666. procedureName: SyntaxTree.IdentifierString;
  2667. BEGIN
  2668. procedureName := "CompareString";
  2669. SaveRegisters();ReleaseUsedRegisters(saved);
  2670. Designate(leftExpression,left);
  2671. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2672. Emit(Push(position,leftSize));
  2673. ReleaseIntermediateOperand(leftSize);
  2674. Emit(Push(position,left.op));
  2675. ReleaseOperand(left);
  2676. Designate(rightExpression,right);
  2677. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2678. Emit(Push(position,rightSize));
  2679. ReleaseIntermediateOperand(rightSize);
  2680. Emit(Push(position,right.op));
  2681. ReleaseOperand(right);
  2682. IF backend.cooperative THEN
  2683. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2684. ELSE
  2685. CallThis(position,runtimeModuleName,procedureName, 4);
  2686. END;
  2687. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2688. Emit(Result(position,reg));
  2689. (*
  2690. AcquireThisRegister(int8,IntermediateCode.Result);
  2691. *)
  2692. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2693. (*
  2694. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2695. *)
  2696. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2697. ReleaseIntermediateOperand(reg);
  2698. BrL(falseLabel);
  2699. END CompareString;
  2700. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2701. VAR
  2702. left,right: Operand;
  2703. leftSize, rightSize: IntermediateCode.Operand;
  2704. saved: RegisterEntry;
  2705. procedureName: SyntaxTree.IdentifierString;
  2706. BEGIN
  2707. procedureName := "CopyString";
  2708. SaveRegisters();ReleaseUsedRegisters(saved);
  2709. Designate(leftExpression,left);
  2710. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2711. Emit(Push(position,leftSize));
  2712. ReleaseIntermediateOperand(leftSize);
  2713. Emit(Push(position,left.op));
  2714. ReleaseOperand(left);
  2715. Designate(rightExpression,right);
  2716. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2717. Emit(Push(position,rightSize));
  2718. ReleaseIntermediateOperand(rightSize);
  2719. Emit(Push(position,right.op));
  2720. ReleaseOperand(right);
  2721. IF backend.cooperative THEN
  2722. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2723. ELSE
  2724. CallThis(position,runtimeModuleName,procedureName,4);
  2725. END;
  2726. RestoreRegisters(saved);
  2727. END CopyString;
  2728. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2729. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2730. leftType,rightType: SyntaxTree.Type;
  2731. leftExpression,rightExpression : SyntaxTree.Expression;
  2732. componentType: IntermediateCode.Type;
  2733. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2734. size: LONGINT;
  2735. BEGIN
  2736. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2737. dest := destination; destination := emptyOperand;
  2738. leftType := x.left.type.resolved;
  2739. rightType := x.right.type.resolved;
  2740. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2741. CASE x.operator OF
  2742. Scanner.Or:
  2743. (* shortcut evaluation of left OR right *)
  2744. IF ~conditional THEN ConditionToValue(x);
  2745. ELSE
  2746. next := NewLabel();
  2747. Condition(x.left,trueLabel,next);
  2748. SetLabel(next);
  2749. Condition(x.right,trueLabel,falseLabel);
  2750. END;
  2751. |Scanner.And:
  2752. (* shortcut evaluation of left & right *)
  2753. IF ~conditional THEN ConditionToValue(x);
  2754. ELSE
  2755. next := NewLabel();
  2756. Condition(x.left,next,falseLabel);
  2757. SetLabel(next);
  2758. Condition(x.right,trueLabel,falseLabel);
  2759. END;
  2760. |Scanner.Is:
  2761. IF ~conditional THEN ConditionToValue(x);
  2762. ELSE
  2763. (* get type desc tag *)
  2764. IF IsPointerToRecord(leftType,recordType) THEN
  2765. Evaluate(x.left,left);
  2766. Dereference(left,recordType,IsUnsafePointer(leftType))
  2767. ELSE
  2768. Designate(x.left,left);
  2769. END;
  2770. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2771. ReleaseOperand(left);
  2772. END;
  2773. |Scanner.Plus:
  2774. Evaluate(x.left,left);
  2775. Evaluate(x.right,right);
  2776. IF leftType IS SyntaxTree.SetType THEN
  2777. InitOperand(result,ModeValue);
  2778. Reuse2a(result.op,left.op,right.op,dest);
  2779. Emit(Or(position,result.op,left.op,right.op));
  2780. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2781. InitOperand(result,ModeValue);
  2782. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2783. Reuse2(result.tag,left.tag,right.tag);
  2784. Emit(Add(position,result.op,left.op,right.op));
  2785. Emit(Add(position,result.tag,left.tag,right.tag))
  2786. ELSE
  2787. InitOperand(result,ModeValue);
  2788. Reuse2a(result.op,left.op,right.op,dest);
  2789. Emit(Add(position,result.op,left.op,right.op));
  2790. END;
  2791. ReleaseOperand(left); ReleaseOperand(right);
  2792. |Scanner.Minus:
  2793. Evaluate(x.left,left);
  2794. Evaluate(x.right,right);
  2795. IF leftType IS SyntaxTree.SetType THEN
  2796. InitOperand(result,ModeValue);
  2797. Reuse1(result.op,right.op);
  2798. Emit(Not(position,result.op,right.op));
  2799. ReleaseOperand(right);
  2800. Emit(And(position,result.op,result.op,left.op));
  2801. ReleaseOperand(left);
  2802. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2803. InitOperand(result,ModeValue);
  2804. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2805. Reuse2(result.tag,left.tag,right.tag);
  2806. Emit(Sub(position,result.op,left.op,right.op));
  2807. Emit(Sub(position,result.tag,left.tag,right.tag));
  2808. ReleaseOperand(left); ReleaseOperand(right)
  2809. ELSE
  2810. InitOperand(result,ModeValue);
  2811. Reuse2a(result.op,left.op,right.op,dest);
  2812. Emit(Sub(position,result.op,left.op,right.op));
  2813. ReleaseOperand(left); ReleaseOperand(right);
  2814. END;
  2815. |Scanner.Times:
  2816. Evaluate(x.left,left);
  2817. Evaluate(x.right,right);
  2818. IF leftType IS SyntaxTree.SetType THEN
  2819. InitOperand(result,ModeValue);
  2820. Reuse2a(result.op,left.op,right.op,dest);
  2821. Emit(And(position,result.op,left.op,right.op));
  2822. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2823. InitOperand(result,ModeValue);
  2824. Reuse1a(result.op,left.op,dest);
  2825. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2826. Emit(Shl(position,result.op,left.op,right.op));
  2827. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2828. InitOperand(result, ModeValue);
  2829. componentType := left.op.type;
  2830. (* TODO: review this *)
  2831. (*
  2832. result.op = left.op * right.op - left.tag * right.tag
  2833. result.tag = left.tag * right.op + left.op * right.tag
  2834. *)
  2835. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2836. Emit(Mul(position,result.op, left.op, right.op));
  2837. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2838. Emit(Mul(position,tempReg, left.tag, right.tag));
  2839. Emit(Sub(position,result.op, result.op, tempReg));
  2840. Reuse2(result.tag, left.tag, right.op);
  2841. Emit(Mul(position,result.tag, left.tag, right.op));
  2842. Emit(Mul(position,tempReg, left.op, right.tag));
  2843. Emit(Add(position,result.tag, result.tag, tempReg));
  2844. ReleaseIntermediateOperand(tempReg)
  2845. ELSE
  2846. InitOperand(result,ModeValue);
  2847. Reuse2a(result.op,left.op,right.op,dest);
  2848. Emit(Mul(position,result.op,left.op,right.op));
  2849. END;
  2850. ReleaseOperand(left); ReleaseOperand(right);
  2851. |Scanner.Div:
  2852. Evaluate(x.left,left);
  2853. Evaluate(x.right,right);
  2854. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2855. InitOperand(result,ModeValue);
  2856. Reuse1a(result.op,left.op,dest);
  2857. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2858. Emit(Shr(position,result.op,left.op,right.op));
  2859. ELSE
  2860. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2861. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2862. IF ~isUnchecked THEN
  2863. exit := NewLabel();
  2864. BrltL(exit,zero,right.op);
  2865. EmitTrap(position,NegativeDivisorTrap);
  2866. SetLabel(exit);
  2867. END;
  2868. END;
  2869. InitOperand(result,ModeValue);
  2870. Reuse2a(result.op,left.op,right.op,dest);
  2871. Emit(Div(position,result.op,left.op,right.op));
  2872. END;
  2873. ReleaseOperand(left); ReleaseOperand(right);
  2874. |Scanner.Mod:
  2875. Evaluate(x.left,left);
  2876. Evaluate(x.right,right);
  2877. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2878. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2879. InitOperand(result,ModeValue);
  2880. Reuse1a(result.op,left.op,dest);
  2881. Emit(And(position,result.op,left.op,right.op));
  2882. ELSE
  2883. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2884. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2885. IF ~isUnchecked THEN
  2886. exit := NewLabel();
  2887. BrltL(exit,zero,right.op);
  2888. EmitTrap(position,NegativeDivisorTrap);
  2889. SetLabel(exit);
  2890. END;
  2891. END;
  2892. InitOperand(result,ModeValue);
  2893. Reuse2a(result.op,left.op,right.op,dest);
  2894. Emit(Mod(position,result.op,left.op,right.op));
  2895. END;
  2896. ReleaseOperand(left); ReleaseOperand(right);
  2897. |Scanner.Slash:
  2898. Evaluate(x.left,left);
  2899. Evaluate(x.right,right);
  2900. IF leftType IS SyntaxTree.SetType THEN
  2901. InitOperand(result,ModeValue);
  2902. Reuse2a(result.op,left.op,right.op,dest);
  2903. Emit(Xor(position,result.op,left.op,right.op));
  2904. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2905. InitOperand(result,ModeValue);
  2906. componentType := left.op.type;
  2907. (* review this *)
  2908. (*
  2909. divisor = right.op * right.op + right.tag * right.tag
  2910. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2911. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2912. *)
  2913. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2914. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2915. Emit(Mul(position,tempReg, right.op, right.op));
  2916. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2917. Emit(Add(position,tempReg, tempReg, tempReg2));
  2918. result.op := tempReg2;
  2919. Emit(Mul(position,result.op, left.op, right.op));
  2920. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2921. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2922. Emit(Add(position,result.op, result.op, tempReg2));
  2923. Emit(Div(position,result.op, result.op, tempReg));
  2924. Reuse2(result.tag, left.tag, right.op);
  2925. Emit(Mul(position,result.tag, left.tag, right.op));
  2926. Emit(Mul(position,tempReg2, left.op, right.tag));
  2927. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2928. Emit(Div(position,result.tag, result.tag, tempReg));
  2929. ReleaseIntermediateOperand(tempReg);
  2930. ReleaseIntermediateOperand(tempReg2)
  2931. ELSE
  2932. InitOperand(result,ModeValue);
  2933. Reuse2a(result.op,left.op,right.op,dest);
  2934. Emit(Div(position,result.op,left.op,right.op));
  2935. END;
  2936. ReleaseOperand(left); ReleaseOperand(right);
  2937. |Scanner.Equal:
  2938. IF ~conditional THEN ConditionToValue(x);
  2939. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2940. CompareString(BreqL,x.left,x.right);
  2941. ELSE
  2942. Evaluate(x.left,left);
  2943. Evaluate(x.right,right);
  2944. IF leftType IS SyntaxTree.RangeType THEN
  2945. ASSERT(rightType IS SyntaxTree.RangeType);
  2946. BrneL(falseLabel, left.op, right.op); (* first *)
  2947. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2948. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2949. ReleaseOperand(left); ReleaseOperand(right);
  2950. BrL(trueLabel)
  2951. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2952. BrneL(falseLabel, left.op, right.op); (* first *)
  2953. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2954. ReleaseOperand(left); ReleaseOperand(right);
  2955. BrL(trueLabel)
  2956. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2957. (* TODO: review this *)
  2958. BrneL(falseLabel, left.op, right.op); (* real part *)
  2959. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2960. ReleaseOperand(left); ReleaseOperand(right);
  2961. BrL(trueLabel)
  2962. ELSE
  2963. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2964. ReleaseOperand(left); ReleaseOperand(right);
  2965. BrL(trueLabel);
  2966. END;
  2967. END;
  2968. |Scanner.LessEqual:
  2969. IF ~conditional THEN ConditionToValue(x);
  2970. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2971. CompareString(BrgeL,x.right,x.left);
  2972. ELSE
  2973. Evaluate(x.left,left);
  2974. Evaluate(x.right,right);
  2975. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2976. Reuse1(temp.op,right.op);
  2977. Emit(And(position,temp.op,left.op,right.op));
  2978. ReleaseOperand(right);
  2979. BreqL(trueLabel,temp.op,left.op);
  2980. BrL(falseLabel);
  2981. ReleaseOperand(temp);ReleaseOperand(left);
  2982. ELSE
  2983. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  2984. ReleaseOperand(left); ReleaseOperand(right);
  2985. BrL(trueLabel);
  2986. END;
  2987. END;
  2988. |Scanner.Less:
  2989. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  2990. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  2991. leftExpression.SetType(system.booleanType);
  2992. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  2993. rightExpression.SetType(system.booleanType);
  2994. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  2995. leftExpression.SetType(system.booleanType);
  2996. Expression(leftExpression);
  2997. ELSIF ~conditional THEN ConditionToValue(x);
  2998. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2999. CompareString(BrltL,x.left,x.right);
  3000. ELSE
  3001. Evaluate(x.left,left);
  3002. Evaluate(x.right,right);
  3003. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3004. ReleaseOperand(left); ReleaseOperand(right);
  3005. BrL(trueLabel);
  3006. END;
  3007. |Scanner.Greater:
  3008. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3009. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  3010. leftExpression.SetType(system.booleanType);
  3011. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3012. rightExpression.SetType(system.booleanType);
  3013. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3014. leftExpression.SetType(system.booleanType);
  3015. Expression(leftExpression);
  3016. ELSIF ~conditional THEN ConditionToValue(x);
  3017. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3018. CompareString(BrltL,x.right,x.left);
  3019. ELSE
  3020. Evaluate(x.left,left);
  3021. Evaluate(x.right,right);
  3022. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3023. ReleaseOperand(left); ReleaseOperand(right);
  3024. BrL(trueLabel);
  3025. END;
  3026. |Scanner.GreaterEqual:
  3027. IF ~conditional THEN ConditionToValue(x);
  3028. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3029. CompareString(BrgeL,x.left,x.right);
  3030. ELSE
  3031. Evaluate(x.left,left);
  3032. Evaluate(x.right,right);
  3033. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3034. Reuse1(temp.op,left.op);
  3035. Emit(And(position,temp.op,left.op,right.op));
  3036. ReleaseOperand(left);
  3037. BreqL(trueLabel, temp.op,right.op);
  3038. ReleaseOperand(temp); ReleaseOperand(right);
  3039. BrL(falseLabel);
  3040. ELSE
  3041. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3042. ReleaseOperand(left); ReleaseOperand(right);
  3043. BrL(trueLabel);
  3044. END;
  3045. END;
  3046. |Scanner.Unequal:
  3047. IF ~conditional THEN ConditionToValue(x);
  3048. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3049. CompareString(BrneL,x.left,x.right);
  3050. ELSE
  3051. Evaluate(x.left,left);
  3052. Evaluate(x.right,right);
  3053. IF leftType IS SyntaxTree.RangeType THEN
  3054. ASSERT(rightType IS SyntaxTree.RangeType);
  3055. BrneL(trueLabel, left.op, right.op); (* first *)
  3056. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3057. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3058. ReleaseOperand(left); ReleaseOperand(right);
  3059. BrL(falseLabel)
  3060. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3061. BrneL(trueLabel, left.op, right.op); (* first *)
  3062. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3063. ReleaseOperand(left); ReleaseOperand(right);
  3064. BrL(falseLabel)
  3065. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3066. (* TODO: review this *)
  3067. BrneL(trueLabel, left.op, right.op); (* real part *)
  3068. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3069. ReleaseOperand(left); ReleaseOperand(right);
  3070. BrL(falseLabel)
  3071. ELSE
  3072. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3073. ReleaseOperand(left); ReleaseOperand(right);
  3074. BrL(trueLabel);
  3075. END;
  3076. END;
  3077. |Scanner.In:
  3078. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3079. Evaluate(x.left,left);
  3080. Evaluate(x.right,right);
  3081. Convert(left.op,setType);
  3082. ReuseCopy(temp.op,right.op);
  3083. Emit(Shr(position,temp.op,temp.op,left.op));
  3084. ReleaseOperand(right); ReleaseOperand(left);
  3085. IntermediateCode.InitImmediate(one,setType,1);
  3086. Emit(And(position,temp.op,temp.op,one));
  3087. IF conditional THEN
  3088. IntermediateCode.InitImmediate(zero,setType,0);
  3089. BrneL(trueLabel,temp.op,zero);
  3090. ReleaseOperand(temp);
  3091. BrL(falseLabel);
  3092. ELSE
  3093. Convert(temp.op,bool);
  3094. result.mode := ModeValue;
  3095. result.op := temp.op;
  3096. result.tag := nil; (* may be left over from calls to evaluate *)
  3097. END;
  3098. ELSE
  3099. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3100. IF x.operator = Scanner.Questionmarks THEN
  3101. leftExpression := x.left;
  3102. rightExpression := x.right;
  3103. ELSE
  3104. leftExpression := x.right;
  3105. rightExpression := x.left;
  3106. END;
  3107. Evaluate(leftExpression, left);
  3108. Emit(Push(position,left.op));
  3109. ReleaseOperand(left);
  3110. Designate(rightExpression, right);
  3111. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3112. IF ~backend.cellsAreObjects THEN
  3113. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3114. END;
  3115. Emit(Push(position,right.op));
  3116. ReleaseOperand(right);
  3117. IF backend.cellsAreObjects THEN
  3118. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3119. ELSE
  3120. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3121. END;
  3122. InitOperand(result, ModeValue);
  3123. result.op := NewRegisterOperand(bool);
  3124. Emit(Result(position,result.op));
  3125. IF conditional THEN
  3126. IntermediateCode.InitImmediate(zero,setType,0);
  3127. BrneL(trueLabel,result.op,zero);
  3128. ReleaseOperand(result);
  3129. BrL(falseLabel);
  3130. END;
  3131. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3132. leftExpression := x.left;
  3133. rightExpression := x.right;
  3134. Evaluate(leftExpression, left);
  3135. Emit(Push(position,left.op));
  3136. ReleaseOperand(left);
  3137. Evaluate(rightExpression, right);
  3138. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3139. IF ~backend.cellsAreObjects THEN
  3140. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3141. END;
  3142. Emit(Push(position,right.op));
  3143. ReleaseOperand(right);
  3144. IF backend.cellsAreObjects THEN
  3145. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3146. ELSE
  3147. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3148. END;
  3149. InitOperand(result, ModeValue);
  3150. result.op := NewRegisterOperand(bool);
  3151. Emit(Result(position,result.op));
  3152. IF conditional THEN
  3153. IntermediateCode.InitImmediate(zero,setType,0);
  3154. BrneL(trueLabel,result.op,zero);
  3155. ReleaseOperand(result);
  3156. BrL(falseLabel);
  3157. END;
  3158. ELSE
  3159. HALT(100);
  3160. END;
  3161. END;
  3162. destination := dest;
  3163. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3164. END VisitBinaryExpression;
  3165. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3166. VAR localResult, operand: Operand;
  3167. BEGIN
  3168. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3169. InitOperand(localResult, ModeValue);
  3170. ASSERT(x.first # NIL);
  3171. Evaluate(x.first, operand);
  3172. localResult.op := operand.op;
  3173. ReleaseOperand(operand);
  3174. UseIntermediateOperand(localResult.op);
  3175. ASSERT(x.last # NIL);
  3176. Evaluate(x.last, operand);
  3177. localResult.tag := operand.op;
  3178. ReleaseOperand(operand);
  3179. UseIntermediateOperand(localResult.tag);
  3180. IF x.step # NIL THEN
  3181. Evaluate(x.step, operand);
  3182. localResult.extra := operand.op;
  3183. ReleaseOperand(operand);
  3184. UseIntermediateOperand(localResult.extra);
  3185. END;
  3186. result := localResult;
  3187. IF Trace THEN TraceExit("VisitRangeExpression") END
  3188. END VisitRangeExpression;
  3189. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3190. BEGIN
  3191. HALT(100); (* should never be evaluated *)
  3192. END VisitTensorRangeExpression;
  3193. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3194. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3195. BEGIN
  3196. IF Trace THEN TraceEnter("VisitConversion") END;
  3197. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3198. dest := destination; destination := emptyOperand;
  3199. Evaluate(x.expression,old);
  3200. InitOperand(result,ModeValue);
  3201. result.op := old.op;
  3202. ASSERT(result.op.mode # 0);
  3203. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3204. (* convert TO a complex number *)
  3205. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3206. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3207. ASSERT(result.op.mode # 0);
  3208. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3209. (* convert FROM a complex number TO a complex number*)
  3210. result.tag := old.tag;
  3211. ASSERT(result.tag.mode # 0);
  3212. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3213. ASSERT(result.tag.mode # 0)
  3214. ELSE
  3215. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3216. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3217. END
  3218. ELSE
  3219. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3220. ASSERT(result.op.mode # 0);
  3221. result.tag := old.tag; (*! probably never used *)
  3222. END;
  3223. destination := dest;
  3224. IF Trace THEN TraceExit("VisitConversion") END;
  3225. END VisitConversion;
  3226. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3227. BEGIN
  3228. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3229. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3230. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3231. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3232. END VisitTypeDeclaration;
  3233. (** designators (expressions) *)
  3234. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3235. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3236. BEGIN
  3237. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3238. IF x.left # NIL THEN Expression(x.left) END;
  3239. Symbol(x.symbol,result);
  3240. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3241. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3242. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3243. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3244. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3245. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3246. END;
  3247. END;
  3248. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3249. END VisitSymbolDesignator;
  3250. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3251. BEGIN
  3252. IF isUnchecked THEN RETURN END;
  3253. IF tagsAvailable THEN
  3254. TrapC(BrltL,index,length,IndexCheckTrap);
  3255. END;
  3256. END BoundCheck;
  3257. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3258. VAR d: IntermediateCode.Operand;
  3259. BEGIN
  3260. IF isUnchecked THEN RETURN END;
  3261. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3262. TrapC(op,dim,d,ArraySizeTrap);
  3263. ReleaseIntermediateOperand(d);
  3264. END DimensionCheck;
  3265. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3266. VAR
  3267. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3268. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3269. expression: SyntaxTree.Expression;
  3270. resultingType, leftType, baseType: SyntaxTree.Type;
  3271. skipLabel1: Label;
  3272. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3273. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3274. variableOp: Operand;
  3275. variable: SyntaxTree.Variable;
  3276. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3277. VAR expression: SyntaxTree.Expression;
  3278. BEGIN
  3279. (* count the number of indices, ranges and tensorRanges in the index list *)
  3280. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3281. FOR i := 0 TO parameters.Length() - 1 DO
  3282. expression := parameters.GetExpression(i);
  3283. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3284. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3285. ELSE INC(indexCount)
  3286. END
  3287. END;
  3288. END CountIndices;
  3289. BEGIN
  3290. ASSERT(tagsAvailable);
  3291. resultingType := x.type.resolved; (* resulting type *)
  3292. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3293. InitOperand(localResult, ModeReference);
  3294. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3295. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3296. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3297. (* a globally defined array destination tag is available -> use and invalidate it*)
  3298. localResult.tag := arrayDestinationTag;
  3299. IntermediateCode.InitOperand(arrayDestinationTag)
  3300. ELSE
  3301. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3302. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3303. Symbol(variable, variableOp);
  3304. ReuseCopy(localResult.tag, variableOp.op);
  3305. ReleaseOperand(variableOp);
  3306. END
  3307. END;
  3308. indexListSize := x.parameters.Length();
  3309. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3310. ASSERT(tensorRangeCount <= 1);
  3311. (* designate the array to be indexed over, perform tensor range check if known *)
  3312. Designate(x.left, array);
  3313. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3314. Dereference(array, leftType,FALSE);
  3315. IF tensorRangeCount=0 THEN
  3316. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3317. END
  3318. END;
  3319. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3320. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3321. srcDimOffset := -indexListSize;
  3322. destDimOffset := -rangeCount
  3323. ELSE
  3324. srcDimOffset := 0;
  3325. destDimOffset := 0
  3326. END;
  3327. indexDim := 0;
  3328. (* use address of source array as basis *)
  3329. (*
  3330. ReuseCopy(localResult.op, array.op);
  3331. *)
  3332. localResult.op := array.op;
  3333. UseIntermediateOperand(localResult.op);
  3334. (* go through the index list *)
  3335. FOR i := 0 TO indexListSize - 1 DO
  3336. expression := x.parameters.GetExpression(i);
  3337. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3338. (* nothing to do *)
  3339. ELSE
  3340. (* determine which dimension of source array is currently looked at *)
  3341. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3342. (* get the memory operand pointing to array descriptor dimension *)
  3343. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3344. (* make a reusable register from it *)
  3345. ReuseCopy(srcDim, tmp);
  3346. ReleaseIntermediateOperand(tmp);
  3347. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3348. ELSE
  3349. srcDim := IntermediateCode.Immediate(int32, i)
  3350. END;
  3351. (* get length and increment of source array for current dimension *)
  3352. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3353. Convert(sourceLength.op, sizeType);
  3354. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3355. Convert(sourceIncrement.op, sizeType);
  3356. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3357. ReleaseIntermediateOperand(srcDim);
  3358. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3359. (* SINGLE INDEX *)
  3360. Evaluate(expression, index);
  3361. ReleaseIntermediateOperand(index.tag);
  3362. index.tag := emptyOperand;
  3363. Convert(index.op, sizeType);
  3364. (* lower bound check *)
  3365. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3366. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3367. ELSIF isUnchecked THEN (* do nothing *)
  3368. ELSE
  3369. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3370. END;
  3371. (* upper bound check *)
  3372. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3373. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3374. ELSIF isUnchecked THEN (* do nothing *)
  3375. ELSE
  3376. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3377. END;
  3378. ReleaseOperand(sourceLength);
  3379. Convert(index.op, addressType);
  3380. summand := index.op;
  3381. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3382. (* RANGE OF INDICES *)
  3383. Evaluate(expression, range);
  3384. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3385. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3386. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3387. ReleaseOperand(range);
  3388. Convert(firstIndex, sizeType);
  3389. Convert(lastIndex, sizeType);
  3390. Convert(stepSize, sizeType);
  3391. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3392. if it is 'MAX(LONGINT)' *)
  3393. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3394. TransferToRegister(lastIndex, lastIndex);
  3395. skipLabel1 := NewLabel();
  3396. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3397. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3398. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3399. SetLabel(skipLabel1)
  3400. END;
  3401. (* check if step size is valid *)
  3402. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3403. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3404. ELSIF isUnchecked THEN (* do nothing *)
  3405. ELSE
  3406. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3407. END;
  3408. (* check lower bound check *)
  3409. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3410. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3411. ELSIF isUnchecked THEN (* do nothing *)
  3412. ELSE
  3413. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3414. END;
  3415. (* check upper bound check *)
  3416. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3417. (* statically open range: nothing to do *)
  3418. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3419. ASSERT(staticLastIndex < staticSourceLength)
  3420. ELSIF isUnchecked THEN (* do nothing *)
  3421. ELSE
  3422. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3423. END;
  3424. (* determine length of target array for current dimension *)
  3425. (* 1. incorporate last index: *)
  3426. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3427. (* last index is static *)
  3428. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3429. targetLength := sourceLength.op
  3430. ELSE
  3431. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3432. END;
  3433. UseIntermediateOperand(targetLength);
  3434. ELSE
  3435. (* targetLength := lastIndex + 1
  3436. Reuse1(targetLength, lastIndex);
  3437. *)
  3438. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3439. END;
  3440. ReleaseOperand(sourceLength);
  3441. ReleaseIntermediateOperand(lastIndex);
  3442. (* 2. incorporate first index: *)
  3443. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3444. (* first index and current target length are static *)
  3445. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3446. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3447. (* first index = 0: nothing to do *)
  3448. ELSE
  3449. (* targetLength := targetLength - firstIndex *)
  3450. TransferToRegister(targetLength, targetLength);
  3451. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3452. END;
  3453. (* clip negative lengths to 0 *)
  3454. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3455. IF staticTargetLength < 0 THEN
  3456. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3457. END
  3458. ELSE
  3459. skipLabel1 := NewLabel();
  3460. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3461. TransferToRegister(targetLength, targetLength);
  3462. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3463. SetLabel(skipLabel1)
  3464. END;
  3465. (* 3. incorporate index step size: *)
  3466. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3467. (*step size and current target length are static *)
  3468. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3469. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3470. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3471. (* step size = 1: nothing to do *)
  3472. ELSE
  3473. (* emit code for this:
  3474. targetLength := (targetLength-1) DIV stepSize +1;
  3475. *)
  3476. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3477. DivInt(targetLength, targetLength, stepSize);
  3478. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3479. END;
  3480. (* determine increment of target array for current dimension *)
  3481. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3482. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3483. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3484. (* step size = 1 *)
  3485. targetIncrement := sourceIncrement.op;
  3486. UseIntermediateOperand(targetIncrement)
  3487. ELSE
  3488. (* targetIncrement := sourceIncrement * stepSize *)
  3489. Reuse1(targetIncrement, stepSize);
  3490. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3491. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3492. END;
  3493. ReleaseIntermediateOperand(stepSize);
  3494. (* write length and increment of target array to descriptor *)
  3495. IF destDimOffset < 0 THEN
  3496. (* determine which dimension of target array is currently looked at *)
  3497. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3498. TransferToRegister(destDim, tmp);
  3499. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3500. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3501. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3502. ReleaseIntermediateOperand(destDim)
  3503. ELSE
  3504. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3505. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3506. END;
  3507. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3508. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3509. INC(indexDim);
  3510. Convert(firstIndex, addressType);
  3511. TransferToRegister(summand, firstIndex);
  3512. ELSE HALT(100);
  3513. END;
  3514. (*
  3515. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3516. *)
  3517. Convert(sourceIncrement.op, addressType);
  3518. Convert(summand, addressType);
  3519. MulInt(summand, summand, sourceIncrement.op);
  3520. ReleaseIntermediateOperand(sourceIncrement.op);
  3521. AddInt(localResult.op, localResult.op, summand);
  3522. ReleaseIntermediateOperand(summand);
  3523. END
  3524. END;
  3525. result := localResult;
  3526. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3527. ReleaseIntermediateOperand(result.tag);
  3528. result.tag := TypeDescriptorAdr(resultingType);
  3529. IF ~newObjectFile THEN
  3530. IntermediateCode.MakeMemory(result.tag,addressType);
  3531. END;
  3532. ELSIF IsDelegate(resultingType) THEN
  3533. ReleaseIntermediateOperand(result.tag);
  3534. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3535. UseIntermediateOperand(result.tag);
  3536. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3537. ReleaseIntermediateOperand(result.tag);
  3538. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3539. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3540. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3541. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3542. (* finalize target array descriptor *)
  3543. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3544. (* write lengths and increments of target array for remaining dimensions *)
  3545. i := indexListSize;
  3546. WHILE indexDim < targetArrayDimensionality DO
  3547. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3548. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3549. ReleaseOperand(sourceLength);
  3550. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3551. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3552. ReleaseOperand(sourceIncrement);
  3553. INC(i); INC(indexDim);
  3554. END;
  3555. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3556. tmp := nil;
  3557. ELSE
  3558. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3559. END;
  3560. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3561. ReleaseIntermediateOperand(tmp);
  3562. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3563. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3564. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3565. ELSE
  3566. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3567. END;
  3568. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3569. ReleaseIntermediateOperand(tmp);
  3570. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3571. (* write dimensionality *)
  3572. IF targetArrayDimensionality # 0 THEN
  3573. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3574. END;
  3575. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3576. END;
  3577. ReleaseOperand(array);
  3578. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3579. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3580. END;
  3581. END MathIndexDesignator;
  3582. (* TENTATIVE *)
  3583. PROCEDURE DumpOperand(operand: Operand);
  3584. BEGIN
  3585. D.Log.String(" op = ");
  3586. IntermediateCode.DumpOperand(D.Log, operand.op );
  3587. D.Log.Ln;
  3588. D.Log.String(" tag = ");
  3589. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3590. D.Log.Ln;
  3591. D.Log.String(" extra = ");
  3592. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3593. D.Log.Ln;
  3594. D.Log.Update
  3595. END DumpOperand;
  3596. (* get the length of an array , trying to make use of static information *)
  3597. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3598. VAR res: IntermediateCode.Operand; size: LONGINT;
  3599. BEGIN
  3600. type := type.resolved;
  3601. IF type IS SyntaxTree.ArrayType THEN
  3602. WITH type: SyntaxTree.ArrayType DO
  3603. IF type.form = SyntaxTree.Static THEN
  3604. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3605. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3606. Evaluate(type.length, op);
  3607. ReleaseIntermediateOperand(op.tag);
  3608. RETURN op.op;*)
  3609. ELSE
  3610. res := tag;
  3611. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3612. IntermediateCode.MakeMemory(res,addressType);
  3613. UseIntermediateOperand(res);
  3614. RETURN res
  3615. END
  3616. END;
  3617. ELSE
  3618. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3619. RETURN IntermediateCode.Immediate(addressType,size);
  3620. END;
  3621. END ArrayLength;
  3622. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3623. BEGIN
  3624. ReleaseIntermediateOperand(res);
  3625. IF IsImmediate(x) & IsImmediate(y) THEN
  3626. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3627. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3628. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3629. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3630. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3631. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3632. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3633. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3634. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3635. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3636. UseIntermediateOperand(res);
  3637. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3638. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3639. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3640. UseIntermediateOperand(res);
  3641. ELSE
  3642. IF ~ReusableRegister(res) THEN
  3643. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3644. ELSE
  3645. UseIntermediateOperand(res);
  3646. END;
  3647. IF IsImmediate(x) & (x.intValue = 0) THEN
  3648. Emit(Mov(position,res,y))
  3649. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3650. Emit(Mov(position,res,x))
  3651. ELSE
  3652. Emit(Add(position,res, x, y));
  3653. END;
  3654. END;
  3655. END AddInt;
  3656. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3657. BEGIN
  3658. ReleaseIntermediateOperand(res);
  3659. IF IsImmediate(x) & IsImmediate(y) THEN
  3660. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3661. ELSE
  3662. IF ~ReusableRegister(res) THEN
  3663. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3664. ELSE UseIntermediateOperand(res);
  3665. END;
  3666. IF IsImmediate(x) & (x.intValue = 1) THEN
  3667. Emit(Mov(position,res,y))
  3668. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3669. Emit(Mov(position,res,x))
  3670. ELSE
  3671. Emit(Mul(position,res, x, y));
  3672. END;
  3673. END;
  3674. END MulInt;
  3675. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3676. BEGIN
  3677. ReleaseIntermediateOperand(res);
  3678. IF IsImmediate(x) & IsImmediate(y) THEN
  3679. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3680. ELSE
  3681. IF ~ReusableRegister(res) THEN
  3682. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3683. ELSE UseIntermediateOperand(res);
  3684. END;
  3685. IF IsImmediate(x) & (x.intValue = 1) THEN
  3686. Emit(Mov(position,res,y))
  3687. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3688. Emit(Mov(position,res,x))
  3689. ELSE
  3690. Emit(Div(position,res, x, y));
  3691. END;
  3692. END;
  3693. END DivInt;
  3694. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3695. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3696. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3697. BEGIN
  3698. type := x.left.type.resolved;
  3699. IF type IS SyntaxTree.StringType THEN
  3700. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3701. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3702. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3703. type := atype;
  3704. x.left.SetType(type);
  3705. END;
  3706. IntermediateCode.InitImmediate(res,addressType,0);
  3707. maxDim := x.parameters.Length()-1;
  3708. FOR i := 0 TO maxDim DO
  3709. e := x.parameters.GetExpression(i);
  3710. Evaluate(e,index);
  3711. Convert(index.op,addressType);
  3712. AddInt(res, res, index.op);
  3713. IF i = 0 THEN
  3714. (*
  3715. ReuseCopy(res, index.op);
  3716. *)
  3717. Designate(x.left,array);
  3718. type := x.left.type.resolved;
  3719. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3720. Dereference(array, type, FALSE);
  3721. END;
  3722. (*
  3723. ELSE AddInt(res, res, index.op);
  3724. *)
  3725. END;
  3726. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3727. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3728. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3729. BoundCheck(index.op, length);
  3730. END;
  3731. ReleaseIntermediateOperand(length);
  3732. END;
  3733. ReleaseOperand(index);
  3734. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3735. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3736. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3737. MulInt(res,res,length);
  3738. END;
  3739. ReleaseIntermediateOperand(length);
  3740. END;
  3741. IF (type IS SyntaxTree.ArrayType) THEN
  3742. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3743. size := StaticSize(system, type);
  3744. IF size # 1 THEN
  3745. length := IntermediateCode.Immediate(addressType,size);
  3746. MulInt(res,res,length);
  3747. END;
  3748. ELSE
  3749. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3750. IF size # 1 THEN
  3751. length := IntermediateCode.Immediate(addressType,size);
  3752. MulInt(res,res,length);
  3753. END;
  3754. END;
  3755. END;
  3756. AddInt(res,res,array.op);
  3757. InitOperand(result,ModeReference);
  3758. result.op := res;
  3759. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3760. ReleaseIntermediateOperand(result.tag);
  3761. result.tag := TypeDescriptorAdr(type);
  3762. IF ~newObjectFile THEN
  3763. IntermediateCode.MakeMemory(result.tag,addressType);
  3764. END
  3765. ELSIF IsDelegate(type) THEN
  3766. ReleaseIntermediateOperand(result.tag);
  3767. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3768. UseIntermediateOperand(result.tag);
  3769. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3770. ReleaseIntermediateOperand(result.tag);
  3771. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3772. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3773. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3774. END;
  3775. ReleaseOperand(array);
  3776. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3777. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3778. END;
  3779. END IndexDesignator;
  3780. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3781. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3782. BEGIN
  3783. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3784. dest := destination; destination := emptyOperand;
  3785. type := x.left.type.resolved;
  3786. IF type IS SyntaxTree.MathArrayType THEN
  3787. MathIndexDesignator(x);
  3788. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3789. IndexDesignator(x);
  3790. END;
  3791. destination := dest;
  3792. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3793. END VisitIndexDesignator;
  3794. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3795. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3796. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3797. procedure: SyntaxTree.Procedure;
  3798. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3799. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3800. BEGIN
  3801. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3802. parameters := expression.parameters;
  3803. moduleName := "FoxArrayBase";
  3804. procedureName := "CopyDescriptor";
  3805. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3806. SaveRegisters();ReleaseUsedRegisters(saved);
  3807. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3808. IF procedure = NIL THEN
  3809. s := "procedure ";
  3810. Strings.Append(s,moduleName);
  3811. Strings.Append(s,".");
  3812. Strings.Append(s,procedureName);
  3813. Strings.Append(s," not present");
  3814. Error(position,s);
  3815. ELSE
  3816. (* push address of temporary variable *)
  3817. Symbol(variable,destOperand);
  3818. Emit(Push(position,destOperand.op));
  3819. ReleaseOperand(destOperand);
  3820. (* push src *)
  3821. Evaluate(expression.left,srcOperand);
  3822. (*
  3823. Dereference(srcOperand,expression.type.resolved);
  3824. Emit(Push(position,srcOperand.tag));
  3825. *)
  3826. Emit(Push(position,srcOperand.op));
  3827. ReleaseOperand(srcOperand);
  3828. tensorFound := FALSE;
  3829. FOR i := 0 TO parameters.Length()-1 DO
  3830. e := parameters.GetExpression(i);
  3831. IF e IS SyntaxTree.TensorRangeExpression THEN
  3832. tensorFound := TRUE;
  3833. ELSIF e IS SyntaxTree.RangeExpression THEN
  3834. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3835. ELSE
  3836. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3837. END;
  3838. END;
  3839. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3840. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3841. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3842. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3843. StaticCallOperand(procOp,procedure);
  3844. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3845. ReleaseOperand(procOp);
  3846. END;
  3847. RestoreRegisters(saved);
  3848. END;
  3849. RETURN variable
  3850. END PrepareTensorDescriptor;
  3851. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3852. VAR
  3853. type, descriptorType, baseType: SyntaxTree.Type;
  3854. operand, tmpOperand, variableOp, variable2Op: Operand;
  3855. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3856. variable, variable2: SyntaxTree.Variable;
  3857. dim, i, size: LONGINT;
  3858. (* TODO: needed? *)
  3859. oldArrayDestinationTag: IntermediateCode.Operand;
  3860. oldArrayDestinationDimension: LONGINT;
  3861. position: LONGINT;
  3862. saved: RegisterEntry;
  3863. PROCEDURE Pass(op: IntermediateCode.Operand);
  3864. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3865. BEGIN
  3866. IF numberRegister >= 0 THEN
  3867. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3868. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3869. Emit(Mov(position,parameterRegister, op));
  3870. ELSE
  3871. Emit(Push(position,op))
  3872. END
  3873. END Pass;
  3874. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3875. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3876. BEGIN
  3877. formalType := formalType.resolved; actualType := actualType.resolved;
  3878. IF IsOpenArray(formalType)THEN
  3879. IF actualType IS SyntaxTree.StringType THEN
  3880. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3881. RETURN;
  3882. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3883. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3884. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3885. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3886. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3887. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3888. ELSE
  3889. tmp := baseReg;
  3890. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3891. IntermediateCode.MakeMemory(tmp,addressType);
  3892. Pass((tmp));
  3893. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3894. END;
  3895. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3896. END;
  3897. END PushArrayLens;
  3898. BEGIN
  3899. IF Trace THEN TraceEnter("PushParameter") END;
  3900. position := expression.position;
  3901. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3902. type := expression.type.resolved;
  3903. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3904. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3905. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3906. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3907. );
  3908. (* TODO: needed? *)
  3909. oldArrayDestinationTag := arrayDestinationTag;
  3910. oldArrayDestinationDimension := arrayDestinationDimension;
  3911. IF IsArrayOfSystemByte(parameter.type) THEN
  3912. Designate(expression,operand);
  3913. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3914. ReleaseIntermediateOperand(operand.tag);
  3915. operand.tag := tmp;
  3916. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3917. Pass((operand.tag));
  3918. END;
  3919. Pass((operand.op));
  3920. ELSIF IsOpenArray(parameter.type) THEN
  3921. Designate(expression,operand);
  3922. baseReg := operand.tag;
  3923. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3924. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3925. END;
  3926. Pass((operand.op)); (* address of the array *)
  3927. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3928. (* case 1 *)
  3929. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3930. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3931. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3932. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3933. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3934. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3935. arrayDestinationTag := sp;
  3936. (* case 1b *)
  3937. IF expression IS SyntaxTree.IndexDesignator THEN
  3938. (*
  3939. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3940. descriptorType := GetMathArrayDescriptorType(dim);
  3941. variable := GetTemporaryVariable(descriptorType,expression.position);
  3942. Symbol(variable,variableOp);
  3943. arrayDestinationTag := variableOp.op;
  3944. *)
  3945. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3946. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3947. arrayDestinationDimension := dim;
  3948. Designate(expression,operand);
  3949. (* case 1a *)
  3950. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3951. Designate(expression,operand);
  3952. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3953. i := 0;
  3954. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3955. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3956. INC(i);
  3957. END;
  3958. type := expression.type.resolved;
  3959. WHILE (i<dim) DO (* remaining static dimensions *)
  3960. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3961. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3962. ReleaseOperand(tmpOperand);
  3963. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3964. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3965. ReleaseOperand(tmpOperand);
  3966. INC(i);
  3967. END;
  3968. dimOp := IntermediateCode.Immediate(addressType,dim);
  3969. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3970. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3971. (* case 1d *)
  3972. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3973. Designate(expression,operand);
  3974. Dereference(operand,type.resolved,FALSE);
  3975. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3976. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3977. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3978. (* case 1f *)
  3979. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3980. Designate(expression,operand);
  3981. FOR i := 0 TO dim-1 DO
  3982. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3983. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3984. ReleaseOperand(tmpOperand);
  3985. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3986. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3987. ReleaseOperand(tmpOperand);
  3988. END;
  3989. dimOp := IntermediateCode.Immediate(addressType,dim);
  3990. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3991. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  3992. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  3993. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  3994. baseType := SemanticChecker.ArrayBase(type,dim);
  3995. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3996. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  3997. ELSE HALT(100);
  3998. END;
  3999. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4000. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4001. (* case 2b *)
  4002. IF expression IS SyntaxTree.IndexDesignator THEN
  4003. descriptorType := GetMathArrayDescriptorType(dim);
  4004. variable := GetTemporaryVariable(descriptorType, FALSE);
  4005. Symbol(variable,variableOp);
  4006. arrayDestinationTag := variableOp.op;
  4007. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4008. arrayDestinationDimension := dim;
  4009. NeedDescriptor := TRUE;
  4010. Designate(expression,operand);
  4011. Pass((operand.tag));
  4012. NeedDescriptor := FALSE;
  4013. (* case 2a *)
  4014. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4015. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4016. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4017. INC(i);
  4018. END;
  4019. IF i = dim THEN
  4020. Designate(expression,operand);
  4021. Pass((operand.tag));
  4022. ELSE (* open-static *)
  4023. type := expression.type.resolved;
  4024. descriptorType := GetMathArrayDescriptorType(dim);
  4025. variable := GetTemporaryVariable(descriptorType, FALSE);
  4026. Symbol(variable,variableOp);
  4027. arrayDestinationTag := variableOp.op;
  4028. Designate(expression,operand);
  4029. FOR i := 0 TO dim-1 DO
  4030. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4031. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4032. ReleaseOperand(tmpOperand);
  4033. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4034. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4035. ReleaseOperand(tmpOperand);
  4036. END;
  4037. dimOp := IntermediateCode.Immediate(addressType,dim);
  4038. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4039. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4040. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4041. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4042. baseType := SemanticChecker.ArrayBase(type,dim);
  4043. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4044. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4045. Pass((arrayDestinationTag));
  4046. END;
  4047. (* case 2d *)
  4048. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4049. Designate(expression,operand);
  4050. Dereference(operand,type.resolved,FALSE);
  4051. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4052. Pass((operand.tag));
  4053. (* case 2f *)
  4054. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4055. descriptorType := GetMathArrayDescriptorType(dim);
  4056. variable := GetTemporaryVariable(descriptorType, FALSE);
  4057. Symbol(variable,variableOp);
  4058. arrayDestinationTag := variableOp.op;
  4059. Designate(expression,operand);
  4060. FOR i := 0 TO dim-1 DO
  4061. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4062. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4063. ReleaseOperand(tmpOperand);
  4064. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4065. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4066. ReleaseOperand(tmpOperand);
  4067. END;
  4068. dimOp := IntermediateCode.Immediate(addressType,dim);
  4069. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4070. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4071. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4072. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4073. baseType := SemanticChecker.ArrayBase(type,dim);
  4074. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4075. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4076. Pass((arrayDestinationTag));
  4077. ELSE HALT(100);
  4078. END;
  4079. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4080. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4081. (* case 3b *)
  4082. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4083. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4084. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4085. Symbol(variable,variableOp);
  4086. LoadValue(variableOp,system.addressType);
  4087. ELSE
  4088. descriptorType := GetMathArrayDescriptorType(dim);
  4089. variable := GetTemporaryVariable(descriptorType, FALSE);
  4090. Symbol(variable,variableOp);
  4091. END;
  4092. arrayDestinationTag := variableOp.op;
  4093. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4094. arrayDestinationDimension := 0;
  4095. Designate(expression,operand);
  4096. Pass((operand.tag));
  4097. (* case 3a *)
  4098. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4099. i := 0;
  4100. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4101. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4102. INC(i);
  4103. END;
  4104. IF i = dim THEN
  4105. Designate(expression,operand);
  4106. Pass((operand.tag));
  4107. ELSE (* open-static *)
  4108. type := expression.type.resolved;
  4109. descriptorType := GetMathArrayDescriptorType(dim);
  4110. variable := GetTemporaryVariable(descriptorType, FALSE);
  4111. Symbol(variable,variableOp);
  4112. arrayDestinationTag := variableOp.op;
  4113. Designate(expression,operand);
  4114. FOR i := 0 TO dim-1 DO
  4115. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4116. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4117. ReleaseOperand(tmpOperand);
  4118. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4119. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4120. ReleaseOperand(tmpOperand);
  4121. END;
  4122. dimOp := IntermediateCode.Immediate(addressType,dim);
  4123. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4124. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4125. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4126. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4127. baseType := SemanticChecker.ArrayBase(type,dim);
  4128. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4129. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4130. Pass((arrayDestinationTag));
  4131. END;
  4132. (* case 3d *)
  4133. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4134. Designate(expression,operand);
  4135. Dereference(operand,type.resolved,FALSE);
  4136. (*
  4137. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4138. *)
  4139. Pass((operand.tag));
  4140. (* case 3f *)
  4141. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4142. descriptorType := GetMathArrayDescriptorType(dim);
  4143. variable := GetTemporaryVariable(descriptorType, FALSE);
  4144. Symbol(variable,variableOp);
  4145. arrayDestinationTag := variableOp.op;
  4146. Designate(expression,operand);
  4147. IF operand.op.type.length >1 THEN (* vector register *)
  4148. variable2 := GetTemporaryVariable(type, FALSE);
  4149. Symbol(variable2, variable2Op);
  4150. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4151. Emit(Mov(position,tmp, operand.op));
  4152. ReleaseOperand(operand);
  4153. Symbol(variable2, operand);
  4154. END;
  4155. FOR i := 0 TO dim-1 DO
  4156. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4157. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4158. ReleaseOperand(tmpOperand);
  4159. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4160. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4161. ReleaseOperand(tmpOperand);
  4162. END;
  4163. dimOp := IntermediateCode.Immediate(addressType,dim);
  4164. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4165. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4166. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4167. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4168. baseType := SemanticChecker.ArrayBase(type,dim);
  4169. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4170. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4171. Pass((arrayDestinationTag));
  4172. ELSE HALT(100);
  4173. END;
  4174. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4175. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4176. (* case 4b *)
  4177. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4178. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4179. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4180. Symbol(variable,variableOp);
  4181. LoadValue(variableOp,system.addressType);
  4182. ELSE
  4183. descriptorType := GetMathArrayDescriptorType(dim);
  4184. variable := GetTemporaryVariable(descriptorType, FALSE);
  4185. Symbol(variable,variableOp);
  4186. END;
  4187. arrayDestinationTag := variableOp.op;
  4188. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4189. arrayDestinationDimension := 0;
  4190. Designate(expression,operand);
  4191. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4192. Symbol(variable,variableOp);
  4193. ELSE
  4194. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4195. Symbol(variable,variableOp);
  4196. MakeMemory(tmp,variableOp.op,addressType,0);
  4197. Emit(Mov(position,tmp,operand.tag));
  4198. ReleaseIntermediateOperand(tmp);
  4199. END;
  4200. Pass((variableOp.op));
  4201. ReleaseOperand(variableOp);
  4202. (* case 4a *)
  4203. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4204. i := 0;
  4205. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4206. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4207. INC(i);
  4208. END;
  4209. IF i = dim THEN
  4210. Designate(expression,operand);
  4211. arrayDestinationTag := operand.tag;
  4212. ELSE (* open-static *)
  4213. type := expression.type.resolved;
  4214. descriptorType := GetMathArrayDescriptorType(dim);
  4215. variable := GetTemporaryVariable(descriptorType, FALSE);
  4216. Symbol(variable,variableOp);
  4217. arrayDestinationTag := variableOp.op;
  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. dimOp := IntermediateCode.Immediate(addressType,dim);
  4228. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4229. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4230. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4231. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4232. baseType := SemanticChecker.ArrayBase(type,dim);
  4233. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4234. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4235. END;
  4236. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4237. Symbol(variable,variableOp);
  4238. MakeMemory(tmp,variableOp.op,addressType,0);
  4239. Emit(Mov(position,tmp,arrayDestinationTag));
  4240. ReleaseIntermediateOperand(tmp);
  4241. Pass((variableOp.op));
  4242. ReleaseOperand(variableOp);
  4243. (* case 4d *)
  4244. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4245. Designate(expression,operand);
  4246. (*
  4247. Dereference(operand,type.resolved,FALSE);
  4248. *)
  4249. (*
  4250. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4251. *)
  4252. Pass((operand.op));
  4253. (* case 4f *)
  4254. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4255. descriptorType := GetMathArrayDescriptorType(dim);
  4256. variable := GetTemporaryVariable(descriptorType, FALSE);
  4257. Symbol(variable,variableOp);
  4258. arrayDestinationTag := variableOp.op;
  4259. Designate(expression,operand);
  4260. FOR i := 0 TO dim-1 DO
  4261. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4262. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4263. ReleaseOperand(tmpOperand);
  4264. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4265. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4266. ReleaseOperand(tmpOperand);
  4267. END;
  4268. dimOp := IntermediateCode.Immediate(addressType,dim);
  4269. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4270. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4271. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4272. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4273. baseType := SemanticChecker.ArrayBase(type,dim);
  4274. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4275. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4276. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4277. Symbol(variable,variableOp);
  4278. MakeMemory(tmp,variableOp.op,addressType,0);
  4279. Emit(Mov(position,tmp,arrayDestinationTag));
  4280. ReleaseIntermediateOperand(tmp);
  4281. Pass((variableOp.op));
  4282. ReleaseOperand(variableOp);
  4283. ELSE HALT(100);
  4284. END;
  4285. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4286. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4287. Designate(expression,operand);
  4288. IF operand.op.type.length > 1 THEN
  4289. Emit(Push(position, operand.op));
  4290. ReleaseOperand(operand);
  4291. ELSE
  4292. size := system.SizeOf(type);
  4293. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4294. size := ToMemoryUnits(system,size);
  4295. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4296. arrayDestinationTag := sp;
  4297. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4298. END;
  4299. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4300. Designate(expression,operand);
  4301. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4302. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4303. Symbol(variable,variableOp);
  4304. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4305. Emit(Mov(position,tmp,operand.op));
  4306. Emit(Push(position,variableOp.op));
  4307. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4308. Pass((operand.op));
  4309. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4310. END;
  4311. ELSE HALT(200)
  4312. END;
  4313. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4314. IF parameter.kind = SyntaxTree.VarParameter THEN
  4315. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4316. Designate(expression, operand);
  4317. Pass((operand.op));
  4318. ELSE
  4319. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4320. Evaluate(expression, operand);
  4321. Pass((operand.extra)); (* step *)
  4322. Pass((operand.tag)); (* last *)
  4323. Pass((operand.op)) (* first *)
  4324. END
  4325. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4326. IF parameter.kind = SyntaxTree.VarParameter THEN
  4327. Designate(expression, operand);
  4328. Pass((operand.op));
  4329. ELSE
  4330. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4331. Evaluate(expression, operand);
  4332. Pass((operand.tag)); (* real part *)
  4333. Pass((operand.op)) (* imaginary part *)
  4334. END
  4335. ELSE
  4336. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4337. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4338. Designate(expression,operand);
  4339. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4340. (* stack allocation *)
  4341. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4342. (*! parameter alignment to be discussed ... *)
  4343. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4344. size := type(SyntaxTree.StringType).length;
  4345. END;
  4346. IF backend.cooperative & parameter.NeedsTrace() THEN
  4347. dst := NewRegisterOperand (addressType);
  4348. Emit(Mov(position,dst, sp));
  4349. IntermediateCode.InitImmediate(null, byteType, 0);
  4350. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4351. ReleaseIntermediateOperand(dst);
  4352. SaveRegisters();ReleaseUsedRegisters(saved);
  4353. (* register dst has been freed before SaveRegisters already *)
  4354. CallAssignMethod(dst, operand.op, parameter.type);
  4355. RestoreRegisters(saved);
  4356. END;
  4357. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4358. ELSIF IsOpenArray(parameter.type) THEN
  4359. Designate(expression,operand);
  4360. baseReg := operand.tag;
  4361. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4362. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4363. END;
  4364. Pass((operand.op)); (* address of the array *)
  4365. ELSIF IsDelegate(parameter.type) THEN
  4366. Evaluate(expression,operand);
  4367. IF backend.cooperative & parameter.NeedsTrace() THEN
  4368. Emit(Push(position, nil));
  4369. dst := NewRegisterOperand (addressType);
  4370. Emit(Mov(position,dst, sp));
  4371. ReleaseIntermediateOperand(dst);
  4372. SaveRegisters();ReleaseUsedRegisters(saved);
  4373. Emit(Push(position, dst));
  4374. (* register dst has been freed before SaveRegisters already *)
  4375. Emit(Push(position, operand.tag));
  4376. CallThis(position,"GarbageCollector","Assign",2);
  4377. RestoreRegisters(saved);
  4378. ELSE
  4379. Pass((operand.tag));
  4380. END;
  4381. Pass((operand.op));
  4382. ELSE
  4383. Evaluate(expression,operand);
  4384. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4385. Emit(Push(position, nil));
  4386. dst := NewRegisterOperand (addressType);
  4387. Emit(Mov(position,dst, sp));
  4388. ReleaseIntermediateOperand(dst);
  4389. SaveRegisters();ReleaseUsedRegisters(saved);
  4390. Emit(Push(position, dst));
  4391. (* register dst has been freed before SaveRegisters already *)
  4392. Emit(Push(position, operand.op));
  4393. CallThis(position,"GarbageCollector","Assign",2);
  4394. RestoreRegisters(saved);
  4395. ELSE
  4396. Pass((operand.op));
  4397. END;
  4398. END;
  4399. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4400. Evaluate(expression,operand);
  4401. Pass((operand.op));
  4402. ELSE (* var parameter *)
  4403. Designate(expression,operand);
  4404. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4405. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4406. Pass((operand.tag));
  4407. END;
  4408. END;
  4409. Pass((operand.op));
  4410. END;
  4411. END;
  4412. (* TODO: needed? *)
  4413. arrayDestinationTag := oldArrayDestinationTag;
  4414. arrayDestinationDimension := oldArrayDestinationDimension;
  4415. IF needsParameterBackup THEN
  4416. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4417. ReuseCopy(parameterBackup, operand.op)
  4418. END;
  4419. ReleaseOperand(operand);
  4420. IF Trace THEN TraceExit("PushParameter") END;
  4421. END PushParameter;
  4422. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4423. VAR prevConditional: BOOLEAN;
  4424. BEGIN
  4425. prevConditional := conditional;
  4426. conditional := FALSE;
  4427. 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
  4428. Expression(x.result); (* use register *)
  4429. END;
  4430. Statement(x.statement);
  4431. conditional := prevConditional;
  4432. IF x.result # NIL THEN Expression(x.result) END;
  4433. 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
  4434. ReleaseIntermediateOperand(result.op);
  4435. END;
  4436. END VisitStatementDesignator;
  4437. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4438. VAR
  4439. procedure: SyntaxTree.Procedure;
  4440. procedureType: SyntaxTree.ProcedureType;
  4441. wasInline: BOOLEAN;
  4442. actualParameters: SyntaxTree.ExpressionList;
  4443. formalParameter: SyntaxTree.Parameter;
  4444. actualParameter: SyntaxTree.Expression;
  4445. i: LONGINT;
  4446. localVariable: SyntaxTree.Variable;
  4447. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4448. src, dest: Operand;
  4449. prevInlineExit : Label;
  4450. prevMapper: SymbolMapper;
  4451. tooComplex: BOOLEAN;
  4452. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4453. BEGIN
  4454. IF e = NIL THEN RETURN TRUE
  4455. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4456. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4457. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4458. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4459. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4460. ELSE RETURN FALSE
  4461. END;
  4462. END SimpleExpression;
  4463. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4464. BEGIN
  4465. RETURN checker.CanPassInRegister(type)
  4466. END FitsInRegister;
  4467. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4468. VAR
  4469. variable: SyntaxTree.Variable;
  4470. variableDesignator: SyntaxTree.Designator;
  4471. BEGIN
  4472. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4473. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4474. variableDesignator.SetType(type);
  4475. RETURN variableDesignator
  4476. END GetTemp;
  4477. BEGIN
  4478. wasInline := currentIsInline;
  4479. prevInlineExit := currentInlineExit;
  4480. prevMapper := currentMapper;
  4481. currentInlineExit := NewLabel();
  4482. tooComplex := FALSE;
  4483. NEW(currentMapper);
  4484. currentIsInline := TRUE;
  4485. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4486. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4487. formalParameter := procedureType.firstParameter;
  4488. actualParameters := x.parameters;
  4489. i := 0;
  4490. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4491. actualParameter := actualParameters.GetExpression(i);
  4492. IF actualParameter.resolved # NIL THEN
  4493. actualParameter := actualParameter.resolved
  4494. END;
  4495. (*
  4496. if expression is simple and can be passed immediately
  4497. or if type fits in register then we can proceed
  4498. otherwise we escape to ordinary procedure call.
  4499. *)
  4500. (* cases where the expression can be mapped identically *)
  4501. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4502. currentMapper.Add(formalParameter, actualParameter, NIL);
  4503. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4504. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4505. (*
  4506. Assign(variableDesignator, actualParameter);
  4507. *)
  4508. Evaluate(actualParameter, src);
  4509. Designate(variableDesignator, dest);
  4510. Emit(Mov(x.position, dest.op, src.op));
  4511. ReleaseOperand(dest);
  4512. ReleaseOperand(src);
  4513. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4514. ELSE tooComplex := TRUE
  4515. END;
  4516. (*
  4517. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4518. currentMapper.Add(formalParameter, actualParameter, NIL);
  4519. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4520. variableDesignator := GetTemp(system.addressType, FALSE);
  4521. Designate(actualParameter, src);
  4522. Designate(variableDesignator, dest);
  4523. IntermediateCode.MakeMemory(dest.op,addressType);
  4524. Emit(Mov(x.position, dest.op, src.op));
  4525. ReleaseOperand(dest);
  4526. IF src.tag.mode # IntermediateCode.Undefined THEN
  4527. tagDesignator := GetTemp(system.addressType, FALSE);
  4528. Designate(tagDesignator, dest);
  4529. IntermediateCode.MakeMemory(dest.op,addressType);
  4530. Emit(Mov(x.position, dest.op, src.op));
  4531. END;
  4532. ReleaseOperand(dest); ReleaseOperand(src);
  4533. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4534. END;
  4535. *)
  4536. formalParameter := formalParameter.nextParameter;
  4537. INC(i);
  4538. END;
  4539. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4540. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4541. currentMapper.Add(NIL, returnDesignator, NIL);
  4542. END;
  4543. localVariable := procedure.procedureScope.firstVariable;
  4544. WHILE ~tooComplex & (localVariable # NIL) DO
  4545. variableDesignator := GetTemp(localVariable.type, FALSE);
  4546. currentMapper.Add(localVariable, variableDesignator, NIL);
  4547. localVariable := localVariable.nextVariable;
  4548. END;
  4549. IF ~tooComplex THEN
  4550. VisitStatementBlock(procedure.procedureScope.body);
  4551. SetLabel(currentInlineExit);
  4552. IF procedureType.returnType # NIL THEN
  4553. Designate(returnDesignator, result);
  4554. IF conditional THEN
  4555. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4556. ValueToCondition(result)
  4557. END;
  4558. END;
  4559. END;
  4560. currentMapper := prevMapper;
  4561. currentInlineExit := prevInlineExit;
  4562. currentIsInline := wasInline;
  4563. RETURN ~tooComplex
  4564. END InlineProcedureCall;
  4565. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4566. VAR
  4567. parameters: SyntaxTree.ExpressionList;
  4568. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4569. designator: SyntaxTree.Designator;
  4570. procedureType: SyntaxTree.ProcedureType;
  4571. formalParameter: SyntaxTree.Parameter;
  4572. operand, returnValue: Operand;
  4573. reg, size, mask, dest: IntermediateCode.Operand;
  4574. saved: RegisterEntry;
  4575. symbol: SyntaxTree.Symbol;
  4576. variable: SyntaxTree.Variable;
  4577. i, parametersSize, returnTypeSize : LONGINT;
  4578. structuredReturnType: BOOLEAN;
  4579. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4580. tempVariableDesignator: SyntaxTree.Designator;
  4581. gap, alignment: LONGINT; (*fld*)
  4582. (* TODO: remove unnecessary backup variables *)
  4583. oldResult: Operand;
  4584. oldCurrentScope: SyntaxTree.Scope;
  4585. oldArrayDestinationTag: IntermediateCode.Operand;
  4586. oldArrayDestinationDimension: LONGINT;
  4587. oldConstantDeclaration: SyntaxTree.Symbol;
  4588. oldDestination: IntermediateCode.Operand;
  4589. oldCurrentLoop: Label;
  4590. oldConditional: BOOLEAN;
  4591. oldTrueLabel, oldFalseLabel: Label;
  4592. oldLocked: BOOLEAN;
  4593. usedRegisters,oldUsedRegisters: RegisterEntry;
  4594. return: IntermediateCode.Operand;
  4595. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4596. arg: IntermediateCode.Operand;
  4597. dummy: IntermediateCode.Operand;
  4598. recordType: SyntaxTree.RecordType;
  4599. operatorSelectionProcedureOperand: Operand;
  4600. operatorSelectionProcedure: SyntaxTree.Procedure;
  4601. fingerPrint: SyntaxTree.FingerPrint;
  4602. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4603. identifierNumber: LONGINT;
  4604. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4605. procedure: SyntaxTree.Procedure;
  4606. PROCEDURE BackupGlobalState;
  4607. BEGIN
  4608. oldResult := result;
  4609. oldCurrentScope := currentScope;
  4610. oldArrayDestinationTag := arrayDestinationTag;
  4611. oldArrayDestinationDimension := arrayDestinationDimension;
  4612. oldConstantDeclaration := constantDeclaration;
  4613. oldDestination := destination;
  4614. oldCurrentLoop := currentLoop;
  4615. oldConditional := conditional;
  4616. oldTrueLabel := trueLabel;
  4617. oldFalseLabel := falseLabel;
  4618. oldLocked := locked;
  4619. oldUsedRegisters := usedRegisters
  4620. END BackupGlobalState;
  4621. PROCEDURE RestoreGlobalState;
  4622. BEGIN
  4623. result := oldResult;
  4624. currentScope := oldCurrentScope;
  4625. arrayDestinationTag := oldArrayDestinationTag;
  4626. arrayDestinationDimension := oldArrayDestinationDimension;
  4627. constantDeclaration := oldConstantDeclaration;
  4628. destination := oldDestination;
  4629. currentLoop := oldCurrentLoop;
  4630. conditional := oldConditional;
  4631. trueLabel := oldTrueLabel;
  4632. falseLabel := oldFalseLabel;
  4633. locked := oldLocked;
  4634. usedRegisters := oldUsedRegisters
  4635. END RestoreGlobalState;
  4636. (** do preparations before parameter push for array-structured object types (ASOTs):
  4637. if ASOT is passed as VAR parameter:
  4638. - allocate temporary variable of math array type
  4639. - copy contents of ASOT to be passed to temporary variable
  4640. - use temporary variable as the actual parameter instead
  4641. - 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)
  4642. **)
  4643. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4644. VAR
  4645. expression: SyntaxTree.Expression;
  4646. BEGIN
  4647. IF actualParameter IS SyntaxTree.Designator THEN
  4648. designator := actualParameter(SyntaxTree.Designator);
  4649. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4650. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4651. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4652. ASSERT(checker # NIL);
  4653. checker.SetCurrentScope(currentScope);
  4654. (* allocate temporary variable *)
  4655. ASSERT(actualParameter.type # NIL);
  4656. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4657. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4658. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4659. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4660. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4661. ASSERT(tempVariableDesignator.type # NIL);
  4662. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4663. (* copy math array stored in actual parameter to temporary variable *)
  4664. BackupGlobalState;
  4665. AssignMathArray(tempVariableDesignator, actualParameter);
  4666. RestoreGlobalState;
  4667. (* use temporary variable as actual parameter instead of the original one *)
  4668. actualParameter := tempVariableDesignator;
  4669. (* create write-back call and store it in linked list *)
  4670. (* create new list entry *)
  4671. IF firstWriteBackCall = NIL THEN
  4672. NEW(firstWriteBackCall);
  4673. currentWriteBackCall := firstWriteBackCall
  4674. ELSE
  4675. ASSERT(currentWriteBackCall # NIL);
  4676. NEW(currentWriteBackCall.next);
  4677. currentWriteBackCall := currentWriteBackCall.next
  4678. END;
  4679. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4680. ASSERT(expression.type = NIL);
  4681. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4682. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4683. (* prepare writeback for any other "normal" indexer *)
  4684. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4685. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4686. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4687. Assign(tempVariableDesignator, actualParameter);
  4688. actualParameter := tempVariableDesignator;
  4689. IF firstWriteBackCall = NIL THEN
  4690. NEW(firstWriteBackCall);
  4691. currentWriteBackCall := firstWriteBackCall
  4692. ELSE
  4693. ASSERT(currentWriteBackCall # NIL);
  4694. NEW(currentWriteBackCall.next);
  4695. currentWriteBackCall := currentWriteBackCall.next
  4696. END;
  4697. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4698. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4699. END
  4700. END
  4701. END PrepareParameter;
  4702. BEGIN
  4703. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4704. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4705. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4706. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4707. IF InlineProcedureCall(x) THEN
  4708. RETURN
  4709. ELSE
  4710. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4711. END
  4712. END;
  4713. END;
  4714. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4715. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4716. dest := destination; destination := emptyOperand;
  4717. SaveRegisters();ReleaseUsedRegisters(saved);
  4718. parameters := x.parameters;
  4719. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4720. (* an operator is called *)
  4721. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4722. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4723. (* check if a dynamic operator call should be performed *)
  4724. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4725. ELSE
  4726. isCallOfDynamicOperator := FALSE
  4727. END;
  4728. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4729. Emit(Push(position, ap));
  4730. END;
  4731. alignment := procedureType.stackAlignment;
  4732. IF alignment > 1 THEN
  4733. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4734. Emit(Mov(position,reg, sp));
  4735. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4736. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4737. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4738. Emit(And(position,sp, sp, mask));
  4739. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4740. Emit(Push(position,reg));
  4741. (*
  4742. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4743. Emit(Mov(position,mem,reg));
  4744. *)
  4745. ReleaseIntermediateOperand(reg);
  4746. END;
  4747. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4748. IntermediateCode.InitImmediate(mask,addressType,-16);
  4749. Emit(And(position,sp, sp, mask));
  4750. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4751. IF gap # 0 THEN
  4752. IntermediateCode.InitImmediate(size,addressType,gap);
  4753. Emit(Sub(position,sp,sp,size))
  4754. END;
  4755. END;
  4756. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4757. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4758. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4759. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4760. ELSE
  4761. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4762. END;
  4763. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4764. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4765. (* push ID *)
  4766. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4767. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4768. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4769. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4770. formalParameter := procedureType.firstParameter;
  4771. FOR i := 0 TO parameters.Length() - 1 DO
  4772. IF formalParameter.access # SyntaxTree.Hidden THEN
  4773. ASSERT(i < 2);
  4774. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4775. (* push pointer *)
  4776. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4777. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4778. (* add dereference *)
  4779. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4780. ReleaseIntermediateOperand(parameterBackups[i]);
  4781. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4782. END;
  4783. Emit(Push(position,parameterBackups[i]));
  4784. ReleaseIntermediateOperand(parameterBackups[i]);
  4785. (* push typetag *)
  4786. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4787. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4788. arg := TypeDescriptorAdr(recordType);
  4789. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4790. Emit(Push(position,arg));
  4791. ELSE
  4792. (* push 'NonPointer' *)
  4793. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4794. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4795. (* push fingerprint *)
  4796. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4797. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4798. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4799. END
  4800. END;
  4801. formalParameter := formalParameter.nextParameter
  4802. END;
  4803. (* for unary operators: complete the information for the second parameter *)
  4804. IF procedureType.numberParameters < 2 THEN
  4805. (* push 'NonPointer' *)
  4806. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4807. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4808. (* push 'NoType' *)
  4809. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4810. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4811. END;
  4812. (* call operator selection procedure *)
  4813. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4814. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4815. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4816. ReleaseOperand(operatorSelectionProcedureOperand);
  4817. (* use the address that the operator selection procedure returned as the target address of the call *)
  4818. InitOperand(operand, ModeValue);
  4819. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4820. Emit(Result(position,operand.op))
  4821. END
  4822. ELSE
  4823. Evaluate(x.left, operand)
  4824. END;
  4825. IF symbol IS SyntaxTree.Procedure THEN
  4826. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4827. Emit(Push(position,operand.tag));
  4828. ELSIF (procedureType.isDelegate) THEN
  4829. Emit(Push(position,operand.tag));
  4830. END;
  4831. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4832. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4833. Emit(Push(position,operand.tag));
  4834. END;
  4835. ELSE HALT(200);
  4836. END;
  4837. ReleaseIntermediateOperand(operand.tag);
  4838. operand.tag := emptyOperand;
  4839. (* determine if a structured return type is needed *)
  4840. structuredReturnType := StructuredReturnType(procedureType);
  4841. IF structuredReturnType THEN
  4842. IF resultDesignator # NIL THEN
  4843. d := resultDesignator;
  4844. ELSE
  4845. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4846. variable.SetUntraced(procedureType.hasUntracedReturn);
  4847. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4848. d.SetType(variable.type);
  4849. END;
  4850. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4851. Designate(d,returnValue);
  4852. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4853. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4854. Emit(Push(position,size));
  4855. Emit(Push(position,returnValue.op));
  4856. ReleaseOperand(returnValue);
  4857. ELSE
  4858. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4859. END;
  4860. END;
  4861. firstWriteBackCall := NIL; (* reset write-back call list *)
  4862. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4863. passByRegister := system.registerParameters > 0;
  4864. registerNumber := 0;
  4865. formalParameter := procedureType.lastParameter;
  4866. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4867. actualParameter := parameters.GetExpression(i);
  4868. PrepareParameter(actualParameter, formalParameter);
  4869. IF passByRegister & (i < system.registerParameters) THEN
  4870. IF ~PassInRegister(formalParameter) THEN
  4871. Error(actualParameter.position,"cannot be passed by register")
  4872. ELSE
  4873. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4874. END;
  4875. INC(registerNumber);
  4876. ELSE
  4877. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4878. END;
  4879. formalParameter := formalParameter.prevParameter;
  4880. END;
  4881. IF passByRegister & (registerNumber > 0) THEN
  4882. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4883. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4884. END;
  4885. ELSE
  4886. hasDynamicOperands := FALSE;
  4887. formalParameter := procedureType.firstParameter;
  4888. FOR i := 0 TO parameters.Length() - 1 DO
  4889. actualParameter := parameters.GetExpression(i);
  4890. IF formalParameter # NIL THEN (* TENTATIVE *)
  4891. PrepareParameter(actualParameter, formalParameter);
  4892. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4893. ASSERT(i < 2);
  4894. hasDynamicOperands := TRUE;
  4895. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4896. ELSE
  4897. IF passByRegister & (registerNumber > 0) THEN
  4898. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4899. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4900. END;
  4901. passByRegister := FALSE;
  4902. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4903. END;
  4904. formalParameter := formalParameter.nextParameter;
  4905. END;
  4906. END;
  4907. END;
  4908. IF symbol IS SyntaxTree.Procedure THEN
  4909. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4910. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4911. Emit(Push(position,reg));
  4912. ReleaseIntermediateOperand(reg);
  4913. END;
  4914. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4915. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4916. parametersSize := ParametersSize(system,procedureType,FALSE);
  4917. END;
  4918. ReleaseParameterRegisters();
  4919. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4920. Emit(Call(position,operand.op,0));
  4921. ELSE
  4922. Emit(Call(position,operand.op,parametersSize));
  4923. END;
  4924. ReleaseOperand(operand);
  4925. IF procedureType.noReturn THEN
  4926. EmitTrap(position,NoReturnTrap);
  4927. END;
  4928. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4929. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4930. Emit(Result(position,return));
  4931. END;
  4932. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4933. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4934. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4935. END;
  4936. size := IntermediateCode.Immediate(addressType,parametersSize);
  4937. Emit(Add(position,sp,sp,size));
  4938. END;
  4939. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4940. IF structuredReturnType THEN
  4941. (* stack pointer rewinding done by callee
  4942. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4943. Emit(Add(position,sp,sp,size));
  4944. *)
  4945. RestoreRegisters(saved);
  4946. InitOperand(result,ModeReference);
  4947. Symbol(variable,result);
  4948. ELSE
  4949. RestoreRegisters(saved);
  4950. InitOperand(result,ModeValue);
  4951. result.op := return;
  4952. END;
  4953. END;
  4954. IF alignment > 1 THEN
  4955. Emit(Pop(position,sp));
  4956. END;
  4957. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4958. Emit(Pop(position, ap));
  4959. END;
  4960. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4961. ValueToCondition(result);
  4962. END;
  4963. destination := dest;
  4964. (* perform all write-back calls in the list *)
  4965. BackupGlobalState;
  4966. currentWriteBackCall := firstWriteBackCall;
  4967. WHILE currentWriteBackCall # NIL DO
  4968. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4969. currentWriteBackCall := currentWriteBackCall.next
  4970. END;
  4971. RestoreGlobalState;
  4972. (* TENATIVE *)
  4973. (*
  4974. IF isOperatorCall THEN
  4975. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4976. END;
  4977. *)
  4978. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  4979. END VisitProcedureCallDesignator;
  4980. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  4981. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  4982. td: SyntaxTree.Symbol;
  4983. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  4984. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4985. BEGIN
  4986. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4987. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4988. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4989. variable.SetType(system.anyType);
  4990. scope.AddVariable(variable);
  4991. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  4992. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  4993. typeDeclaration.SetDeclaredType(hiddenPointerType);
  4994. typeDeclaration.SetScope(module.module.moduleScope);
  4995. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  4996. hiddenPointerType.SetState(SyntaxTree.Resolved);
  4997. END;
  4998. RETURN hiddenPointerType;
  4999. END GetHiddenPointerType;
  5000. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5001. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5002. BEGIN
  5003. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5004. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5005. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  5006. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  5007. scope.AddVariable(variable);
  5008. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5009. variable.SetType(system.anyType);
  5010. scope.AddVariable(variable);
  5011. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5012. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  5013. typeDeclaration.SetDeclaredType(delegatePointerType);
  5014. typeDeclaration.SetScope(module.module.moduleScope);
  5015. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5016. delegatePointerType.SetState(SyntaxTree.Resolved);
  5017. END;
  5018. RETURN delegatePointerType
  5019. END GetDelegateType;
  5020. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5021. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5022. such as in VAR a: RECORD ... END;
  5023. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5024. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5025. *)
  5026. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5027. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5028. BEGIN (* no code emission *)
  5029. source := NIL;
  5030. x := x.resolved;
  5031. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5032. x := GetHiddenPointerType();
  5033. ELSIF IsDelegate(x) THEN
  5034. x := GetDelegateType();
  5035. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5036. ELSE HALT(200);
  5037. END;
  5038. td := x.typeDeclaration;
  5039. IF td = NIL THEN
  5040. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5041. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5042. ASSERT(td # NIL);
  5043. END;
  5044. IF newObjectFile THEN
  5045. GetCodeSectionNameForSymbol(td,name);
  5046. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5047. meta.CheckTypeDeclaration(x);
  5048. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5049. ELSE
  5050. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5051. END;
  5052. IF backend.cooperative THEN
  5053. offset := 0;
  5054. ELSE
  5055. IF x IS SyntaxTree.CellType THEN
  5056. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5057. IF baseRecord = NIL THEN
  5058. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5059. ELSE
  5060. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5061. END;
  5062. ELSE
  5063. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5064. END;
  5065. END;
  5066. ELSE
  5067. offset := 0;
  5068. source := module.allSections.FindBySymbol(td); (*TODO*)
  5069. IF source = NIL THEN
  5070. null := 0;
  5071. GetCodeSectionNameForSymbol(td,name);
  5072. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5073. IntermediateCode.InitImmediate(op,addressType,0);
  5074. source(IntermediateCode.Section).Emit(Data(position,op));
  5075. source.SetReferenced(FALSE)
  5076. ELSE
  5077. name := source.name;
  5078. END;
  5079. END;
  5080. RETURN td
  5081. END GetBackendType;
  5082. BEGIN
  5083. (*td := t.typeDeclaration;*)
  5084. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5085. (*
  5086. IF t IS SyntaxTree.PointerType THEN
  5087. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5088. ELSE
  5089. source := GetBackendType(t);
  5090. END;
  5091. *)
  5092. IF newObjectFile THEN
  5093. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5094. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5095. IntermediateCode.SetOffset(res,offset);
  5096. ELSE
  5097. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5098. END;
  5099. (*
  5100. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5101. make memory should be used when tag is used, not earlier
  5102. *)
  5103. RETURN res
  5104. END TypeDescriptorAdr;
  5105. (*
  5106. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5107. VAR result: IntermediateCode.Operand;
  5108. BEGIN
  5109. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5110. operand.tag := TypeDescriptorAdr(operand.type);
  5111. IntermediateCode.MakeMemory(operand.tag,addressType);
  5112. UseIntermediateOperand(operand.tag);
  5113. END;
  5114. END MakeTypeTag;
  5115. *)
  5116. PROCEDURE ProfilerInit;
  5117. VAR reg: IntermediateCode.Operand;
  5118. BEGIN
  5119. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5120. Emit(Call(position,reg,0));
  5121. END ProfilerInit;
  5122. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5123. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5124. BEGIN
  5125. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5126. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5127. THEN
  5128. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5129. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5130. Emit(Push(position,reg));
  5131. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5132. Emit(Push(position,reg));
  5133. StaticCallOperand(result,procedure);
  5134. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5135. ReleaseOperand(result);
  5136. END;
  5137. END ProfilerEnterExit;
  5138. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5139. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5140. BEGIN
  5141. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5142. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5143. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5144. profileInit.Emit(Push(position,reg));
  5145. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5146. profileInit.Emit(Push(position,reg));
  5147. NEW(string, LEN(name)); COPY(name, string^);
  5148. sv := SyntaxTree.NewStringValue(-1,string);
  5149. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5150. sv.SetType(type);
  5151. Designate(sv,result);
  5152. profileInit.Emit(Push(position,result.tag));
  5153. profileInit.Emit(Push(position,result.op));
  5154. StaticCallOperand(result,procedure);
  5155. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5156. ReleaseOperand(result);
  5157. END;
  5158. END ProfilerAddProcedure;
  5159. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5160. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5161. BEGIN
  5162. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5163. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5164. profileInit.Emit(Push(position,reg));
  5165. profileInitPatchPosition := profileInit.pc;
  5166. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5167. NEW(string, LEN(name)); COPY(name, string^);
  5168. sv := SyntaxTree.NewStringValue(-1,string);
  5169. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5170. sv.SetType(type);
  5171. Designate(sv,result);
  5172. profileInit.Emit(Push(position,result.tag));
  5173. profileInit.Emit(Push(position,result.op));
  5174. StaticCallOperand(result,procedure);
  5175. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5176. ReleaseOperand(result);
  5177. END;
  5178. END ProfilerAddModule;
  5179. PROCEDURE ProfilerPatchInit;
  5180. VAR reg: IntermediateCode.Operand;
  5181. BEGIN
  5182. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5183. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5184. EmitLeave(profileInit,position,0);
  5185. profileInit.Emit(Exit(position,0,0));
  5186. END ProfilerPatchInit;
  5187. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5188. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5189. VAR
  5190. id: LONGINT;
  5191. leftType, rightType: SyntaxTree.Type;
  5192. procedureType: SyntaxTree.ProcedureType;
  5193. runtimeProcedure: SyntaxTree.Procedure;
  5194. runtimeProcedureOperand, operatorOperand: Operand;
  5195. kind: SET;
  5196. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5197. VAR
  5198. arg: IntermediateCode.Operand;
  5199. recordType: SyntaxTree.RecordType;
  5200. fingerPrint: SyntaxTree.FingerPrint;
  5201. BEGIN
  5202. IF type = NIL THEN
  5203. (* no type: push 'NoType' *)
  5204. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5205. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5206. ELSIF IsStrictlyPointerToRecord(type) THEN
  5207. (* pointer to record type: push typetag *)
  5208. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5209. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5210. arg := TypeDescriptorAdr(recordType);
  5211. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5212. ELSE
  5213. (* non-pointer to record type: push fingerprint *)
  5214. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5215. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5216. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5217. END;
  5218. operatorInitializationCodeSection.Emit(Push(position,arg))
  5219. END PushTypeInfo;
  5220. BEGIN
  5221. ASSERT(operatorInitializationCodeSection # NIL);
  5222. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5223. procedureType := operator.type(SyntaxTree.ProcedureType);
  5224. (* determine types *)
  5225. leftType := procedureType.firstParameter.type;
  5226. IF procedureType.numberParameters = 2 THEN
  5227. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5228. rightType := procedureType.firstParameter.nextParameter.type;
  5229. ELSE
  5230. rightType := NIL
  5231. END;
  5232. (* determine operator kind *)
  5233. IF IsStrictlyPointerToRecord(leftType) THEN
  5234. kind := {LhsIsPointer}
  5235. ELSE
  5236. kind := {}
  5237. END;
  5238. IF IsStrictlyPointerToRecord(rightType) THEN
  5239. kind := kind + {RhsIsPointer}
  5240. END;
  5241. IF kind # {} THEN (* TODO: to be removed later on *)
  5242. (* at least one of the types is a pointer to record *)
  5243. (* emit a code that registers this specific operator in the runtime *)
  5244. dump := operatorInitializationCodeSection.comments;
  5245. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5246. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5247. (* push ID *)
  5248. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5249. id := Global.GetSymbol(module.module.case, operator.name);
  5250. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5251. (* push kind *)
  5252. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5253. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5254. (* push type infos *)
  5255. PushTypeInfo(leftType);
  5256. PushTypeInfo(rightType);
  5257. (* push operator address *)
  5258. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5259. StaticCallOperand(operatorOperand, operator);
  5260. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5261. ReleaseOperand(operatorOperand);
  5262. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5263. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5264. ReleaseOperand(runtimeProcedureOperand)
  5265. END
  5266. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5267. END
  5268. END RegisterDynamicOperator;
  5269. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5270. VAR
  5271. traceModule: SyntaxTree.Module;
  5272. procedure: SyntaxTree.Procedure;
  5273. procedureVariable: SyntaxTree.Variable;
  5274. s,msg: Basic.MessageString;
  5275. res: Operand;
  5276. i: LONGINT;
  5277. sv: SyntaxTree.StringValue;
  5278. type: SyntaxTree.Type;
  5279. recordType: SyntaxTree.RecordType;
  5280. printout: Printout.Printer;
  5281. stringWriter: Streams.StringWriter;
  5282. expression: SyntaxTree.Expression;
  5283. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5284. BEGIN
  5285. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5286. IF procedure = NIL THEN
  5287. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5288. END;
  5289. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5290. s := "procedure ";
  5291. Strings.Append(s,backend.traceModuleName);
  5292. Strings.Append(s,".");
  5293. Strings.Append(s,procedureName);
  5294. Strings.Append(s," not present");
  5295. Error(position,s);
  5296. RETURN FALSE
  5297. ELSE
  5298. RETURN TRUE
  5299. END;
  5300. END GetProcedure;
  5301. PROCEDURE CallProcedure;
  5302. VAR size: LONGINT;
  5303. BEGIN
  5304. IF procedure # NIL THEN
  5305. StaticCallOperand(result,procedure);
  5306. size := ProcedureParametersSize(system,procedure);
  5307. ELSE
  5308. Symbol(procedureVariable, result);
  5309. LoadValue(result, procedureVariable.type.resolved);
  5310. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5311. END;
  5312. Emit(Call(position,result.op,size));
  5313. END CallProcedure;
  5314. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5315. VAR res: Operand; string: SyntaxTree.String;
  5316. BEGIN
  5317. IF GetProcedure("String") THEN
  5318. NEW(string, LEN(s)); COPY(s, string^);
  5319. sv := SyntaxTree.NewStringValue(-1,string);
  5320. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5321. sv.SetType(type);
  5322. Designate(sv,res);
  5323. Emit(Push(position,res.tag));
  5324. Emit(Push(position,res.op));
  5325. ReleaseOperand(res);
  5326. CallProcedure;
  5327. END;
  5328. END String;
  5329. PROCEDURE Integer(op: IntermediateCode.Operand);
  5330. BEGIN
  5331. IF GetProcedure("Int") THEN
  5332. Emit(Push(position,op));
  5333. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5334. CallProcedure;
  5335. END;
  5336. END Integer;
  5337. PROCEDURE Float(op: IntermediateCode.Operand);
  5338. BEGIN
  5339. IF GetProcedure("HIntHex") THEN
  5340. Emit(Push(position,op));
  5341. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5342. CallProcedure;
  5343. END;
  5344. END Float;
  5345. PROCEDURE Set(op: IntermediateCode.Operand);
  5346. BEGIN
  5347. IF GetProcedure("Set") THEN
  5348. Emit(Push(position,op));
  5349. (*
  5350. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5351. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5352. *)
  5353. CallProcedure;
  5354. END;
  5355. END Set;
  5356. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5357. BEGIN
  5358. IF GetProcedure("Boolean") THEN
  5359. Emit(Push(position,op));
  5360. CallProcedure;
  5361. END;
  5362. END Boolean;
  5363. PROCEDURE Char(op: IntermediateCode.Operand);
  5364. BEGIN
  5365. IF GetProcedure("Char") THEN
  5366. Emit(Push(position,op));
  5367. CallProcedure;
  5368. END;
  5369. END Char;
  5370. PROCEDURE Address(op: IntermediateCode.Operand);
  5371. BEGIN
  5372. IF GetProcedure("Address") THEN
  5373. Emit(Push(position,op));
  5374. CallProcedure;
  5375. END;
  5376. END Address;
  5377. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5378. BEGIN
  5379. IF GetProcedure("String") THEN
  5380. Emit(Push(position,tag));
  5381. Emit(Push(position,op));
  5382. CallProcedure;
  5383. END;
  5384. END StringOperand;
  5385. PROCEDURE Ln;
  5386. BEGIN
  5387. IF GetProcedure("Ln") THEN
  5388. CallProcedure;
  5389. END;
  5390. END Ln;
  5391. BEGIN
  5392. IF backend.traceModuleName = "" THEN RETURN END;
  5393. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5394. IF GetProcedure("Enter") THEN
  5395. CallProcedure
  5396. END;
  5397. NEW(stringWriter,LEN(s));
  5398. FOR i := 0 TO x.Length()-1 DO
  5399. msg := "";
  5400. expression := x.GetExpression(i);
  5401. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5402. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5403. ELSE
  5404. Global.GetModuleName(module.module, s);
  5405. END;
  5406. IF i = 0 THEN
  5407. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5408. stringWriter.String(":");
  5409. END;
  5410. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5411. IF ~(expression IS SyntaxTree.StringValue) THEN
  5412. printout.Expression(expression);
  5413. stringWriter.Get(s);
  5414. Strings.Append(msg,s);
  5415. Strings.Append(msg,"= ");
  5416. ELSE stringWriter.Get(s); (* remove from string writer *)
  5417. Strings.Append(msg, s);
  5418. END;
  5419. String(msg);
  5420. IF SemanticChecker.IsStringType(expression.type) THEN
  5421. Designate(expression,res);
  5422. StringOperand(res.op,res.tag);
  5423. ELSE
  5424. Evaluate(expression,res);
  5425. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5426. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5427. Convert(res.op,int64);
  5428. END;
  5429. Integer(res.op);
  5430. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5431. Boolean(res.op);
  5432. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5433. Set(res.op);
  5434. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5435. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5436. Convert(res.op,float64);
  5437. END;
  5438. Float(res.op);
  5439. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5440. Char(res.op);
  5441. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5442. Address(res.op);String("H");
  5443. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5444. Address(res.op);String("H");
  5445. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5446. Address(res.op);String("H");
  5447. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5448. Address(res.op);String("H");
  5449. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5450. String("NIL");
  5451. ELSE HALT(200);
  5452. END;
  5453. END;
  5454. ReleaseOperand(res);
  5455. String("; ");
  5456. END;
  5457. IF GetProcedure("Exit") THEN
  5458. CallProcedure
  5459. ELSE
  5460. Ln;
  5461. END;
  5462. END;
  5463. END SystemTrace;
  5464. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5465. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5466. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5467. BEGIN
  5468. type := type.resolved;
  5469. IF type IS SyntaxTree.RecordType THEN
  5470. WITH type: SyntaxTree.RecordType DO
  5471. baseType := type.baseType;
  5472. IF baseType # NIL THEN
  5473. baseType := baseType.resolved;
  5474. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5475. InitFields(baseType,adr,offset);
  5476. END;
  5477. variable := type.recordScope.firstVariable;
  5478. WHILE variable # NIL DO
  5479. IF variable.initializer # NIL THEN
  5480. Evaluate(variable.initializer,initializerOp);
  5481. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5482. Emit(Mov(position,mem,initializerOp.op));
  5483. ReleaseOperand(initializerOp);
  5484. ReleaseIntermediateOperand(mem);
  5485. END;
  5486. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5487. variable := variable.nextVariable
  5488. END;
  5489. END;
  5490. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5491. WITH type: SyntaxTree.ArrayType DO
  5492. IF type.form = SyntaxTree.Static THEN
  5493. baseType := type.arrayBase;
  5494. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5495. FOR i := 0 TO type.staticLength-1 DO
  5496. InitFields(baseType,adr,offset+i*size);
  5497. END;
  5498. END;
  5499. END;
  5500. ELSIF type IS SyntaxTree.MathArrayType THEN
  5501. WITH type: SyntaxTree.MathArrayType DO
  5502. IF type.form = SyntaxTree.Open THEN
  5503. dim := DynamicDim(type);
  5504. imm := IntermediateCode.Immediate(addressType,dim);
  5505. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5506. baseType := SemanticChecker.ArrayBase(type,dim);
  5507. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5508. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5509. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5510. ReleaseIntermediateOperand(imm);
  5511. (* flags remain empty (=0) for open array *)
  5512. ELSIF type.form = SyntaxTree.Static THEN
  5513. baseType := type.arrayBase;
  5514. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5515. FOR i := 0 TO type.staticLength-1 DO
  5516. InitFields(baseType,adr,offset+i*size);
  5517. END;
  5518. END;
  5519. END;
  5520. END;
  5521. END InitFields;
  5522. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5523. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5524. BEGIN
  5525. type := variable.type.resolved;
  5526. IF (type IS SyntaxTree.MathArrayType) THEN
  5527. WITH type: SyntaxTree.MathArrayType DO
  5528. IF type.form = SyntaxTree.Open THEN
  5529. Symbol(variable,operand);
  5530. InitFields(type, operand.tag,0);
  5531. ELSIF type.form = SyntaxTree.Tensor THEN
  5532. Symbol(variable, operand);
  5533. MakeMemory(tmp,operand.op,addressType,0);
  5534. ReleaseOperand(operand);
  5535. Emit(Mov(position,tmp, nil ) );
  5536. ReleaseIntermediateOperand(tmp);
  5537. END;
  5538. END;
  5539. ELSE
  5540. Symbol(variable,operand);
  5541. IF variable.initializer # NIL THEN
  5542. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5543. reference.SetType(variable.type.resolved);
  5544. reference.SetAssignable(TRUE);
  5545. Assign(reference,variable.initializer);
  5546. END;
  5547. InitFields(type, operand.op,0);
  5548. ReleaseOperand(operand);
  5549. END;
  5550. END InitVariable;
  5551. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5552. VAR end: Label;
  5553. BEGIN
  5554. IF type.form = SyntaxTree.Tensor THEN
  5555. InitOperand(result,ModeValue);
  5556. ReuseCopy(result.op,base);
  5557. end := NewLabel();
  5558. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5559. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5560. SetLabel(end);
  5561. Convert(result.op,int32);
  5562. ELSE
  5563. InitOperand(result,ModeValue);
  5564. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5565. END
  5566. END MathArrayDim;
  5567. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5568. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5569. BEGIN
  5570. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5571. MakeMemory(mem,base,addressType,offset);
  5572. Emit(Mov(position,mem,value));
  5573. ReleaseIntermediateOperand(mem);
  5574. END PutMathArrayField;
  5575. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5576. VAR mem: IntermediateCode.Operand;
  5577. BEGIN
  5578. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5579. MakeMemory(mem,base,addressType,offset);
  5580. Emit(Mov(position,mem,value));
  5581. ReleaseIntermediateOperand(mem);
  5582. END PutMathArrayFieldOffset;
  5583. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5584. BEGIN
  5585. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5586. MakeMemory(value,base,addressType,offset);
  5587. END GetMathArrayField;
  5588. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5589. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5590. BEGIN
  5591. IF incr THEN
  5592. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5593. ELSE
  5594. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5595. END;
  5596. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5597. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5598. ELSE
  5599. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5600. Emit(Mov(position,reg,dim));
  5601. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5602. Emit(Add(position,reg,reg,base));
  5603. MakeMemory(mem, reg, addressType, offset);
  5604. ReleaseIntermediateOperand(reg);
  5605. Emit(Mov(position,mem,value));
  5606. ReleaseIntermediateOperand(mem);
  5607. END;
  5608. END PutMathArrayLenOrIncr;
  5609. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5610. BEGIN
  5611. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5612. END PutMathArrayLength;
  5613. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5614. BEGIN
  5615. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5616. END PutMathArrayIncrement;
  5617. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5618. BEGIN
  5619. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5620. END GetMathArrayIncrement;
  5621. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5622. BEGIN
  5623. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5624. END GetMathArrayLength;
  5625. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5626. VAR dimOp: IntermediateCode.Operand;
  5627. BEGIN
  5628. dimOp := IntermediateCode.Immediate(int32, dim);
  5629. GetMathArrayLength(type, operand, dimOp, check, result);
  5630. END GetMathArrayLengthAt;
  5631. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5632. VAR dimOp: IntermediateCode.Operand;
  5633. BEGIN
  5634. dimOp := IntermediateCode.Immediate(int32, dim);
  5635. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5636. END GetMathArrayIncrementAt;
  5637. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5638. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5639. offset: LONGINT;
  5640. BEGIN
  5641. IF increment THEN
  5642. offset := MathIncrOffset;
  5643. ELSE
  5644. offset := MathLenOffset;
  5645. END;
  5646. INC(offset,operand.dimOffset*2);
  5647. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5648. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5649. END;
  5650. (* static dimension *)
  5651. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5652. IF check & (type.form = SyntaxTree.Tensor) THEN
  5653. DimensionCheck(operand.tag,dim,BrltL);
  5654. END;
  5655. val := SHORT(dim.intValue);
  5656. IF type.form # SyntaxTree.Tensor THEN
  5657. t := SemanticChecker.ArrayBase(type,val);
  5658. type := t.resolved(SyntaxTree.MathArrayType);
  5659. IF type.form = SyntaxTree.Static THEN
  5660. IF increment THEN
  5661. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5662. ELSE
  5663. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5664. END;
  5665. InitOperand(result,ModeValue);
  5666. result.op := res;
  5667. RETURN;
  5668. END;
  5669. END;
  5670. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5671. MakeMemory(res,operand.tag,addressType,offset);
  5672. (*
  5673. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5674. *)
  5675. InitOperand(result,ModeValue);
  5676. result.op := res;
  5677. ELSE
  5678. Convert(dim,addressType);
  5679. IF check THEN
  5680. IF type.form = SyntaxTree.Tensor THEN
  5681. DimensionCheck(operand.tag,dim,BrltL);
  5682. ELSIF isUnchecked THEN (* do nothing *)
  5683. ELSE
  5684. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5685. END;
  5686. END;
  5687. end := NewLabel(); next := NIL;
  5688. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5689. Emit(Mov(position,res,dim));
  5690. Convert(res,int32);
  5691. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5692. WHILE t IS SyntaxTree.MathArrayType DO
  5693. type := t(SyntaxTree.MathArrayType);
  5694. IF type.form = SyntaxTree.Static THEN
  5695. imm := IntermediateCode.Immediate(int32,val);
  5696. next := NewLabel();
  5697. BrneL(next,imm,res);
  5698. IF increment THEN
  5699. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5700. ELSE
  5701. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5702. END;
  5703. Emit(MovReplace(position,res,imm));
  5704. BrL(end);
  5705. ELSE hasDynamicPart := TRUE;
  5706. END;
  5707. t := type.arrayBase.resolved;
  5708. val := val + 1;
  5709. IF next # NIL THEN SetLabel(next) END;
  5710. END;
  5711. IF hasDynamicPart THEN
  5712. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5713. Emit(Mov(position,res2,dim));
  5714. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5715. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5716. Emit(Add(position,res2,res2,imm));
  5717. Emit(Add(position,res2,res2,operand.tag));
  5718. IntermediateCode.MakeMemory(res2,int32);
  5719. Emit(MovReplace(position,res,res2));
  5720. ReleaseIntermediateOperand(res2);
  5721. END;
  5722. SetLabel(end);
  5723. Convert(res,int32);
  5724. InitOperand(result,ModeValue);
  5725. result.op := res;
  5726. END;
  5727. END MathArrayLenOrIncr;
  5728. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5729. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5730. offset: LONGINT;
  5731. BEGIN
  5732. offset := operand.dimOffset+DynamicDim(type)-1;
  5733. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5734. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5735. val := SHORT(dim.intValue);
  5736. t := SemanticChecker.ArrayBase(type,val);
  5737. type := t.resolved(SyntaxTree.ArrayType);
  5738. IF type.form = SyntaxTree.Static THEN
  5739. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5740. ELSE
  5741. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5742. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5743. END;
  5744. UseIntermediateOperand(res);
  5745. InitOperand(result,ModeValue);
  5746. result.op := res;
  5747. ELSE
  5748. Convert(dim,addressType);
  5749. IF ~isUnchecked THEN
  5750. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5751. END;
  5752. end := NewLabel(); next := NIL;
  5753. (* ReuseCopy(dim,res); *)
  5754. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5755. Emit(Mov(position,res,dim));
  5756. Convert(res,int32);
  5757. Convert(res,int32);
  5758. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5759. WHILE t IS SyntaxTree.ArrayType DO
  5760. type := t(SyntaxTree.ArrayType);
  5761. IF type.form = SyntaxTree.Static THEN
  5762. imm := IntermediateCode.Immediate(int32,val);
  5763. next := NewLabel();
  5764. BrneL(next,imm,res);
  5765. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5766. Emit(MovReplace(position,res,imm));
  5767. BrL(end);
  5768. ELSE hasDynamicPart := TRUE;
  5769. END;
  5770. t := type.arrayBase.resolved;
  5771. val := val + 1;
  5772. IF next # NIL THEN SetLabel(next) END;
  5773. END;
  5774. IF hasDynamicPart THEN
  5775. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5776. Convert(res2,addressType);
  5777. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5778. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5779. Emit(Sub(position,res2,imm,res2));
  5780. Emit(Add(position,res2,res2,operand.tag));
  5781. IntermediateCode.MakeMemory(res2,int32);
  5782. Emit(MovReplace(position,res,res2));
  5783. ReleaseIntermediateOperand(res2);
  5784. END;
  5785. SetLabel(end);
  5786. Convert(res,int32);
  5787. InitOperand(result,ModeValue);
  5788. result.op := res;
  5789. END;
  5790. END ArrayLen;
  5791. (**
  5792. create a temporary variable in current scope
  5793. **)
  5794. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5795. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5796. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5797. BEGIN
  5798. IF ~register THEN
  5799. variable := temporaries.GetFreeVariable(type, index);
  5800. ELSE
  5801. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5802. END;
  5803. scope := currentScope;
  5804. IF variable = NIL THEN
  5805. COPY("@hiddenIRVar",string);
  5806. Basic.AppendNumber(string,index);
  5807. name := SyntaxTree.NewIdentifier(string);
  5808. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5809. variable.SetType(type);
  5810. variable.SetAccess(SyntaxTree.Hidden);
  5811. IF ~register THEN
  5812. temporaries.AddVariable(variable);
  5813. IF scope.lastVariable # NIL THEN
  5814. offset := scope.lastVariable.offsetInBits;
  5815. ELSE
  5816. offset := 0;
  5817. END;
  5818. DEC(offset,system.SizeOf(variable.type));
  5819. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5820. variable(SyntaxTree.Variable).SetOffset(offset);
  5821. scope.AddVariable(variable(SyntaxTree.Variable));
  5822. scope.EnterSymbol(variable, duplicate);
  5823. ASSERT(~duplicate);
  5824. InitVariable(variable(SyntaxTree.Variable));
  5825. ELSE
  5826. variable.SetUseRegister(TRUE);
  5827. variable(SyntaxTree.Variable).SetOffset(0);
  5828. END;
  5829. ELSE
  5830. InitVariable(variable(SyntaxTree.Variable));
  5831. (*
  5832. ASSERT(variable.type.resolved = type.resolved)
  5833. *)
  5834. END;
  5835. RETURN variable(SyntaxTree.Variable)
  5836. END GetTemporaryVariable;
  5837. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5838. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5839. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5840. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5841. i: LONGINT; duplicate: BOOLEAN;
  5842. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5843. VAR variable: SyntaxTree.Variable;
  5844. BEGIN
  5845. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5846. variable.SetType(type);
  5847. recordScope.AddVariable(variable);
  5848. END AddVariable;
  5849. BEGIN
  5850. name := "@ArrayDescriptor";
  5851. Basic.AppendNumber(name,dimensions);
  5852. identifier := SyntaxTree.NewIdentifier(name);
  5853. parentScope := module.module.moduleScope;
  5854. symbol := parentScope.FindSymbol(identifier);
  5855. IF symbol # NIL THEN
  5856. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5857. type := typeDeclaration.declaredType;
  5858. ELSE
  5859. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5860. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5861. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5862. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5863. recordType.SetTypeDeclaration(typeDeclaration);
  5864. recordType.SetState(SyntaxTree.Resolved);
  5865. typeDeclaration.SetDeclaredType(recordType);
  5866. AddVariable("@ptr",system.anyType);
  5867. AddVariable("@adr",system.addressType);
  5868. AddVariable("@flags",system.addressType);
  5869. AddVariable("@dim",system.addressType);
  5870. AddVariable("@elementSize",system.addressType);
  5871. FOR i := 0 TO dimensions-1 DO
  5872. name := "@len";
  5873. Basic.AppendNumber(name,i);
  5874. AddVariable(name,system.addressType);
  5875. name := "@incr";
  5876. Basic.AppendNumber(name,i);
  5877. AddVariable(name,system.addressType);
  5878. END;
  5879. parentScope.AddTypeDeclaration(typeDeclaration);
  5880. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5881. ASSERT(~duplicate);
  5882. type := recordType;
  5883. END;
  5884. RETURN type
  5885. END GetMathArrayDescriptorType;
  5886. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5887. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5888. BEGIN
  5889. type := GetMathArrayDescriptorType(dimensions);
  5890. Emit(Push(position,op.op));
  5891. (* push type descriptor *)
  5892. reg := TypeDescriptorAdr(type);
  5893. IF ~newObjectFile THEN
  5894. IntermediateCode.MakeMemory(reg,addressType);
  5895. END;
  5896. Emit(Push(position,reg));
  5897. ReleaseIntermediateOperand(reg);
  5898. (* push realtime flag: false by default *)
  5899. Emit(Push(position,false));
  5900. CallThis(position,"Heaps","NewRec",3);
  5901. END NewMathArrayDescriptor;
  5902. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5903. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5904. BEGIN
  5905. NEW(string, LEN(s)); COPY(s, string^);
  5906. sv := SyntaxTree.NewStringValue(-1,string);
  5907. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5908. sv.SetType(type);
  5909. Designate(sv,res);
  5910. Emit(Push(position,res.tag));
  5911. Emit(Push(position,res.op));
  5912. ReleaseOperand(res);
  5913. END PushConstString;
  5914. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5915. BEGIN
  5916. IF b THEN
  5917. Emit(Push(-1, true));
  5918. ELSE
  5919. Emit(Push(-1, false));
  5920. END;
  5921. END PushConstBoolean;
  5922. PROCEDURE PushConstSet(v: SET);
  5923. VAR value: SyntaxTree.Value; op: Operand;
  5924. BEGIN
  5925. value := SyntaxTree.NewSetValue(-1, v);
  5926. value.SetType(system.setType);
  5927. Evaluate(value, op);
  5928. Emit(Push(-1, op.op));
  5929. ReleaseOperand(op);
  5930. END PushConstSet;
  5931. PROCEDURE PushConstInteger(v: LONGINT);
  5932. VAR value: SyntaxTree.Value; op: Operand;
  5933. BEGIN
  5934. value := SyntaxTree.NewIntegerValue(-1, v);
  5935. value.SetType(system.longintType);
  5936. Evaluate(value, op);
  5937. Emit(Push(-1, op.op));
  5938. ReleaseOperand(op);
  5939. END PushConstInteger;
  5940. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5941. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5942. section: IntermediateCode.Section;
  5943. BEGIN
  5944. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5945. Global.GetSymbolSegmentedName(symbol, name);
  5946. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5947. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5948. procedure.SetScope(moduleScope);
  5949. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5950. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5951. procedure.SetAccess(SyntaxTree.Hidden);
  5952. currentScope := procedureScope;
  5953. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5954. EmitEnter(section, -1,NIL,0,0,0);
  5955. RETURN section;
  5956. END OpenInitializer;
  5957. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5958. BEGIN
  5959. EmitLeave(section, 0, 0 );
  5960. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5961. section := prev;
  5962. END CloseInitializer;
  5963. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5964. VAR name: SyntaxTree.IdentifierString;
  5965. variable: SyntaxTree.Variable;
  5966. type: SyntaxTree.Type;
  5967. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5968. BEGIN
  5969. InitOperand(op,ModeReference);
  5970. op.op := fp;
  5971. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5972. Dereference(op, x, FALSE);
  5973. result := op;
  5974. Symbol(symbol, op);
  5975. END Field;
  5976. PROCEDURE Direction(direction: LONGINT): SET;
  5977. BEGIN
  5978. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  5979. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  5980. ELSE HALT(100);
  5981. END;
  5982. END Direction;
  5983. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  5984. VAR name: ARRAY 256 OF CHAR; op: Operand;
  5985. BEGIN
  5986. Field(port, op);
  5987. ToMemory(op.op,addressType,0);
  5988. Emit(Push(-1, op.op));
  5989. ReleaseOperand(op);
  5990. Basic.GetString(modifier.identifier, name);
  5991. PushConstString(name);
  5992. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  5993. ASSERT(SemanticChecker.IsStringType(value.type));
  5994. Designate(value, op);
  5995. Emit(Push(modifier.position, op.tag));
  5996. Emit(Push(modifier.position, op.op));
  5997. ReleaseOperand(op);
  5998. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  5999. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6000. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6001. Evaluate(value, op);
  6002. Emit(Push(modifier.position, op.op));
  6003. ReleaseOperand(op);
  6004. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6005. ELSE
  6006. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6007. END;
  6008. END AddPortProperty;
  6009. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  6010. VAR modifier: SyntaxTree.Modifier;
  6011. BEGIN
  6012. modifier := variable.modifiers;
  6013. WHILE modifier # NIL DO
  6014. AddPortProperty(variable,modifier, modifier.expression);
  6015. modifier := modifier.nextModifier;
  6016. END;
  6017. END AddPortProperties;
  6018. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6019. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6020. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6021. PROCEDURE PushLens(type: SyntaxTree.Type);
  6022. BEGIN
  6023. IF type IS SyntaxTree.ArrayType THEN
  6024. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6025. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6026. Emit(Push(-1, op.op));
  6027. ReleaseOperand(op);
  6028. INC(dim);
  6029. ELSE
  6030. baseType := type;
  6031. END;
  6032. END PushLens;
  6033. BEGIN
  6034. (* cell *)
  6035. IF variable.type IS SyntaxTree.ArrayType THEN
  6036. type := variable.type;
  6037. dim := 0;
  6038. PushLens(type);
  6039. portType := baseType.resolved(SyntaxTree.PortType);
  6040. ELSE
  6041. portType := variable.type(SyntaxTree.PortType);
  6042. END;
  6043. PushSelfPointer();
  6044. (* port *)
  6045. Field(variable, op);
  6046. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6047. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6048. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6049. Designate(d, op);*)
  6050. Emit(Push(-1, op.op));
  6051. ReleaseOperand(op);
  6052. (* name *)
  6053. PushConstString(name);
  6054. (* inout *)
  6055. PushConstSet(Direction(portType.direction));
  6056. (* width *)
  6057. PushConstInteger(portType.sizeInBits);
  6058. IF variable.type IS SyntaxTree.PortType THEN
  6059. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6060. AddPortProperties(variable);
  6061. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6062. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6063. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6064. Emit(Add(position,reg, sp, size));
  6065. (* dim *)
  6066. PushConstInteger(dim);
  6067. (* len array *)
  6068. Emit(Push(position, reg));
  6069. ReleaseIntermediateOperand(reg);
  6070. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6071. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6072. Emit(Add(position, sp,sp, size));
  6073. END;
  6074. END Variable;
  6075. BEGIN
  6076. IF backend.cellsAreObjects THEN
  6077. variable := x.cellScope.firstVariable;
  6078. WHILE (variable # NIL) DO
  6079. type := variable.type.resolved;
  6080. WHILE (type IS SyntaxTree.ArrayType) DO
  6081. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6082. END;
  6083. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6084. (*!!! check port arrays !! *)
  6085. Global.GetSymbolName(variable,name);
  6086. Variable(name,variable);
  6087. END;
  6088. variable := variable.nextVariable;
  6089. END;
  6090. ELSE HALT(200)
  6091. END;
  6092. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6093. (*
  6094. x.typeDeclaration.GetName(componentName);
  6095. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6096. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6097. instanceType.SetDataMemorySize(dataMemorySize);
  6098. END;
  6099. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6100. instanceType.SetInstructionMemorySize(codeMemorySize)
  6101. END;
  6102. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6103. instanceType.AddCapability(ActiveCells.VectorCapability)
  6104. END;
  6105. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6106. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6107. END;
  6108. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6109. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6110. END;
  6111. AddDevices(instanceType, x);
  6112. *)
  6113. (*
  6114. IF x.isCellNet THEN
  6115. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6116. END;
  6117. *)
  6118. (*currentActiveCellsScope := instanceType;*)
  6119. (*
  6120. parameter := x.firstParameter;
  6121. portIndex := 0;
  6122. WHILE parameter # NIL DO
  6123. parameter.GetName(parameterName);
  6124. parameterType := parameter.type.resolved;
  6125. Parameter(parameterName, parameterType);
  6126. (*
  6127. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6128. ParameterArray(parameterType);
  6129. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6130. FOR i := 0 TO len-1 DO
  6131. COPY(parameterName,name);
  6132. AppendIndex(name,i);
  6133. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6134. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6135. INC(portIndex);
  6136. END;
  6137. ELSE
  6138. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6139. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6140. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6141. INC(portIndex);
  6142. END;
  6143. *)
  6144. parameter := parameter.nextParameter;
  6145. END;
  6146. *)
  6147. (*
  6148. Scope(x.cellScope);
  6149. currentActiveCellsScope := prevActiveCellsScope;
  6150. AddModules(instanceType,x.cellScope);
  6151. *)
  6152. END AddPorts;
  6153. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6154. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6155. BEGIN
  6156. Symbol(cell,op);
  6157. ToMemory(op.op,addressType,0);
  6158. Emit(Push(position,op.op));
  6159. ReleaseOperand(op);
  6160. Basic.GetString(property.name, name);
  6161. PushConstString(name);
  6162. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6163. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6164. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6165. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6166. Designate(d, op);
  6167. IF SemanticChecker.IsStringType(property.type) THEN
  6168. Emit(Push(-1, op.tag))
  6169. END;
  6170. Emit(Push(-1, op.op));
  6171. ReleaseOperand(op);
  6172. END;
  6173. IF SemanticChecker.IsStringType(property.type) THEN
  6174. ASSERT(SemanticChecker.IsStringType(value.type));
  6175. Designate(value, op);
  6176. Emit(Push(property.position, op.tag));
  6177. Emit(Push(property.position, op.op));
  6178. ReleaseOperand(op);
  6179. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6180. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6181. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6182. Evaluate(value, op);
  6183. Emit(Push(property.position, op.op));
  6184. ReleaseOperand(op);
  6185. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6186. ELSE
  6187. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6188. END;
  6189. END AddProperty;
  6190. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6191. VAR symbol: SyntaxTree.Symbol;
  6192. BEGIN
  6193. WHILE modifier # NIL DO
  6194. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6195. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6196. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6197. ELSE
  6198. (*! move this check to checker *)
  6199. Error(modifier.position, "undefined property");
  6200. END;
  6201. modifier := modifier.nextModifier;
  6202. END;
  6203. END AddModifiers;
  6204. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6205. VAR last: SyntaxTree.Modifier;
  6206. BEGIN
  6207. IF to = NIL THEN
  6208. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6209. ELSE
  6210. last := to;
  6211. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6212. last := last.nextModifier;
  6213. END;
  6214. IF last.identifier # this.identifier THEN
  6215. ASSERT(last.nextModifier = NIL);
  6216. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6217. END;
  6218. END;
  6219. END AppendModifier;
  6220. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6221. BEGIN
  6222. WHILE this # NIL DO
  6223. AppendModifier(to, this);
  6224. this := this.nextModifier;
  6225. END;
  6226. END AppendModifiers;
  6227. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6228. VAR op: Operand;
  6229. BEGIN
  6230. Evaluate(p, op);
  6231. Emit(Push(p.position, op.op));
  6232. ReleaseOperand(op);
  6233. (*
  6234. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6235. p := p(SyntaxTree.Designator).left;
  6236. END;
  6237. IF p # NIL THEN
  6238. Evaluate(p, op);
  6239. Emit(Push(p.position, op.op));
  6240. ReleaseOperand(op);
  6241. ELSE
  6242. Emit(Push(-1, nil));
  6243. END;
  6244. *)
  6245. END PushPort;
  6246. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6247. VAR
  6248. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6249. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6250. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6251. tmp:IntermediateCode.Operand;
  6252. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6253. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6254. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6255. dest: IntermediateCode.Operand;
  6256. staticLength: LONGINT; itype: IntermediateCode.Type;
  6257. convert,isTensor: BOOLEAN;
  6258. recordType: SyntaxTree.RecordType;
  6259. baseType: SyntaxTree.Type;
  6260. flags: SET;
  6261. left: SyntaxTree.Expression;
  6262. call: SyntaxTree.Designator;
  6263. procedure: SyntaxTree.Procedure;
  6264. temporaryVariable: SyntaxTree.Variable;
  6265. dummy: IntermediateCode.Operand;
  6266. customBuiltin: SyntaxTree.CustomBuiltin;
  6267. isVarPar: ARRAY 3 OF BOOLEAN;
  6268. callsection: Sections.Section;
  6269. segmentedName: Basic.SegmentedName;
  6270. needsTrace: BOOLEAN;
  6271. n: ARRAY 256 OF CHAR;
  6272. modifier: SyntaxTree.Modifier;
  6273. previous, init: IntermediateCode.Section;
  6274. prevScope: SyntaxTree.Scope;
  6275. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6276. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6277. priority: IntermediateCode.Operand;
  6278. op,callop: Operand;
  6279. BEGIN
  6280. IF type = NIL THEN RETURN END;
  6281. type := type.resolved;
  6282. IF type IS SyntaxTree.PointerType THEN
  6283. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6284. END;
  6285. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6286. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6287. recordScope := type(SyntaxTree.RecordType).recordScope;
  6288. IF recordScope.bodyProcedure # NIL THEN
  6289. procedure := recordScope.bodyProcedure;
  6290. body := procedure.procedureScope.body;
  6291. Emit(Push(position,self));
  6292. IF body.isActive THEN
  6293. StaticCallOperand(callop,procedure);
  6294. Emit(Push(position,callop.op));
  6295. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6296. Convert(priority,sizeType);
  6297. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6298. END;
  6299. Emit(Push(position,priority));
  6300. ReleaseIntermediateOperand(priority);
  6301. IF backend.cooperative THEN
  6302. Emit(Push(position,self));
  6303. CallThis(position,"Activities","Create",3)
  6304. ELSE
  6305. flags := 0;
  6306. IF body.isSafe THEN
  6307. flags := 1;
  6308. END;
  6309. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6310. Emit(Push(position,self));
  6311. CallThis(position,"Objects","CreateProcess",4)
  6312. END;
  6313. ELSE
  6314. Emit(Push(position,self));
  6315. StaticCallOperand(callop,procedure);
  6316. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6317. END;
  6318. Emit(Pop(position,self));
  6319. END;
  6320. END CallBodies;
  6321. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6322. BEGIN
  6323. actualType := actualType.resolved;
  6324. IF actualType IS SyntaxTree.StringType THEN
  6325. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6326. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6327. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6328. ELSE
  6329. tmp := op.tag;
  6330. IntermediateCode.MakeMemory(tmp,addressType);
  6331. Emit(Push(position,tmp));
  6332. END;
  6333. Emit(Push(position,op.op))
  6334. END PushString;
  6335. PROCEDURE PushTD(type: SyntaxTree.Type);
  6336. VAR op: IntermediateCode.Operand;
  6337. BEGIN
  6338. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6339. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6340. ELSE
  6341. IF type.resolved IS SyntaxTree.PointerType THEN
  6342. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6343. END;
  6344. op := TypeDescriptorAdr(type.resolved);
  6345. IF ~newObjectFile THEN
  6346. IntermediateCode.MakeMemory(op,addressType);
  6347. END;
  6348. Emit(Push(position,op));
  6349. END
  6350. END PushTD;
  6351. BEGIN
  6352. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6353. dest := destination; destination := emptyOperand;
  6354. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6355. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6356. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6357. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6358. CASE x.id OF
  6359. (* ---- COPY ----- *)
  6360. |Global.Copy:
  6361. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6362. (* ---- EXCL, INCL----- *)
  6363. |Global.Excl,Global.Incl:
  6364. Evaluate(p0,s0);
  6365. Evaluate(p1,s1);
  6366. Convert(s1.op,setType);
  6367. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6368. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6369. END;
  6370. ReuseCopy(res,s0.op);
  6371. ReleaseOperand(s0);
  6372. Reuse1(tmp,s1.op);
  6373. ReleaseOperand(s1);
  6374. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6375. IF x.id = Global.Excl THEN
  6376. Emit(Not(position,tmp,tmp));
  6377. Emit(And(position,res,res,tmp));
  6378. ELSE
  6379. Emit(Or(position,res,res,tmp));
  6380. END;
  6381. ReleaseIntermediateOperand(tmp);
  6382. Designate(p0,s0);
  6383. ToMemory(s0.op,setType,0);
  6384. Emit(Mov(position,s0.op,res));
  6385. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6386. (* ---- DISPOSE ----- *)
  6387. |Global.Dispose:
  6388. Designate(p0,s0);
  6389. Emit(Push(position,s0.op));
  6390. ReleaseOperand(s0);
  6391. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6392. (* ---- GETPROCEDURE ----- *)
  6393. |Global.GetProcedure:
  6394. Designate(p0,s0);
  6395. PushString(s0,p0.type);
  6396. Designate(p1,s1);
  6397. PushString(s1,p1.type);
  6398. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6399. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6400. ELSE PushTD(procedureType.firstParameter.type)
  6401. END;
  6402. PushTD(procedureType.returnType);
  6403. Designate(p2,s2);
  6404. Emit(Push(position,s2.op));
  6405. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6406. CallThis(position,"Modules","GetProcedure", 7);
  6407. (* ---- ASH, LSH, ROT ----- *)
  6408. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6409. Evaluate(p0,s0);
  6410. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6411. (* make unsigned arguments in order to produced a logical shift *)
  6412. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6413. convert:= TRUE;
  6414. itype := s0.op.type;
  6415. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6416. Convert(s0.op,itype);
  6417. ELSE
  6418. convert := FALSE;
  6419. END;
  6420. END;
  6421. Evaluate(p1,s1);
  6422. IF IsIntegerConstant(p1,hint) THEN
  6423. ReuseCopy(reg,s0.op);
  6424. IF hint > 0 THEN
  6425. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6426. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6427. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6428. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6429. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6430. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6431. END;
  6432. ELSIF hint < 0 THEN
  6433. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6434. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6435. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6436. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6437. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6438. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6439. END;
  6440. END;
  6441. ReleaseOperand(s0); ReleaseOperand(s1);
  6442. ELSE
  6443. exit := NewLabel();
  6444. end := NewLabel();
  6445. ReuseCopy(reg,s0.op);
  6446. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6447. Reuse1(tmp,s1.op);
  6448. Emit(Neg(position,tmp,s1.op));
  6449. Convert(tmp,s1.op.type);
  6450. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6451. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6452. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6453. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6454. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6455. END;
  6456. ReleaseIntermediateOperand(tmp);
  6457. BrL(end);
  6458. SetLabel(exit);
  6459. ReuseCopy(tmp,s1.op);
  6460. Convert(tmp,s1.op.type);
  6461. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6462. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6463. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6464. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6465. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6466. END;
  6467. ReleaseIntermediateOperand(tmp);
  6468. SetLabel(end);
  6469. ReleaseOperand(s0); ReleaseOperand(s1);
  6470. END;
  6471. InitOperand(result,ModeValue);
  6472. IF convert THEN
  6473. itype := reg.type;
  6474. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6475. Convert(reg,itype);
  6476. END;
  6477. result.op := reg;
  6478. (* ---- CAP ----- *)
  6479. |Global.Cap:
  6480. Evaluate(p0,result);
  6481. ReuseCopy(reg,result.op);
  6482. ReleaseIntermediateOperand(result.op);
  6483. ignore := NewLabel();
  6484. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6485. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6486. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6487. SetLabel(ignore);
  6488. result.op := reg;
  6489. (* ---- CHR ----- *)
  6490. |Global.Chr, Global.Chr32:
  6491. Evaluate(p0,result);
  6492. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6493. |Global.Entier, Global.EntierH:
  6494. Evaluate(p0,result);
  6495. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6496. (* ---- MIN and MAX ----- *)
  6497. |Global.Max,Global.Min:
  6498. Evaluate(p0,s0);
  6499. Evaluate(p1,s1);
  6500. Reuse2(res,s0.op,s1.op);
  6501. else := NewLabel();
  6502. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6503. ELSE BrltL(else,s1.op,s0.op) END;
  6504. Emit(Mov(position,res,s0.op));
  6505. ReleaseOperand(s0);
  6506. end := NewLabel();
  6507. BrL(end);
  6508. SetLabel(else);
  6509. Emit(MovReplace(position,res,s1.op));
  6510. SetLabel(end);
  6511. ReleaseOperand(s1);
  6512. InitOperand(result,ModeValue);
  6513. result.op := res;
  6514. (* ---- ODD ----- *)
  6515. |Global.Odd:
  6516. IF ~conditional THEN
  6517. ConditionToValue(x)
  6518. ELSE
  6519. Evaluate(p0,result);
  6520. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6521. Reuse1(res,result.op);
  6522. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6523. ReleaseIntermediateOperand(result.op);
  6524. result.op := res;
  6525. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6526. ReleaseOperand(result);
  6527. BrL(falseLabel);
  6528. END;
  6529. (* ---- ORD ----- *)
  6530. |Global.Ord, Global.Ord32:
  6531. Evaluate(p0,result);
  6532. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6533. (* ---- SHORT, LONG ----- *)
  6534. |Global.Short, Global.Long:
  6535. Evaluate(p0,result);
  6536. IF x.type IS SyntaxTree.ComplexType THEN
  6537. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6538. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6539. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6540. ELSE
  6541. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6542. END
  6543. (* ---- HALT, SYSTEM.HALT----- *)
  6544. |Global.Halt, Global.systemHalt:
  6545. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6546. EmitTrap (position, val);
  6547. (* ---- ASSERT ----- *)
  6548. |Global.Assert:
  6549. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6550. trueL := NewLabel();
  6551. falseL := NewLabel();
  6552. Condition(p0,trueL,falseL);
  6553. IF p1 = NIL THEN val := AssertTrap
  6554. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6555. END;
  6556. SetLabel(falseL);
  6557. EmitTrap(position,val);
  6558. SetLabel(trueL);
  6559. END;
  6560. (*
  6561. Emit(TrapC(result.op,val);
  6562. *)
  6563. (* ---- INC, DEC----- *)
  6564. |Global.Inc,Global.Dec:
  6565. Expression(p0); adr := result.op;
  6566. LoadValue(result,p0.type); l := result;
  6567. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6568. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6569. END;
  6570. IF x.id = Global.Inc THEN
  6571. Emit(Add(position,l.op,l.op,r.op));
  6572. ELSE
  6573. Emit(Sub(position,l.op,l.op,r.op));
  6574. END;
  6575. ReleaseOperand(l); ReleaseOperand(r);
  6576. (* ---- LEN ----- *)
  6577. |Global.Len: (* dynamic length, static length done by checker *)
  6578. Designate(p0,operand);
  6579. IF p1 = NIL THEN
  6580. InitOperand(l,ModeValue);
  6581. l.op := IntermediateCode.Immediate(int32,0);
  6582. ELSE
  6583. Evaluate(p1,l);
  6584. END;
  6585. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6586. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6587. Dereference(operand, p0.type.resolved, FALSE);
  6588. END;
  6589. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6590. ReleaseOperand(operand); ReleaseOperand(l);
  6591. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6592. ASSERT(p1 # NIL);
  6593. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6594. Dereference(operand,p0.type.resolved,FALSE);
  6595. END;
  6596. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6597. ReleaseOperand(operand); ReleaseOperand(l);
  6598. ELSE HALT(100);
  6599. END;
  6600. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6601. (* ---- FIRST ---- *)
  6602. |Global.First:
  6603. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6604. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6605. ELSE
  6606. Designate(p0, result)
  6607. END
  6608. (* ---- LAST ---- *)
  6609. |Global.Last:
  6610. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6611. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6612. ELSE
  6613. Designate(p0, result);
  6614. (* make sure result.op is a register *)
  6615. tmp := result.op;
  6616. ReuseCopy(result.op, result.op);
  6617. ReleaseIntermediateOperand(tmp);
  6618. (* add offset to result.op *)
  6619. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6620. END
  6621. (* ---- STEP ---- *)
  6622. |Global.Step:
  6623. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6624. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6625. ELSE
  6626. Designate(p0, result);
  6627. (* make sure result.op is a register *)
  6628. tmp := result.op;
  6629. ReuseCopy(result.op, result.op);
  6630. ReleaseIntermediateOperand(tmp);
  6631. (* add offset to result.op *)
  6632. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6633. END
  6634. (* ---- RE ---- *)
  6635. |Global.Re:
  6636. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6637. Designate(p0, result)
  6638. ELSE
  6639. Evaluate(p0, result)
  6640. END
  6641. (* ---- IM ---- *)
  6642. |Global.Im:
  6643. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6644. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6645. Designate(p0, result);
  6646. (* make sure result.op is a register *)
  6647. tmp := result.op;
  6648. ReuseCopy(result.op, result.op);
  6649. ReleaseIntermediateOperand(tmp);
  6650. (* add offset to result.op *)
  6651. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6652. (* ---- ABS ----- *)
  6653. |Global.Abs:
  6654. Evaluate(p0,operand);
  6655. type := p0.type.resolved;
  6656. InitOperand(result,ModeValue);
  6657. Reuse1a(result.op,operand.op,dest);
  6658. Emit(Abs(position,result.op,operand.op));
  6659. ReleaseOperand(operand);
  6660. (* ---- WAIT ----- *)
  6661. |Global.Wait:
  6662. Evaluate(p0,operand);
  6663. Emit(Push(position,operand.op));
  6664. ReleaseOperand(operand);
  6665. CallThis(position,"Activities","Wait", 1);
  6666. (* ---- NEW ----- *)
  6667. |Global.New:
  6668. (*! the following code is only correct for "standard" Oberon calling convention *)
  6669. type := p0.type.resolved;
  6670. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6671. THEN
  6672. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6673. IF backend.cooperative THEN
  6674. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6675. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6676. IF recordType.isObject THEN
  6677. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6678. IF recordType.IsActive() THEN
  6679. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6680. END;
  6681. IF recordType.IsProtected() THEN
  6682. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6683. END;
  6684. ELSE
  6685. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6686. END;
  6687. END;
  6688. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6689. CallThis(position,"Runtime","New", 1);
  6690. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6691. Emit(Result(position, pointer));
  6692. exit := NewLabel();
  6693. BreqL(exit,pointer,nil);
  6694. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6695. GetRecordTypeName (recordType,name);
  6696. IF ~recordType.isObject THEN
  6697. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6698. END;
  6699. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6700. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6701. IF recordType.isObject THEN
  6702. IF recordType.IsProtected() THEN
  6703. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6704. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6705. END;
  6706. IF recordType.IsActive() THEN
  6707. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6708. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6709. END;
  6710. END;
  6711. END;
  6712. (* initialize fields *)
  6713. IF type(SyntaxTree.PointerType).isPlain THEN
  6714. size := 0;
  6715. ELSIF recordType.isObject THEN
  6716. size := BaseObjectTypeSize;
  6717. ELSE
  6718. size := BaseRecordTypeSize;
  6719. END;
  6720. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6721. (* call initializer *)
  6722. constructor := GetConstructor(recordType);
  6723. IF constructor # NIL THEN
  6724. (*! should be unified with ProcedureCallDesignator *)
  6725. Emit(Push(position,pointer));
  6726. ReleaseIntermediateOperand(pointer);
  6727. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6728. FOR i := 1 TO x.parameters.Length()-1 DO
  6729. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6730. formalParameter := formalParameter.nextParameter;
  6731. END;
  6732. (* static call of the constructor *)
  6733. GetCodeSectionNameForSymbol(constructor,name);
  6734. ASSERT(~constructor.isInline);
  6735. IF constructor.scope.ownerModule # module.module THEN
  6736. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6737. ELSE
  6738. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6739. END;
  6740. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6741. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6742. Emit(Pop(position,pointer));
  6743. END;
  6744. (* call bodies *)
  6745. CallBodies(pointer,p0.type);
  6746. SetLabel(exit);
  6747. needsTrace := p0.NeedsTrace();
  6748. IF needsTrace THEN ModifyAssignments(true) END;
  6749. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6750. Emit(Push(position, pointer));
  6751. CallThis(position,"GarbageCollector","Watch",0);
  6752. Emit(Pop(position, pointer));
  6753. END;
  6754. Designate(p0,l);
  6755. IF needsTrace THEN
  6756. CallAssignPointer(l.op, pointer);
  6757. ELSE
  6758. ToMemory(l.op,addressType,0);
  6759. Emit(Mov(position,l.op,pointer));
  6760. END;
  6761. ReleaseIntermediateOperand(pointer);
  6762. ReleaseOperand(l);
  6763. IF needsTrace THEN ModifyAssignments(false) END;
  6764. ELSE
  6765. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6766. IF temporaryVariable # NIL THEN
  6767. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6768. ELSE
  6769. Designate(p0,l);
  6770. END;
  6771. (* l.op contains address of pointer to record *)
  6772. Emit(Push(position,l.op)); (* address for use after syscall *)
  6773. Emit(Push(position,l.op));
  6774. ReleaseOperand(l);
  6775. (* push type descriptor *)
  6776. reg := TypeDescriptorAdr(recordType);
  6777. IF ~newObjectFile THEN
  6778. IntermediateCode.MakeMemory(reg,addressType);
  6779. END;
  6780. Emit(Push(position,reg));
  6781. ReleaseIntermediateOperand(reg);
  6782. (* push realtime flag *)
  6783. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6784. ELSE Emit(Push(position,false));
  6785. END;
  6786. CallThis(position,"Heaps","NewRec", 3);
  6787. (* check allocation success, if not successful then do not call initializers and bodies *)
  6788. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6789. Emit(Pop(position,pointer));
  6790. MakeMemory(reg,pointer,addressType,0);
  6791. ReleaseIntermediateOperand(pointer);
  6792. pointer := reg;
  6793. exit := NewLabel();
  6794. BreqL(exit,pointer,nil);
  6795. Emit(Push(position,pointer));
  6796. (* initialize fields *)
  6797. InitFields(recordType, pointer,0);
  6798. (* call initializer *)
  6799. constructor := GetConstructor(recordType);
  6800. IF constructor # NIL THEN
  6801. (*! should be unified with ProcedureCallDesignator *)
  6802. Emit(Push(position,pointer));
  6803. ReleaseIntermediateOperand(pointer);
  6804. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6805. FOR i := 1 TO x.parameters.Length()-1 DO
  6806. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6807. formalParameter := formalParameter.nextParameter;
  6808. END;
  6809. (* static call of the constructor *)
  6810. GetCodeSectionNameForSymbol(constructor,name);
  6811. ASSERT(~constructor.isInline);
  6812. IF constructor.scope.ownerModule # module.module THEN
  6813. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6814. ELSE
  6815. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6816. END;
  6817. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6818. ELSE
  6819. ReleaseIntermediateOperand(pointer);
  6820. END;
  6821. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6822. Emit(Pop(position,pointer));
  6823. IF temporaryVariable # NIL THEN
  6824. Designate(p0,l);
  6825. ToMemory(l.op,addressType,0);
  6826. Emit(Mov(position,l.op,pointer));
  6827. ReleaseOperand(l);
  6828. result.tag := emptyOperand;
  6829. END;
  6830. (* call bodies *)
  6831. CallBodies(pointer,p0.type);
  6832. ReleaseIntermediateOperand(pointer);
  6833. IF temporaryVariable # NIL THEN
  6834. end := NewLabel();
  6835. BrL(end);
  6836. SetLabel(exit);
  6837. Designate(p0,l);
  6838. ToMemory(l.op,addressType,0);
  6839. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6840. ReleaseOperand(l);
  6841. SetLabel(end);
  6842. ELSE
  6843. SetLabel(exit);
  6844. END;
  6845. END;
  6846. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6847. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6848. dim := 0;
  6849. IF p1 # NIL THEN
  6850. FOR i := 1 TO x.parameters.Length()-1 DO
  6851. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6852. parameter := x.parameters.GetExpression(i);
  6853. Evaluate(parameter,r);
  6854. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6855. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6856. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6857. END;
  6858. Emit(Push(position,r.op));
  6859. IF i=1 THEN
  6860. ReuseCopy(reg,r.op);
  6861. ELSE
  6862. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6863. END;
  6864. ReleaseOperand(r);
  6865. INC(dim);
  6866. END;
  6867. Convert(reg,addressType);
  6868. ELSE
  6869. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6870. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6871. END;
  6872. openDim := dim;
  6873. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6874. IF backend.cooperative THEN
  6875. size := ToMemoryUnits(system,system.SizeOf(type));
  6876. WHILE type IS SyntaxTree.ArrayType DO
  6877. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6878. END;
  6879. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6880. IF (size # 1) THEN
  6881. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6882. END;
  6883. Emit(Push(position,reg));
  6884. size := ToMemoryUnits(system,system.SizeOf(type));
  6885. IF (size # 1) THEN
  6886. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6887. END;
  6888. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6889. Emit(Push(position,reg));
  6890. ReleaseIntermediateOperand(reg);
  6891. CallThis(position,"Runtime","New", 1);
  6892. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6893. Emit(Result(position, pointer));
  6894. exit := NewLabel();
  6895. else := NewLabel();
  6896. BreqL(else,pointer,nil);
  6897. IF ~type.hasPointers THEN
  6898. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6899. ELSIF type IS SyntaxTree.RecordType THEN
  6900. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6901. ELSIF type IS SyntaxTree.ProcedureType THEN
  6902. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6903. ELSE
  6904. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6905. END;
  6906. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6907. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6908. 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))));
  6909. IF type IS SyntaxTree.RecordType THEN
  6910. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6911. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6912. ELSE
  6913. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6914. END;
  6915. i := openDim;
  6916. WHILE i > 0 DO
  6917. DEC (i);
  6918. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6919. END;
  6920. needsTrace := p0.NeedsTrace();
  6921. IF needsTrace THEN ModifyAssignments(true) END;
  6922. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6923. Emit(Push(position, pointer));
  6924. CallThis(position,"GarbageCollector","Watch",0);
  6925. Emit(Pop(position, pointer));
  6926. END;
  6927. Designate(p0,l);
  6928. IF needsTrace THEN
  6929. CallAssignPointer(l.op, pointer);
  6930. ModifyAssignments(false);
  6931. ELSE
  6932. ToMemory(l.op,addressType,0);
  6933. Emit(Mov(position,l.op,pointer));
  6934. END;
  6935. ReleaseIntermediateOperand(pointer);
  6936. ReleaseOperand(l);
  6937. BrL(exit);
  6938. SetLabel(else);
  6939. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6940. Designate(p0,l);
  6941. IF needsTrace THEN
  6942. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6943. ELSE
  6944. ToMemory(l.op,addressType,0);
  6945. Emit(Mov(position,l.op,pointer));
  6946. END;
  6947. ReleaseOperand(l);
  6948. SetLabel(exit);
  6949. ELSE
  6950. (*! the following code is only correct for "standard" Oberon calling convention *)
  6951. IF SemanticChecker.ContainsPointer(type) THEN
  6952. IF type IS SyntaxTree.ArrayType THEN
  6953. staticLength := 1;
  6954. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6955. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6956. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6957. END;
  6958. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6959. Emit(Mul(position,reg,reg,tmp));
  6960. END;
  6961. Designate(p0,l);
  6962. IF openDim > 0 THEN
  6963. Emit(Push(position,l.op)); (* address for use after syscall *)
  6964. END;
  6965. Emit(Push(position,l.op)); (* address *)
  6966. ReleaseOperand(l);
  6967. tmp := TypeDescriptorAdr(type);
  6968. IF ~newObjectFile THEN
  6969. IntermediateCode.MakeMemory(tmp,addressType);
  6970. END;
  6971. Emit(Push(position,tmp)); (* type descriptor *)
  6972. ReleaseIntermediateOperand(tmp);
  6973. Emit(Push(position,reg)); (* number Elements *)
  6974. ReleaseIntermediateOperand(reg);
  6975. tmp := IntermediateCode.Immediate(addressType,dim);
  6976. Emit(Push(position,tmp)); (* dimensions *)
  6977. (* push realtime flag *)
  6978. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6979. ELSE Emit(Push(position,false));
  6980. END;
  6981. CallThis(position,"Heaps","NewArr",5)
  6982. ELSE
  6983. size := ToMemoryUnits(system,system.SizeOf(type));
  6984. IF (size # 1) THEN
  6985. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6986. END;
  6987. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  6988. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  6989. Emit(Add(position,reg,reg,tmp));
  6990. Designate(p0,l);
  6991. IF openDim >0 THEN
  6992. Emit(Push(position,l.op)); (* address for use after syscall *)
  6993. END;
  6994. Emit(Push(position,l.op)); (* address for syscall *)
  6995. ReleaseOperand(l); (* pointer address *)
  6996. Emit(Push(position,reg)); (* size *)
  6997. ReleaseIntermediateOperand(reg);
  6998. (* push realtime flag *)
  6999. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7000. ELSE Emit(Push(position,false));
  7001. END;
  7002. CallThis(position,"Heaps","NewSys", 3)
  7003. END;
  7004. IF openDim > 0 THEN
  7005. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7006. Emit(Pop(position,adr));
  7007. ToMemory(adr,addressType,0);
  7008. ReuseCopy(tmp,adr);
  7009. ReleaseIntermediateOperand(adr);
  7010. adr := tmp;
  7011. else := NewLabel();
  7012. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7013. i := openDim-1;
  7014. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7015. WHILE (i >= 0) DO
  7016. Emit(Pop(position,reg));
  7017. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7018. Emit(Mov(position,res,reg));
  7019. DEC(i);
  7020. END;
  7021. ReleaseIntermediateOperand(adr);
  7022. ReleaseIntermediateOperand(reg);
  7023. exit := NewLabel();
  7024. BrL(exit);
  7025. SetLabel(else);
  7026. (* else part: array could not be allocated *)
  7027. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7028. Emit(Add(position,sp,sp,tmp));
  7029. SetLabel(exit);
  7030. END;
  7031. END;
  7032. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7033. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7034. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7035. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7036. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7037. left.SetType(procedure.type);
  7038. formalParameter := procedureType.firstParameter;
  7039. (* push array to allocate *)
  7040. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7041. formalParameter :=formalParameter.nextParameter;
  7042. (* push length array *)
  7043. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7044. (* push size *)
  7045. type := t0;
  7046. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7047. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7048. END;
  7049. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7050. Emit(Push(position,tmp));
  7051. (* *)
  7052. IF SemanticChecker.ContainsPointer(type) THEN
  7053. tmp := TypeDescriptorAdr(type);
  7054. IF ~newObjectFile THEN
  7055. IntermediateCode.MakeMemory(tmp,addressType);
  7056. END;
  7057. ELSE
  7058. tmp := IntermediateCode.Immediate(addressType, 0);
  7059. END;
  7060. Emit(Push(position,tmp)); (* type descriptor *)
  7061. StaticCallOperand(result,procedure);
  7062. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7063. ReleaseOperand(result);
  7064. END;
  7065. (*
  7066. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7067. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7068. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7069. *)
  7070. ELSE
  7071. dim := 0;
  7072. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7073. (* generate geometry descriptor *)
  7074. Designate(p0,l);
  7075. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7076. ReleaseOperand(l);
  7077. isTensor := TRUE;
  7078. ELSE
  7079. isTensor := FALSE;
  7080. END;
  7081. FOR i := 1 TO x.parameters.Length()-1 DO
  7082. IF ~isTensor THEN
  7083. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7084. END;
  7085. parameter := x.parameters.GetExpression(i);
  7086. Evaluate(parameter,r);
  7087. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7088. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7089. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7090. END;
  7091. Emit(Push(position,r.op));
  7092. IF i=1 THEN
  7093. ReuseCopy(reg,r.op);
  7094. ELSE
  7095. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7096. END;
  7097. ReleaseOperand(r);
  7098. INC(dim);
  7099. END;
  7100. Convert(reg,addressType);
  7101. openDim := dim;
  7102. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7103. (*! the following code is only correct for "standard" Oberon calling convention *)
  7104. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7105. t := type;
  7106. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7107. staticLength := 1;
  7108. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7109. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7110. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7111. END;
  7112. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7113. Emit(Mul(position,reg,reg,tmp));
  7114. END;
  7115. Designate(p0,l);
  7116. IF isTensor THEN
  7117. Dereference(l,type,FALSE);
  7118. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7119. END;
  7120. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7121. Emit(Push(position,l.tag)); (* address *)
  7122. ReleaseOperand(l);
  7123. tmp := TypeDescriptorAdr(t);
  7124. IF ~newObjectFile THEN
  7125. IntermediateCode.MakeMemory(tmp,addressType);
  7126. END;
  7127. Emit(Push(position,tmp)); (* type descriptor *)
  7128. ReleaseIntermediateOperand(tmp);
  7129. Emit(Push(position,reg)); (* number Elements *)
  7130. ReleaseIntermediateOperand(reg);
  7131. tmp := IntermediateCode.Immediate(addressType,0);
  7132. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7133. (* push realtime flag: false by default *)
  7134. Emit(Push(position,false));
  7135. CallThis(position,"Heaps","NewArr",5);
  7136. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7137. Emit(Pop(position,adr));
  7138. GetMathArrayField(tmp,adr,MathPtrOffset);
  7139. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7140. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7141. PutMathArrayField(adr,reg,MathAdrOffset);
  7142. ReleaseIntermediateOperand(tmp);
  7143. ReleaseIntermediateOperand(reg);
  7144. ELSE
  7145. IF isTensor THEN
  7146. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7147. ELSE
  7148. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7149. END;
  7150. IF (size # 1) THEN
  7151. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7152. END;
  7153. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7154. Emit(Add(position,reg,reg,tmp));
  7155. Designate(p0,l);
  7156. IF isTensor THEN
  7157. Dereference(l,type,FALSE);
  7158. END;
  7159. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7160. Emit(Push(position,l.tag)); (* address for syscall *)
  7161. ReleaseOperand(l); (* pointer address *)
  7162. Emit(Push(position,reg)); (* size *)
  7163. ReleaseIntermediateOperand(reg);
  7164. (* push realtime flag: false by default *)
  7165. Emit(Push(position,false));
  7166. CallThis(position,"Heaps","NewSys",3);
  7167. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7168. Emit(Pop(position,adr));
  7169. GetMathArrayField(tmp,adr,MathPtrOffset);
  7170. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7171. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7172. PutMathArrayField(adr,reg,MathAdrOffset);
  7173. ReleaseIntermediateOperand(tmp);
  7174. ReleaseIntermediateOperand(reg);
  7175. END;
  7176. flags := {};
  7177. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7178. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7179. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7180. PutMathArrayField(adr,tmp,MathDimOffset);
  7181. else := NewLabel();
  7182. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7183. i := openDim-1;
  7184. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7185. IF isTensor THEN
  7186. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7187. ELSE
  7188. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7189. END;
  7190. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7191. WHILE (i >= 0) DO
  7192. Emit(Pop(position,reg));
  7193. PutMathArrayLength(adr,reg,i);
  7194. PutMathArrayIncrement(adr,tmp,i);
  7195. IF i > 0 THEN
  7196. IF i=openDim-1 THEN
  7197. ReuseCopy(tmp,tmp);
  7198. END;
  7199. Emit(Mul(position,tmp,tmp,reg));
  7200. END;
  7201. DEC(i);
  7202. END;
  7203. ReleaseIntermediateOperand(adr);
  7204. ReleaseIntermediateOperand(reg);
  7205. ReleaseIntermediateOperand(tmp);
  7206. exit := NewLabel();
  7207. BrL(exit);
  7208. SetLabel(else);
  7209. (* else part: array could not be allocated *)
  7210. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7211. Emit(Add(position,sp,sp,tmp));
  7212. SetLabel(exit);
  7213. END;
  7214. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7215. THEN
  7216. IF ~backend.cellsAreObjects THEN RETURN END;
  7217. IF InCellScope(currentScope) THEN
  7218. PushSelfPointer()
  7219. ELSE
  7220. Emit(Push(position, nil));
  7221. END;
  7222. (* push temp address *)
  7223. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7224. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7225. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7226. (* l.op contains address of pointer to record *)
  7227. Emit(Push(position,l.op)); (* address for use after syscall *)
  7228. ReleaseOperand(l);
  7229. (* push type descriptor *)
  7230. reg := TypeDescriptorAdr(baseType);
  7231. IF ~newObjectFile THEN
  7232. IntermediateCode.MakeMemory(reg,addressType);
  7233. END;
  7234. Emit(Push(position,reg));
  7235. ReleaseIntermediateOperand(reg);
  7236. (* push name *)
  7237. (*Global.GetSymbolName(p0, n);*)
  7238. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7239. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7240. ELSE
  7241. Global.GetModuleName(module.module, n);
  7242. END;
  7243. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7244. (*type.typeDeclaration.GetName(n);*)
  7245. PushConstString(n);
  7246. (* push cellnet boolean *)
  7247. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7248. (* push engine boolean *)
  7249. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7250. (* allocate *)
  7251. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7252. (* add capabilities *)
  7253. modifier := p0(SyntaxTree.Designator).modifiers;
  7254. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7255. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7256. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7257. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7258. END;
  7259. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7260. (*
  7261. modifier := baseType(SyntaxTree.CellType).modifiers;
  7262. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7263. modifier := p0(SyntaxTree.Designator).modifiers;
  7264. *)
  7265. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7266. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7267. (* l.op contains address of pointer to record *)
  7268. ToMemory(l.op,addressType,0);
  7269. (* l.op contains value of pointer to record *)
  7270. Emit(Push(position,l.op)); (* address for use after syscall *)
  7271. ReleaseOperand(l);
  7272. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7273. prevScope := currentScope;
  7274. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7275. previous := section;
  7276. section := init;
  7277. (* add ports *)
  7278. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7279. CloseInitializer(previous);
  7280. currentScope := prevScope;
  7281. Symbol(temporaryVariable,l);
  7282. ToMemory(l.op,addressType,0);
  7283. Emit(Push(position,l.op));
  7284. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7285. (*
  7286. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7287. IF constructor # NIL THEN
  7288. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7289. FOR i := 1 TO x.parameters.Length()-1 DO
  7290. p := x.parameters.GetExpression(i);
  7291. Global.GetSymbolName(parameter,name);
  7292. Evaluate(p, value);
  7293. ASSERT(value.type # NIL);
  7294. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7295. par := instance.AddParameter(name);
  7296. par.SetInteger(value.integer);
  7297. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7298. par := instance.AddParameter(name);
  7299. par.SetBoolean(value.boolean);
  7300. ELSE Error(x.position,NotYetImplemented)
  7301. END;
  7302. parameter := parameter.nextParameter
  7303. END;
  7304. END;
  7305. *)
  7306. (* call initializer *)
  7307. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7308. IF constructor # NIL THEN
  7309. (*! should be unified with ProcedureCallDesignator *)
  7310. IF backend.cellsAreObjects THEN
  7311. Symbol(temporaryVariable,l);
  7312. ToMemory(l.op,addressType,0);
  7313. Emit(Push(position,l.op));
  7314. ReleaseOperand(l);
  7315. END;
  7316. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7317. FOR i := 1 TO x.parameters.Length()-1 DO
  7318. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7319. formalParameter := formalParameter.nextParameter;
  7320. END;
  7321. (* static call of the constructor *)
  7322. Global.GetSymbolSegmentedName(constructor,name);
  7323. ASSERT(~constructor.isInline);
  7324. IF constructor.scope.ownerModule # module.module THEN
  7325. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7326. ELSE
  7327. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7328. END;
  7329. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7330. (*ELSE
  7331. ReleaseIntermediateOperand(pointer);*)
  7332. END;
  7333. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7334. ToMemory(l.op, addressType, 0);
  7335. Designate(p0,s0);
  7336. ToMemory(s0.op,addressType,0);
  7337. Emit(Mov(position,s0.op,l.op));
  7338. ReleaseOperand(l);
  7339. ReleaseOperand(s0);
  7340. result.tag := emptyOperand;
  7341. (* start *)
  7342. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7343. (* push cell *)
  7344. Symbol(temporaryVariable, l);
  7345. ToMemory(l.op,addressType,0);
  7346. Emit(Push(-1,l.op));
  7347. (* push delegate *)
  7348. Emit(Push(-1,l.op));
  7349. ReleaseOperand(l);
  7350. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7351. Emit(Push(position, s1.op));
  7352. ReleaseOperand(s1);
  7353. CallThis(position,"ActiveCellsRuntime","Start",3);
  7354. END;
  7355. (*IF temporaryVariable # NIL THEN
  7356. end := NewLabel();
  7357. BrL(end);
  7358. SetLabel(exit);
  7359. Designate(p0,l);
  7360. ToMemory(l.op,addressType,0);
  7361. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7362. ReleaseOperand(l);
  7363. SetLabel(end);
  7364. ELSE
  7365. SetLabel(exit);
  7366. END;
  7367. *)
  7368. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7369. ELSE (* no pointer to record, no pointer to array *)
  7370. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7371. (* ignore new statement *)
  7372. Warning(p0.position, "cannot run on final hardware");
  7373. ELSE
  7374. HALT(200);
  7375. END;
  7376. END;
  7377. (* ---- ADDRESSOF----- *)
  7378. |Global.systemAdr:
  7379. Designate(p0,s0);
  7380. s0.mode := ModeValue;
  7381. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7382. ReleaseIntermediateOperand(s0.op);
  7383. s0.op := s0.tag;
  7384. IntermediateCode.InitOperand(s0.tag);
  7385. END;
  7386. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7387. result := s0;
  7388. (* ---- BIT ----- *)
  7389. |Global.systemBit:
  7390. Evaluate(p0,s0);
  7391. ToMemory(s0.op,addressType,0);
  7392. ReuseCopy(res,s0.op);
  7393. ReleaseOperand(s0);
  7394. Evaluate(p1,s1);
  7395. Emit(Ror(position,res,res,s1.op));
  7396. ReleaseOperand(s1);
  7397. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7398. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7399. IF ~conditional THEN
  7400. InitOperand(result,ModeValue); result.op := res;
  7401. ELSE
  7402. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7403. BrL(falseLabel);
  7404. ReleaseIntermediateOperand(res);
  7405. END;
  7406. (* --- MSK ----*)
  7407. |Global.systemMsk:
  7408. Evaluate(p0,s0);
  7409. Evaluate(p1,s1);
  7410. ReuseCopy(res,s0.op);
  7411. ReleaseOperand(s0);
  7412. Emit(And(position,res,res,s1.op));
  7413. ReleaseOperand(s1);
  7414. InitOperand(result,ModeValue);
  7415. result.op := res;
  7416. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7417. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7418. Evaluate(p0,s0);
  7419. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7420. ReleaseOperand(s0);
  7421. InitOperand(result,ModeValue);
  7422. result.op := res;
  7423. (* ---- SYSTEM.GetStackPointer ----- *)
  7424. |Global.systemGetStackPointer:
  7425. InitOperand(result,ModeValue);
  7426. result.op := sp;
  7427. (* ---- SYSTEM.GetFramePointer ----- *)
  7428. |Global.systemGetFramePointer:
  7429. InitOperand(result,ModeValue);
  7430. result.op := fp;
  7431. (* ---- SYSTEM.GetActivity ----- *)
  7432. |Global.systemGetActivity:
  7433. ASSERT(backend.cooperative);
  7434. InitOperand(result,ModeValue);
  7435. result.op := ap;
  7436. (* ---- SYSTEM.SetStackPointer ----- *)
  7437. |Global.systemSetStackPointer:
  7438. Evaluate(p0,s0); (* *)
  7439. Emit(Mov(position,sp,s0.op));
  7440. ReleaseOperand(s0);
  7441. (* ---- SYSTEM.SetFramePointer ----- *)
  7442. |Global.systemSetFramePointer:
  7443. Evaluate(p0,s0); (* *)
  7444. Emit(Mov(position,fp,s0.op));
  7445. ReleaseOperand(s0);
  7446. (* ---- SYSTEM.Activity ----- *)
  7447. |Global.systemSetActivity:
  7448. ASSERT(backend.cooperative);
  7449. Evaluate(p0,s0); (* *)
  7450. Emit(Mov(position,ap,s0.op));
  7451. ReleaseOperand(s0);
  7452. (* ---- SYSTEM.VAL ----- *)
  7453. |Global.systemVal:
  7454. Expression(p1);
  7455. s1 := result;
  7456. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7457. IF s1.mode = ModeReference THEN
  7458. (* nothing to be done if not record type, just take over new type *)
  7459. IF (type IS SyntaxTree.RecordType) THEN
  7460. ReleaseIntermediateOperand(s1.tag);
  7461. s1.tag := TypeDescriptorAdr(type);
  7462. IF ~newObjectFile THEN
  7463. IntermediateCode.MakeMemory(s1.tag,addressType);
  7464. END;
  7465. UseIntermediateOperand(s1.tag);
  7466. END;
  7467. result := s1;
  7468. ELSE (* copy over result to different type, may not use convert *)
  7469. itype := IntermediateCode.GetType(system,type);
  7470. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7471. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7472. Emit(Mov(position,s0.op,s1.op));
  7473. ReleaseOperand(s1);
  7474. InitOperand(result,ModeValue);
  7475. result.op := s0.op;
  7476. ELSE (* different size, must convert *)
  7477. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7478. Convert(s1.op, IntermediateCode.GetType(system,type));
  7479. result := s1;
  7480. END;
  7481. END;
  7482. (* ---- SYSTEM.GET ----- *)
  7483. |Global.systemGet:
  7484. Evaluate(p0,s0); (* adr *)
  7485. Designate(p1,s1); (* variable *)
  7486. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7487. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7488. Emit(Mov(position,s1.op,s0.op));
  7489. ReleaseOperand(s1);
  7490. ReleaseOperand(s0);
  7491. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7492. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7493. Evaluate(p0,s0); (* *)
  7494. Evaluate(p1,s1); (* variable *)
  7495. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7496. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7497. (* real part *)
  7498. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7499. Emit(Mov(position,res, s1.op));
  7500. ReleaseIntermediateOperand(res);
  7501. (* imaginary part *)
  7502. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7503. Emit(Mov(position,res, s1.tag));
  7504. ReleaseIntermediateOperand(res);
  7505. ReleaseOperand(s1);
  7506. ReleaseOperand(s0);
  7507. ELSE
  7508. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7509. ReleaseOperand(s0);
  7510. Emit(Mov(position,res,s1.op));
  7511. ReleaseIntermediateOperand(res);
  7512. ReleaseOperand(s1);
  7513. END;
  7514. (* ---- SYSTEM.MOVE ----- *)
  7515. |Global.systemMove:
  7516. Evaluate(p0,s0);
  7517. Evaluate(p1,s1);
  7518. Evaluate(p2,s2);
  7519. Emit(Copy(position,s1.op,s0.op,s2.op));
  7520. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7521. (* ---- SYSTEM.NEW ----- *)
  7522. |Global.systemNew:
  7523. Designate(p0,s0);
  7524. Emit(Push(position,s0.op));
  7525. ReleaseOperand(s0);
  7526. Evaluate(p1,s1);
  7527. Emit(Push(position,s1.op));
  7528. ReleaseOperand(s1);
  7529. (* push realtime flag: false by default *)
  7530. Emit(Push(position,false));
  7531. CallThis(position,"Heaps","NewSys",3);
  7532. (* ---- SYSTEM.CALL ----- *)
  7533. |Global.systemRef:
  7534. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7535. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7536. s0.mode := ModeValue;
  7537. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7538. result := s0
  7539. (* ---- INCR ----- *)
  7540. |Global.Incr:
  7541. Designate(p0,operand);
  7542. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7543. Dereference(operand,p0.type.resolved,FALSE);
  7544. END;
  7545. ASSERT(p1 # NIL);
  7546. Evaluate(p1,l);
  7547. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7548. ReleaseOperand(operand); ReleaseOperand(l);
  7549. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7550. (* ---- SUM ----- *)
  7551. |Global.Sum: HALT(200);
  7552. (* ---- CAS ----- *)
  7553. |Global.Cas:
  7554. needsTrace := p0.NeedsTrace();
  7555. IF needsTrace THEN ModifyAssignments(true) END;
  7556. Designate(p0,s0);
  7557. Evaluate(p1,s1);
  7558. Evaluate(p2,s2);
  7559. IF needsTrace THEN
  7560. Emit(Push(position, s0.op));
  7561. Emit(Push(position, s1.op));
  7562. Emit(Push(position, s2.op));
  7563. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7564. ELSE
  7565. Emit(Cas(position,s0.op,s1.op,s2.op));
  7566. END;
  7567. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7568. IF needsTrace THEN ModifyAssignments(false) END;
  7569. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7570. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7571. IF conditional THEN
  7572. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7573. BrL(falseLabel);
  7574. ReleaseIntermediateOperand(res);
  7575. END;
  7576. (* ---- DIM ----- *)
  7577. |Global.Dim:
  7578. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7579. Designate(p0,s0);
  7580. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7581. Dereference(s0,p0.type.resolved,FALSE);
  7582. END;
  7583. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7584. ReleaseOperand(s0);
  7585. (* ---- RESHAPE ----- *)
  7586. |Global.Reshape:
  7587. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7588. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7589. left.SetType(procedure.type);
  7590. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7591. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7592. END;
  7593. (* ---- SYSTEM.TYPECODE ----- *)
  7594. |Global.systemTypeCode:
  7595. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7596. IF type.resolved IS SyntaxTree.PointerType THEN
  7597. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7598. END;
  7599. result.op := TypeDescriptorAdr(type);
  7600. IF ~newObjectFile THEN
  7601. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7602. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7603. END;
  7604. result.mode := ModeValue;
  7605. (* ---- SYSTEM.TRACE ----- *)
  7606. |Global.systemTrace:
  7607. SystemTrace(x.parameters, x.position);
  7608. (* ----- CONNECT ------*)
  7609. |Global.Connect:
  7610. IF backend.cellsAreObjects THEN
  7611. PushPort(p0);
  7612. PushPort(p1);
  7613. IF p2 # NIL THEN
  7614. Evaluate(p2, s2);
  7615. Emit(Push(p2.position, s2.op));
  7616. ReleaseOperand(s2);
  7617. ELSE
  7618. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7619. END;
  7620. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7621. ELSE
  7622. Warning(x.position, "cannot run on final hardware");
  7623. END;
  7624. (* ----- DELEGATE ------*)
  7625. |Global.Delegate:
  7626. IF backend.cellsAreObjects THEN
  7627. PushPort(p0);
  7628. PushPort(p1);
  7629. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7630. ELSE
  7631. Warning(x.position, "cannot run on final hardware");
  7632. END;
  7633. (* ----- SEND ------*)
  7634. |Global.Send:
  7635. Evaluate(p0,s0);
  7636. Evaluate(p1,s1);
  7637. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7638. Emit(Push(position,s0.op));
  7639. Emit(Push(position,s1.op));
  7640. (*
  7641. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7642. *)
  7643. IF ~backend.cellsAreObjects THEN
  7644. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7645. END;
  7646. ReleaseOperand(s0);
  7647. ReleaseOperand(s1);
  7648. IF backend.cellsAreObjects THEN
  7649. CallThis(position,"ActiveCellsRuntime","Send",2);
  7650. ELSE
  7651. CallThis(position,ChannelModuleName,"Send",2);
  7652. END;
  7653. (* ----- RECEIVE ------*)
  7654. |Global.Receive:
  7655. Evaluate(p0,s0);
  7656. Emit(Push(position,s0.op));
  7657. Designate(p1,s1);
  7658. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7659. Emit(Push(position,s1.op));
  7660. IF p2 # NIL THEN
  7661. Designate(p2,s2);
  7662. Emit(Push(position,s2.op));
  7663. END;
  7664. (*
  7665. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7666. *)
  7667. IF ~backend.cellsAreObjects THEN
  7668. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7669. END;
  7670. ReleaseOperand(s0);
  7671. ReleaseOperand(s1);
  7672. ReleaseOperand(s2);
  7673. IF backend.cellsAreObjects THEN
  7674. IF p2 = NIL THEN
  7675. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7676. ELSE
  7677. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7678. END;
  7679. ELSE
  7680. IF p2 = NIL THEN
  7681. CallThis(position,ChannelModuleName,"Receive",2)
  7682. ELSE
  7683. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7684. END;
  7685. END;
  7686. | Global.systemSpecial:
  7687. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7688. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7689. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7690. (* determine if parameters are of the VAR kind *)
  7691. ASSERT(x.parameters.Length() <= 3);
  7692. formalParameter := procedureType.firstParameter;
  7693. FOR i := 0 TO x.parameters.Length() - 1 DO
  7694. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7695. formalParameter := formalParameter.nextParameter
  7696. END;
  7697. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7698. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7699. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7700. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7701. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7702. IF procedureType.returnType # NIL THEN
  7703. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7704. Emit(Result(position, res));
  7705. (*InitOperand(result,ModeValue);
  7706. result.op := res;
  7707. *)
  7708. IF ~conditional THEN
  7709. InitOperand(result,ModeValue); result.op := res;
  7710. ELSE
  7711. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7712. BrL(falseLabel);
  7713. ReleaseIntermediateOperand(res);
  7714. END;
  7715. END
  7716. ELSE (* function not yet implemented *)
  7717. Error(position,"not yet implemented");
  7718. END;
  7719. destination := dest;
  7720. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7721. END VisitBuiltinCallDesignator;
  7722. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7723. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7724. BEGIN
  7725. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7726. dest := destination; destination := emptyOperand;
  7727. Expression(x.left);
  7728. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7729. ELSIF isUnchecked THEN (* no check *)
  7730. ELSE
  7731. trueL := NewLabel();
  7732. falseL := NewLabel();
  7733. IF IsPointerToRecord(x.left.type,recordType) THEN
  7734. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7735. Emit(Mov(position,tag, result.op));
  7736. IF result.mode # ModeValue THEN
  7737. ptr := tag;
  7738. IntermediateCode.MakeMemory(ptr,addressType);
  7739. Emit(Mov(position,tag, ptr));
  7740. END;
  7741. IF ~backend.cooperative THEN
  7742. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7743. END;
  7744. IntermediateCode.MakeMemory(tag,addressType);
  7745. ELSE
  7746. tag := result.tag;
  7747. END;
  7748. TypeTest(tag,x.type,trueL,falseL);
  7749. ReleaseIntermediateOperand(tag);
  7750. SetLabel(falseL);
  7751. EmitTrap(position,TypeCheckTrap);
  7752. SetLabel(trueL);
  7753. END;
  7754. destination := dest;
  7755. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7756. END VisitTypeGuardDesignator;
  7757. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7758. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7759. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7760. VAR label: Label; pc: LONGINT;
  7761. BEGIN
  7762. IF backend.cooperative & ~isUnchecked THEN
  7763. pc := section.pc;
  7764. label := NewLabel();
  7765. BrneL(label, operand.op, nil);
  7766. EmitTrap(position, NilPointerTrap);
  7767. SetLabel(label);
  7768. INC(statCoopNilCheck, section.pc - pc);
  7769. END;
  7770. END NilCheck;
  7771. BEGIN
  7772. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7773. ReuseCopy(dereferenced,operand.op);
  7774. ReleaseOperand(operand);
  7775. operand.mode := ModeReference;
  7776. operand.op := dereferenced;
  7777. operand.tag := dereferenced;
  7778. UseIntermediateOperand(operand.tag);
  7779. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7780. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7781. ReleaseIntermediateOperand(operand.tag);
  7782. operand.tag := TypeDescriptorAdr(type);
  7783. IF ~newObjectFile THEN
  7784. IntermediateCode.MakeMemory(operand.tag,addressType);
  7785. END;
  7786. ELSE
  7787. IF ~backend.cooperative THEN
  7788. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7789. END;
  7790. IntermediateCode.MakeMemory(operand.tag,addressType);
  7791. END;
  7792. NilCheck(operand.op);
  7793. ELSIF type IS SyntaxTree.ArrayType THEN
  7794. IF isUnsafe THEN
  7795. NilCheck(operand.op);
  7796. ReleaseIntermediateOperand(operand.tag);
  7797. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7798. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7799. ELSE
  7800. operand.tag := emptyOperand;
  7801. END;
  7802. ELSE
  7803. NilCheck(operand.op);
  7804. IF backend.cooperative THEN
  7805. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7806. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7807. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7808. ELSE
  7809. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7810. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7811. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7812. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7813. END;
  7814. END;
  7815. ELSIF type IS SyntaxTree.MathArrayType THEN
  7816. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7817. IntermediateCode.MakeMemory(operand.op,addressType);
  7818. ELSE HALT(100);
  7819. END;
  7820. END Dereference;
  7821. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7822. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7823. BEGIN
  7824. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7825. dest := destination; destination := emptyOperand;
  7826. Evaluate(x.left,d);
  7827. type := x.type.resolved;
  7828. prevIsUnchecked := isUnchecked;
  7829. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7830. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7831. END;
  7832. Dereference(d,type,IsUnsafePointer(x.left.type));
  7833. isUnchecked := prevIsUnchecked;
  7834. result := d;
  7835. 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
  7836. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7837. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7838. END;
  7839. END;
  7840. destination := dest;
  7841. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7842. END VisitDereferenceDesignator;
  7843. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7844. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7845. BEGIN
  7846. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7847. dest := destination; destination := emptyOperand;
  7848. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7849. tag := result.op;
  7850. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7851. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7852. StaticCallOperand(result,procedure.super);
  7853. ReleaseIntermediateOperand(result.tag);
  7854. UseIntermediateOperand(tag); (* necessary ? *)
  7855. result.tag := tag;
  7856. destination := dest;
  7857. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7858. END VisitSupercallDesignator;
  7859. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7860. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7861. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7862. name: Basic.SegmentedName;
  7863. BEGIN
  7864. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7865. dest := destination; destination := emptyOperand;
  7866. scope := currentScope;
  7867. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7868. scope := scope.outerScope;
  7869. END;
  7870. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7871. IF newObjectFile THEN
  7872. moduleSection := meta.ModuleSection();
  7873. IF backend.cooperative THEN
  7874. moduleOffset := 0;
  7875. ELSE
  7876. moduleOffset := moduleSection.pc;
  7877. END;
  7878. result.mode := ModeValue;
  7879. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7880. ELSE
  7881. Symbol(moduleSelf,result);
  7882. IntermediateCode.MakeMemory(result.op,addressType);
  7883. END
  7884. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7885. result.mode := ModeValue;
  7886. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7887. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7888. ELSE
  7889. GetBaseRegister(basereg,currentScope,scope);
  7890. InitOperand(result,ModeReference);
  7891. result.op := basereg;
  7892. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7893. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7894. IF backend.cooperative THEN
  7895. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7896. END;
  7897. (* tag must be loaded when dereferencing SELF pointer *)
  7898. END;
  7899. destination := dest;
  7900. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7901. END VisitSelfDesignator;
  7902. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7903. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7904. BEGIN
  7905. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7906. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7907. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7908. parameter := procedureType.returnParameter;
  7909. VisitParameter(parameter);
  7910. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7911. END VisitResultDesignator;
  7912. (** values *)
  7913. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7914. BEGIN
  7915. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7916. IF conditional THEN
  7917. IF x.value THEN BrL(trueLabel)
  7918. ELSE BrL(falseLabel)
  7919. END;
  7920. ELSE
  7921. InitOperand(result,ModeValue);
  7922. IF x.value THEN result.op := true ELSE result.op := false END;
  7923. END;
  7924. END VisitBooleanValue;
  7925. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7926. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7927. BEGIN
  7928. Global.GetModuleSegmentedName(module.module, name);
  7929. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7930. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7931. RETURN section
  7932. END GetDataSection;
  7933. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7934. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7935. BEGIN
  7936. type := vop.type;
  7937. data := GetDataSection();
  7938. pc := EnterImmediate(data,vop);
  7939. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7940. IntermediateCode.MakeMemory(vop, type);
  7941. END GetImmediateMem;
  7942. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7943. BEGIN
  7944. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7945. InitOperand(result,ModeValue);
  7946. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7947. IF ~supportedImmediate(result.op) &~inData THEN
  7948. GetImmediateMem(result.op)
  7949. END;
  7950. END VisitIntegerValue;
  7951. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7952. BEGIN
  7953. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7954. InitOperand(result,ModeValue);
  7955. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7956. END VisitCharacterValue;
  7957. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7958. BEGIN
  7959. IF Trace THEN TraceEnter("VisitSetValue") END;
  7960. InitOperand(result,ModeValue);
  7961. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7962. END VisitSetValue;
  7963. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7964. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7965. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7966. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7967. BEGIN
  7968. numberElements := x.elements.Length();
  7969. FOR i := 0 TO numberElements-1 DO
  7970. expression := x.elements.GetExpression(i);
  7971. IF expression IS SyntaxTree.MathArrayExpression THEN
  7972. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7973. ELSE
  7974. inData := TRUE;
  7975. Evaluate(expression,op);
  7976. irv.Emit(Data(position,op.op));
  7977. inData := FALSE;
  7978. ReleaseOperand(op);
  7979. END;
  7980. END;
  7981. END RecursiveData;
  7982. BEGIN
  7983. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  7984. IF ~TryConstantDeclaration() THEN
  7985. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  7986. GetCodeSectionNameForSymbol(constantDeclaration,name);
  7987. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  7988. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  7989. ELSE
  7990. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  7991. END;
  7992. RecursiveData(x.array);
  7993. InitOperand(result,ModeReference);
  7994. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  7995. END
  7996. END VisitMathArrayValue;
  7997. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  7998. VAR constant: Sections.Section;
  7999. BEGIN
  8000. IF constantDeclaration = NIL THEN
  8001. RETURN FALSE
  8002. ELSE
  8003. (* Is a constant in this module: did we generate it already? *)
  8004. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8005. IF constant # NIL THEN
  8006. InitOperand(result,ModeReference);
  8007. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8008. RETURN TRUE;
  8009. END;
  8010. END;
  8011. RETURN FALSE
  8012. END TryConstantDeclaration;
  8013. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8014. BEGIN
  8015. constantDeclaration := x;
  8016. x.value.resolved.Accept(SELF);
  8017. END VisitConstant;
  8018. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8019. BEGIN
  8020. IF Trace THEN TraceEnter("VisitRealValue") END;
  8021. InitOperand(result,ModeValue);
  8022. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8023. END VisitRealValue;
  8024. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8025. VAR
  8026. componentType: SyntaxTree.Type;
  8027. BEGIN
  8028. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8029. ASSERT(x.type IS SyntaxTree.ComplexType);
  8030. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8031. InitOperand(result,ModeValue);
  8032. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8033. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8034. END VisitComplexValue;
  8035. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8036. VAR i: LONGINT; name: Basic.SegmentedName;
  8037. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8038. BEGIN
  8039. IF Trace THEN TraceEnter("VisitStringValue") END;
  8040. IF ~TryConstantDeclaration() THEN
  8041. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8042. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8043. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8044. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8045. ELSE
  8046. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8047. END;
  8048. FOR i := 0 TO x.length-1 DO
  8049. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8050. irv.Emit(Data(position,op));
  8051. END;
  8052. InitOperand(result,ModeReference);
  8053. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8054. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8055. END
  8056. END VisitStringValue;
  8057. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8058. BEGIN
  8059. IF Trace THEN TraceEnter("VisitNilValue") END;
  8060. InitOperand(result,ModeValue);
  8061. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8062. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8063. END VisitNilValue;
  8064. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8065. BEGIN
  8066. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8067. InitOperand(result,ModeValue);
  8068. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8069. END VisitEnumerationValue;
  8070. (** symbols *)
  8071. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8072. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8073. END VisitImport;
  8074. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8075. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8076. BEGIN
  8077. IF scope # baseScope THEN
  8078. (* left := [fp+8] *)
  8079. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8080. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8081. ReuseCopy(left,right);
  8082. ReleaseIntermediateOperand(right);
  8083. scope := scope.outerScope; DEC(level);
  8084. (* { left := [left+8] } *)
  8085. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8086. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8087. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8088. Emit(Mov(position,left,right));
  8089. scope := scope.outerScope; DEC(level);
  8090. END;
  8091. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8092. result := left;
  8093. ELSE
  8094. result := fp;
  8095. END;
  8096. END GetBaseRegister;
  8097. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8098. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8099. BEGIN
  8100. IF Trace THEN TraceEnter("VisitVariable"); END;
  8101. type := x.type.resolved;
  8102. IF (x.useRegister) THEN
  8103. InitOperand(result, ModeValue);
  8104. IF x.registerNumber < 0 THEN
  8105. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8106. reg := x.registerNumber;
  8107. ELSE
  8108. reg := registerUsageCount.Map(x.registerNumber);
  8109. UseRegister(reg);
  8110. END;
  8111. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8112. ELSIF x.externalName # NIL THEN
  8113. InitOperand(result,ModeReference);
  8114. Basic.ToSegmentedName(x.externalName^, name);
  8115. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8116. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8117. InitOperand(result,ModeReference);
  8118. GetBaseRegister(result.op,currentScope,x.scope);
  8119. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8120. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8121. InitOperand(result,ModeReference);
  8122. GetCodeSectionNameForSymbol(x,name);
  8123. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8124. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8125. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8126. InitOperand(result,ModeReference);
  8127. GetCodeSectionNameForSymbol(x,name);
  8128. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8129. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8130. ELSE (* field, left designator must have been emitted *)
  8131. ASSERT(result.mode = ModeReference);
  8132. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8133. ReuseCopy(temp,result.op);
  8134. ReleaseIntermediateOperand(result.op);
  8135. result.op := temp;
  8136. END;
  8137. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8138. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8139. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8140. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8141. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8142. END;
  8143. END;
  8144. END;
  8145. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8146. ValueToCondition(result);
  8147. ELSIF type IS SyntaxTree.ProcedureType THEN
  8148. ReleaseIntermediateOperand(result.tag);
  8149. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8150. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8151. UseIntermediateOperand(result.tag);
  8152. ELSE
  8153. result.tag := nil; (* nil *)
  8154. END;
  8155. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8156. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8157. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8158. ReleaseIntermediateOperand(result.tag);
  8159. Global.GetSymbolSegmentedName(x,name);
  8160. Basic.AppendToSegmentedName(name,"@len");
  8161. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8162. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8163. ELSE
  8164. END;
  8165. ELSE
  8166. ReleaseIntermediateOperand(result.tag);
  8167. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8168. END;
  8169. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8170. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8171. ReleaseIntermediateOperand(result.tag);
  8172. result.tag := result.op;
  8173. UseIntermediateOperand(result.tag);
  8174. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8175. IntermediateCode.MakeMemory(result.op,addressType);
  8176. END;
  8177. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8178. ReleaseIntermediateOperand(result.tag);
  8179. result.tag := TypeDescriptorAdr(type);
  8180. IF ~newObjectFile THEN
  8181. IntermediateCode.MakeMemory(result.tag,addressType);
  8182. END;
  8183. UseIntermediateOperand(result.tag);
  8184. (* tag for pointer type computed not here but during dereferencing *)
  8185. END;
  8186. IF Trace THEN TraceExit("VisitVariable") END;
  8187. END VisitVariable;
  8188. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8189. BEGIN
  8190. VisitVariable(property);
  8191. END VisitProperty;
  8192. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8193. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8194. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8195. BEGIN
  8196. type := x.type.resolved;
  8197. IF Trace THEN TraceEnter("VisitParameter") END;
  8198. IF x.ownerType IS SyntaxTree.CellType THEN
  8199. ptype := x.type.resolved;
  8200. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8201. IF ~(ptype IS SyntaxTree.PortType) THEN
  8202. InitOperand(result,ModeReference);
  8203. GetCodeSectionNameForSymbol(x,name);
  8204. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8205. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8206. RETURN
  8207. ELSE
  8208. InitOperand(result, ModeValue);
  8209. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8210. adr := 0;
  8211. WHILE parameter # x DO
  8212. parameterType := parameter.type;
  8213. inc := 1;
  8214. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8215. INC(adr, inc);
  8216. parameter := parameter.nextParameter
  8217. END;
  8218. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8219. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8220. RETURN
  8221. END;
  8222. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8223. InitOperand(result,ModeReference);
  8224. GetCodeSectionNameForSymbol(x,name);
  8225. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8226. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8227. RETURN
  8228. ELSE
  8229. GetBaseRegister(basereg,currentScope,x.scope);
  8230. InitOperand(result,ModeReference);
  8231. result.op := basereg;
  8232. END;
  8233. IF IsOpenArray(type) THEN
  8234. result.tag := basereg;
  8235. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8236. IntermediateCode.MakeMemory(result.op,addressType);
  8237. IF Global.IsOberonProcedure(x.ownerType) THEN
  8238. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8239. UseIntermediateOperand(result.tag);
  8240. ELSE
  8241. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8242. END;
  8243. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8244. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8245. ELSIF IsStaticArray(type) THEN
  8246. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8247. IntermediateCode.MakeMemory(result.op,addressType);
  8248. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8249. ELSIF type IS SyntaxTree.MathArrayType THEN
  8250. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8251. WITH type: SyntaxTree.MathArrayType DO
  8252. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8253. IF type.form = SyntaxTree.Tensor THEN
  8254. ELSIF type.form = SyntaxTree.Open THEN
  8255. result.tag := result.op;
  8256. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8257. IntermediateCode.MakeMemory(result.op,addressType);
  8258. UseIntermediateOperand(result.tag);
  8259. ELSIF type.form = SyntaxTree.Static THEN
  8260. IF x.kind = SyntaxTree.ConstParameter THEN
  8261. IntermediateCode.MakeMemory(result.op,addressType);
  8262. END;
  8263. ELSE HALT(100)
  8264. END;
  8265. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8266. IF type.form = SyntaxTree.Tensor THEN
  8267. ToMemory(result.op,addressType,0);
  8268. ELSIF type.form = SyntaxTree.Open THEN
  8269. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8270. ReuseCopy(result.tag, mem);
  8271. ReleaseIntermediateOperand(mem);
  8272. ReleaseIntermediateOperand(result.op);
  8273. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8274. ELSIF type.form = SyntaxTree.Static THEN
  8275. IntermediateCode.MakeMemory(result.op,addressType);
  8276. ELSE HALT(100)
  8277. END;
  8278. ELSE HALT(100)
  8279. END;
  8280. END;
  8281. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8282. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8283. IntermediateCode.MakeMemory(result.op,addressType);
  8284. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8285. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8286. END;
  8287. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8288. ValueToCondition(result);
  8289. ELSIF type IS SyntaxTree.ProcedureType THEN
  8290. ReleaseIntermediateOperand(result.tag);
  8291. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8292. IF x.kind = SyntaxTree.VarParameter THEN
  8293. ReuseCopy(result.tag,result.op);
  8294. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8295. IntermediateCode.MakeMemory(result.tag,addressType);
  8296. ELSE
  8297. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8298. UseIntermediateOperand(result.tag);
  8299. END;
  8300. ELSE
  8301. result.tag := nil;
  8302. END;
  8303. (* tag for pointer type computed not here but during dereferencing *)
  8304. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8305. ReleaseIntermediateOperand(result.tag);
  8306. result.tag := basereg;
  8307. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8308. IntermediateCode.MakeMemory(result.tag,addressType);
  8309. UseIntermediateOperand(result.tag);
  8310. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8311. ReleaseIntermediateOperand(result.tag);
  8312. result.tag := TypeDescriptorAdr(type);
  8313. IF ~newObjectFile THEN
  8314. IntermediateCode.MakeMemory(result.tag,addressType);
  8315. END;
  8316. UseIntermediateOperand(result.tag);
  8317. END;
  8318. IF Trace THEN TraceExit("VisitParameter") END;
  8319. END VisitParameter;
  8320. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8321. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8322. BEGIN
  8323. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8324. (* left.p: left already emitted *)
  8325. tag := result.op; (* value of pointer to left *)
  8326. (* get type desc *)
  8327. tmp := result.tag;
  8328. IntermediateCode.MakeMemory(tmp,addressType);
  8329. (* get method adr *)
  8330. Reuse1(reg,tmp);
  8331. ReleaseIntermediateOperand(tmp);
  8332. IF backend.cooperative THEN
  8333. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8334. WHILE recordType.baseType # NIL DO
  8335. recordType := recordType.GetBaseRecord ();
  8336. END;
  8337. GetRecordTypeName (recordType,name);
  8338. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8339. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8340. offset := 0;
  8341. ELSE
  8342. offset := 2;
  8343. END;
  8344. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8345. ELSE
  8346. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8347. END;
  8348. InitOperand(operand,ModeReference);
  8349. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8350. operand.op := reg;
  8351. operand.tag := tag;
  8352. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8353. END DynamicCallOperand;
  8354. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8355. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8356. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8357. BEGIN
  8358. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8359. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8360. tag := operand.op;
  8361. ReleaseIntermediateOperand(operand.tag);
  8362. ELSE tag := nil
  8363. END;
  8364. IF x.isInline THEN
  8365. sectionType := Sections.InlineCodeSection;
  8366. ELSE
  8367. sectionType := Sections.CodeSection;
  8368. END;
  8369. IF x.externalName # NIL THEN
  8370. Basic.ToSegmentedName(x.externalName^, name);
  8371. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8372. ELSE
  8373. GetCodeSectionNameForSymbol(x, name);
  8374. IF (x.scope.ownerModule = module.module) THEN
  8375. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8376. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8377. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8378. IF source.pc = 0 THEN (* no code yet *)
  8379. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8380. END;
  8381. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8382. bits := x.procedureScope.body.code.inlineCode;
  8383. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8384. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8385. binary.CopyBits(bits, 0, bits.GetSize());
  8386. source.SetResolved(binary);
  8387. ELSE
  8388. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8389. END;
  8390. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8391. END;
  8392. InitOperand(operand,ModeValue);
  8393. operand.op := reg;
  8394. operand.tag := tag;
  8395. IF Trace THEN TraceExit("StaticCallOperand") END;
  8396. END StaticCallOperand;
  8397. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8398. (* handle expressions of the form designator.procedure or procedure *)
  8399. BEGIN
  8400. IF Trace THEN TraceEnter("VisitProcedure") END;
  8401. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8402. DynamicCallOperand(result,x);
  8403. ELSIF x.isInline THEN
  8404. StaticCallOperand(result,x);
  8405. ELSE
  8406. StaticCallOperand(result,x);
  8407. END;
  8408. IF Trace THEN TraceExit("VisitProcedure") END;
  8409. END VisitProcedure;
  8410. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8411. BEGIN
  8412. VisitProcedure(x);
  8413. END VisitOperator;
  8414. (** statements *)
  8415. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8416. BEGIN
  8417. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8418. Expression(x.call);
  8419. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8420. ReleaseOperand(result)
  8421. END;
  8422. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8423. END VisitProcedureCallStatement;
  8424. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8425. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8426. leftBase, rightBase: SyntaxTree.Type;
  8427. procedureName,s: SyntaxTree.IdentifierString;
  8428. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8429. size: LONGINT; rightKind: LONGINT;
  8430. dummy: IntermediateCode.Operand;
  8431. CONST moduleName = "FoxArrayBase";
  8432. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8433. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8434. BEGIN
  8435. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8436. IF base IS SyntaxTree.MathArrayType THEN
  8437. base := OpenArray(base(SyntaxTree.MathArrayType));
  8438. END;
  8439. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8440. result.SetArrayBase(base);
  8441. RETURN result
  8442. END OpenArray;
  8443. BEGIN
  8444. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8445. SaveRegisters();ReleaseUsedRegisters(saved);
  8446. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8447. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8448. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8449. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8450. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8451. IF leftType.form = SyntaxTree.Tensor THEN
  8452. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8453. ELSIF leftType.form = SyntaxTree.Open THEN
  8454. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8455. ELSIF leftType.form = SyntaxTree.Static THEN
  8456. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8457. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8458. END;
  8459. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8460. IF procedure = NIL THEN
  8461. s := "Instruction not supported on target, emulation procedure ";
  8462. Strings.Append(s,moduleName);
  8463. Strings.Append(s,".");
  8464. Strings.Append(s,procedureName);
  8465. Strings.Append(s," not present");
  8466. Error(position,s);
  8467. ELSE
  8468. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8469. parameter.SetType(leftType);
  8470. parameter.SetAccess(SyntaxTree.Internal);
  8471. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8472. parameter.SetKind(rightKind);
  8473. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8474. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8475. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8476. StaticCallOperand(result,procedure);
  8477. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8478. ReleaseOperand(result);
  8479. END;
  8480. RestoreRegisters(saved);
  8481. END;
  8482. END AssignMathArray;
  8483. VAR modifyAssignmentCounter := 0: LONGINT;
  8484. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8485. VAR processor,mem,dst: IntermediateCode.Operand;
  8486. BEGIN
  8487. IF value.intValue = true.intValue THEN
  8488. INC(modifyAssignmentCounter);
  8489. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8490. modifyAssignmentsPC := section.pc;
  8491. ELSE
  8492. DEC(modifyAssignmentCounter);
  8493. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8494. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8495. END;
  8496. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8497. dst := NewRegisterOperand (addressType);
  8498. Emit(Mov(position,dst, processor));
  8499. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8500. Emit(Mov(position,mem, value));
  8501. ReleaseIntermediateOperand(dst);
  8502. END ModifyAssignments;
  8503. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8504. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8505. BEGIN
  8506. type := left.type.resolved;
  8507. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8508. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8509. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8510. IF procedureType.returnParameter = parameter THEN
  8511. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8512. END;
  8513. END;
  8514. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8515. END CopySize;
  8516. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8517. VAR
  8518. leftO, rightO: Operand;
  8519. mem, sizeOp: IntermediateCode.Operand;
  8520. leftType, rightType, componentType: SyntaxTree.Type;
  8521. size: LONGINT;
  8522. parameters: SyntaxTree.ExpressionList;
  8523. procedure: SyntaxTree.Procedure;
  8524. call: SyntaxTree.ProcedureCallDesignator;
  8525. designator: SyntaxTree.Designator;
  8526. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8527. VAR procedureType: SyntaxTree.ProcedureType;
  8528. BEGIN
  8529. IF ReturnedAsParameter(right.type) THEN
  8530. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8531. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8532. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8533. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8534. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8535. IF right.id = Global.Reshape THEN RETURN TRUE
  8536. END;
  8537. END;
  8538. END;
  8539. END;
  8540. RETURN FALSE
  8541. END CanPassAsResultParameter;
  8542. BEGIN
  8543. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8544. leftType := left.type.resolved; rightType:= right.type.resolved;
  8545. IF backend.cooperative & left.NeedsTrace() THEN
  8546. ModifyAssignments(true);
  8547. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8548. Designate(right, rightO);
  8549. Designate(left, leftO);
  8550. ASSERT(leftO.mode = ModeReference);
  8551. TransferToRegister(leftO.op, leftO.op);
  8552. TransferToRegister(rightO.op, rightO.op);
  8553. Emit(Push(position, leftO.op));
  8554. Emit(Push(position, rightO.op));
  8555. CallAssignMethod(leftO.op, rightO.op, left.type);
  8556. Emit(Pop(position, rightO.op));
  8557. Emit(Pop(position, leftO.op));
  8558. sizeOp := CopySize(left);
  8559. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8560. ReleaseOperand(leftO);
  8561. ReleaseOperand(rightO);
  8562. ELSE
  8563. Evaluate(right,rightO);
  8564. Designate(left,leftO);
  8565. ASSERT(leftO.mode = ModeReference);
  8566. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8567. (* copy procedure address first *)
  8568. MakeMemory(mem,leftO.op,addressType,0);
  8569. Emit(Mov(position,mem,rightO.op));
  8570. ReleaseIntermediateOperand(mem);
  8571. (* copy pointer address *)
  8572. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8573. CallAssignPointer(leftO.tag, rightO.tag);
  8574. ELSE
  8575. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8576. CallAssignPointer(leftO.op, rightO.op);
  8577. END;
  8578. ReleaseOperand(leftO);
  8579. ReleaseOperand(rightO);
  8580. END;
  8581. ModifyAssignments(false);
  8582. RETURN;
  8583. END;
  8584. IF CanPassAsResultParameter(right) THEN
  8585. procedureResultDesignator := left(SyntaxTree.Designator);
  8586. Expression(right);
  8587. procedureResultDesignator := NIL;
  8588. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8589. (* left <-- ALIAS OF right: zerocopy *)
  8590. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8591. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8592. designator.SetType(procedure.type);
  8593. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8594. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8595. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8596. END;
  8597. ELSIF leftType IS SyntaxTree.RangeType THEN
  8598. (* LHS is of array range type *)
  8599. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8600. Evaluate(right, rightO);
  8601. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8602. (* first *)
  8603. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8604. Emit(Mov(position,mem, rightO.op));
  8605. ReleaseIntermediateOperand(mem);
  8606. (* last *)
  8607. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8608. Emit(Mov(position,mem, rightO.tag));
  8609. ReleaseIntermediateOperand(mem);
  8610. (* step *)
  8611. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8612. Emit(Mov(position,mem, rightO.extra));
  8613. ReleaseIntermediateOperand(mem);
  8614. ReleaseOperand(rightO);
  8615. ReleaseOperand(leftO)
  8616. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8617. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8618. Evaluate(right, rightO);
  8619. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8620. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8621. (* real part *)
  8622. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8623. Emit(Mov(position,mem, rightO.op));
  8624. ReleaseIntermediateOperand(mem);
  8625. (* imaginary part *)
  8626. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8627. Emit(Mov(position,mem, rightO.tag));
  8628. ReleaseIntermediateOperand(mem);
  8629. ReleaseOperand(rightO);
  8630. ReleaseOperand(leftO)
  8631. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8632. OR (leftType IS SyntaxTree.PortType) THEN
  8633. (* rightO := leftO;*)
  8634. Evaluate(right,rightO);
  8635. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8636. Designate(left,leftO);
  8637. IF leftO.mode = ModeReference THEN
  8638. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8639. destination := mem;
  8640. ELSE
  8641. destination := leftO.op;
  8642. END;
  8643. ReleaseOperand(leftO);
  8644. IF destination.mode # IntermediateCode.Undefined THEN
  8645. Emit(Mov(position,destination,rightO.op));
  8646. END;
  8647. ReleaseOperand(rightO);
  8648. ReleaseIntermediateOperand(mem);
  8649. IntermediateCode.InitOperand(destination);
  8650. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8651. Evaluate(right,rightO);
  8652. Designate(left,leftO);
  8653. MakeMemory(mem,leftO.op,addressType,0);
  8654. Emit(Mov(position,mem,rightO.op));
  8655. ReleaseIntermediateOperand(mem);
  8656. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8657. (* delegate *)
  8658. (*
  8659. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8660. *)
  8661. Emit(Mov(position,leftO.tag,rightO.tag));
  8662. END;
  8663. ReleaseOperand(leftO);
  8664. ReleaseOperand(rightO);
  8665. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8666. Designate(right,rightO);
  8667. Designate(left,leftO);
  8668. sizeOp := CopySize(left);
  8669. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8670. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8671. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8672. IF (rightType IS SyntaxTree.StringType) THEN
  8673. CopyString(left,right);
  8674. 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
  8675. Designate(right,rightO);
  8676. Designate(left,leftO);
  8677. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8678. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8679. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8680. ELSE
  8681. HALT(201)
  8682. END;
  8683. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8684. AssignMathArray(left,right);
  8685. ELSE
  8686. HALT(200);
  8687. END;
  8688. END Assign;
  8689. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8690. BEGIN
  8691. IF Trace THEN TraceEnter("VisitAssignment") END;
  8692. Assign(x.left,x.right);
  8693. IF Trace THEN TraceExit("VisitAssignment") END;
  8694. END VisitAssignment;
  8695. PROCEDURE EmitCooperativeSwitch;
  8696. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8697. BEGIN
  8698. ASSERT (cooperativeSwitches);
  8699. pc := section.pc;
  8700. IF lastSwitchPC = section.pc THEN RETURN END;
  8701. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8702. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8703. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8704. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8705. INC(statCoopSwitch, section.pc - pc);
  8706. END EmitCooperativeSwitch;
  8707. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8708. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8709. BEGIN
  8710. p0 := communication.left; p1 := communication.right;
  8711. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8712. Evaluate(p0,s0);
  8713. Evaluate(p1,s1);
  8714. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8715. Emit(Push(position,s0.op));
  8716. Emit(Push(position,s1.op));
  8717. (*
  8718. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8719. *)
  8720. IF ~backend.cellsAreObjects THEN
  8721. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8722. END;
  8723. ReleaseOperand(s0);
  8724. ReleaseOperand(s1);
  8725. IF backend.cellsAreObjects THEN
  8726. CallThis(position,"ActiveCellsRuntime","Send",2);
  8727. ELSE
  8728. CallThis(position,ChannelModuleName,"Send",2);
  8729. END;
  8730. (* ----- RECEIVE ------*)
  8731. ELSE
  8732. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8733. tmp := p0; p0 := p1; p1 := tmp;
  8734. END;
  8735. Evaluate(p0,s0);
  8736. Emit(Push(position,s0.op));
  8737. Designate(p1,s1);
  8738. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8739. Emit(Push(position,s1.op));
  8740. (*
  8741. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8742. *)
  8743. IF ~backend.cellsAreObjects THEN
  8744. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8745. END;
  8746. ReleaseOperand(s0);
  8747. ReleaseOperand(s1);
  8748. IF backend.cellsAreObjects THEN
  8749. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8750. ELSE
  8751. CallThis(position,ChannelModuleName,"Receive",2)
  8752. END;
  8753. END;
  8754. END VisitCommunicationStatement;
  8755. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8756. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8757. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8758. VAR true, false: Label; condition, value: BOOLEAN;
  8759. BEGIN
  8760. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8761. IF condition THEN
  8762. true := NewLabel();
  8763. false := NewLabel();
  8764. Condition(if.condition,true,false);
  8765. SetLabel(true);
  8766. StatementSequence(if.statements);
  8767. BrL(end);
  8768. SetLabel(false);
  8769. ELSE
  8770. IF value THEN (* always true *)
  8771. escape := TRUE;
  8772. StatementSequence(if.statements);
  8773. (* no branch necessary -- rest skipped *)
  8774. END;
  8775. END;
  8776. END IfPart;
  8777. BEGIN
  8778. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8779. end := NewLabel();
  8780. elsifs := x.ElsifParts();
  8781. IfPart(x.ifPart);
  8782. FOR i := 0 TO elsifs-1 DO
  8783. IF ~escape THEN
  8784. elsif := x.GetElsifPart(i);
  8785. IfPart(elsif);
  8786. END;
  8787. END;
  8788. IF (x.elsePart # NIL) & ~escape THEN
  8789. StatementSequence(x.elsePart);
  8790. END;
  8791. SetLabel(end);
  8792. IF Trace THEN TraceExit("VisitIfStatement") END;
  8793. END VisitIfStatement;
  8794. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8795. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8796. BEGIN
  8797. (*IF x.variable.type.resolved = x.type.resolved THEN
  8798. (* always true, do nothing *)
  8799. ELSE*)
  8800. Designate(x.variable,res);
  8801. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8802. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8803. END;
  8804. trueL := NewLabel();
  8805. TypeTest(res.tag,x.type,trueL,falseL);
  8806. ReleaseOperand(res);
  8807. SetLabel(trueL);
  8808. StatementSequence(x.statements);
  8809. BrL(endL);
  8810. END WithPart;
  8811. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8812. VAR endL,falseL: Label;i: LONGINT;
  8813. BEGIN
  8814. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8815. endL := NewLabel();
  8816. FOR i := 0 TO x.WithParts()-1 DO
  8817. falseL := NewLabel();
  8818. WithPart(x.GetWithPart(i),falseL,endL);
  8819. SetLabel(falseL);
  8820. END;
  8821. IF x.elsePart = NIL THEN
  8822. IF ~isUnchecked THEN
  8823. EmitTrap(position,WithTrap);
  8824. END;
  8825. ELSE
  8826. StatementSequence(x.elsePart)
  8827. END;
  8828. SetLabel(endL);
  8829. IF Trace THEN TraceExit("VisitWithStatement") END;
  8830. END VisitWithStatement;
  8831. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8832. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8833. out,else: Label; label: Label;
  8834. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8835. symbol: SyntaxTree.Symbol;
  8836. BEGIN
  8837. (*! split case statement into if-elsif statements for large case label lists *)
  8838. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8839. size := x.max-x.min+1;
  8840. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8841. END;
  8842. Evaluate(x.variable,var);
  8843. ReuseCopy(tmp,var.op);
  8844. ReleaseIntermediateOperand(var.op);
  8845. var.op := tmp;
  8846. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8847. Convert(var.op,addressType);
  8848. size := x.max-x.min+1;
  8849. else := NewLabel();
  8850. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8851. (*
  8852. UniqueId(name,module.module,"case",caseId);
  8853. *)
  8854. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8855. Global.GetModuleSegmentedName(module.module, name);
  8856. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8857. symbol := SyntaxTree.NewSymbol(name[1]);
  8858. symbol.SetScope(moduleScope);
  8859. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8860. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8861. section.isCaseTable := TRUE;
  8862. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8863. ReuseCopy(res,var.op);
  8864. ReleaseOperand(var);
  8865. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8866. Emit(Add(position,res,res,jmp));
  8867. IntermediateCode.MakeMemory(res,addressType);
  8868. Emit(Br(position,res));
  8869. ReleaseIntermediateOperand(res);
  8870. out := NewLabel();
  8871. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8872. part := x.GetCasePart(i);
  8873. constant := part.firstConstant;
  8874. label := NewLabel();
  8875. SetLabel(label);
  8876. WHILE(constant # NIL) DO (* case labels for this case part *)
  8877. FOR j := constant.min TO constant.max DO
  8878. fixups[j-x.min] := label;
  8879. END;
  8880. constant := constant.next;
  8881. END;
  8882. StatementSequence(part.statements);
  8883. BrL(out);
  8884. END;
  8885. SetLabel(else);
  8886. FOR i := 0 TO size-1 DO
  8887. IF fixups[i] = NIL THEN
  8888. fixups[i] := else;
  8889. END;
  8890. END;
  8891. IF x.elsePart # NIL THEN
  8892. StatementSequence(x.elsePart);
  8893. ELSIF ~isUnchecked THEN
  8894. EmitTrap(position,CaseTrap);
  8895. END;
  8896. SetLabel(out);
  8897. FOR i := 0 TO size-1 DO
  8898. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8899. section.Emit(Data(position,op));
  8900. END;
  8901. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8902. END VisitCaseStatement;
  8903. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8904. VAR start: Label; true,false: Label;
  8905. BEGIN
  8906. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8907. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8908. start := NewLabel();
  8909. true := NewLabel();
  8910. false := NewLabel();
  8911. SetLabel(start);
  8912. Condition(x.condition,true,false);
  8913. SetLabel(true);
  8914. StatementSequence(x.statements);
  8915. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8916. BrL(start);
  8917. SetLabel(false);
  8918. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8919. END VisitWhileStatement;
  8920. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8921. VAR false,true: Label;
  8922. BEGIN
  8923. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8924. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8925. true := NewLabel();
  8926. false := NewLabel();
  8927. SetLabel(false);
  8928. StatementSequence(x.statements);
  8929. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8930. Condition(x.condition,true,false);
  8931. SetLabel(true);
  8932. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8933. END VisitRepeatStatement;
  8934. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8935. VAR
  8936. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8937. temporaryVariable: SyntaxTree.Variable;
  8938. temporaryVariableDesignator : SyntaxTree.Designator;
  8939. BEGIN
  8940. IF Trace THEN TraceEnter("VisitForStatement") END;
  8941. true := NewLabel();
  8942. false := NewLabel();
  8943. start := NewLabel();
  8944. Assign(x.variable,x.from);
  8945. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8946. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8947. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8948. Assign(temporaryVariableDesignator,x.to);
  8949. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8950. IF by > 0 THEN
  8951. cmp := Scanner.LessEqual
  8952. ELSE
  8953. cmp := Scanner.GreaterEqual
  8954. END;
  8955. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8956. binary.SetType(system.booleanType);
  8957. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8958. SetLabel(start);
  8959. Condition(binary,true,false);
  8960. SetLabel(true);
  8961. StatementSequence(x.statements);
  8962. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8963. binary.SetType(x.variable.type);
  8964. Assign(x.variable,binary);
  8965. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8966. BrL(start);
  8967. SetLabel(false);
  8968. IF Trace THEN TraceExit("VisitForStatement") END;
  8969. END VisitForStatement;
  8970. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8971. VAR prevLoop: Label;
  8972. BEGIN
  8973. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8974. prevLoop := currentLoop;
  8975. currentLoop := NewLabel();
  8976. StatementSequence(x.statements);
  8977. SetLabel(currentLoop);
  8978. currentLoop := prevLoop;
  8979. IF Trace THEN TraceExit("VisitExitableBlock") END;
  8980. END VisitExitableBlock;
  8981. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  8982. VAR prevLoop,start: Label;
  8983. BEGIN
  8984. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  8985. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8986. start := NewLabel();
  8987. prevLoop := currentLoop;
  8988. SetLabel(start);
  8989. currentLoop := NewLabel();
  8990. StatementSequence(x.statements);
  8991. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8992. BrL(start);
  8993. SetLabel(currentLoop);
  8994. currentLoop := prevLoop;
  8995. IF Trace THEN TraceExit("VisitLoopStatement") END;
  8996. END VisitLoopStatement;
  8997. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  8998. VAR outer: SyntaxTree.Statement;
  8999. BEGIN
  9000. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9001. IF locked THEN (* r if we jump out of an exclusive block *)
  9002. outer := x.outer;
  9003. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9004. outer := outer.outer;
  9005. END;
  9006. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9007. Lock(FALSE);
  9008. END;
  9009. END;
  9010. BrL(currentLoop);
  9011. IF Trace THEN TraceExit("VisitExitStatement") END;
  9012. END VisitExitStatement;
  9013. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9014. VAR
  9015. expression, parameterDesignator: SyntaxTree.Expression;
  9016. type, componentType: SyntaxTree.Type;
  9017. res, right: Operand;
  9018. left, mem, reg: IntermediateCode.Operand;
  9019. parameter: SyntaxTree.Parameter;
  9020. procedure: SyntaxTree.Procedure;
  9021. procedureType: SyntaxTree.ProcedureType;
  9022. returnTypeOffset: LONGINT;
  9023. delegate: BOOLEAN;
  9024. map: SymbolMap;
  9025. BEGIN
  9026. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9027. expression := x.returnValue;
  9028. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9029. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9030. IF currentIsInline THEN
  9031. IF expression # NIL THEN
  9032. map := currentMapper.Get(NIL);
  9033. IF map # NIL THEN
  9034. Assign(map.to, expression);
  9035. END;
  9036. END;
  9037. BrL(currentInlineExit);
  9038. RETURN;
  9039. END;
  9040. IF expression # NIL THEN
  9041. type := expression.type.resolved;
  9042. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9043. IF locked THEN Lock(FALSE) END;
  9044. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9045. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9046. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9047. (* return without structured return parameter *)
  9048. Evaluate(expression,res);
  9049. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9050. IF locked OR profile THEN
  9051. Emit(Push(position,res.op));
  9052. IF delegate THEN HALT(200) END;
  9053. ReleaseOperand(res);
  9054. IF locked THEN Lock(FALSE) END;
  9055. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9056. reg := NewRegisterOperand(res.op.type);
  9057. Emit(Pop(position,reg));
  9058. Emit(Return(position,reg));
  9059. ReleaseIntermediateOperand(reg);
  9060. ELSE
  9061. Emit(Return(position,res.op));
  9062. ReleaseOperand(res);
  9063. END;
  9064. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9065. THEN
  9066. (* return using structured return parameter *)
  9067. 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)
  9068. OR SemanticChecker.IsPointerType(type));
  9069. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9070. parameter :=procedureType.returnParameter;
  9071. ASSERT(parameter # NIL);
  9072. returnTypeOffset := parameter.offsetInBits;
  9073. (*
  9074. IF parameter# NIL THEN
  9075. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9076. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9077. ELSE
  9078. returnTypeOffset := system.offsetFirstParameter
  9079. END;
  9080. *)
  9081. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9082. IF type IS SyntaxTree.RangeType THEN
  9083. (* array range type *)
  9084. Evaluate(expression, right);
  9085. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9086. Emit(Mov(position,mem, right.op)); (* first *)
  9087. ReleaseIntermediateOperand(mem);
  9088. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9089. Emit(Mov(position,mem, right.tag)); (* last *)
  9090. ReleaseIntermediateOperand(mem);
  9091. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9092. Emit(Mov(position,mem, right.extra)); (* step *)
  9093. ReleaseIntermediateOperand(mem);
  9094. ReleaseOperand(right);
  9095. ELSIF type IS SyntaxTree.ComplexType THEN
  9096. Evaluate(expression, right);
  9097. componentType := type(SyntaxTree.ComplexType).componentType;
  9098. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9099. Emit(Mov(position,mem, right.op)); (* real part *)
  9100. ReleaseIntermediateOperand(mem);
  9101. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9102. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9103. ReleaseIntermediateOperand(mem);
  9104. ReleaseOperand(right);
  9105. ELSE (* covers cases: pointer / record / array *)
  9106. parameter := procedureType.returnParameter;
  9107. checker.SetCurrentScope(currentScope);
  9108. ASSERT(parameter # NIL);
  9109. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9110. Assign(parameterDesignator,expression);
  9111. END;
  9112. ReleaseIntermediateOperand(left);
  9113. IF locked THEN Lock(FALSE) END;
  9114. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9115. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9116. parameter := procedureType.returnParameter;
  9117. checker.SetCurrentScope(currentScope);
  9118. IF parameter = NIL THEN
  9119. Error(procedure.position, "structured return of parameter of procedure not found");
  9120. ELSE
  9121. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9122. Assign(parameterDesignator,expression);
  9123. END;
  9124. IF locked THEN Lock(FALSE) END;
  9125. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9126. ELSE
  9127. HALT(200);
  9128. END;
  9129. ELSE
  9130. IF locked THEN Lock(FALSE) END;
  9131. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9132. END;
  9133. IF backend.cooperative THEN
  9134. BrL(exitLabel);
  9135. ELSE
  9136. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9137. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9138. END;
  9139. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9140. END VisitReturnStatement;
  9141. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9142. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9143. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9144. statements: SyntaxTree.StatementSequence;
  9145. name, suffix: SyntaxTree.IdentifierString;
  9146. BEGIN
  9147. Strings.IntToStr(awaitProcCounter,suffix);
  9148. Strings.Concat("@AwaitProcedure",suffix,name);
  9149. identifier := SyntaxTree.NewIdentifier(name);
  9150. INC(awaitProcCounter);
  9151. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9152. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9153. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9154. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9155. procedure.SetAccess(SyntaxTree.Hidden);
  9156. procedure.SetScope(currentScope);
  9157. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9158. procedureType.SetReturnType(system.booleanType);
  9159. procedure.SetType(procedureType);
  9160. body := SyntaxTree.NewBody(x.position,procedureScope);
  9161. procedureScope.SetBody(body);
  9162. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9163. returnStatement.SetReturnValue(x.condition);
  9164. statements := SyntaxTree.NewStatementSequence();
  9165. statements.AddStatement(returnStatement);
  9166. body.SetStatementSequence(statements);
  9167. currentScope.AddProcedure(procedure);
  9168. RETURN procedure
  9169. END MakeAwaitProcedure;
  9170. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9171. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9172. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9173. BEGIN
  9174. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9175. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9176. IF backend.cooperative THEN
  9177. start := NewLabel();
  9178. true := NewLabel();
  9179. false := NewLabel();
  9180. SetLabel(start);
  9181. Condition(x.condition,true,false);
  9182. SetLabel(false);
  9183. PushSelfPointer();
  9184. CallThis(position,"ExclusiveBlocks","Await",1);
  9185. BrL(start);
  9186. SetLabel(true);
  9187. PushSelfPointer();
  9188. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9189. ELSE
  9190. proc := MakeAwaitProcedure(x);
  9191. Emit(Push(position,fp));
  9192. GetCodeSectionNameForSymbol(proc,name);
  9193. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9194. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9195. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9196. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9197. Emit(Result(position,res));
  9198. (*
  9199. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9200. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9201. *)
  9202. InitOperand(result,ModeValue);
  9203. result.op := res;
  9204. label := NewLabel();
  9205. BreqL(label, result.op, SELF.true);
  9206. ReleaseOperand(result);
  9207. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9208. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9209. Emit(Push(position,res));
  9210. Emit(Push(position,fp));
  9211. PushSelfPointer();
  9212. Emit(Push(position,nil));
  9213. CallThis(position,"Objects","Await",4);
  9214. SetLabel(label);
  9215. END;
  9216. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9217. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9218. END VisitAwaitStatement;
  9219. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9220. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9221. BEGIN
  9222. FOR i := 0 TO x.Length() - 1 DO
  9223. statement := x.GetStatement( i );
  9224. Statement(statement);
  9225. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9226. END;
  9227. END StatementSequence;
  9228. PROCEDURE PushSelfPointer;
  9229. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9230. BEGIN
  9231. scope := currentScope;
  9232. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9233. scope := scope.outerScope;
  9234. END;
  9235. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9236. IF ~newObjectFile THEN
  9237. Symbol(moduleSelf,op);
  9238. IntermediateCode.MakeMemory(op.op,addressType);
  9239. ELSE
  9240. moduleSection := meta.ModuleSection();
  9241. IF backend.cooperative THEN
  9242. moduleOffset := 0;
  9243. ELSE
  9244. moduleOffset := moduleSection.pc;
  9245. END;
  9246. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9247. END;
  9248. ELSE
  9249. GetBaseRegister(op.op,currentScope,scope);
  9250. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9251. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9252. IF backend.cooperative THEN
  9253. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9254. END;
  9255. IntermediateCode.MakeMemory(op.op,addressType);
  9256. END;
  9257. Emit(Push(position,op.op));
  9258. ReleaseOperand(op);
  9259. END PushSelfPointer;
  9260. PROCEDURE Lock(lock: BOOLEAN);
  9261. BEGIN
  9262. IF Trace THEN TraceEnter("Lock") END;
  9263. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9264. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9265. ASSERT(modifyAssignmentCounter = 0);
  9266. IF dump # NIL THEN
  9267. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9268. dump.Ln;dump.Update;
  9269. END;
  9270. PushSelfPointer;
  9271. IF backend.cooperative THEN
  9272. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9273. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9274. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9275. END;
  9276. ELSE
  9277. Emit(Push(position,true));
  9278. IF lock THEN CallThis(position,"Objects","Lock",2)
  9279. ELSE CallThis(position,"Objects","Unlock",2);
  9280. END;
  9281. END;
  9282. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9283. IF Trace THEN TraceExit("Lock") END;
  9284. END Lock;
  9285. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9286. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9287. BEGIN
  9288. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9289. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9290. previouslyUnchecked := isUnchecked;
  9291. isUnchecked := isUnchecked OR x.isUnchecked;
  9292. previouslyCooperativeSwitches := cooperativeSwitches;
  9293. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9294. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9295. IF x.statements # NIL THEN
  9296. StatementSequence(x.statements);
  9297. END;
  9298. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9299. isUnchecked := previouslyUnchecked;
  9300. cooperativeSwitches := previouslyCooperativeSwitches;
  9301. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9302. END VisitStatementBlock;
  9303. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9304. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9305. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9306. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9307. procedure: SyntaxTree.Procedure;
  9308. map: SymbolMap;
  9309. BEGIN
  9310. scope := currentScope;
  9311. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9312. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9313. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9314. return := emptyOperand;
  9315. IF Trace THEN TraceEnter("VisitCode") END;
  9316. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9317. NEW(in, x.inRules.Length());
  9318. FOR i := 0 TO LEN(in)-1 DO
  9319. statement := x.inRules.GetStatement(i);
  9320. WITH statement: SyntaxTree.Assignment DO
  9321. Evaluate(statement.right, operand);
  9322. result := operand.op;
  9323. NEW(str, 64);
  9324. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9325. in[i] := result; IntermediateCode.SetString(in[i], str);
  9326. ReleaseIntermediateOperand(operand.tag);
  9327. END;
  9328. END;
  9329. ELSE in := NIL
  9330. END;
  9331. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9332. NEW(out, x.outRules.Length());
  9333. FOR i := 0 TO LEN(out)-1 DO
  9334. statement := x.outRules.GetStatement(i);
  9335. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9336. WITH statement: SyntaxTree.Assignment DO
  9337. Evaluate(statement.left, operand); (*?? or designate *)
  9338. result := operand.op;
  9339. NEW(str, 64);
  9340. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9341. out[i] := result; IntermediateCode.SetString(out[i], str);
  9342. ReleaseIntermediateOperand(operand.tag);
  9343. |statement: SyntaxTree.ReturnStatement DO
  9344. NEW(str, 64);
  9345. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9346. IF currentIsInline THEN
  9347. map := currentMapper.Get(NIL);
  9348. Evaluate(map.to, operand);
  9349. out[i] := operand.op;
  9350. ELSE
  9351. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9352. END;
  9353. IntermediateCode.SetString(out[i], str);
  9354. ReleaseIntermediateOperand(operand.tag);
  9355. return := out[i];
  9356. ELSE
  9357. END;
  9358. END;
  9359. ELSE out := NIL
  9360. END;
  9361. Emit(Asm(x.position,x.sourceCode, in, out));
  9362. IF in # NIL THEN
  9363. FOR i := 0 TO LEN(in)-1 DO
  9364. ReleaseIntermediateOperand(in[i]);
  9365. END;
  9366. END;
  9367. IF out # NIL THEN
  9368. FOR i := 0 TO LEN(out)-1 DO
  9369. WITH statement: SyntaxTree.Assignment DO
  9370. ReleaseIntermediateOperand(out[i]);
  9371. ELSE
  9372. IF currentIsInline THEN
  9373. ReleaseIntermediateOperand(out[i]);
  9374. END;
  9375. END;
  9376. statement := x.outRules.GetStatement(i);
  9377. END;
  9378. END;
  9379. IF return.mode # IntermediateCode.Undefined THEN
  9380. IF currentIsInline THEN
  9381. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9382. Symbol(procedureType.returnParameter, par);
  9383. MakeMemory(mem, par.op, return.type, 0);
  9384. ReleaseOperand(par);
  9385. Emit(Mov(position, mem, return));
  9386. ReleaseIntermediateOperand(mem);
  9387. ELSE
  9388. Emit(Return(position,return));
  9389. END;
  9390. IF currentIsInline THEN RETURN END;
  9391. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9392. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9393. ReleaseIntermediateOperand(return);
  9394. END;
  9395. IF Trace THEN TraceExit("VisitCode") END;
  9396. END VisitCode;
  9397. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9398. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9399. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9400. const, call: IntermediateCode.Operand;
  9401. parameterDesignator: SyntaxTree.Expression;
  9402. saved: RegisterEntry;
  9403. name: Basic.SegmentedName;
  9404. BEGIN
  9405. IF Trace THEN TraceEnter("ParameterCopies") END;
  9406. parameter := x.firstParameter;
  9407. WHILE parameter # NIL DO
  9408. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9409. type := parameter.type.resolved;
  9410. IF IsOpenArray(type) THEN
  9411. VisitParameter(parameter);
  9412. op := result;
  9413. IF backend.cooperative & parameter.NeedsTrace() THEN
  9414. length := GetArrayLength(type, op.tag);
  9415. size := NewRegisterOperand(addressType);
  9416. base := ArrayBaseType(type);
  9417. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9418. Emit(Mul(position, size, length, const));
  9419. dst := NewRegisterOperand (addressType);
  9420. Emit(Mov(position, dst, size));
  9421. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9422. Emit(Sub(position,dst,sp,dst));
  9423. Emit(And(position,dst,dst,const));
  9424. Emit(Mov(position,sp,dst));
  9425. par := fp;
  9426. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9427. IntermediateCode.InitImmediate(null, byteType, 0);
  9428. Emit(Fill(position, dst, size, null));
  9429. ReleaseIntermediateOperand(dst);
  9430. ReleaseIntermediateOperand(length);
  9431. SaveRegisters();ReleaseUsedRegisters(saved);
  9432. (* register dst has been freed before SaveRegisters already *)
  9433. base := ArrayBaseType(type);
  9434. (* assign method of open array *)
  9435. IF base.IsRecordType() THEN
  9436. Emit (Push(position, length));
  9437. Emit (Push(position, dst));
  9438. Emit (Push(position, op.op));
  9439. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9440. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9441. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9442. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9443. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9444. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9445. Emit (Push(position,length));
  9446. Emit (Push(position, dst));
  9447. Emit (Push(position, length));
  9448. Emit (Push(position, op.op));
  9449. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9450. ELSE
  9451. Emit (Push(position, length));
  9452. Emit (Push(position, dst));
  9453. Emit (Push(position, length));
  9454. Emit (Push(position, op.op));
  9455. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9456. ASSERT(ArrayBaseType(type).IsPointer());
  9457. END;
  9458. RestoreRegisters(saved);
  9459. ELSE
  9460. temp := GetDynamicSize(type,op.tag);
  9461. ReuseCopy(size,temp);
  9462. ReleaseIntermediateOperand(temp);
  9463. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9464. Emit(Sub(position,size,sp,size));
  9465. Emit(And(position,size,size,const));
  9466. Emit(Mov(position,sp,size));
  9467. par := fp;
  9468. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9469. ReleaseIntermediateOperand(size);
  9470. size := GetDynamicSize(type,op.tag);
  9471. END;
  9472. Emit(Copy(position,sp,op.op,size));
  9473. ReleaseIntermediateOperand(size);
  9474. ReleaseOperand(op);
  9475. IntermediateCode.MakeMemory(par,addressType);
  9476. Emit(Mov(position,par,sp));
  9477. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9478. checker.SetCurrentScope(currentScope);
  9479. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9480. Assign(parameterDesignator,parameterDesignator);
  9481. END;
  9482. END;
  9483. parameter := parameter.nextParameter;
  9484. END;
  9485. IF Trace THEN TraceExit("ParameterCopies") END;
  9486. END ParameterCopies;
  9487. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9488. VAR x: SyntaxTree.Variable;
  9489. BEGIN
  9490. x := scope.firstVariable;
  9491. WHILE x # NIL DO
  9492. InitVariable(x);
  9493. x := x.nextVariable;
  9494. END;
  9495. END InitVariables;
  9496. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9497. BEGIN
  9498. IF (symbol # NIL) THEN
  9499. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9500. ELSE
  9501. RETURN 0
  9502. END;
  9503. END GetFingerprint;
  9504. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9505. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9506. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9507. saved: RegisterEntry;
  9508. BEGIN
  9509. IF Trace THEN TraceEnter("Body") END;
  9510. ReleaseUsedRegisters(saved); (* just in case ... *)
  9511. section := ir;
  9512. exitLabel := NewLabel ();
  9513. IF moduleBody THEN moduleBodySection := section END;
  9514. IF ir.comments # NIL THEN
  9515. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9516. commentPrintout.SingleStatement(TRUE);
  9517. dump := ir.comments;
  9518. ELSE
  9519. commentPrintout := NIL;
  9520. dump := NIL;
  9521. END;
  9522. registerUsageCount.Init;
  9523. prevScope := currentScope;
  9524. currentScope := scope;
  9525. lastSwitchPC := 0;
  9526. cooperativeSwitches := backend.cooperative;
  9527. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9528. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9529. IF x # NIL THEN
  9530. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9531. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9532. IF moduleBody THEN
  9533. ProfilerInit();
  9534. ELSE
  9535. Basic.SegmentedNameToString(ir.name, string);
  9536. ProfilerAddProcedure(numberProcedures,string);
  9537. ProfilerEnterExit(numberProcedures,TRUE);
  9538. END;
  9539. END;
  9540. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9541. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9542. END;
  9543. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9544. IF moduleBody THEN
  9545. InitVariables(moduleScope)
  9546. END;
  9547. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9548. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9549. IF procedure = cellScope.bodyProcedure THEN
  9550. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9551. StaticCallOperand(op, cellScope.constructor);
  9552. Emit(Call(position,op.op,0));
  9553. END;
  9554. END;
  9555. END;
  9556. ParameterCopies(procedureType);
  9557. InitVariables(scope);
  9558. IF x.code = NIL THEN
  9559. VisitStatementBlock(x);
  9560. ELSE
  9561. VisitCode(x.code)
  9562. END;
  9563. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9564. ir.SetFinally(ir.pc);
  9565. StatementSequence(x.finally)
  9566. END;
  9567. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9568. IF ~backend.cooperative THEN
  9569. ProfilerEnterExit(numberProcedures,FALSE);
  9570. END;
  9571. INC(numberProcedures);
  9572. END;
  9573. END;
  9574. IF backend.cooperative THEN
  9575. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9576. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9577. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9578. (*! not required any more? check and delete!
  9579. PushSelfPointer();
  9580. CallThis(position,"Modules","SetInitialized",1);
  9581. *)
  9582. (*
  9583. SetLabel(end);
  9584. *)
  9585. END;
  9586. IF x # NIL THEN
  9587. SELF.position := x.position;
  9588. END;
  9589. CheckRegistersFree();
  9590. ASSERT(modifyAssignmentCounter = 0);
  9591. currentScope := prevScope;
  9592. IF Trace THEN TraceExit("Body") END;
  9593. END Body;
  9594. END ImplementationVisitor;
  9595. MetaDataGenerator=OBJECT
  9596. VAR
  9597. implementationVisitor: ImplementationVisitor;
  9598. declarationVisitor: DeclarationVisitor;
  9599. module: Sections.Module;
  9600. moduleName: ARRAY 128 OF CHAR;
  9601. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9602. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9603. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9604. TypeTags: LONGINT; (* type extension level support *)
  9605. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9606. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9607. BEGIN
  9608. IF implementationVisitor.backend.cooperative THEN
  9609. TypeTags := MAX(LONGINT); (* only 3 extensions allowed *)
  9610. BaseTypesTableOffset := 0;
  9611. MethodTableOffset := 2;
  9612. TypeRecordBaseOffset := 0;
  9613. ELSIF simple THEN
  9614. TypeTags := 3; (* only 3 extensions allowed *)
  9615. BaseTypesTableOffset := -1;
  9616. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9617. TypeRecordBaseOffset := TypeTags;
  9618. ELSE
  9619. TypeTags := 16;
  9620. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9621. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9622. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9623. END;
  9624. SELF.simple := simple;
  9625. SELF.implementationVisitor := implementationVisitor;
  9626. SELF.declarationVisitor := declarationVisitor;
  9627. implementationVisitor.meta := SELF;
  9628. declarationVisitor.meta := SELF;
  9629. END InitMetaDataGenerator;
  9630. PROCEDURE SetModule(module: Sections.Module);
  9631. BEGIN
  9632. SELF.module := module
  9633. END SetModule;
  9634. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9635. BEGIN
  9636. IF implementationVisitor.backend.cooperative THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9637. END GetTypeRecordBaseOffset;
  9638. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9639. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9640. BEGIN
  9641. INC(dataAdrOffset,6);
  9642. Info(section,"headerAdr");
  9643. Address(section,0);
  9644. Info(section,"typeDesc");
  9645. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9646. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9647. NamedSymbol(section, name, symbol, 0, offset);
  9648. Info(section,"mark: LONGINT;");
  9649. Longint(section,-1);
  9650. Info(section,"dataAdr-: ADDRESS");
  9651. Symbol(section,section, dataAdrOffset,0);
  9652. Info(section,"size-: SIZE");
  9653. Address(section,0);
  9654. Info(section,"nextRealtime: HeapBlock;");
  9655. Address(section,0);
  9656. END HeapBlock;
  9657. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9658. VAR i: LONGINT;
  9659. BEGIN
  9660. INC(dataAdrOffset,14);
  9661. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9662. Info(section,"count*: LONGINT");
  9663. Longint(section,0);
  9664. Info(section,"locked*: BOOLEAN");
  9665. Longint(section,0);
  9666. Info(section,"awaitingLock*: ProcessQueue");
  9667. Address(section,0);
  9668. Address(section,0);
  9669. Info(section,"awaitingCond*: ProcessQueue");
  9670. Address(section,0);
  9671. Address(section,0);
  9672. Info(section,"lockedBy*: ANY");
  9673. Address(section,0);
  9674. Info(section,"lock*: ANY");
  9675. Address(section,0);
  9676. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9677. Longint(section,1);
  9678. FOR i := 2 TO 6 DO
  9679. Longint(section,0);
  9680. END;
  9681. END ProtectedHeapBlock;
  9682. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9683. BEGIN
  9684. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9685. END Info;
  9686. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9687. VAR op: IntermediateCode.Operand;
  9688. BEGIN
  9689. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9690. section.Emit(Data(-11,op));
  9691. END Address;
  9692. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9693. VAR op: IntermediateCode.Operand;
  9694. BEGIN
  9695. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9696. section.Emit(Data(-12,op));
  9697. END Size;
  9698. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9699. VAR op: IntermediateCode.Operand;
  9700. BEGIN
  9701. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9702. section.Emit(Data(-1,op));
  9703. END Set;
  9704. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9705. VAR op: IntermediateCode.Operand;
  9706. BEGIN
  9707. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9708. section.Emit(Data(-1,op));
  9709. END Longint;
  9710. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9711. VAR op,noOperand: IntermediateCode.Operand;
  9712. BEGIN
  9713. IntermediateCode.InitOperand(noOperand);
  9714. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9715. section.PatchOperands(pc,op,noOperand,noOperand);
  9716. END PatchLongint;
  9717. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9718. VAR op, noOperand: IntermediateCode.Operand;
  9719. BEGIN
  9720. IntermediateCode.InitOperand(noOperand);
  9721. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9722. section.PatchOperands(pc,op,noOperand,noOperand);
  9723. END PatchSymbol;
  9724. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9725. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9726. BEGIN
  9727. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9728. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9729. section.Emit(Data(-1,op));
  9730. END Boolean;
  9731. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9732. VAR op: IntermediateCode.Operand;
  9733. BEGIN
  9734. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9735. section.Emit(Data(-1,op));
  9736. END Char;
  9737. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9738. VAR i: LONGINT;
  9739. BEGIN
  9740. Info(section,str);
  9741. i := 0;
  9742. WHILE(str[i] # 0X) DO
  9743. Char(section,str[i]);
  9744. INC(i);
  9745. END;
  9746. Char(section,0X);
  9747. END String;
  9748. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9749. VAR op: IntermediateCode.Operand;
  9750. BEGIN
  9751. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9752. IntermediateCode.SetOffset(op,realOffset);
  9753. section.Emit(Data(-1,op));
  9754. END NamedSymbol;
  9755. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9756. BEGIN
  9757. IF symbol= NIL THEN
  9758. Address( section, realOffset);
  9759. ASSERT(virtualOffset = 0);
  9760. ELSE
  9761. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9762. END;
  9763. END Symbol;
  9764. (* OutPointers delivers
  9765. {pointerOffset}
  9766. *)
  9767. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9768. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9769. BEGIN
  9770. type := type.resolved;
  9771. IF type IS SyntaxTree.AnyType THEN
  9772. Symbol(section, symbol, 0, (offset ));
  9773. INC(numberPointers);
  9774. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9775. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9776. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9777. ELSIF type IS SyntaxTree.PointerType THEN
  9778. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9779. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9780. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9781. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9782. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9783. ELSIF (type IS SyntaxTree.RecordType) THEN
  9784. (* never treat a record like a pointer, even if the pointer field is set! *)
  9785. WITH type: SyntaxTree.RecordType DO
  9786. base := type.GetBaseRecord();
  9787. IF base # NIL THEN
  9788. Pointers(offset,symbol,section, base,numberPointers);
  9789. END;
  9790. variable := type.recordScope.firstVariable;
  9791. WHILE(variable # NIL) DO
  9792. IF ~(variable.untraced) THEN
  9793. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9794. END;
  9795. variable := variable.nextVariable;
  9796. END;
  9797. END;
  9798. ELSIF (type IS SyntaxTree.CellType) THEN
  9799. WITH type: SyntaxTree.CellType DO
  9800. base := type.GetBaseRecord();
  9801. IF base # NIL THEN
  9802. Pointers(offset,symbol,section, base,numberPointers);
  9803. END;
  9804. variable := type.cellScope.firstVariable;
  9805. WHILE(variable # NIL) DO
  9806. IF ~(variable.untraced) THEN
  9807. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9808. END;
  9809. variable := variable.nextVariable;
  9810. END;
  9811. END;
  9812. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9813. WITH type: SyntaxTree.ArrayType DO
  9814. IF type.form= SyntaxTree.Static THEN
  9815. n := type.staticLength;
  9816. base := type.arrayBase.resolved;
  9817. WHILE(base IS SyntaxTree.ArrayType) DO
  9818. type := base(SyntaxTree.ArrayType);
  9819. n := n* type.staticLength;
  9820. base := type.arrayBase.resolved;
  9821. END;
  9822. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9823. IF SemanticChecker.ContainsPointer(base) THEN
  9824. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9825. FOR i := 0 TO n-1 DO
  9826. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9827. END;
  9828. END;
  9829. ELSE
  9830. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9831. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9832. END;
  9833. END;
  9834. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9835. WITH type: SyntaxTree.MathArrayType DO
  9836. IF type.form = SyntaxTree.Static THEN
  9837. n := type.staticLength;
  9838. base := type.arrayBase.resolved;
  9839. WHILE(base IS SyntaxTree.MathArrayType) DO
  9840. type := base(SyntaxTree.MathArrayType);
  9841. n := n* type.staticLength;
  9842. base := type.arrayBase.resolved;
  9843. END;
  9844. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9845. IF SemanticChecker.ContainsPointer(base) THEN
  9846. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9847. FOR i := 0 TO n-1 DO
  9848. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9849. END;
  9850. END;
  9851. ELSE
  9852. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9853. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9854. END
  9855. END;
  9856. (* ELSE no pointers in type *)
  9857. END;
  9858. END Pointers;
  9859. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9860. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9861. BEGIN
  9862. IF pool.Has(name) THEN
  9863. RETURN pool.GetInt(name)
  9864. ELSE
  9865. position := source.pc;
  9866. pool.PutInt(name, position);
  9867. StringPool.GetString(name, s);
  9868. Info(source, s);
  9869. i := 0;
  9870. REPEAT
  9871. ch := s[i]; INC(i);
  9872. Char( source, ch);
  9873. UNTIL ch = 0X;
  9874. END;
  9875. RETURN position;
  9876. END DynamicName;
  9877. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9878. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9879. BEGIN
  9880. COPY(moduleName,name);
  9881. Strings.Append(name,suffix);
  9882. Basic.ToSegmentedName(name, pooledName);
  9883. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9884. IF implementationVisitor.backend.cooperative THEN
  9885. Info(section, "TypeDescriptor");
  9886. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9887. NamedSymbol(section, pooledName,NIL, 0, 0);
  9888. BasePointer(section);
  9889. offset := 0;
  9890. ELSE
  9891. HeapBlock(mName,typeName,section,2);
  9892. Info(section, "HeapBlock");
  9893. (*
  9894. Symbol(section,section,2,0);
  9895. *)
  9896. Address(section,0); (* empty such that GC does not go on traversing *)
  9897. Info(section, "TypeDescriptor");
  9898. Address(section,0);
  9899. offset := section.pc;
  9900. END;
  9901. RETURN section
  9902. END Block;
  9903. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9904. VAR name: Basic.SegmentedName;
  9905. BEGIN
  9906. Info(source,"ArrayHeader");
  9907. IF implementationVisitor.backend.cooperative THEN
  9908. sizePC := source.pc;
  9909. Address(source,0);
  9910. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9911. IF baseType # "" THEN
  9912. Basic.ToSegmentedName(baseType, name);
  9913. NamedSymbol(source, name,NIL, 0, 0);
  9914. ELSE
  9915. Address(source,0);
  9916. END;
  9917. Address(source,0);
  9918. ELSE
  9919. Address(source,0);
  9920. Address(source,0);
  9921. Address(source,0);
  9922. sizePC := source.pc;
  9923. Address(source,0);
  9924. Info(source,"array data");
  9925. END;
  9926. END Array;
  9927. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9928. BEGIN
  9929. IF implementationVisitor.backend.cooperative THEN
  9930. PatchLongint(section, pc, size);
  9931. PatchLongint(section, pc + 3, size);
  9932. ELSE
  9933. PatchLongint(section, pc, size);
  9934. END;
  9935. END PatchArray;
  9936. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9937. VAR
  9938. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9939. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9940. poolMap: Basic.HashTableInt;
  9941. namePool: IntermediateCode.Section;
  9942. namePoolOffset: LONGINT;
  9943. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9944. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9945. BEGIN
  9946. Basic.SegmentedNameToString(s1.name,n1);
  9947. Basic.SegmentedNameToString(s2.name,n2);
  9948. index := 0;
  9949. ch1 := n1[index];
  9950. ch2 := n2[index];
  9951. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9952. INC(index);
  9953. ch1 := n1[index];
  9954. ch2 := n2[index];
  9955. END;
  9956. RETURN ch1 < ch2;
  9957. END Compare;
  9958. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9959. VAR
  9960. i, j: LONGINT;
  9961. x, t: Sections.Section;
  9962. BEGIN
  9963. IF lo < hi THEN
  9964. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9965. WHILE i <= j DO
  9966. WHILE Compare(list[i], x) DO INC(i) END;
  9967. WHILE Compare(x, list[j]) DO DEC(j) END;
  9968. IF i <= j THEN
  9969. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9970. INC(i); DEC(j)
  9971. END
  9972. END;
  9973. IF lo < j THEN QuickSort(list, lo, j) END;
  9974. IF i < hi THEN QuickSort(list, i, hi) END
  9975. END;
  9976. END QuickSort;
  9977. (*
  9978. ExportDesc* = RECORD
  9979. fp*: ADDRESS;
  9980. name* {UNTRACED}: DynamicName;
  9981. adr*: ADDRESS;
  9982. exports*: LONGINT;
  9983. dsc* {UNTRACED}: ExportArray
  9984. END;
  9985. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  9986. *)
  9987. PROCEDURE ExportDesc2(
  9988. source: IntermediateCode.Section;
  9989. namePool: IntermediateCode.Section;
  9990. fingerPrinter: FingerPrinter.FingerPrinter;
  9991. symbol: Sections.Section;
  9992. name: StringPool.Index;
  9993. VAR patchAdr: LONGINT
  9994. ): BOOLEAN;
  9995. VAR fingerPrint: SyntaxTree.FingerPrint;
  9996. BEGIN
  9997. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  9998. & (symbol.type # Sections.InlineCodeSection)
  9999. THEN
  10000. *)
  10001. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10002. & (symbol.type # Sections.InlineCodeSection))
  10003. THEN
  10004. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10005. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10006. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10007. Longint(source,fingerPrint.shallow);
  10008. ELSE
  10009. Longint(source, 0);
  10010. END;
  10011. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10012. Symbol(source, symbol,0,0);
  10013. patchAdr := source.pc;
  10014. Address(source,0);
  10015. Address(source,0);
  10016. END;
  10017. RETURN TRUE
  10018. ELSE
  10019. RETURN FALSE
  10020. END;
  10021. END ExportDesc2;
  10022. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10023. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10024. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10025. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10026. TYPE
  10027. Scope = RECORD
  10028. elements: LONGINT;
  10029. gelements: LONGINT;
  10030. section: IntermediateCode.Section;
  10031. patchAdr: LONGINT;
  10032. arraySizePC: LONGINT;
  10033. beginPC: LONGINT; (* current scope start pc *)
  10034. END;
  10035. BEGIN
  10036. Basic.InitSegmentedName(prev);
  10037. olevel := -1;
  10038. scopes[0].section := source;
  10039. FOR s := 0 TO LEN(sections)-1 DO
  10040. symbol := sections[s];
  10041. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10042. this := sections[s].name;
  10043. level := 0;
  10044. WHILE (this[level] > 0) DO
  10045. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10046. INC(level);
  10047. END;
  10048. WHILE level < olevel DO
  10049. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10050. IF olevel > 0 THEN
  10051. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10052. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10053. END;
  10054. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10055. DEC(olevel);
  10056. END;
  10057. IF (this[level] > 0) THEN
  10058. IF level > olevel THEN
  10059. (*TRACE("opening",level); *)
  10060. IF scopes[level].section = NIL THEN
  10061. arrayName := ".@ExportArray";
  10062. Strings.AppendInt(arrayName, level);
  10063. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10064. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10065. END;
  10066. scopes[level].beginPC := scopes[level].section.pc;
  10067. olevel := level;
  10068. scopes[olevel].elements := 0;
  10069. END;
  10070. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10071. sym := sections[s];
  10072. ELSE
  10073. sym := NIL;
  10074. END;
  10075. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10076. THEN
  10077. INC(scopes[olevel].elements);
  10078. END;
  10079. (*StringPool.GetString(this[level], name);*)
  10080. (*TRACE(level, name);*)
  10081. (* enter string in scope *)
  10082. INC(level);
  10083. END;
  10084. END;
  10085. Basic.SegmentedNameToString(this, name);
  10086. (*TRACE(level, "enter", name);*)
  10087. prev := this;
  10088. END;
  10089. END;
  10090. WHILE 0 <= olevel DO
  10091. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10092. IF olevel > 0 THEN
  10093. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10094. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10095. END;
  10096. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10097. DEC(olevel);
  10098. END;
  10099. level := 0;
  10100. WHILE (level < LEN(scopes)) DO
  10101. IF scopes[level].section # NIL THEN
  10102. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10103. END;
  10104. INC(level);
  10105. END;
  10106. END Export;
  10107. BEGIN
  10108. NEW(fingerPrinter, module.system);
  10109. NEW(poolMap, 64);
  10110. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10111. NEW(sectionArray, module.allSections.Length());
  10112. FOR i := 0 TO module.allSections.Length() - 1 DO
  10113. section := module.allSections.GetSection(i);
  10114. sectionArray[i] := section;
  10115. END;
  10116. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10117. Export(sectionArray^);
  10118. END ExportDesc;
  10119. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10120. VAR
  10121. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10122. BEGIN
  10123. Info(source, "exception table offsets array descriptor");
  10124. size := 0;
  10125. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10126. Info(source, "exception table content");
  10127. FOR i := 0 TO module.allSections.Length() - 1 DO
  10128. p := module.allSections.GetSection(i);
  10129. IF p.type = Sections.CodeSection THEN
  10130. finallyPC := p(IntermediateCode.Section).finally;
  10131. IF finallyPC>=0 THEN
  10132. Symbol( source, p, 0,0);
  10133. Symbol( source, p, finallyPC, 0);
  10134. Symbol( source, p, finallyPC,0);
  10135. INC(size);
  10136. END;
  10137. END
  10138. END;
  10139. PatchArray(source,sizePC,size);
  10140. END ExceptionArray;
  10141. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10142. VAR i: LONGINT; ch: CHAR;
  10143. BEGIN
  10144. i := 0;
  10145. REPEAT
  10146. ch := name[i]; INC(i);
  10147. Char( section, ch);
  10148. UNTIL ch = 0X;
  10149. WHILE i < 32 DO
  10150. Char( section, 0X); INC(i);
  10151. END;
  10152. END Name;
  10153. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10154. VAR i: LONGINT; ch: CHAR;
  10155. BEGIN
  10156. i := 0;
  10157. REPEAT
  10158. ch := name[i]; INC(i);
  10159. Char( section, ch);
  10160. UNTIL ch = 0X;
  10161. WHILE i < 128 DO
  10162. Char( section, 0X); INC(i);
  10163. END;
  10164. END LongName;
  10165. PROCEDURE References(section: IntermediateCode.Section);
  10166. CONST
  10167. rfDirect = 1X; rfIndirect = 3X;
  10168. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10169. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10170. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10171. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10172. rfArrayFlag = 80X;
  10173. VAR
  10174. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10175. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10176. VAR char: CHAR;
  10177. BEGIN
  10178. IF type = NIL THEN char := rfLongint
  10179. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10180. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10181. ELSIF type IS SyntaxTree.CharacterType THEN
  10182. IF type.sizeInBits = 8 THEN char := rfChar8
  10183. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10184. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10185. END;
  10186. ELSIF type IS SyntaxTree.IntegerType THEN
  10187. IF type.sizeInBits = 8 THEN char := rfShortint
  10188. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10189. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10190. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10191. END;
  10192. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10193. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10194. ELSIF type IS SyntaxTree.FloatType THEN
  10195. IF type.sizeInBits = 32 THEN char := rfReal
  10196. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10197. END;
  10198. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10199. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10200. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10201. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10202. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10203. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10204. END;
  10205. IF arrayOf THEN
  10206. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10207. ELSE
  10208. Char(section,char)
  10209. END;
  10210. INC(size);
  10211. END BaseType;
  10212. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10213. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10214. BEGIN
  10215. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10216. IF destination = NIL THEN
  10217. (* imported unused record type *)
  10218. Char(section,0X); (* nil type *)
  10219. INC(size);
  10220. type.typeDeclaration.GetName(name);
  10221. (*
  10222. this happens when a symbol from a different module is used but the type desciptor is not necessary to be present in the current module
  10223. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10224. *)
  10225. ELSE
  10226. IF type.pointerType # NIL THEN
  10227. Char(section,rfRecordPointer)
  10228. ELSE
  10229. Char(section,rfRecord);
  10230. END;
  10231. INC(size);
  10232. Longint(section,(destination.offset ));
  10233. INC(size,4);
  10234. END;
  10235. END RecordType;
  10236. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10237. BEGIN
  10238. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10239. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10240. INC(size);
  10241. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10242. END;
  10243. IF type.form = SyntaxTree.Static THEN
  10244. Longint(section,type.staticLength)
  10245. ELSE
  10246. Longint(section,0)
  10247. END;
  10248. INC(size,4);
  10249. END ArrayType;
  10250. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10251. BEGIN
  10252. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10253. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType THEN
  10254. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10255. INC(size);
  10256. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10257. END;
  10258. IF type.form = SyntaxTree.Static THEN
  10259. Longint(section,type.staticLength)
  10260. ELSE
  10261. Longint(section,0)
  10262. END;
  10263. INC(size,4);
  10264. END MathArrayType;
  10265. PROCEDURE Type(type: SyntaxTree.Type);
  10266. BEGIN
  10267. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10268. IF type IS SyntaxTree.BasicType THEN
  10269. BaseType(FALSE,type)
  10270. ELSIF type IS SyntaxTree.RecordType THEN
  10271. RecordType(type(SyntaxTree.RecordType));
  10272. ELSIF type IS SyntaxTree.ArrayType THEN
  10273. ArrayType(type(SyntaxTree.ArrayType))
  10274. ELSIF type IS SyntaxTree.EnumerationType THEN
  10275. BaseType(FALSE,module.system.longintType)
  10276. ELSIF type IS SyntaxTree.PointerType THEN
  10277. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10278. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10279. ELSE
  10280. BaseType(FALSE,type)
  10281. END;
  10282. ELSIF type IS SyntaxTree.ProcedureType THEN
  10283. BaseType(FALSE,type);
  10284. ELSIF type IS SyntaxTree.MathArrayType THEN
  10285. MathArrayType(type(SyntaxTree.MathArrayType));
  10286. ELSIF type IS SyntaxTree.CellType THEN
  10287. BaseType(FALSE,module.system.anyType);
  10288. ELSE HALT(200)
  10289. END;
  10290. END Type;
  10291. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10292. VAR name: ARRAY 256 OF CHAR;
  10293. BEGIN
  10294. IF variable.externalName # NIL THEN RETURN END;
  10295. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10296. INC(size);
  10297. variable.GetName(name);
  10298. Type(variable.type);
  10299. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10300. INC(size,4);
  10301. String(section,name);
  10302. INC(size,Strings.Length(name)+1);
  10303. END WriteVariable;
  10304. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10305. VAR name: ARRAY 256 OF CHAR;
  10306. BEGIN
  10307. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10308. INC(size);
  10309. variable.GetName(name);
  10310. Type(variable.type);
  10311. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10312. INC(size,4);
  10313. variable.GetName(name);
  10314. String(section,name);
  10315. INC(size,Strings.Length(name)+1);
  10316. END WriteParameter;
  10317. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10318. BEGIN
  10319. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10320. IF type IS SyntaxTree.ArrayType THEN
  10321. WITH type: SyntaxTree.ArrayType DO
  10322. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10323. ELSE Char(section,rfOpenArray)
  10324. END;
  10325. INC(size);
  10326. END
  10327. ELSIF type IS SyntaxTree.MathArrayType THEN
  10328. WITH type: SyntaxTree.MathArrayType DO
  10329. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10330. ELSE Char(section,rfOpenArray)
  10331. END;
  10332. INC(size);
  10333. END
  10334. ELSIF type IS SyntaxTree.RecordType THEN
  10335. Char(section,rfRecord);
  10336. INC(size);
  10337. ELSE
  10338. BaseType(FALSE,type);
  10339. END;
  10340. END ReturnType;
  10341. PROCEDURE Procedure(s: Sections.Section);
  10342. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10343. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10344. name: ARRAY 256 OF CHAR;
  10345. BEGIN
  10346. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10347. procedure.GetName(name);
  10348. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10349. Char(section,0F9X);
  10350. INC(size);
  10351. Symbol(section,s,0,0);
  10352. INC(size,4);
  10353. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10354. INC(size,4);
  10355. Longint(section,procedureType.numberParameters);
  10356. INC(size,4);
  10357. ReturnType(procedureType.returnType);
  10358. Longint(section,0); (*! level *)
  10359. INC(size,4);
  10360. Longint(section,0);
  10361. INC(size,4);
  10362. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10363. (*
  10364. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10365. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10366. recordName := "";
  10367. IF record.pointerType # NIL THEN
  10368. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10369. ELSE
  10370. DeclarationName(record.typeDeclaration,recordName);
  10371. END;
  10372. i := 0;
  10373. Info(section,recordName);
  10374. WHILE recordName[i] # 0X DO
  10375. Char(section,recordName[i]); INC(i);
  10376. INC(size);
  10377. END;
  10378. Char(section,".");
  10379. INC(size);
  10380. END;
  10381. *)
  10382. String(section,name);
  10383. INC(size,Strings.Length(name)+1);
  10384. parameter := procedureType.firstParameter;
  10385. WHILE(parameter # NIL) DO
  10386. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10387. parameter := parameter.nextParameter;
  10388. END;
  10389. variable := procedure.procedureScope.firstVariable;
  10390. WHILE(variable # NIL) DO
  10391. WriteVariable(variable,FALSE);
  10392. variable := variable.nextVariable;
  10393. END;
  10394. END Procedure;
  10395. PROCEDURE Scope(s: Sections.Section);
  10396. BEGIN
  10397. Char(section,0F8X);
  10398. INC(size);
  10399. Symbol(section,s,0,0); (* start *)
  10400. INC(size,4);
  10401. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10402. INC(size,4);
  10403. String(section,"$$");
  10404. INC(size,3);
  10405. (* removed variables -- wrongly interpreted by Reflection
  10406. variable := module.module.moduleScope.firstVariable;
  10407. WHILE(variable # NIL) DO
  10408. WriteVariable(variable,FALSE);
  10409. variable := variable.nextVariable;
  10410. END;
  10411. *)
  10412. END Scope;
  10413. BEGIN
  10414. Array(section,sizePC,"");
  10415. size := 0;
  10416. Char(section,0FFX); (* sign for trap writer *)
  10417. INC(size);
  10418. FOR i := 0 TO module.allSections.Length() - 1 DO
  10419. s := module.allSections.GetSection(i);
  10420. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10421. Scope(s) (*! must be first procedure in ref section *)
  10422. END
  10423. END;
  10424. FOR i := 0 TO module.allSections.Length() - 1 DO
  10425. s := module.allSections.GetSection(i);
  10426. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10427. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10428. Procedure(s)
  10429. END
  10430. END;
  10431. PatchArray(section,sizePC,size);
  10432. END References;
  10433. (*
  10434. Command* = RECORD
  10435. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10436. name*: Name; (* name of the procedure *)
  10437. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10438. entryAdr* : ADDRESS; (* entry address of procedure *)
  10439. END;
  10440. *)
  10441. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10442. VAR
  10443. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10444. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10445. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10446. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10447. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10448. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10449. BEGIN
  10450. RETURN
  10451. (type = NIL) OR
  10452. (type.resolved IS SyntaxTree.RecordType) OR
  10453. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10454. (type.resolved IS SyntaxTree.AnyType);
  10455. END TypeAllowed;
  10456. BEGIN
  10457. numberParameters := procedureType.numberParameters;
  10458. RETURN
  10459. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10460. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10461. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10462. END GetProcedureAllowed;
  10463. PROCEDURE WriteType(type : SyntaxTree.Type);
  10464. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10465. name: Basic.SegmentedName; offset: LONGINT;
  10466. BEGIN
  10467. IF type = NIL THEN
  10468. Address(source,0);
  10469. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10470. Address(source,1);
  10471. ELSE
  10472. type := type.resolved;
  10473. IF type IS SyntaxTree.PointerType THEN
  10474. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10475. END;
  10476. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10477. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10478. name[0] := typeDeclaration.name; name[1] := -1;
  10479. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10480. ASSERT(section # NIL);
  10481. ELSE
  10482. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10483. (* TODO *)
  10484. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10485. END;
  10486. IF implementationVisitor.backend.cooperative THEN
  10487. offset := 0;
  10488. ELSE
  10489. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10490. END;
  10491. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10492. END;
  10493. END WriteType;
  10494. BEGIN
  10495. Info(source, "command array descriptor");
  10496. Array(source,sizePC,"Modules.Command");
  10497. numberCommands := 0;
  10498. Info(source, "command array content");
  10499. FOR i := 0 TO module.allSections.Length() - 1 DO
  10500. p := module.allSections.GetSection(i);
  10501. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10502. procedure := p.symbol(SyntaxTree.Procedure);
  10503. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10504. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10505. procedure.GetName(name);
  10506. Name(source,name);
  10507. numberParameters := procedureType.numberParameters;
  10508. (* offset of type of first parameter *)
  10509. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10510. ELSE WriteType(procedureType.firstParameter.type)
  10511. END;
  10512. (* offset of type of return parameter *)
  10513. WriteType(procedureType.returnType);
  10514. (* command name *)
  10515. (* command code offset *)
  10516. Symbol(source,p,0,0);
  10517. INC(numberCommands);
  10518. IF Trace THEN
  10519. D.Ln;
  10520. END;
  10521. END;
  10522. END
  10523. END;
  10524. PatchArray(source,sizePC,numberCommands);
  10525. END CommandArray;
  10526. (* to prevent from double import of different module aliases *)
  10527. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10528. VAR i: SyntaxTree.Import;
  10529. BEGIN
  10530. i := module.module.moduleScope.firstImport;
  10531. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10532. i := i.nextImport;
  10533. END;
  10534. RETURN i = import
  10535. END IsFirstDirectOccurence;
  10536. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10537. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10538. BEGIN
  10539. Array(source,pc,"");
  10540. Info(source, "import module array data");
  10541. IF implementationVisitor.backend.cooperative THEN
  10542. offset := 0;
  10543. ELSE
  10544. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10545. END;
  10546. import := module.module.moduleScope.firstImport;
  10547. numberImports := 0;
  10548. WHILE import # NIL DO
  10549. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10550. Global.GetModuleSegmentedName(import.module,name);
  10551. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10552. NamedSymbol(source, name, NIL, 0, offset);
  10553. INC(numberImports);
  10554. END;
  10555. import := import.nextImport
  10556. END;
  10557. PatchArray(source,pc,numberImports);
  10558. END ImportsArray;
  10559. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10560. VAR
  10561. p: Sections.Section; sizePC, size, i: LONGINT;
  10562. BEGIN
  10563. Info(source, "Type info section");
  10564. size := 0;
  10565. Array(source,sizePC,"Modules.TypeDesc");
  10566. FOR i := 0 TO module.allSections.Length() - 1 DO
  10567. p := module.allSections.GetSection(i);
  10568. WITH p: IntermediateCode.Section DO
  10569. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10570. Symbol(source,p,0,0);
  10571. INC(size);
  10572. END;
  10573. END
  10574. END;
  10575. PatchArray(source,sizePC,size);
  10576. END TypeInfoSection;
  10577. (*
  10578. ProcTableEntry* = RECORD
  10579. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10580. noPtr*: LONGINT;
  10581. END;
  10582. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10583. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10584. *)
  10585. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10586. VAR
  10587. destination: Sections.Section;
  10588. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10589. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10590. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10591. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10592. BEGIN
  10593. Info(procArray,"pcFrom");
  10594. Symbol(procArray,destination,0,0);
  10595. Info(procArray,"pcTo");
  10596. Symbol(procArray,destination,destination.pc,0);
  10597. Info(procArray,"pcStatementBegin");
  10598. Symbol(procArray,destination,destination.validPAFEnter,0);
  10599. Info(procArray,"pcStatementEnd");
  10600. Symbol(procArray,destination,destination.validPAFExit,0);
  10601. IF ~implementationVisitor.backend.cooperative THEN
  10602. Basic.SegmentedNameToString(destination.name, string);
  10603. Info(pointerArray,string);
  10604. procedure := destination.symbol(SyntaxTree.Procedure);
  10605. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10606. variable := procedure.procedureScope.firstVariable;
  10607. WHILE(variable # NIL) DO
  10608. IF ~(variable.untraced) THEN
  10609. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10610. END;
  10611. variable := variable.nextVariable
  10612. END;
  10613. parameter := procedureType.firstParameter;
  10614. WHILE(parameter # NIL) DO
  10615. IF ~(parameter.untraced) THEN
  10616. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10617. END;
  10618. parameter := parameter.nextParameter;
  10619. END;
  10620. END;
  10621. Info(procArray,"numberPointers");
  10622. Longint(procArray,numberPointers);
  10623. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10624. INC(pointerArraySize, numberPointers);
  10625. END PointerOffsets;
  10626. BEGIN
  10627. maxPointers := 0;
  10628. Info(procArray, "proc array descriptor");
  10629. Address(procArray,0);
  10630. Address(procArray,0);
  10631. Address(procArray,0);
  10632. procArraySizePC := procArray.pc;
  10633. Address(procArray,0);
  10634. procArraySize := 0;
  10635. IF ~implementationVisitor.backend.cooperative THEN
  10636. Info(pointerArray, "pointer array descriptor");
  10637. Address(pointerArray,0);
  10638. Address(pointerArray,0);
  10639. Address(pointerArray,0);
  10640. pointerArraySizePC := pointerArray.pc;
  10641. Address(pointerArray,0);
  10642. pointerArraySize := 0;
  10643. END;
  10644. procArraySize := 0;
  10645. FOR i := 0 TO module.allSections.Length() - 1 DO
  10646. destination := module.allSections.GetSection(i);
  10647. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10648. PointerOffsets(destination(IntermediateCode.Section));
  10649. INC(procArraySize);
  10650. END
  10651. END;
  10652. PatchLongint(procArray,procArraySizePC,procArraySize);
  10653. IF ~implementationVisitor.backend.cooperative THEN
  10654. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10655. END;
  10656. END PointersInProcTables;
  10657. (*
  10658. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10659. VAR
  10660. next*: Module; (** once a module is published, all fields are read-only *)
  10661. name*: Name;
  10662. init, published: BOOLEAN;
  10663. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10664. sb*: ADDRESS; <- set to beginning of data section by loader
  10665. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10666. command*: POINTER TO ARRAY OF Command;
  10667. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10668. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10669. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10670. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10671. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10672. data*, code*: Bytes;
  10673. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10674. export*: ExportDesc;
  10675. term*: TerminationHandler;
  10676. exTable*: ExceptionTable;
  10677. noProcs*: LONGINT;
  10678. firstProc*: ADDRESS; <- done by loader
  10679. maxPtrs*: LONGINT;
  10680. crc*: LONGINT;
  10681. *)
  10682. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10683. BEGIN
  10684. Info(section, "cycle");
  10685. Size(section,0);
  10686. Info(section, "references");
  10687. Size(section,0);
  10688. Info(section, "nextMarked");
  10689. Address(section,0);
  10690. Info(section, "nextWatched");
  10691. Address(section,0);
  10692. END BasePointer;
  10693. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10694. BEGIN
  10695. BasePointer(section);
  10696. Info(section, "action");
  10697. Address(section,0);
  10698. Info(section, "monitor");
  10699. Address(section,0);
  10700. END BaseObject;
  10701. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10702. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10703. pooledName: Basic.SegmentedName;
  10704. symbol: SyntaxTree.Symbol;
  10705. BEGIN
  10706. Global.GetModuleName(module.module,name);
  10707. Strings.Append(name,".@Module.@Descriptor");
  10708. Basic.ToSegmentedName(name, pooledName);
  10709. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10710. Symbol(section,descriptorSection,0,0);
  10711. Info(descriptorSection, "descriptor");
  10712. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10713. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10714. Address(descriptorSection,0);
  10715. Global.GetModuleName(module.module,name);
  10716. Strings.Append(name,".@Trace");
  10717. Basic.ToSegmentedName(name, pooledName);
  10718. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10719. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10720. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10721. END ModuleDescriptor;
  10722. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10723. VAR name: ARRAY 128 OF CHAR;
  10724. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10725. symbol: SyntaxTree.Symbol;
  10726. BEGIN
  10727. Global.GetModuleName(module.module,name);
  10728. Strings.Append(name,".@Module");
  10729. Basic.ToSegmentedName(name, pooledName);
  10730. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10731. moduleSection.SetExported(TRUE);
  10732. IF moduleSection.pc = 0 THEN
  10733. IF implementationVisitor.backend.cooperative THEN
  10734. Info(moduleSection, "descriptor");
  10735. ModuleDescriptor(moduleSection);
  10736. BaseObject(moduleSection);
  10737. implementationVisitor.CreateTraceModuleMethod(module.module);
  10738. ELSE
  10739. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10740. Info(moduleSection, "HeapBlock");
  10741. Symbol(moduleSection,moduleSection,2,0);
  10742. Info(moduleSection, "TypeDescriptor");
  10743. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10744. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10745. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10746. END;
  10747. END;
  10748. RETURN moduleSection;
  10749. END ModuleSection;
  10750. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10751. VAR
  10752. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10753. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10754. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10755. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10756. referenceSectionOffset : LONGINT;
  10757. BEGIN
  10758. Global.GetModuleName(module.module,moduleName);
  10759. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10760. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10761. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10762. ImportsArray(importSection);
  10763. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10764. CommandArray(commandsSection);
  10765. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10766. ExceptionArray(exceptionSection);
  10767. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10768. TypeInfoSection(typeInfoSection);
  10769. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10770. References(referenceSection);
  10771. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10772. IF ~implementationVisitor.backend.cooperative THEN
  10773. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10774. ELSE
  10775. ptrTableSection := NIL;
  10776. END;
  10777. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10778. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10779. Array(emptyArraySection,temp,"");
  10780. moduleSection := ModuleSection();
  10781. Info(moduleSection, "nextRoot*: RootObject");
  10782. Address(moduleSection,0);
  10783. Info(moduleSection, "next*: Module");
  10784. Address(moduleSection,0);
  10785. Info(moduleSection, "name*: Name");
  10786. Name(moduleSection,moduleName);
  10787. Info(moduleSection, "init, published: BOOLEAN");
  10788. Boolean(moduleSection,FALSE);
  10789. Boolean(moduleSection,FALSE);
  10790. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10791. Boolean(moduleSection,FALSE);
  10792. Boolean(moduleSection,FALSE);
  10793. Info(moduleSection, "refcnt*: LONGINT");
  10794. Longint(moduleSection,0);
  10795. Info(moduleSection, "sb*: ADDRESS");
  10796. Address(moduleSection,0);
  10797. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10798. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10799. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10800. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10801. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10802. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10803. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10804. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10805. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10806. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10807. Info(moduleSection, "procTable*: ProcTable");
  10808. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10809. Info(moduleSection, "ptrTable*: PtrTable");
  10810. IF ~implementationVisitor.backend.cooperative THEN
  10811. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10812. ELSE
  10813. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10814. END;
  10815. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10816. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10817. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10818. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10819. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10820. Info(moduleSection, "export*: ExportDesc");
  10821. ExportDesc(moduleSection);
  10822. Info(moduleSection, "term*: TerminationHandler");
  10823. Address(moduleSection,0);
  10824. Info(moduleSection, "exTable*: ExceptionTable");
  10825. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10826. Info(moduleSection, "noProcs*: LONGINT");
  10827. Longint(moduleSection,numberProcs);
  10828. Info(moduleSection, "firstProc*: ADDRESS");
  10829. Address(moduleSection,0);
  10830. Info(moduleSection, "maxPtrs*: LONGINT");
  10831. Longint(moduleSection,maxPointers);
  10832. Info(moduleSection, "crc*: LONGINT");
  10833. Longint(moduleSection, 0); (*! must be implemented *)
  10834. Info(moduleSection, "body*: ADDRESS");
  10835. Symbol(moduleSection, bodyProc, 0,0);
  10836. IF implementationVisitor.backend.cooperative THEN
  10837. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10838. Info(moduleSection, "monitor.owner");
  10839. Address(moduleSection,0);
  10840. Info(moduleSection, "monitor.nestingLevel");
  10841. Address(moduleSection,0);
  10842. Info(moduleSection, "monitor.blockedQueue");
  10843. Address(moduleSection,0); Address(moduleSection,0);
  10844. Info(moduleSection, "monitor.waitingQueue");
  10845. Address(moduleSection,0); Address(moduleSection,0);
  10846. Info(moduleSection, "monitor.waitingSentinel");
  10847. Address(moduleSection,0);
  10848. END;
  10849. END Module;
  10850. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10851. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10852. BEGIN
  10853. Array(source,pc,"");
  10854. Info(source, "pointer offsets array data");
  10855. IF scope IS SyntaxTree.RecordScope THEN
  10856. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10857. ELSIF scope IS SyntaxTree.CellScope THEN
  10858. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10859. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10860. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10861. WHILE variable # NIL DO
  10862. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10863. symbol := module.allSections.FindBySymbol(variable);
  10864. ASSERT(symbol # NIL);
  10865. Pointers(0,symbol, source,variable.type,numberPointers);
  10866. END;
  10867. variable := variable.nextVariable;
  10868. END;
  10869. END;
  10870. PatchArray(source,pc,numberPointers);
  10871. END PointerArray;
  10872. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10873. VAR recordType: SyntaxTree.RecordType;
  10874. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10875. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10876. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10877. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10878. segmentedName: Basic.SegmentedName;
  10879. td: SyntaxTree.TypeDeclaration;
  10880. BEGIN
  10881. Array(source,pc,"Modules.FieldEntry");
  10882. Info(source, "FieldArray");
  10883. IF x IS SyntaxTree.RecordType THEN
  10884. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10885. ELSE
  10886. variable := NIL;
  10887. END;
  10888. size :=0;
  10889. WHILE variable # NIL DO
  10890. type := variable.type.resolved;
  10891. Info(source,"offset");
  10892. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10893. Info(source,"type class");
  10894. IF type IS SyntaxTree.PointerType THEN
  10895. Size(source, 1);
  10896. ELSIF type IS SyntaxTree.RecordType THEN
  10897. Size(source, 2);
  10898. ELSIF type IS SyntaxTree.NumberType THEN
  10899. Size(source, 3);
  10900. ELSE
  10901. Size(source, 0);
  10902. END;
  10903. Info(source, "type desc");
  10904. IF type IS SyntaxTree.RecordType THEN
  10905. td := type(SyntaxTree.RecordType).typeDeclaration;
  10906. Global.GetSymbolSegmentedName(td,segmentedName);
  10907. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10908. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10909. ELSE
  10910. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10911. END;
  10912. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10913. Symbol(source, tir, 0, offset);
  10914. ELSE
  10915. Address(source, 0);
  10916. END;
  10917. Info(source,"flags");
  10918. Set(source, {});
  10919. Info(source,"name");
  10920. variable.GetName(name);
  10921. LongName(source, name);
  10922. variable := variable.nextVariable;
  10923. INC(size);
  10924. END;
  10925. PatchArray(source,pc,size);
  10926. END FieldArray;
  10927. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10928. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10929. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10930. sectionName: Basic.SectionName;
  10931. CONST MPO=-40000000H;
  10932. BEGIN
  10933. (*
  10934. TypeDesc* = POINTER TO RECORD
  10935. descSize: LONGINT;
  10936. sentinel: LONGINT; (* = MPO-4 *)
  10937. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10938. flags*: SET;
  10939. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10940. name*: Name;
  10941. END;
  10942. *)
  10943. (* source := module.sections.FindByName(...) *)
  10944. Global.GetSymbolSegmentedName(td,name);
  10945. Basic.AppendToSegmentedName(name,"@Info");
  10946. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10947. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10948. Address(source,MPO-4);
  10949. Info(source, "type tag pointer");
  10950. Symbol( source, tag, offset, 0);
  10951. Info(source, "type flags");
  10952. flags := {};
  10953. IF isProtected THEN INCL(flags,31) END;
  10954. Set( source, flags);
  10955. Info(source, "pointer to module");
  10956. moduleSection := ModuleSection();
  10957. Symbol( source, moduleSection, moduleSection.pc,0);
  10958. Info(source, "type name");
  10959. i := 0;
  10960. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10961. (*
  10962. Global.GetSymbolSegmentedName(td,name);
  10963. Basic.SegmentedNameToString(name, sectionName);
  10964. *)
  10965. Name(source,sectionName);
  10966. source.SetReferenced(FALSE);
  10967. Global.GetSymbolSegmentedName(td,name);
  10968. Basic.AppendToSegmentedName(name,"@Fields");
  10969. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10970. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10971. Info(fieldSection, "HeapBlock");
  10972. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10973. Info(fieldSection, "TypeDescriptor");
  10974. Address(fieldSection,0);
  10975. Info(source, "FieldArray ref");
  10976. Symbol(source, fieldSection, fieldSection.pc, 0);
  10977. FieldArray(fieldSection);
  10978. RETURN source;
  10979. END NewTypeDescriptorInfo;
  10980. PROCEDURE NewTypeDescriptor;
  10981. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  10982. procedure: SyntaxTree.Procedure;
  10983. baseRecord: SyntaxTree.RecordType; baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  10984. numberPointers: LONGINT; padding,offset, i: LONGINT;
  10985. CONST MPO=-40000000H;
  10986. PROCEDURE TdTable(size: LONGINT);
  10987. VAR i: LONGINT;
  10988. BEGIN
  10989. Info(source, "tag table");
  10990. baseRecord := recordType;
  10991. i := 0;
  10992. WHILE baseRecord # NIL DO
  10993. INC(i);
  10994. baseRecord := baseRecord.GetBaseRecord();
  10995. END;
  10996. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  10997. WHILE i < size DO
  10998. Address(source,0);
  10999. INC(i);
  11000. END;
  11001. baseRecord := recordType;
  11002. WHILE baseRecord # NIL DO
  11003. baseTD := baseRecord.typeDeclaration;
  11004. Global.GetSymbolSegmentedName(baseTD,name);
  11005. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11006. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11007. ELSE
  11008. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11009. END;
  11010. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*module.system.addressSize);
  11011. Symbol(source, tir, 0, offset);
  11012. baseRecord := baseRecord.GetBaseRecord();
  11013. END;
  11014. END TdTable;
  11015. PROCEDURE MethodTable;
  11016. VAR i,methods: LONGINT;
  11017. BEGIN
  11018. Info(source, "method table");
  11019. IF recordType # NIL THEN
  11020. methods := recordType.recordScope.numberMethods;
  11021. FOR i := methods-1 TO 0 BY -1 DO
  11022. procedure := recordType.recordScope.FindMethod(i);
  11023. Global.GetSymbolSegmentedName(procedure, name);
  11024. NamedSymbol(source, name,procedure, 0,0);
  11025. END;
  11026. END;
  11027. END MethodTable;
  11028. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11029. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11030. BEGIN
  11031. Info(source, "method table");
  11032. baseRecord := recordType;
  11033. WHILE baseRecord.baseType # NIL DO
  11034. baseRecord := baseRecord.GetBaseRecord ();
  11035. END;
  11036. GetRecordTypeName (baseRecord, name);
  11037. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11038. IF name = stackFrame THEN
  11039. start := 0;
  11040. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11041. baseRecord := recordType;
  11042. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11043. baseRecord := baseRecord.GetBaseRecord ();
  11044. END;
  11045. IF baseRecord # NIL THEN
  11046. GetRecordTypeName (baseRecord, name);
  11047. IF pointer & ~baseRecord.isObject THEN
  11048. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11049. END;
  11050. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11051. ELSIF recordType.isObject THEN
  11052. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11053. ELSIF pointer THEN
  11054. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11055. ELSE
  11056. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11057. END;
  11058. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11059. Symbol(source, tir, 0, 0);
  11060. start := 0;
  11061. baseRecord := recordType;
  11062. WHILE (baseRecord # NIL) DO
  11063. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11064. baseRecord := baseRecord.GetBaseRecord ();
  11065. END;
  11066. ELSE
  11067. (* explicit trace method: *)
  11068. procedure := recordType.recordScope.FindMethod(0);
  11069. IF ~procedure.isFinalizer THEN
  11070. Global.GetSymbolSegmentedName(procedure, name);
  11071. NamedSymbol(source, name,procedure, 0,0);
  11072. END;
  11073. start := 1;
  11074. END;
  11075. IF (name # stackFrame) & recordType.isObject THEN
  11076. baseRecord := recordType;
  11077. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11078. baseRecord := baseRecord.GetBaseRecord ();
  11079. END;
  11080. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11081. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11082. ELSE
  11083. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11084. END;
  11085. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11086. Symbol(source, tir, 0, 0);
  11087. END;
  11088. methods := recordType.recordScope.numberMethods;
  11089. FOR i := start TO methods-1 DO
  11090. procedure := recordType.recordScope.FindMethod(i);
  11091. IF ~procedure.isFinalizer THEN
  11092. Global.GetSymbolSegmentedName(procedure, name);
  11093. NamedSymbol(source, name,procedure, 0,0);
  11094. END;
  11095. END;
  11096. END CooperativeMethodTable;
  11097. BEGIN
  11098. Global.GetSymbolSegmentedName(td,name);
  11099. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11100. source.SetExported(IsExported(td));
  11101. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11102. IF implementationVisitor.backend.cooperative THEN
  11103. base := NIL;
  11104. baseRecord := recordType.GetBaseRecord();
  11105. IF baseRecord # NIL THEN
  11106. baseTD := baseRecord.typeDeclaration;
  11107. END;
  11108. IF ~recordType.isObject THEN
  11109. Info(source, "parent");
  11110. IF baseRecord # NIL THEN
  11111. Global.GetSymbolSegmentedName(baseTD,name);
  11112. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11113. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11114. ELSE
  11115. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11116. END;
  11117. Symbol(source, tir, 0, 0);
  11118. ELSE
  11119. Address(source,0);
  11120. END;
  11121. Info(source, "record size");
  11122. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11123. source.SetReferenced(FALSE);
  11124. CooperativeMethodTable(FALSE);
  11125. base := source;
  11126. Global.GetSymbolSegmentedName(td,name);
  11127. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11128. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11129. source.SetExported(IsExported(td));
  11130. source.SetReferenced(FALSE);
  11131. END;
  11132. Info(source, "parent");
  11133. IF baseRecord # NIL THEN
  11134. Global.GetSymbolSegmentedName(baseTD,name);
  11135. sym := baseTD;
  11136. IF ~recordType.isObject THEN
  11137. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11138. sym := NIL;
  11139. END;
  11140. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11141. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11142. ELSE
  11143. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11144. END;
  11145. Symbol(source, tir, 0, 0);
  11146. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11147. Address(source,0);
  11148. ELSE
  11149. IF recordType.isObject THEN
  11150. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11151. ELSE
  11152. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11153. END;
  11154. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11155. Symbol(source, tir, 0, 0);
  11156. END;
  11157. Info(source, "base record descriptor");
  11158. Symbol(source, base, 0, 0);
  11159. CooperativeMethodTable(TRUE);
  11160. source.SetReferenced(FALSE);
  11161. IF recordType.hasPointers THEN
  11162. IF ~HasExplicitTraceMethod (recordType) THEN
  11163. implementationVisitor.CreateTraceMethod(recordType);
  11164. END;
  11165. implementationVisitor.CreateResetProcedure(recordType);
  11166. implementationVisitor.CreateAssignProcedure(recordType);
  11167. END;
  11168. ELSIF ~simple THEN
  11169. (*
  11170. MethodEnd = MPO
  11171. ---
  11172. methods (# methods)
  11173. ---
  11174. tags (16)
  11175. ---
  11176. TypeDesc = TypeInfoAdr
  11177. ---
  11178. td adr ---> rec size
  11179. ----
  11180. pointer offsets
  11181. ----
  11182. (padding)
  11183. -----
  11184. empty [2 addresses aligned]
  11185. empty
  11186. empty
  11187. numPtrs
  11188. ---
  11189. pointer offsets
  11190. ---
  11191. *)
  11192. Info(source, "MethodEnd = MPO");
  11193. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11194. source(IntermediateCode.Section).Emit(Data(-1,op));
  11195. MethodTable;
  11196. TdTable(TypeTags);
  11197. Info(source, "type descriptor info pointer");
  11198. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11199. IF (cellType # NIL) THEN
  11200. IF cellType.sizeInBits < 0 THEN
  11201. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11202. END;
  11203. Info(source, "cell size");
  11204. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11205. ELSE
  11206. Info(source, "record size");
  11207. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11208. END;
  11209. Info(source, "pointer offsets pointer");
  11210. padding := 1- source.pc MOD 2;
  11211. Symbol(source, source, source.pc+1+padding,0);
  11212. IF padding >0 THEN
  11213. Info(source, "padding");
  11214. FOR i := 1 TO padding DO Address(source,0) END;
  11215. END;
  11216. IF cellType # NIL THEN
  11217. PointerArray(source, cellType.cellScope, numberPointers);
  11218. ELSE
  11219. PointerArray(source, recordType.recordScope, numberPointers);
  11220. END;
  11221. ELSE
  11222. (*
  11223. simple:
  11224. methods
  11225. tag(2)
  11226. tag(1)
  11227. td adr ---> tag(0)
  11228. td adr ---> size
  11229. *)
  11230. MethodTable;
  11231. TdTable(TypeTags);
  11232. Info(source, "record size");
  11233. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11234. source.SetReferenced(FALSE);
  11235. END;
  11236. END NewTypeDescriptor;
  11237. BEGIN
  11238. x := x.resolved;
  11239. IF (x IS SyntaxTree.PointerType) THEN
  11240. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11241. END;
  11242. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11243. recordType := x(SyntaxTree.RecordType);
  11244. td := x.typeDeclaration;
  11245. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11246. ASSERT(td # NIL);
  11247. section := module.allSections.FindBySymbol(td); (* TODO *)
  11248. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11249. IF implementationVisitor.newObjectFile THEN
  11250. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11251. NewTypeDescriptor
  11252. END;
  11253. ELSE
  11254. (* data section in intermediate code *)
  11255. Global.GetSymbolSegmentedName(td,name);
  11256. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11257. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11258. tir.Emit(Data(-1,op));
  11259. END;
  11260. END;
  11261. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11262. cellType := x(SyntaxTree.CellType);
  11263. td := x.typeDeclaration;
  11264. section := module.allSections.FindBySymbol(td); (* TODO *)
  11265. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11266. IF implementationVisitor.newObjectFile THEN
  11267. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11268. NewTypeDescriptor
  11269. END;
  11270. END;
  11271. END;
  11272. END
  11273. END CheckTypeDeclaration
  11274. END MetaDataGenerator;
  11275. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11276. VAR
  11277. trace-: BOOLEAN;
  11278. traceString-: SyntaxTree.IdentifierString;
  11279. traceModuleName-: SyntaxTree.IdentifierString;
  11280. newObjectFile-: BOOLEAN;
  11281. profile-: BOOLEAN;
  11282. noRuntimeChecks: BOOLEAN;
  11283. simpleMetaData-: BOOLEAN;
  11284. noAsserts: BOOLEAN;
  11285. optimize-: BOOLEAN;
  11286. cooperative-: BOOLEAN;
  11287. preregisterStatic-: BOOLEAN;
  11288. dump-: Basic.Writer;
  11289. cellsAreObjects: BOOLEAN;
  11290. PROCEDURE &InitIntermediateBackend*;
  11291. BEGIN
  11292. simpleMetaData := FALSE;
  11293. newObjectFile := FALSE;
  11294. InitBackend;
  11295. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11296. SetTraceModuleName(DefaultTraceModuleName);
  11297. END InitIntermediateBackend;
  11298. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11299. VAR
  11300. declarationVisitor: DeclarationVisitor;
  11301. implementationVisitor: ImplementationVisitor;
  11302. module: Sections.Module;
  11303. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11304. meta: MetaDataGenerator;
  11305. BEGIN
  11306. ResetError;
  11307. Global.GetSymbolName(x,name);
  11308. IF activeCellsSpecification # NIL THEN
  11309. GetDescription(instructionSet);
  11310. activeCellsSpecification.SetInstructionSet(instructionSet)
  11311. END;
  11312. NEW(module,x,system); (* backend structures *)
  11313. Global.GetModuleName(x, name);
  11314. module.SetModuleName(name);
  11315. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11316. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11317. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11318. declarationVisitor.Module(x,module);
  11319. IF newObjectFile & ~meta.simple THEN
  11320. meta.Module(implementationVisitor.moduleBodySection);
  11321. END;
  11322. GetDescription(platformName);
  11323. module.SetPlatformName(platformName);
  11324. RETURN module
  11325. END GenerateIntermediate;
  11326. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11327. BEGIN RETURN TRUE
  11328. END SupportedImmediate;
  11329. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11330. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11331. END ProcessSyntaxTreeModule;
  11332. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11333. VAR
  11334. result: Sections.Module;
  11335. traceName: Basic.MessageString;
  11336. BEGIN
  11337. ASSERT(intermediateCodeModule IS Sections.Module);
  11338. result := intermediateCodeModule(Sections.Module);
  11339. IF trace THEN
  11340. traceName := "intermediate code trace: ";
  11341. Strings.Append(traceName,traceString);
  11342. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11343. IF (traceString="") OR (traceString="*") THEN
  11344. result.Dump(dump);
  11345. dump.Update
  11346. ELSE
  11347. Sections.DumpFiltered(dump, result, traceString);
  11348. END
  11349. END;
  11350. RETURN result
  11351. END ProcessIntermediateCodeModule;
  11352. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11353. BEGIN instructionSet := "Intermediate";
  11354. END GetDescription;
  11355. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11356. BEGIN
  11357. SELF.newObjectFile := newObjectFile;
  11358. SELF.simpleMetaData := simpleMetaData;
  11359. END SetNewObjectFile;
  11360. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11361. BEGIN COPY(name, traceModuleName)
  11362. END SetTraceModuleName;
  11363. PROCEDURE DefineOptions(options: Options.Options);
  11364. BEGIN
  11365. DefineOptions^(options);
  11366. options.Add(0X,"trace",Options.String);
  11367. options.Add(0X,"runtime",Options.String);
  11368. options.Add(0X,"newObjectFile",Options.Flag);
  11369. options.Add(0X,"traceModule",Options.String);
  11370. options.Add(0X,"profile",Options.Flag);
  11371. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11372. options.Add(0X,"noAsserts",Options.Flag);
  11373. options.Add(0X,"metaData",Options.String);
  11374. options.Add('o',"optimize", Options.Flag);
  11375. options.Add(0X,"preregisterStatic", Options.Flag);
  11376. options.Add(0X,"cellsAreObjects", Options.Flag);
  11377. END DefineOptions;
  11378. PROCEDURE GetOptions(options: Options.Options);
  11379. VAR name,string: SyntaxTree.IdentifierString;
  11380. BEGIN
  11381. GetOptions^(options);
  11382. trace := options.GetString("trace",traceString);
  11383. profile := options.GetFlag("profile");
  11384. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11385. noAsserts := options.GetFlag("noAsserts");
  11386. cooperative := options.GetFlag("cooperative");
  11387. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11388. IF cooperative THEN
  11389. SetRuntimeModuleName("CPU") END
  11390. END;
  11391. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11392. simpleMetaData := TRUE
  11393. END;
  11394. IF options.GetString("metaData",string) THEN
  11395. IF string = "simple" THEN simpleMetaData := TRUE
  11396. ELSIF string ="full" THEN simpleMetaData := FALSE
  11397. END;
  11398. END;
  11399. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11400. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11401. optimize := options.GetFlag("optimize");
  11402. preregisterStatic := options.GetFlag("preregisterStatic");
  11403. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11404. END GetOptions;
  11405. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11406. BEGIN RETURN SymbolFileFormat.Get()
  11407. END DefaultSymbolFileFormat;
  11408. END IntermediateBackend;
  11409. (* ----------------------------------- register allocation ------------------------------------- *)
  11410. (* register mapping scheme
  11411. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11412. spill offset
  11413. part(n) --> ticket <--> physical register
  11414. spill offset
  11415. *)
  11416. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11417. emptyOperand: IntermediateCode.Operand;
  11418. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11419. statCoopResetVariables: LONGINT;
  11420. statCoopModifyAssignments: LONGINT;
  11421. modifyAssignmentsPC : LONGINT;
  11422. statCoopNilCheck: LONGINT;
  11423. statCoopSwitch: LONGINT;
  11424. statCoopAssignProcedure: LONGINT;
  11425. statCoopTraceMethod: LONGINT;
  11426. statCoopResetProcedure: LONGINT;
  11427. statCoopTraceModule: LONGINT;
  11428. PROCEDURE ResetStatistics*;
  11429. BEGIN
  11430. statCoopResetVariables := 0;
  11431. statCoopModifyAssignments := 0;
  11432. statCoopNilCheck:= 0;
  11433. statCoopSwitch:= 0;
  11434. statCoopAssignProcedure:= 0;
  11435. statCoopTraceMethod:= 0;
  11436. statCoopResetProcedure:= 0;
  11437. statCoopTraceModule:= 0;
  11438. END ResetStatistics;
  11439. PROCEDURE Statistics*;
  11440. BEGIN
  11441. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11442. TRACE(statCoopNilCheck, statCoopSwitch);
  11443. TRACE(statCoopAssignProcedure,
  11444. statCoopTraceMethod,
  11445. statCoopResetProcedure,
  11446. statCoopTraceModule)
  11447. END Statistics;
  11448. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11449. BEGIN
  11450. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11451. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11452. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11453. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11454. RETURN ~(parameter.type.resolved IS SyntaxTree.ArrayType) & ~(parameter.type.resolved IS SyntaxTree.MathArrayType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11455. END
  11456. END PassBySingleReference;
  11457. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11458. BEGIN
  11459. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11460. END PassInRegister;
  11461. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11462. VAR new: RegisterEntry;
  11463. BEGIN
  11464. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11465. IF queue = NIL THEN
  11466. queue := new
  11467. ELSE
  11468. new.next := queue;
  11469. IF queue#NIL THEN queue.prev := new END;
  11470. queue := new
  11471. END;
  11472. END AddRegisterEntry;
  11473. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11474. VAR this: RegisterEntry;
  11475. BEGIN
  11476. this := queue;
  11477. WHILE (this # NIL) & (this.register # register) DO
  11478. this := this.next;
  11479. END;
  11480. IF this = NIL THEN
  11481. RETURN FALSE
  11482. END;
  11483. ASSERT(this # NIL);
  11484. IF this = queue THEN queue := queue.next END;
  11485. IF this.prev # NIL THEN this.prev.next := this.next END;
  11486. IF this.next # NIL THEN this.next.prev := this.prev END;
  11487. RETURN TRUE
  11488. END RemoveRegisterEntry;
  11489. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11490. BEGIN ASSERT(cond);
  11491. END Assert;
  11492. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11493. BEGIN
  11494. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11495. END ReusableRegister;
  11496. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11497. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11498. BEGIN
  11499. procedure := moduleScope.bodyProcedure;
  11500. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11501. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11502. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11503. procedure.SetScope(moduleScope);
  11504. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11505. procedure.SetAccess(SyntaxTree.Hidden);
  11506. moduleScope.SetBodyProcedure(procedure);
  11507. moduleScope.AddProcedure(procedure);
  11508. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11509. END;
  11510. END EnsureBodyProcedure;
  11511. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11512. VAR import: SyntaxTree.Import;
  11513. s: Basic.MessageString;
  11514. selfName: SyntaxTree.IdentifierString;
  11515. module: SyntaxTree.Module;
  11516. BEGIN
  11517. scope.ownerModule.GetName(selfName);
  11518. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11519. module := scope.ownerModule
  11520. ELSE
  11521. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11522. IF import = NIL THEN
  11523. RETURN NIL
  11524. ELSIF import.module = NIL THEN
  11525. RETURN NIL
  11526. ELSE module := import.module
  11527. END;
  11528. END;
  11529. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11530. END GetSymbol;
  11531. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11532. BEGIN
  11533. op.mode := mode;
  11534. IntermediateCode.InitOperand(op.op);
  11535. IntermediateCode.InitOperand(op.tag);
  11536. IntermediateCode.InitOperand(op.extra);
  11537. op.dimOffset := 0;
  11538. END InitOperand;
  11539. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11540. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11541. BEGIN RETURN IntermediateCode.GetType(system, type)
  11542. END GetType;
  11543. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11544. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11545. BEGIN
  11546. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11547. (*
  11548. UniqueId(name,module,name,adr);
  11549. *)
  11550. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11551. constant.SetValue(value);
  11552. module.moduleScope.AddConstant(constant);
  11553. constant.SetScope(module.moduleScope);
  11554. RETURN constant
  11555. END BuildConstant;
  11556. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11557. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11558. BEGIN
  11559. variable := scope.firstVariable;
  11560. WHILE variable # NIL DO
  11561. IF variable.NeedsTrace() THEN
  11562. RETURN TRUE;
  11563. END;
  11564. variable := variable.nextVariable;
  11565. END;
  11566. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11567. WHILE parameter # NIL DO
  11568. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11569. RETURN TRUE;
  11570. END;
  11571. parameter := parameter.nextParameter;
  11572. END;
  11573. RETURN FALSE;
  11574. END HasPointers;
  11575. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11576. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11577. END IsVariableParameter;
  11578. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11579. VAR parameter: SyntaxTree.Parameter;
  11580. BEGIN
  11581. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11582. WHILE parameter # NIL DO
  11583. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11584. IF parameter.movable THEN RETURN TRUE END;
  11585. parameter := parameter.nextParameter;
  11586. END;
  11587. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11588. END HasVariableParameters;
  11589. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11590. BEGIN
  11591. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11592. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11593. END HasExplicitTraceMethod;
  11594. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11595. BEGIN
  11596. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11597. IF (expression IS SyntaxTree.IntegerValue) THEN
  11598. val := expression(SyntaxTree.IntegerValue).value;
  11599. RETURN TRUE
  11600. ELSE
  11601. RETURN FALSE
  11602. END;
  11603. END IsIntegerConstant;
  11604. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11605. BEGIN
  11606. IF val <= 0 THEN RETURN FALSE END;
  11607. exp := 0;
  11608. WHILE ~ODD(val) DO
  11609. val := val DIV 2;
  11610. INC(exp)
  11611. END;
  11612. RETURN val = 1
  11613. END PowerOf2;
  11614. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11615. VAR procedure: SyntaxTree.Procedure;
  11616. BEGIN
  11617. procedure := record.recordScope.constructor;
  11618. IF procedure = NIL THEN
  11619. record := record.GetBaseRecord();
  11620. IF record # NIL THEN
  11621. procedure := GetConstructor(record)
  11622. END;
  11623. END;
  11624. RETURN procedure;
  11625. END GetConstructor;
  11626. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11627. BEGIN
  11628. value := SHORT(op.intValue);
  11629. RETURN op.mode = IntermediateCode.ModeImmediate;
  11630. END IsIntegerImmediate;
  11631. (** whether a type strictily is a pointer to record or object type
  11632. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11633. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11634. BEGIN
  11635. IF type = NIL THEN
  11636. RETURN FALSE
  11637. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11638. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11639. ELSE
  11640. RETURN FALSE
  11641. END
  11642. END IsStrictlyPointerToRecord;
  11643. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11644. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11645. END IsUnsafePointer;
  11646. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11647. BEGIN type := type.resolved;
  11648. IF type IS SyntaxTree.PointerType THEN
  11649. type := type(SyntaxTree.PointerType).pointerBase;
  11650. type := type.resolved;
  11651. IF type IS SyntaxTree.RecordType THEN
  11652. recordType := type(SyntaxTree.RecordType);
  11653. RETURN TRUE
  11654. ELSE
  11655. RETURN FALSE
  11656. END
  11657. ELSIF type IS SyntaxTree.RecordType THEN
  11658. recordType := type(SyntaxTree.RecordType);
  11659. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11660. ELSIF type IS SyntaxTree.ObjectType THEN
  11661. RETURN TRUE
  11662. ELSIF type IS SyntaxTree.AnyType THEN
  11663. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11664. ELSE
  11665. RETURN FALSE
  11666. END;
  11667. END IsPointerToRecord;
  11668. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11669. BEGIN
  11670. type := type.resolved;
  11671. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11672. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11673. END IsArrayOfSystemByte;
  11674. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11675. BEGIN
  11676. IF type = NIL THEN RETURN FALSE END;
  11677. type := type.resolved;
  11678. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11679. END IsOpenArray;
  11680. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11681. BEGIN
  11682. IF type = NIL THEN RETURN FALSE END;
  11683. type := type.resolved;
  11684. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11685. END IsStaticArray;
  11686. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11687. VAR size: LONGINT;
  11688. BEGIN
  11689. size := 1;
  11690. WHILE (IsStaticArray(type)) DO
  11691. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11692. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11693. END;
  11694. RETURN size;
  11695. END StaticArrayNumElements;
  11696. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11697. BEGIN
  11698. WHILE (IsStaticArray(type)) DO
  11699. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11700. END;
  11701. RETURN type;
  11702. END StaticArrayBaseType;
  11703. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11704. BEGIN
  11705. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11706. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11707. END;
  11708. RETURN type;
  11709. END ArrayBaseType;
  11710. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11711. BEGIN
  11712. IF type = NIL THEN RETURN FALSE END;
  11713. type := type.resolved;
  11714. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11715. END IsDelegate;
  11716. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11717. VAR i: LONGINT;
  11718. BEGIN
  11719. i := 0; type := type.resolved;
  11720. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11721. INC(i);
  11722. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11723. END;
  11724. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11725. INC(i);
  11726. type := type(SyntaxTree.MathArrayType).arrayBase;
  11727. IF type # NIL THEN type := type.resolved END;
  11728. END;
  11729. RETURN i
  11730. END DynamicDim;
  11731. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11732. BEGIN
  11733. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11734. type := type(SyntaxTree.ArrayType).arrayBase;
  11735. END;
  11736. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11737. type := type(SyntaxTree.MathArrayType).arrayBase;
  11738. END;
  11739. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11740. END StaticSize;
  11741. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11742. BEGIN
  11743. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11744. END IsImmediate;
  11745. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11746. BEGIN
  11747. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11748. END IsAddress;
  11749. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11750. BEGIN
  11751. RETURN (x.mode = IntermediateCode.ModeRegister);
  11752. END IsRegister;
  11753. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11754. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11755. BEGIN
  11756. typeDeclaration := recordType.typeDeclaration;
  11757. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11758. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11759. END GetRecordTypeName;
  11760. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11761. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11762. BEGIN
  11763. parSize := 0;
  11764. IF StructuredReturnType(procedureType) THEN
  11765. parameter := procedureType.returnParameter;
  11766. INC(parSize,system.SizeOfParameter(parameter));
  11767. parSize := parSize + (-parSize) MOD system.addressSize;
  11768. END;
  11769. parameter :=procedureType.lastParameter;
  11770. WHILE (parameter # NIL) DO
  11771. INC(parSize,system.SizeOfParameter(parameter));
  11772. parSize := parSize + (-parSize) MOD system.addressSize;
  11773. parameter := parameter.prevParameter;
  11774. END;
  11775. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11776. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11777. RETURN ToMemoryUnits(system,parSize)
  11778. END ParametersSize;
  11779. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11780. BEGIN
  11781. IF type = NIL THEN RETURN FALSE
  11782. ELSE
  11783. type := type.resolved;
  11784. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11785. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11786. END
  11787. END ReturnedAsParameter;
  11788. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11789. BEGIN
  11790. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11791. END StructuredReturnType;
  11792. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11793. BEGIN
  11794. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11795. END IsNested;
  11796. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11797. BEGIN
  11798. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11799. scope := scope.outerScope;
  11800. END;
  11801. RETURN scope # NIL;
  11802. END InCellScope;
  11803. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11804. BEGIN
  11805. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11806. RETURN 0
  11807. ELSE
  11808. *)
  11809. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11810. (*END;*)
  11811. END ProcedureParametersSize;
  11812. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11813. VAR dataUnit: LONGINT;
  11814. BEGIN dataUnit := system.dataUnit;
  11815. ASSERT(size MOD system.dataUnit = 0);
  11816. RETURN size DIV system.dataUnit
  11817. END ToMemoryUnits;
  11818. PROCEDURE Get*(): Backend.Backend;
  11819. VAR backend: IntermediateBackend;
  11820. BEGIN NEW(backend); RETURN backend
  11821. END Get;
  11822. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11823. VAR instruction: IntermediateCode.Instruction;
  11824. BEGIN
  11825. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11826. RETURN instruction
  11827. END Nop;
  11828. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11829. VAR instruction: IntermediateCode.Instruction;
  11830. BEGIN
  11831. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11832. RETURN instruction
  11833. END Mov;
  11834. (* like Mov but ensures that no new register will be allocated for dest *)
  11835. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11836. VAR instruction: IntermediateCode.Instruction;
  11837. BEGIN
  11838. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11839. RETURN instruction
  11840. END MovReplace;
  11841. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11842. VAR instruction: IntermediateCode.Instruction;
  11843. BEGIN
  11844. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11845. RETURN instruction
  11846. END Conv;
  11847. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11848. VAR instruction: IntermediateCode.Instruction;
  11849. BEGIN
  11850. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11851. RETURN instruction
  11852. END Call;
  11853. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11854. VAR op1, op2: IntermediateCode.Operand;
  11855. VAR instruction: IntermediateCode.Instruction;
  11856. BEGIN
  11857. IntermediateCode.InitNumber(op1,pcOffset);
  11858. IntermediateCode.InitNumber(op2,callingConvention);
  11859. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11860. RETURN instruction
  11861. END Exit;
  11862. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11863. VAR instruction: IntermediateCode.Instruction;
  11864. BEGIN
  11865. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11866. RETURN instruction
  11867. END Return;
  11868. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11869. VAR instruction: IntermediateCode.Instruction;
  11870. BEGIN
  11871. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11872. RETURN instruction
  11873. END Result;
  11874. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11875. VAR op1: IntermediateCode.Operand;
  11876. VAR instruction: IntermediateCode.Instruction;
  11877. BEGIN
  11878. IntermediateCode.InitNumber(op1,nr);
  11879. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11880. RETURN instruction
  11881. END Trap;
  11882. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11883. VAR instruction: IntermediateCode.Instruction;
  11884. BEGIN
  11885. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11886. RETURN instruction
  11887. END Br;
  11888. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11889. VAR instruction: IntermediateCode.Instruction;
  11890. BEGIN
  11891. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11892. RETURN instruction
  11893. END Breq;
  11894. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11895. VAR instruction: IntermediateCode.Instruction;
  11896. BEGIN
  11897. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11898. RETURN instruction
  11899. END Brne;
  11900. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11901. VAR instruction: IntermediateCode.Instruction;
  11902. BEGIN
  11903. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11904. RETURN instruction
  11905. END Brge;
  11906. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11907. VAR instruction: IntermediateCode.Instruction;
  11908. BEGIN
  11909. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11910. RETURN instruction
  11911. END Brlt;
  11912. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11913. VAR instruction: IntermediateCode.Instruction;
  11914. BEGIN
  11915. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11916. RETURN instruction
  11917. END Pop;
  11918. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11919. VAR instruction: IntermediateCode.Instruction;
  11920. BEGIN
  11921. ASSERT(op.mode # IntermediateCode.Undefined);
  11922. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11923. RETURN instruction
  11924. END Push;
  11925. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11926. VAR instruction: IntermediateCode.Instruction;
  11927. BEGIN
  11928. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11929. RETURN instruction
  11930. END Neg;
  11931. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11932. VAR instruction: IntermediateCode.Instruction;
  11933. BEGIN
  11934. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11935. RETURN instruction
  11936. END Not;
  11937. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11938. VAR instruction: IntermediateCode.Instruction;
  11939. BEGIN
  11940. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11941. RETURN instruction
  11942. END Abs;
  11943. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11944. VAR instruction: IntermediateCode.Instruction;
  11945. BEGIN
  11946. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  11947. RETURN instruction
  11948. END Mul;
  11949. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11950. VAR instruction: IntermediateCode.Instruction;
  11951. BEGIN
  11952. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  11953. RETURN instruction
  11954. END Div;
  11955. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11956. VAR instruction: IntermediateCode.Instruction;
  11957. BEGIN
  11958. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  11959. RETURN instruction
  11960. END Mod;
  11961. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11962. VAR instruction: IntermediateCode.Instruction;
  11963. BEGIN
  11964. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  11965. RETURN instruction
  11966. END Sub;
  11967. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11968. VAR instruction: IntermediateCode.Instruction;
  11969. BEGIN
  11970. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  11971. RETURN instruction
  11972. END Add;
  11973. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11974. VAR instruction: IntermediateCode.Instruction;
  11975. BEGIN
  11976. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  11977. RETURN instruction
  11978. END And;
  11979. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11980. VAR instruction: IntermediateCode.Instruction;
  11981. BEGIN
  11982. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  11983. RETURN instruction
  11984. END Or;
  11985. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11986. VAR instruction: IntermediateCode.Instruction;
  11987. BEGIN
  11988. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  11989. RETURN instruction
  11990. END Xor;
  11991. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11992. VAR instruction: IntermediateCode.Instruction;
  11993. BEGIN
  11994. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  11995. RETURN instruction
  11996. END Shl;
  11997. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11998. VAR instruction: IntermediateCode.Instruction;
  11999. BEGIN
  12000. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12001. RETURN instruction
  12002. END Shr;
  12003. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12004. VAR instruction: IntermediateCode.Instruction;
  12005. BEGIN
  12006. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12007. RETURN instruction
  12008. END Rol;
  12009. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12010. VAR instruction: IntermediateCode.Instruction;
  12011. BEGIN
  12012. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12013. RETURN instruction
  12014. END Ror;
  12015. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12016. VAR instruction: IntermediateCode.Instruction;
  12017. BEGIN
  12018. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12019. RETURN instruction
  12020. END Cas;
  12021. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12022. VAR instruction: IntermediateCode.Instruction;
  12023. BEGIN
  12024. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12025. RETURN instruction
  12026. END Copy;
  12027. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12028. VAR instruction: IntermediateCode.Instruction;
  12029. BEGIN
  12030. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12031. RETURN instruction
  12032. END Fill;
  12033. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12034. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12035. BEGIN
  12036. string := IntermediateCode.String(s);
  12037. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12038. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12039. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12040. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12041. RETURN instruction
  12042. END Asm;
  12043. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12044. VAR instruction: IntermediateCode.Instruction;
  12045. BEGIN
  12046. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12047. RETURN instruction
  12048. END Data;
  12049. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12050. VAR instruction: IntermediateCode.Instruction;
  12051. BEGIN
  12052. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12053. IntermediateCode.SetSubType(instruction, subtype);
  12054. RETURN instruction
  12055. END SpecialInstruction;
  12056. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12057. VAR op1: IntermediateCode.Operand;
  12058. VAR instruction: IntermediateCode.Instruction;
  12059. BEGIN
  12060. (*! generate a warning if size exceeds a certain limit *)
  12061. (*
  12062. ASSERT(bytes < 1000000); (* sanity check *)
  12063. *)
  12064. ASSERT(0 <= units); (* sanity check *)
  12065. IntermediateCode.InitNumber(op1,units);
  12066. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12067. RETURN instruction
  12068. END Reserve;
  12069. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12070. VAR op1: IntermediateCode.Operand;
  12071. VAR instruction: IntermediateCode.Instruction;
  12072. BEGIN
  12073. IntermediateCode.InitNumber(op1,position);
  12074. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12075. RETURN instruction
  12076. END LabelInstruction;
  12077. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12078. VAR pc: LONGINT;
  12079. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12080. BEGIN
  12081. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12082. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12083. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12084. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12085. IF instr.op1.type.form = IntermediateCode.Float THEN
  12086. RETURN instr.op1.floatValue = op.floatValue
  12087. ELSE
  12088. RETURN instr.op1.intValue = op.intValue
  12089. END;
  12090. END ProvidesValue;
  12091. BEGIN
  12092. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12093. pc := 0;
  12094. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12095. INC(pc);
  12096. END;
  12097. IF pc = data.pc THEN
  12098. data.Emit(Data(-1,vop));
  12099. END;
  12100. RETURN pc
  12101. END EnterImmediate;
  12102. PROCEDURE Init;
  12103. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12104. BEGIN
  12105. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12106. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12107. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12108. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12109. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12110. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12111. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12112. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12113. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12114. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12115. IntermediateCode.InitOperand(emptyOperand);
  12116. FOR i := 0 TO NumberSystemCalls-1 DO
  12117. name := "@SystemCall";
  12118. Basic.AppendNumber(name,i);
  12119. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12120. END;
  12121. END Init;
  12122. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12123. BEGIN
  12124. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12125. END IsExported;
  12126. BEGIN
  12127. Init;
  12128. END FoxIntermediateBackend.
  12129. Compiler.Compile FoxIntermediateBackend.Mod ~