FoxIntermediateBackend.Mod 520 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364
  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; align: LONGINT;
  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.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  300. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))));
  301. IF ~x.fixed THEN
  302. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  303. ELSE
  304. align := x.alignment;
  305. END;
  306. irv.SetPositionOrAlignment(x.fixed, align);
  307. meta.CheckTypeDeclaration(x.type);
  308. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  309. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  310. END;
  311. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  312. END VisitVariable;
  313. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  314. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; op: Operand;
  315. BEGIN
  316. ASSERT(currentScope.outerScope IS SyntaxTree.CellScope);
  317. (* code section for variable *)
  318. Global.GetSymbolSegmentedName(x,name);
  319. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  320. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  321. (*
  322. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  323. *)
  324. IF x.defaultValue = NIL THEN
  325. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))))
  326. ELSE
  327. implementationVisitor.inData := TRUE;
  328. implementationVisitor.Evaluate(x.defaultValue, op);
  329. irv.Emit(Data(x.position,op.op));
  330. implementationVisitor.inData := FALSE;
  331. END;
  332. meta.CheckTypeDeclaration(x.type);
  333. END VisitParameter;
  334. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  335. BEGIN
  336. Type(x.declaredType); (* => code in objects *)
  337. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  338. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  339. END;
  340. END VisitTypeDeclaration;
  341. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  342. BEGIN
  343. IF (SyntaxTree.Public * x.access # {}) THEN
  344. implementationVisitor.VisitConstant(x);
  345. END;
  346. END VisitConstant;
  347. PROCEDURE Scope(x: SyntaxTree.Scope);
  348. VAR procedure: SyntaxTree.Procedure;
  349. constant: SyntaxTree.Constant;
  350. variable: SyntaxTree.Variable;
  351. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  352. BEGIN
  353. prevScope := currentScope;
  354. currentScope := x;
  355. (* constants treated in implementation visitor *)
  356. typeDeclaration := x.firstTypeDeclaration;
  357. WHILE typeDeclaration # NIL DO
  358. VisitTypeDeclaration(typeDeclaration);
  359. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  360. END;
  361. variable := x.firstVariable;
  362. WHILE variable # NIL DO
  363. VisitVariable(variable);
  364. variable := variable.nextVariable;
  365. END;
  366. procedure := x.firstProcedure;
  367. WHILE procedure # NIL DO
  368. VisitProcedure(procedure);
  369. procedure := procedure.nextProcedure;
  370. END;
  371. constant := x.firstConstant;
  372. WHILE constant # NIL DO
  373. VisitConstant(constant);
  374. constant := constant.nextConstant;
  375. END;
  376. currentScope := prevScope;
  377. END Scope;
  378. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  379. VAR parameter: SyntaxTree.Parameter;
  380. BEGIN
  381. parameter := first;
  382. WHILE parameter # NIL DO
  383. VisitParameter(parameter);
  384. parameter := parameter.nextParameter;
  385. END;
  386. END Parameters;
  387. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  388. VAR scope: SyntaxTree.ProcedureScope;
  389. prevScope: SyntaxTree.Scope;
  390. inline, finalizer: BOOLEAN;
  391. procedureType: SyntaxTree.ProcedureType;
  392. pc: LONGINT;
  393. stackSize: LONGINT;
  394. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  395. null,size,src,dest,fp,res: IntermediateCode.Operand;
  396. cc: LONGINT;
  397. cellType: SyntaxTree.CellType;
  398. registerNumber: LONGINT;
  399. registerClass: IntermediateCode.RegisterClass;
  400. type: IntermediateCode.Type;
  401. formalParameter: SyntaxTree.Parameter;
  402. recordType: SyntaxTree.RecordType;
  403. isModuleBody: BOOLEAN;
  404. PROCEDURE Signature;
  405. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  406. BEGIN
  407. procedureType := x.type(SyntaxTree.ProcedureType);
  408. returnType := procedureType.returnType;
  409. IF returnType # NIL THEN
  410. meta.CheckTypeDeclaration(returnType)
  411. END;
  412. parameter := procedureType.firstParameter;
  413. WHILE parameter # NIL DO
  414. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  415. parameter := parameter.nextParameter;
  416. END;
  417. END Signature;
  418. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  419. VAR result: BOOLEAN;
  420. BEGIN
  421. result := FALSE;
  422. IF x = SyntaxTree.invalidExpression THEN
  423. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  424. result := TRUE;
  425. value := x.resolved(SyntaxTree.IntegerValue).value;
  426. ELSE
  427. Error(x.position,"expression is not an integer constant");
  428. END;
  429. RETURN result;
  430. END CheckIntegerValue;
  431. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  432. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  433. BEGIN
  434. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  435. WHILE (this # NIL) & (this.identifier # id) DO
  436. this := this.nextModifier;
  437. END;
  438. IF this # NIL THEN
  439. IF this.expression = NIL THEN
  440. Error(this.position,"expected expression value");
  441. ELSIF CheckIntegerValue(this.expression,value) THEN
  442. END;
  443. RETURN TRUE
  444. ELSE RETURN FALSE
  445. END;
  446. END HasValue;
  447. BEGIN
  448. IF x.externalName # NIL THEN RETURN END;
  449. (*
  450. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  451. *)
  452. (* code section for this procedure *)
  453. scope := x.procedureScope;
  454. prevScope := currentScope;
  455. currentScope := scope;
  456. procedureType := x.type(SyntaxTree.ProcedureType);
  457. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  458. implementationVisitor.temporaries.Init;
  459. implementationVisitor.usedRegisters := NIL;
  460. implementationVisitor.registerUsageCount.Init;
  461. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  462. IF (scope.body # NIL) & (x.isInline) THEN
  463. inline := TRUE;
  464. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  465. ir.SetExported(IsExported(x));
  466. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  467. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  468. IF backend.cellsAreObjects THEN
  469. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  470. ir.SetExported(IsExported(x));
  471. ELSE
  472. RETURN; (* cellnet cannot be compiled for final static hardware *)
  473. END;
  474. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  475. inline := FALSE;
  476. AddBodyCallStub(x);
  477. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  478. ir.SetExported(IsExported(x));
  479. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  480. inline := FALSE;
  481. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  482. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := ActiveCells.defaultDataMemorySize END;
  483. AddBodyCallStub(x);
  484. AddStackAllocation(x,stackSize);
  485. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  486. ir.SetExported(IsExported(x));
  487. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  488. inline := FALSE;
  489. Parameters(procedureType.firstParameter);
  490. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  491. ir.SetExported(IsExported(x));
  492. ELSE
  493. inline := FALSE;
  494. IF x.isEntry OR x.isExit THEN
  495. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  496. ir.SetExported(TRUE);
  497. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  498. ELSE
  499. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  500. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  501. END;
  502. END;
  503. cc := procedureType.callingConvention;
  504. IF scope.body # NIL THEN
  505. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  506. registerNumber := 0;
  507. IF ~inline THEN
  508. IF scope.lastVariable = NIL THEN
  509. stackSize := 0
  510. ELSE
  511. stackSize := scope.lastVariable.offsetInBits;
  512. IF stackSize <0 THEN stackSize := -stackSize END;
  513. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  514. END;
  515. (*
  516. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  517. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  518. *)
  519. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  520. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  521. END;
  522. pc := ir.pc-1;
  523. (*
  524. ir.Emit(Nop(position)); (* placeholder for fill *)
  525. *)
  526. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  527. formalParameter := procedureType.lastParameter;
  528. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  529. IF ~PassInRegister(formalParameter) THEN
  530. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  531. ELSE
  532. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  533. type := GetType(system, formalParameter.type);
  534. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  535. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  536. ir.Emit(Mov(-1,dest, src));
  537. implementationVisitor.ReleaseIntermediateOperand(src);
  538. INC(registerNumber);
  539. formalParameter := formalParameter.prevParameter;
  540. END;
  541. END;
  542. END;
  543. ir.EnterValidPAF;
  544. END;
  545. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  546. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  547. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  548. IF scope.lastVariable # NIL THEN
  549. stackSize := scope.lastVariable.offsetInBits;
  550. IF stackSize <0 THEN stackSize := -stackSize END;
  551. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  552. END;
  553. END;
  554. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  555. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  556. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  557. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  558. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  559. ir.EmitAt(pc,Push(size));
  560. implementationVisitor.StaticCallOperand(result,procedure);
  561. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  562. END;
  563. *)
  564. END;
  565. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  566. IF stackSize > 0 THEN
  567. IF (stackSize MOD system.addressSize = 0) THEN
  568. null := IntermediateCode.Immediate(addressType,0);
  569. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  570. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  571. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  572. ELSE
  573. null := IntermediateCode.Immediate(int8,0);
  574. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  575. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  576. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  577. END;
  578. (*! should potentially be called by backend -- enter might initialize
  579. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  580. *)
  581. END;
  582. ir.ExitValidPAF;
  583. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  584. finalizer := FALSE;
  585. IF backend.cooperative & x.isFinalizer THEN
  586. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  587. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  588. GetRecordTypeName(recordType,name);
  589. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  590. END;
  591. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  592. IF backend.cooperative THEN
  593. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  594. IF implementationVisitor.profile & ~isModuleBody THEN
  595. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  596. END;
  597. END;
  598. implementationVisitor.EmitLeave(ir, x.position,cc);
  599. IF finalizer THEN
  600. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  601. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  602. ir.Emit(Br(x.position,dest));
  603. ELSE
  604. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  605. END;
  606. ELSE
  607. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  608. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  609. END;
  610. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  611. IF backend.cooperative THEN
  612. IF HasPointers (scope) THEN
  613. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  614. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  615. ir.Emit(Result(x.position, res));
  616. ir.Emit(Push(x.position, res));
  617. implementationVisitor.ResetVariables(scope);
  618. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  619. ir.Emit(Pop(x.position, res));
  620. ir.Emit(Return(x.position, res));
  621. ELSE
  622. implementationVisitor.ResetVariables(scope);
  623. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  624. END;
  625. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  626. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  627. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  628. ir.Emit(Result(x.position, res));
  629. ir.Emit(Push(x.position, res));
  630. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  631. ir.Emit(Pop(x.position, res));
  632. ir.Emit(Return(x.position, res));
  633. ELSE
  634. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  635. END;
  636. END;
  637. implementationVisitor.EmitLeave(ir,x.position,cc);
  638. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  639. ELSE
  640. ir.Emit(Nop(x.position));
  641. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  642. implementationVisitor.EmitLeave(ir,x.position,cc);
  643. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  644. END;
  645. END;
  646. END
  647. END;
  648. ELSE (* force body for procedures *)
  649. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  650. ir.EnterValidPAF;
  651. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  652. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  653. ir.ExitValidPAF;
  654. implementationVisitor.EmitLeave(ir,x.position,cc);
  655. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  656. END;
  657. Scope(scope);
  658. Signature;
  659. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  660. currentScope := prevScope;
  661. END Procedure;
  662. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  663. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  664. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  665. BEGIN
  666. ASSERT (bodyProcedure # NIL);
  667. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  668. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  669. procedure.SetScope(bodyProcedure.scope);
  670. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  671. procedure.SetAccess(SyntaxTree.Hidden);
  672. Global.GetSymbolSegmentedName (procedure,name);
  673. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  674. ir.SetExported(TRUE);
  675. ir.SetPriority(InitPriority);
  676. Global.GetSymbolSegmentedName (bodyProcedure,name);
  677. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  678. implementationVisitor.currentScope := module.module.moduleScope;
  679. implementationVisitor.section := ir;
  680. implementationVisitor.PushSelfPointer();
  681. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  682. ELSIF backend.preregisterStatic THEN
  683. implementationVisitor.currentScope := module.module.moduleScope;
  684. implementationVisitor.section := ir;
  685. implementationVisitor.PushSelfPointer();
  686. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  687. ELSE
  688. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  689. ir.Emit(Call(bodyProcedure.position,op, 0));
  690. END;
  691. END AddBodyCallStub;
  692. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  693. VAR name: Basic.SegmentedName;
  694. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  695. BEGIN
  696. Global.GetSymbolSegmentedName (symbol,name);
  697. Basic.RemoveSuffix(name);
  698. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  699. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  700. ir.SetExported(TRUE);
  701. ir.SetPriority(FirstPriority);
  702. IntermediateCode.InitImmediate(op,addressType,initStack);
  703. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  704. END AddStackAllocation;
  705. (** entry function to visit a complete module *)
  706. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  707. VAR
  708. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  709. hasDynamicOperatorDeclarations: BOOLEAN;
  710. operator: SyntaxTree.Operator;
  711. import: SyntaxTree.Import;
  712. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  713. BEGIN
  714. type := type.resolved;
  715. IF type IS SyntaxTree.RecordType THEN
  716. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  717. ELSIF (type IS SyntaxTree.ArrayType) THEN
  718. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  719. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  720. END;
  721. ELSIF type IS SyntaxTree.MathArrayType THEN
  722. WITH type: SyntaxTree.MathArrayType DO
  723. IF type.form = SyntaxTree.Open THEN
  724. RETURN TRUE
  725. ELSIF type.form = SyntaxTree.Static THEN
  726. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  727. END;
  728. END;
  729. END;
  730. RETURN FALSE
  731. END TypeNeedsInitialization;
  732. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  733. VAR variable: SyntaxTree.Variable;
  734. BEGIN
  735. variable := scope.firstVariable;
  736. WHILE variable # NIL DO
  737. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  738. IF variable.initializer # NIL THEN RETURN TRUE END;
  739. variable := variable.nextVariable;
  740. END;
  741. RETURN FALSE
  742. END ScopeNeedsInitialization;
  743. BEGIN
  744. ASSERT(x # NIL); ASSERT(module # NIL);
  745. SELF.module := module;
  746. (* add import names to the generated Sections.Module *)
  747. import := x.moduleScope.firstImport;
  748. WHILE import # NIL DO
  749. import.module.GetName(idstr);
  750. module.imports.AddName(idstr);
  751. import := import.nextImport
  752. END;
  753. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  754. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  755. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  756. moduleSelf.SetType(system.anyType);
  757. moduleSelf.SetScope(x.moduleScope);
  758. moduleSelf.SetUntraced(TRUE);
  759. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  760. ir.SetExported(TRUE);
  761. IntermediateCode.InitImmediate(op,addressType,0);
  762. ir.Emit(Data(-1,op));
  763. END;
  764. implementationVisitor.module := module;
  765. implementationVisitor.moduleScope := x.moduleScope;
  766. implementationVisitor.moduleSelf := moduleSelf;
  767. implementationVisitor.canBeLoaded := TRUE;
  768. meta.SetModule(module);
  769. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  770. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  771. END;
  772. IF backend.profile THEN
  773. EnsureBodyProcedure(x.moduleScope);
  774. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  775. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  776. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  777. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  778. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  779. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  780. Global.GetModuleName(module.module,idstr);
  781. implementationVisitor.ProfilerAddModule(idstr);
  782. implementationVisitor.numberProcedures := 0;
  783. END;
  784. implementationVisitor.profile := backend.profile;
  785. (* check if there is at least one dynamic operator locally defined *)
  786. hasDynamicOperatorDeclarations := FALSE;
  787. operator := x.moduleScope.firstOperator;
  788. WHILE operator # NIL DO
  789. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  790. operator := operator.nextOperator
  791. END;
  792. (* add operator initialization code section *)
  793. IF hasDynamicOperatorDeclarations THEN
  794. EnsureBodyProcedure(x.moduleScope);
  795. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  796. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  797. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  798. END;
  799. Scope(x.moduleScope);
  800. IF hasDynamicOperatorDeclarations THEN
  801. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  802. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  803. END;
  804. IF backend.profile THEN
  805. implementationVisitor.ProfilerPatchInit;
  806. END;
  807. END Module;
  808. END DeclarationVisitor;
  809. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  810. RegisterUsageCount*=OBJECT
  811. VAR used: UsedArray; count: LONGINT;
  812. PROCEDURE &Init;
  813. VAR i: LONGINT;
  814. BEGIN
  815. count := 0;
  816. IF used = NIL THEN NEW(used,64); END;
  817. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  818. END Init;
  819. PROCEDURE Grow;
  820. VAR new: UsedArray; size,i: LONGINT;
  821. BEGIN
  822. size := LEN(used)*2;
  823. NEW(new,size);
  824. FOR i := 0 TO LEN(used)-1 DO
  825. new[i].count := used[i].count;
  826. new[i].type := used[i].type;
  827. new[i].map := used[i].map
  828. END;
  829. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  830. used := new
  831. END Grow;
  832. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  833. BEGIN
  834. INC(count);
  835. IF count = LEN(used) THEN Grow END;
  836. used[count].type := type;
  837. used[count].class := class;
  838. used[count].map := count;
  839. RETURN count;
  840. END Next;
  841. PROCEDURE IncUse(register: LONGINT);
  842. BEGIN
  843. INC(used[register].count);
  844. (*
  845. IF (register = 1) & (count > 30) THEN
  846. D.TraceBack;
  847. END;
  848. *)
  849. END IncUse;
  850. PROCEDURE DecUse(register: LONGINT);
  851. BEGIN
  852. DEC(used[register].count);
  853. END DecUse;
  854. PROCEDURE Map(register: LONGINT): LONGINT;
  855. VAR map : LONGINT;
  856. BEGIN
  857. IF register > 0 THEN
  858. map := used[register].map;
  859. WHILE register # map DO register := map; map := used[register].map END;
  860. END;
  861. RETURN register
  862. END Map;
  863. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  864. BEGIN
  865. used[register].map:= to;
  866. used[to].count := used[register].count;
  867. used[register].count := 0;
  868. END Remap;
  869. PROCEDURE Use(register: LONGINT): LONGINT;
  870. BEGIN
  871. IF register< LEN(used) THEN
  872. RETURN used[register].count
  873. ELSE
  874. RETURN 0
  875. END
  876. END Use;
  877. END RegisterUsageCount;
  878. RegisterEntry = POINTER TO RECORD
  879. prev,next: RegisterEntry;
  880. register: LONGINT;
  881. registerClass: IntermediateCode.RegisterClass;
  882. type: IntermediateCode.Type;
  883. END;
  884. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  885. Variables = OBJECT (Basic.List)
  886. VAR
  887. inUse: VariableUse;
  888. registerIndex: LONGINT;
  889. PROCEDURE & Init;
  890. VAR i: LONGINT;
  891. BEGIN
  892. InitList(16);
  893. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  894. registerIndex := 1024;
  895. END Init;
  896. PROCEDURE GetUsage(VAR use: VariableUse);
  897. BEGIN
  898. use := inUse;
  899. END GetUsage;
  900. PROCEDURE SetUsage(CONST use: VariableUse);
  901. BEGIN
  902. inUse := use;
  903. END SetUsage;
  904. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  905. VAR any: ANY;
  906. BEGIN
  907. any := Get(i);;
  908. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  909. END GetVariable;
  910. PROCEDURE Occupy(pos: LONGINT);
  911. BEGIN
  912. INCL(inUse[pos DIV 32], pos MOD 32);
  913. END Occupy;
  914. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  915. BEGIN
  916. Occupy(Length());
  917. Add(v);
  918. END AddVariable;
  919. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  920. VAR var : SyntaxTree.Variable;
  921. BEGIN
  922. FOR pos := 0 TO Length()-1 DO
  923. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  924. var := GetVariable(pos);
  925. IF type.SameType(var.type) THEN
  926. Occupy(pos); RETURN var
  927. END;
  928. END;
  929. END;
  930. pos := Length();
  931. RETURN NIL
  932. END GetFreeVariable;
  933. END Variables;
  934. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  935. SymbolMapper = OBJECT
  936. VAR
  937. first: SymbolMap;
  938. PROCEDURE & Init;
  939. BEGIN
  940. first := NIL;
  941. END Init;
  942. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  943. VAR new: SymbolMap;
  944. BEGIN
  945. NEW(new); new.this := this; new.to := to; new.tag := tag;
  946. new.next := first; first := new;
  947. END Add;
  948. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  949. VAR s: SymbolMap;
  950. BEGIN
  951. s := first;
  952. WHILE (s # NIL) & (s.this # this) DO
  953. s := s.next
  954. END;
  955. RETURN s
  956. END Get;
  957. END SymbolMapper;
  958. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  959. VAR
  960. system: Global.System;
  961. section: IntermediateCode.Section;
  962. module: Sections.Module;
  963. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  964. awaitProcCounter, labelId, constId, caseId: LONGINT;
  965. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  966. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  967. checker: SemanticChecker.Checker;
  968. backend: IntermediateBackend;
  969. meta: MetaDataGenerator;
  970. position: LONGINT;
  971. moduleSelf: SyntaxTree.Variable;
  972. (* variables for hand over of variables / temporary state *)
  973. currentScope: SyntaxTree.Scope;
  974. constantDeclaration : SyntaxTree.Symbol;
  975. result: Operand; (* result of the most recent expression / statement *)
  976. destination: IntermediateCode.Operand;
  977. arrayDestinationTag: IntermediateCode.Operand;
  978. arrayDestinationDimension:LONGINT;
  979. currentLoop: Label; (* variable to hand over loop exit jump list *)
  980. conditional: BOOLEAN;
  981. trueLabel, falseLabel, exitLabel: Label;
  982. locked: BOOLEAN;
  983. (*
  984. usedRegisters: Registers;
  985. *)
  986. registerUsageCount: RegisterUsageCount;
  987. usedRegisters: RegisterEntry;
  988. (* useful operands and types *)
  989. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  990. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  991. commentPrintout: Printout.Printer;
  992. dump: Streams.Writer;
  993. tagsAvailable : BOOLEAN;
  994. supportedInstruction: SupportedInstructionProcedure;
  995. supportedImmediate: SupportedImmediateProcedure;
  996. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  997. emitLabels: BOOLEAN;
  998. runtimeModuleName : SyntaxTree.IdentifierString;
  999. newObjectFile: BOOLEAN;
  1000. indexCounter: LONGINT;
  1001. profile: BOOLEAN;
  1002. profileId, profileInit: IntermediateCode.Section;
  1003. profileInitPatchPosition: LONGINT;
  1004. numberProcedures: LONGINT;
  1005. procedureResultDesignator : SyntaxTree.Designator;
  1006. operatorInitializationCodeSection: IntermediateCode.Section;
  1007. fingerPrinter: FingerPrinter.FingerPrinter;
  1008. temporaries: Variables;
  1009. canBeLoaded : BOOLEAN;
  1010. currentIsInline: BOOLEAN;
  1011. currentMapper: SymbolMapper;
  1012. currentInlineExit: Label;
  1013. moduleBodySection: IntermediateCode.Section;
  1014. NeedDescriptor : BOOLEAN;
  1015. cooperativeSwitches: BOOLEAN;
  1016. lastSwitchPC: LONGINT;
  1017. isUnchecked: BOOLEAN;
  1018. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1019. newObjectFile: BOOLEAN);
  1020. BEGIN
  1021. SELF.system := system;
  1022. SELF.runtimeModuleName := runtime;
  1023. currentScope := NIL;
  1024. hiddenPointerType := NIL;
  1025. delegatePointerType := NIL;
  1026. awaitProcCounter := 0;
  1027. labelId := 0; constId := 0; labelId := 0;
  1028. SELF.checker := checker;
  1029. SELF.backend := backend;
  1030. position := Diagnostics.Invalid;
  1031. conditional := FALSE;
  1032. locked := FALSE;
  1033. InitOperand(result,ModeUndefined);
  1034. addressType := IntermediateCode.GetType(system,system.addressType);
  1035. setType := IntermediateCode.GetType(system,system.setType);
  1036. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1037. byteType := IntermediateCode.GetType(system, system.byteType);
  1038. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1039. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1040. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1041. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1042. nil := IntermediateCode.Immediate(addressType,0);
  1043. IntermediateCode.InitOperand(destination);
  1044. tagsAvailable := TRUE;
  1045. supportedInstruction := supportedInstructionProcedure;
  1046. supportedImmediate := supportedImmediateProcedure;
  1047. inData := FALSE;
  1048. SELF.emitLabels := emitLabels;
  1049. IntermediateCode.InitOperand(arrayDestinationTag);
  1050. bool := IntermediateCode.GetType(system,system.booleanType);
  1051. IntermediateCode.InitImmediate(false,bool,0);
  1052. IntermediateCode.InitImmediate(true,bool,1);
  1053. SELF.newObjectFile := newObjectFile;
  1054. indexCounter := 0;
  1055. NEW(registerUsageCount);
  1056. usedRegisters := NIL;
  1057. procedureResultDesignator := NIL;
  1058. NEW(fingerPrinter, system);
  1059. NEW(temporaries);
  1060. currentIsInline := FALSE;
  1061. NeedDescriptor := FALSE;
  1062. isUnchecked := backend.noRuntimeChecks;
  1063. END Init;
  1064. TYPE Context = RECORD
  1065. section: IntermediateCode.Section;
  1066. registerUsageCount: RegisterUsageCount;
  1067. usedRegisters: RegisterEntry;
  1068. END;
  1069. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1070. VAR context: Context;
  1071. BEGIN
  1072. context.section := section;
  1073. context.registerUsageCount := registerUsageCount;
  1074. context.usedRegisters := usedRegisters;
  1075. section := new;
  1076. NEW(registerUsageCount);
  1077. usedRegisters := NIL;
  1078. RETURN context;
  1079. END SwitchContext;
  1080. PROCEDURE ReturnToContext(context: Context);
  1081. BEGIN
  1082. section := context.section;
  1083. registerUsageCount := context.registerUsageCount;
  1084. usedRegisters := context.usedRegisters;
  1085. END ReturnToContext;
  1086. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1087. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1088. BEGIN
  1089. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1090. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1091. END;
  1092. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1093. section.SetExported(IsExported(syntaxTreeSymbol));
  1094. RETURN section
  1095. END NewSection;
  1096. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1097. VAR new: LONGINT;
  1098. BEGIN
  1099. new := registerUsageCount.Next(type,class);
  1100. UseRegister(new);
  1101. RETURN new
  1102. END AcquireRegister;
  1103. (** get the name for the code section that represens a certain symbol
  1104. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1105. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1106. VAR
  1107. operatorFingerPrint: SyntaxTree.FingerPrint;
  1108. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1109. BEGIN
  1110. Global.GetSymbolSegmentedName(symbol, name);
  1111. (* if the symbol is an operator, then append the fingerprint to the name *)
  1112. IF symbol IS SyntaxTree.Operator THEN
  1113. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1114. string := "[";
  1115. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1116. Strings.Append(string, operatorFingerPrintString);
  1117. Strings.Append(string, "]");
  1118. Basic.AppendToSegmentedName(name,string);
  1119. END
  1120. END GetCodeSectionNameForSymbol;
  1121. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1122. BEGIN
  1123. IF dump # NIL THEN
  1124. dump.String("enter "); dump.String(s); dump.Ln;
  1125. END;
  1126. END TraceEnter;
  1127. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1128. BEGIN
  1129. IF dump # NIL THEN
  1130. dump.String("exit "); dump.String(s); dump.Ln;
  1131. END;
  1132. END TraceExit;
  1133. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1134. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1135. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1136. VAR i: LONGINT;
  1137. BEGIN
  1138. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1139. IF op.rule # NIL THEN
  1140. FOR i := 0 TO LEN(op.rule)-1 DO
  1141. CheckRegister(op.rule[i])
  1142. END;
  1143. END;
  1144. END CheckRegister;
  1145. BEGIN
  1146. CheckRegister(instruction.op1);
  1147. CheckRegister(instruction.op2);
  1148. CheckRegister(instruction.op3);
  1149. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1150. ELSE section.Emit(instruction);
  1151. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1152. END;
  1153. END Emit;
  1154. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1155. BEGIN
  1156. IF backend.cooperative THEN
  1157. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1158. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1159. ELSE
  1160. Emit(Trap(position,trapNo));
  1161. END;
  1162. END EmitTrap;
  1163. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1164. VAR name: Basic.SegmentedName;
  1165. VAR op1, op2, reg: IntermediateCode.Operand;
  1166. VAR call, nocall: Label;
  1167. VAR parametersSize: LONGINT;
  1168. VAR prevSection: IntermediateCode.Section;
  1169. VAR prevDump: Streams.Writer;
  1170. VAR body: SyntaxTree.Body;
  1171. BEGIN
  1172. ASSERT(~procedure.type(SyntaxTree.ProcedureType).noPAF);
  1173. prevSection := SELF.section;
  1174. SELF.section := section;
  1175. prevDump := dump;
  1176. dump := section.comments;
  1177. IF backend.hasLinkRegister THEN
  1178. Emit(Push(-1, lr));
  1179. END;
  1180. Emit(Push(-1,fp));
  1181. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1182. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1183. GetCodeSectionNameForSymbol(procedure, name);
  1184. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1185. ELSE
  1186. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1187. END;
  1188. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1189. Emit(Push(-1,op1));
  1190. Emit(Mov(-1,fp, sp));
  1191. body := procedure.procedureScope.body;
  1192. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1193. NEW(call, section);
  1194. NEW(nocall, section);
  1195. reg := NewRegisterOperand(addressType);
  1196. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1197. Emit(Sub(-1,reg, sp, op1));
  1198. BrltL(call, sp, reg);
  1199. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1200. BrgeL(nocall, sp, op2);
  1201. call.Resolve(section.pc);
  1202. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1203. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1204. Emit(Push(-1, op2));
  1205. Emit(Push(-1, reg));
  1206. ReleaseIntermediateOperand(reg);
  1207. CallThis(position, "Activities","ExpandStack",2);
  1208. Emit(Result(-1, sp));
  1209. nocall.Resolve(section.pc);
  1210. END;
  1211. ELSE
  1212. Emit(Mov(-1, fp, sp));
  1213. END;
  1214. Emit(Enter(-1, callconv, varSize));
  1215. SELF.section := prevSection;
  1216. dump := prevDump;
  1217. END EmitEnter;
  1218. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1219. VAR op1,op2: IntermediateCode.Operand;
  1220. VAR instruction: IntermediateCode.Instruction;
  1221. BEGIN
  1222. IntermediateCode.InitNumber(op1,callconv);
  1223. IntermediateCode.InitNumber(op2,varSize);
  1224. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1225. RETURN instruction
  1226. END Enter;
  1227. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1228. VAR op1: IntermediateCode.Operand;
  1229. VAR instruction: IntermediateCode.Instruction;
  1230. BEGIN
  1231. IntermediateCode.InitNumber(op1,callconv);
  1232. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1233. RETURN instruction
  1234. END Leave;
  1235. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1236. VAR prevSection: IntermediateCode.Section;
  1237. VAR op2: IntermediateCode.Operand;
  1238. BEGIN
  1239. prevSection := SELF.section;
  1240. SELF.section := section;
  1241. Emit(Leave(position, callconv));
  1242. IF backend.cooperative THEN
  1243. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1244. Emit(Add(position, sp, fp, op2));
  1245. ELSE
  1246. Emit(Mov(position, sp, fp));
  1247. END;
  1248. Emit(Pop(position, fp));
  1249. SELF.section := prevSection;
  1250. END EmitLeave;
  1251. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1252. VAR m: SymbolMap;
  1253. BEGIN
  1254. position := x.position;
  1255. IF currentIsInline THEN
  1256. m := currentMapper.Get(x);
  1257. IF m # NIL THEN
  1258. (*
  1259. Printout.Info("mapping from", x);
  1260. Printout.Info("mapping to ", m.to);
  1261. *)
  1262. m.to.Accept(SELF);
  1263. op := result;
  1264. IF m.tag # NIL THEN
  1265. ReleaseIntermediateOperand(result.tag);
  1266. m.tag.Accept(SELF);
  1267. op.tag := result.op;
  1268. ReleaseIntermediateOperand(result.tag);
  1269. END;
  1270. RETURN
  1271. END;
  1272. END;
  1273. x.Accept(SELF);
  1274. op := result;
  1275. END Symbol;
  1276. PROCEDURE Expression(x: SyntaxTree.Expression);
  1277. BEGIN
  1278. position := x.position;
  1279. constantDeclaration := NIL;
  1280. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1281. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1282. END;
  1283. IF x.resolved # NIL THEN
  1284. x.resolved.Accept(SELF)
  1285. ELSE
  1286. x.Accept(SELF)
  1287. END;
  1288. (* check this, was commented out in ActiveCells3 *)
  1289. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1290. Error(x.position, "unsupported modifier");
  1291. END;
  1292. END Expression;
  1293. (*
  1294. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1295. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1296. BEGIN
  1297. temporaries.GetUsage(current);
  1298. FOR i := 0 TO LEN(current)-1 DO
  1299. set := current[i] - previous[i];
  1300. IF set # {} THEN
  1301. FOR j := 0 TO MAX(SET)-1 DO
  1302. IF j IN set THEN
  1303. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1304. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1305. Symbol(variable, op);
  1306. MakeMemory(tmp,op.op,addressType,0);
  1307. ReleaseOperand(op);
  1308. Emit(Mov(position,tmp, nil ) );
  1309. ReleaseIntermediateOperand(tmp);
  1310. END;
  1311. END;
  1312. END;
  1313. END;
  1314. END;
  1315. END ResetUsedTemporaries;
  1316. *)
  1317. PROCEDURE Statement(x: SyntaxTree.Statement);
  1318. VAR use: VariableUse;
  1319. BEGIN
  1320. temporaries.GetUsage(use);
  1321. position := x.position;
  1322. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1323. IF commentPrintout # NIL THEN
  1324. commentPrintout.Statement(x);
  1325. dump.Ln;
  1326. (*dump.Update;*)
  1327. END;
  1328. x.Accept(SELF);
  1329. (*
  1330. CheckRegistersFree();
  1331. *)
  1332. (*ResetUsedTemporaries(use);*)
  1333. temporaries.SetUsage(use);
  1334. END Statement;
  1335. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1336. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1337. *)
  1338. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1339. BEGIN
  1340. IF op.mode = IntermediateCode.ModeMemory THEN
  1341. ReuseCopy(res,op);
  1342. ELSE
  1343. res := op;
  1344. UseIntermediateOperand(res);
  1345. END;
  1346. IntermediateCode.AddOffset(res,offset);
  1347. IntermediateCode.MakeMemory(res,type);
  1348. END MakeMemory;
  1349. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1350. VAR mem: IntermediateCode.Operand;
  1351. BEGIN
  1352. MakeMemory(mem,res,type,offset);
  1353. ReleaseIntermediateOperand(res);
  1354. res := mem;
  1355. END ToMemory;
  1356. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1357. VAR mem: IntermediateCode.Operand;
  1358. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1359. componentType: SyntaxTree.Type;
  1360. BEGIN
  1361. type := type.resolved;
  1362. IF operand.mode = ModeReference THEN
  1363. IF type IS SyntaxTree.RangeType THEN
  1364. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1365. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1366. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1367. ReleaseOperand(operand);
  1368. operand.op := firstOp;
  1369. operand.tag := lastOp;
  1370. operand.extra := stepOp;
  1371. ELSIF type IS SyntaxTree.ComplexType THEN
  1372. componentType := type(SyntaxTree.ComplexType).componentType;
  1373. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1374. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1375. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1376. ReleaseOperand(operand);
  1377. operand.op := firstOp;
  1378. operand.tag := lastOp
  1379. ELSE
  1380. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1381. ReleaseIntermediateOperand(operand.op);
  1382. operand.op := mem;
  1383. END;
  1384. operand.mode := ModeValue;
  1385. END;
  1386. ASSERT(operand.mode = ModeValue);
  1387. END LoadValue;
  1388. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1389. VAR prevConditional: BOOLEAN;
  1390. BEGIN
  1391. prevConditional := conditional;
  1392. conditional := FALSE;
  1393. InitOperand(result, ModeUndefined);
  1394. Expression(x);
  1395. op := result;
  1396. LoadValue(op,x.type.resolved);
  1397. conditional := prevConditional;
  1398. END Evaluate;
  1399. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1400. VAR prevConditional: BOOLEAN;
  1401. BEGIN
  1402. prevConditional := conditional;
  1403. conditional := FALSE;
  1404. InitOperand(result,ModeUndefined);
  1405. Expression(x);
  1406. op := result;
  1407. (*
  1408. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1409. *)
  1410. conditional := prevConditional;
  1411. END Designate;
  1412. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1413. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1414. BEGIN
  1415. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1416. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1417. conditional := TRUE;
  1418. trueLabel := trueL; falseLabel := falseL;
  1419. Expression(x);
  1420. trueL := trueLabel; falseL := falseLabel;
  1421. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1422. END Condition;
  1423. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1424. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1425. BEGIN
  1426. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1427. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1428. RETURN op
  1429. END NewRegisterOperand;
  1430. PROCEDURE UnuseRegister(register: LONGINT);
  1431. BEGIN
  1432. IF (register > 0) THEN
  1433. register := registerUsageCount.Map(register);
  1434. registerUsageCount.DecUse(register);
  1435. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1436. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1437. END;
  1438. IF registerUsageCount.Use(register)=0 THEN
  1439. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1440. Warning(position, "register cannot be removed");
  1441. END;
  1442. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1443. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1444. END;
  1445. ELSIF registerUsageCount.Use(register)<0 THEN
  1446. Warning(position, "register removed too often");
  1447. IF dump # NIL THEN
  1448. dump.String("register removed too often"); dump.Ln; dump.Update;
  1449. END;
  1450. D.TraceBack;
  1451. END;
  1452. END;
  1453. END UnuseRegister;
  1454. PROCEDURE UseRegister(register: LONGINT);
  1455. BEGIN
  1456. IF (register > 0) THEN
  1457. register := registerUsageCount.Map(register);
  1458. registerUsageCount.IncUse(register);
  1459. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1460. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1461. END;
  1462. IF registerUsageCount.Use(register)=1 THEN
  1463. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1464. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1465. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1466. END;
  1467. END;
  1468. END;
  1469. END UseRegister;
  1470. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1471. BEGIN
  1472. UnuseRegister(op.register)
  1473. END ReleaseIntermediateOperand;
  1474. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1475. BEGIN
  1476. UseRegister(op.register)
  1477. END UseIntermediateOperand;
  1478. PROCEDURE ReleaseOperand(CONST op: Operand);
  1479. BEGIN
  1480. UnuseRegister(op.op.register);
  1481. UnuseRegister(op.tag.register);
  1482. UnuseRegister(op.extra.register);
  1483. END ReleaseOperand;
  1484. (* save registers marked in array "markedRegisters" to the stack
  1485. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1486. *)
  1487. PROCEDURE SaveRegisters();
  1488. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1489. BEGIN
  1490. entry := usedRegisters;
  1491. WHILE entry # NIL DO
  1492. type := registerUsageCount.used[entry.register].type;
  1493. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1494. Emit(Push(position,op));
  1495. entry := entry.next;
  1496. END;
  1497. END SaveRegisters;
  1498. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1499. BEGIN
  1500. saved := usedRegisters;
  1501. usedRegisters := NIL;
  1502. END ReleaseUsedRegisters;
  1503. (** remove parameter registers from used queue *)
  1504. PROCEDURE ReleaseParameterRegisters;
  1505. VAR entry,prev,next: RegisterEntry;
  1506. BEGIN
  1507. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1508. WHILE entry # NIL DO
  1509. next := entry.next;
  1510. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1511. registerUsageCount.DecUse(entry.register);
  1512. ASSERT(registerUsageCount.Use(entry.register)=0);
  1513. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1514. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1515. END;
  1516. ELSIF prev = NIL THEN
  1517. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1518. ELSE
  1519. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1520. END;
  1521. entry := next;
  1522. END;
  1523. END ReleaseParameterRegisters;
  1524. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1525. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1526. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1527. BEGIN
  1528. entry := saved;
  1529. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1530. entry := prev;
  1531. WHILE entry # NIL DO
  1532. prev := entry.prev;
  1533. type := entry.type;
  1534. class := entry.registerClass;
  1535. IntermediateCode.InitRegister(op,type,class,entry.register);
  1536. (*
  1537. new := registerUsageCount.Next(type,class);
  1538. registerUsageCount.Remap(entry.register,new);
  1539. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1540. dump.String("remap register "); dump.Int(entry.register,1);
  1541. dump.String("to "); dump.Int(new,1);
  1542. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1543. END;
  1544. entry.register := new;
  1545. *)
  1546. Emit(Pop(position,op));
  1547. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1548. entry := prev;
  1549. END;
  1550. (*
  1551. usedRegisters := saved;
  1552. *)
  1553. END RestoreRegisters;
  1554. PROCEDURE CheckRegistersFree;
  1555. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1556. BEGIN
  1557. IF usedRegisters # NIL THEN
  1558. r := usedRegisters;
  1559. WHILE r # NIL DO
  1560. warning := "register ";
  1561. Strings.AppendInt(warning, r.register);
  1562. Strings.Append(warning, " not released.");
  1563. Warning(position,warning);
  1564. r := r .next;
  1565. END;
  1566. END;
  1567. FOR i := 0 TO registerUsageCount.count-1 DO
  1568. IF registerUsageCount.used[i].count < 0 THEN
  1569. warning := "register ";
  1570. Strings.AppendInt(warning, i);
  1571. Strings.Append(warning, " unused too often.");
  1572. Warning(position,warning);
  1573. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1574. warning := "register ";
  1575. Strings.AppendInt(warning, i);
  1576. Strings.Append(warning, " not unused often enough.");
  1577. Warning(position,warning);
  1578. END;
  1579. END;
  1580. END CheckRegistersFree;
  1581. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1582. Otherwise allocate a new register.
  1583. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1584. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1585. BEGIN
  1586. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1587. UseIntermediateOperand(result);
  1588. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1589. UseIntermediateOperand(result);
  1590. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1591. END;
  1592. END Reuse2;
  1593. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1594. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1595. If not then allocate a new register.
  1596. Does NOT necessarily keep the content of src1 or src2 in result!
  1597. *)
  1598. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1599. BEGIN
  1600. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1601. UseIntermediateOperand(result);
  1602. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1603. UseIntermediateOperand(result);
  1604. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1605. result := alternative; alternative := emptyOperand;
  1606. UseIntermediateOperand(result);
  1607. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1608. END;
  1609. END Reuse2a;
  1610. (* like reuse2 but only one source *)
  1611. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1612. BEGIN
  1613. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1614. UseIntermediateOperand(result);
  1615. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1616. END;
  1617. END Reuse1;
  1618. (* like reuse2a but only one source *)
  1619. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1620. BEGIN
  1621. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1622. UseIntermediateOperand(result);
  1623. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1624. UseIntermediateOperand(result);
  1625. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1626. END;
  1627. END Reuse1a;
  1628. (* like reuse1 but guarantees that content of src1 is in result *)
  1629. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1630. BEGIN
  1631. IF ReusableRegister(src1) THEN
  1632. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1633. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1634. UseIntermediateOperand(result);
  1635. ELSE
  1636. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1637. Emit(Mov(position,result,src1));
  1638. END
  1639. END ReuseCopy;
  1640. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1641. BEGIN
  1642. IF ReusableRegister(src) THEN
  1643. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1644. ELSE
  1645. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1646. Emit(Mov(position,result,src));
  1647. ReleaseIntermediateOperand(src);
  1648. END
  1649. END TransferToRegister;
  1650. (** labels and branches **)
  1651. PROCEDURE NewLabel(): Label;
  1652. VAR label: Label;
  1653. BEGIN
  1654. NEW(label,section); RETURN label;
  1655. END NewLabel;
  1656. PROCEDURE SetLabel(label: Label);
  1657. BEGIN label.Resolve(section.pc);
  1658. END SetLabel;
  1659. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1660. BEGIN
  1661. ASSERT(label # NIL);
  1662. IF label.pc < 0 THEN (* label not yet set *)
  1663. label.AddFixup(section.pc);
  1664. END;
  1665. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1666. END LabelOperand;
  1667. PROCEDURE BrL(label: Label);
  1668. BEGIN
  1669. Emit(Br(position,LabelOperand(label)));
  1670. END BrL;
  1671. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1672. BEGIN
  1673. Emit(Brge(position,LabelOperand(label),left,right));
  1674. END BrgeL;
  1675. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1676. BEGIN
  1677. Emit(Brlt(position,LabelOperand(label),left,right));
  1678. END BrltL;
  1679. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1680. BEGIN
  1681. Emit(Breq(position,LabelOperand(label),left,right));
  1682. END BreqL;
  1683. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1684. BEGIN
  1685. Emit(Brne(position,LabelOperand(label),left,right));
  1686. END BrneL;
  1687. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1688. VAR new: IntermediateCode.Operand;
  1689. BEGIN
  1690. IF Trace THEN TraceEnter("Convert") END;
  1691. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1692. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1693. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1694. & (operand.offset = 0)
  1695. THEN
  1696. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1697. Emit(Conv(position,new,operand));
  1698. ELSE
  1699. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1700. Emit(Conv(position,new,operand));
  1701. ReleaseIntermediateOperand(operand);
  1702. END;
  1703. operand := new;
  1704. 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
  1705. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1706. ELSE
  1707. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1708. Emit(Conv(position,new,operand));
  1709. ReleaseIntermediateOperand(operand);
  1710. operand := new;
  1711. END;
  1712. IF Trace THEN TraceExit("Convert") END;
  1713. END Convert;
  1714. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1715. VAR exit: Label;
  1716. BEGIN
  1717. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1718. exit := NewLabel();
  1719. br(exit,left,right);
  1720. EmitTrap(position,trapNo);
  1721. SetLabel(exit);
  1722. END TrapC;
  1723. (** expressions *)
  1724. (** emit necessary runtime check for set elements **)
  1725. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1726. VAR max: IntermediateCode.Operand;
  1727. BEGIN
  1728. IF isUnchecked THEN RETURN END;
  1729. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1730. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1731. TrapC(BrgeL, max, o, SetElementTrap);
  1732. END;
  1733. END CheckSetElement;
  1734. (** the set that a range represents **)
  1735. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1736. VAR
  1737. operand: Operand;
  1738. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1739. BEGIN
  1740. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1741. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1742. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1743. one := IntermediateCode.Immediate(setType, 1);
  1744. Evaluate(x, operand);
  1745. Convert(operand.op, setType);
  1746. Convert(operand.tag, setType);
  1747. CheckSetElement(operand.op);
  1748. CheckSetElement(operand.tag);
  1749. (* create mask for lower bound
  1750. i.e. shift 11111111 to the left by the value of the lower bound
  1751. *)
  1752. Reuse1(temp, operand.op);
  1753. Emit(Shl(position,temp, allBits, operand.op));
  1754. ReleaseIntermediateOperand(operand.op);
  1755. operand.op := temp;
  1756. (* create mask for upper bound
  1757. i.e. shift 11111111 to the right by the difference between the
  1758. upper bound and the maximum number of set elements
  1759. *)
  1760. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1761. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1762. Reuse1(temp, operand.tag);
  1763. ELSE
  1764. Reuse1(temp, operand.tag);
  1765. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1766. Emit(Sub(position,temp, size, operand.tag));
  1767. END;
  1768. Emit(Shr(position,temp, allBits, operand.tag));
  1769. ReleaseIntermediateOperand(operand.tag);
  1770. operand.tag := temp;
  1771. Reuse2(resultingSet, operand.op, operand.tag);
  1772. (* intersect the two masks *)
  1773. Emit(And(position,resultingSet, operand.op, operand.tag));
  1774. ReleaseOperand(operand);
  1775. RETURN resultingSet
  1776. END SetFromRange;
  1777. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1778. VAR
  1779. res, operand: Operand;
  1780. temp, one, noBits, dest: IntermediateCode.Operand;
  1781. expression: SyntaxTree.Expression;
  1782. i: LONGINT;
  1783. BEGIN
  1784. IF Trace THEN TraceEnter("VisitSet") END;
  1785. dest := destination;
  1786. destination := emptyOperand;
  1787. noBits := IntermediateCode.Immediate(setType, 0);
  1788. one := IntermediateCode.Immediate(setType, 1);
  1789. (* start off with the empty set *)
  1790. InitOperand(res, ModeValue);
  1791. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1792. Emit(Mov(position,res.op, noBits));
  1793. FOR i := 0 TO x.elements.Length() - 1 DO
  1794. expression := x.elements.GetExpression(i);
  1795. IF expression IS SyntaxTree.RangeExpression THEN
  1796. (* range of set elements *)
  1797. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1798. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1799. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1800. ReleaseIntermediateOperand(temp)
  1801. ELSE
  1802. (* singelton element *)
  1803. Evaluate(expression, operand);
  1804. Convert(operand.op, setType);
  1805. CheckSetElement(operand.op);
  1806. (* create subset containing single element *)
  1807. Reuse1(temp, operand.op);
  1808. Emit(Shl(position,temp, one, operand.op));
  1809. ReleaseOperand(operand);
  1810. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1811. ReleaseIntermediateOperand(temp);
  1812. END
  1813. END;
  1814. result := res;
  1815. destination := dest;
  1816. IF Trace THEN TraceExit("VisitSet") END;
  1817. END VisitSet;
  1818. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1819. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1820. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1821. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1822. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1823. element: SyntaxTree.IntegerValue;
  1824. BEGIN
  1825. numberElements := x.elements.Length();
  1826. expression := index.parameters.GetExpression(dim);
  1827. element := expression(SyntaxTree.IntegerValue);
  1828. FOR i := 0 TO numberElements-1 DO
  1829. expression := x.elements.GetExpression(i);
  1830. element.SetValue(i);
  1831. IF expression IS SyntaxTree.MathArrayExpression THEN
  1832. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1833. ELSE
  1834. Assign(index,expression);
  1835. END;
  1836. END;
  1837. END RecursiveAssignment;
  1838. BEGIN
  1839. variable := GetTemporaryVariable(x.type, FALSE);
  1840. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1841. designator.SetType(variable.type);
  1842. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1843. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1844. FOR i := 0 TO dim-1 DO
  1845. element := SyntaxTree.NewIntegerValue(x.position,0);
  1846. element.SetType(system.longintType);
  1847. index.parameters.AddExpression(element);
  1848. END;
  1849. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1850. RecursiveAssignment(x,0);
  1851. Expression(designator);
  1852. END VisitMathArrayExpression;
  1853. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1854. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1855. BEGIN
  1856. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1857. dest := destination; destination := emptyOperand;
  1858. IF x.operator = Scanner.Not THEN
  1859. IF conditional THEN
  1860. Condition(x.left,falseLabel,trueLabel)
  1861. ELSE
  1862. Evaluate(x.left,operand);
  1863. InitOperand(result,ModeValue);
  1864. Reuse1a(result.op,operand.op,dest);
  1865. Emit(Xor(position,result.op,operand.op,true));
  1866. ReleaseOperand(operand);
  1867. END;
  1868. ELSIF x.operator = Scanner.Minus THEN
  1869. Evaluate(x.left,operand);
  1870. InitOperand(result,ModeValue);
  1871. Reuse1a(result.op,operand.op,dest);
  1872. type := x.left.type.resolved;
  1873. IF type IS SyntaxTree.SetType THEN
  1874. Emit(Not(position,result.op,operand.op));
  1875. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1876. Reuse1(result.tag,operand.tag);
  1877. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1878. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1879. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1880. Emit(Neg(position,result.op,operand.op));
  1881. ELSE HALT(200)
  1882. END;
  1883. ReleaseOperand(operand);
  1884. ELSIF x.operator = Scanner.Address THEN
  1885. Designate(x.left,operand);
  1886. operand.mode := ModeValue;
  1887. t0 := x.left.type.resolved;
  1888. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1889. ReleaseIntermediateOperand(operand.op);
  1890. operand.op := operand.tag;
  1891. IntermediateCode.InitOperand(operand.tag);
  1892. END;
  1893. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1894. result := operand;
  1895. ELSE HALT(100)
  1896. END;
  1897. destination := dest;
  1898. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1899. END VisitUnaryExpression;
  1900. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1901. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1902. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1903. BEGIN
  1904. type := type.resolved;
  1905. originalType := type;
  1906. IF type IS SyntaxTree.PointerType THEN
  1907. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1908. END;
  1909. IF type IS SyntaxTree.ObjectType THEN
  1910. BrL(trueL);
  1911. ELSE
  1912. ASSERT(type IS SyntaxTree.RecordType);
  1913. (*
  1914. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1915. *)
  1916. ReuseCopy(left,tag);
  1917. right := TypeDescriptorAdr(type);
  1918. IF ~newObjectFile THEN
  1919. IntermediateCode.MakeMemory(right,addressType);
  1920. END;
  1921. IF backend.cooperative THEN
  1922. repeatL := NewLabel();
  1923. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1924. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1925. END;
  1926. SetLabel(repeatL);
  1927. BreqL(trueL,left,right);
  1928. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1929. BrneL(repeatL,left,nil);
  1930. ELSE
  1931. level := type(SyntaxTree.RecordType).Level();
  1932. (* get type desc tag of level relative to base tag *)
  1933. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1934. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1935. IntermediateCode.MakeMemory(left,addressType);
  1936. BreqL(trueL,left,right);
  1937. END;
  1938. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1939. BrL(falseL);
  1940. END;
  1941. END TypeTest;
  1942. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1943. BEGIN
  1944. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1945. IF dump # NIL THEN
  1946. dump.String(s); dump.Ln;
  1947. END;
  1948. END Error;
  1949. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1950. BEGIN
  1951. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1952. IF dump # NIL THEN
  1953. dump.String(s); dump.Ln; dump.Update;
  1954. END;
  1955. END Warning;
  1956. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1957. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1958. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1959. operand:Operand;
  1960. BEGIN
  1961. previousSection := section;
  1962. Global.GetModuleName(mod,name);
  1963. Strings.Append(name,".@Trace");
  1964. Basic.ToSegmentedName(name, pooledName);
  1965. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1966. IF dump # NIL THEN dump := section.comments END;
  1967. variable := mod.moduleScope.firstVariable;
  1968. WHILE variable # NIL DO
  1969. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1970. Symbol(variable, operand);
  1971. register := operand.op;
  1972. CallTraceMethod(register, variable.type);
  1973. ReleaseIntermediateOperand(register);
  1974. END;
  1975. variable := variable.nextVariable;
  1976. END;
  1977. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1978. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1979. Emit(Br(position,op));
  1980. INC(statCoopTraceModule, section.pc);
  1981. section := previousSection;
  1982. IF dump # NIL THEN dump := section.comments END;
  1983. END CreateTraceModuleMethod;
  1984. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1985. BEGIN
  1986. Emit (Push(position, dst));
  1987. Emit (Push(position, src));
  1988. CallThis(position,"GarbageCollector","Assign", 2);
  1989. END CallAssignPointer;
  1990. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1991. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1992. BEGIN
  1993. IF SemanticChecker.IsPointerType (type) THEN
  1994. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  1995. ELSIF type.IsRecordType() THEN
  1996. Emit (Push(position,dst));
  1997. Emit (Push(position,src));
  1998. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  1999. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2000. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2001. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2002. ELSIF IsStaticArray(type) THEN
  2003. size := StaticArrayNumElements(type);
  2004. base := StaticArrayBaseType(type);
  2005. IF base.IsRecordType() THEN
  2006. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2007. Emit (Push(position, dst));
  2008. Emit (Push(position, src));
  2009. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2010. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2011. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2012. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2013. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2014. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2015. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2016. Emit (Push(position, dst));
  2017. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2018. Emit (Push(position, src));
  2019. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2020. ELSE
  2021. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2022. Emit (Push(position, dst));
  2023. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2024. Emit (Push(position, src));
  2025. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2026. ASSERT(StaticArrayBaseType(type).IsPointer());
  2027. END;
  2028. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2029. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2030. Emit (Push(position, dst));
  2031. Emit (Push(position, src));
  2032. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2033. ELSE HALT(100); (* missing ? *)
  2034. END;
  2035. END CallAssignMethod;
  2036. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2037. VAR name: Basic.SegmentedName;
  2038. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2039. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2040. context: Context;
  2041. BEGIN
  2042. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2043. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2044. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2045. GetRecordTypeName (recordType,name);
  2046. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2047. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2048. IF dump # NIL THEN dump := section.comments END;
  2049. variable := recordType.recordScope.firstVariable;
  2050. WHILE variable # NIL DO
  2051. IF variable.NeedsTrace() THEN
  2052. dst := NewRegisterOperand (addressType);
  2053. src := NewRegisterOperand (addressType);
  2054. Emit (Mov(position, dst, parameter1));
  2055. Emit (Mov(position, src, parameter2));
  2056. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2057. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2058. CallAssignMethod(dst, src, variable.type);
  2059. ReleaseIntermediateOperand(src);
  2060. ReleaseIntermediateOperand(dst);
  2061. END;
  2062. variable := variable.nextVariable;
  2063. END;
  2064. recordBase := recordType.GetBaseRecord();
  2065. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2066. GetRecordTypeName (recordBase,name);
  2067. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2068. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2069. Emit(Br(position,op));
  2070. ELSE
  2071. Emit(Exit(position,0,0));
  2072. END;
  2073. IF ~recordType.isObject THEN
  2074. GetRecordTypeName (recordType,name);
  2075. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2076. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2077. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2078. NEW(registerUsageCount);
  2079. usedRegisters := NIL;
  2080. dst := NewRegisterOperand (addressType);
  2081. src := NewRegisterOperand (addressType);
  2082. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2083. Emit(Mov(position, dst, parameter1));
  2084. Emit(Mov(position, src, parameter2));
  2085. label := NewLabel();
  2086. SetLabel(label);
  2087. Emit(Push(position, dst));
  2088. Emit(Push(position, src));
  2089. GetRecordTypeName (recordType,name);
  2090. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2091. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2092. Emit(Call(position, op, 0));
  2093. Emit(Pop(position, src));
  2094. Emit(Pop(position, dst));
  2095. Emit(Add(position, dst, dst, ofs));
  2096. Emit(Add(position, src, src, ofs));
  2097. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2098. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2099. Emit(Exit(position,0,0));
  2100. END;
  2101. INC(statCoopAssignProcedure, section.pc);
  2102. ReturnToContext(context);
  2103. IF dump # NIL THEN dump := section.comments END;
  2104. END CreateAssignProcedure;
  2105. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2106. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2107. BEGIN
  2108. IF IsUnsafePointer (type) THEN
  2109. skip := NewLabel();
  2110. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2111. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2112. BreqL (skip, op, nil);
  2113. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2114. SetLabel (skip);
  2115. ELSIF SemanticChecker.IsPointerType (type) THEN
  2116. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2117. CallThis(position,"GarbageCollector","Mark", 1);
  2118. ELSIF type.IsRecordType() THEN
  2119. Emit (Push(position,register));
  2120. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2121. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2122. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2123. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2124. ELSIF IsStaticArray(type) THEN
  2125. size := StaticArrayNumElements(type);
  2126. base := StaticArrayBaseType(type);
  2127. IF base.IsRecordType() THEN
  2128. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2129. Emit (Push(position, register));
  2130. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2131. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2132. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2133. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2134. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2135. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2136. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2137. Emit (Push(position, register));
  2138. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2139. ELSE
  2140. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2141. Emit (Push(position, register));
  2142. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2143. ASSERT(base.IsPointer());
  2144. END;
  2145. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2146. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2147. CallThis(position,"GarbageCollector","Mark", 1);
  2148. ELSE HALT(100); (* missing ? *)
  2149. END;
  2150. END CallTraceMethod;
  2151. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2152. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2153. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2154. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2155. BEGIN
  2156. previousSection := section;
  2157. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2158. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2159. GetRecordTypeName (recordType,name);
  2160. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2161. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2162. IF dump # NIL THEN dump := section.comments END;
  2163. variable := recordType.recordScope.firstVariable;
  2164. WHILE variable # NIL DO
  2165. IF variable.NeedsTrace() THEN
  2166. register := NewRegisterOperand (addressType);
  2167. Emit (Mov(position,register,parameter1));
  2168. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2169. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2170. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2171. END;
  2172. CallTraceMethod(register, variable.type);
  2173. ReleaseIntermediateOperand(register);
  2174. END;
  2175. variable := variable.nextVariable;
  2176. END;
  2177. recordBase := recordType.GetBaseRecord();
  2178. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2179. recordBase := recordBase.GetBaseRecord();
  2180. END;
  2181. IF recordBase # NIL THEN
  2182. GetRecordTypeName (recordBase,name);
  2183. IF HasExplicitTraceMethod (recordBase) THEN
  2184. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2185. ELSE
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2187. END;
  2188. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2189. Emit(Br(position,op));
  2190. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2191. Emit(Exit(position,0,0));
  2192. ELSE
  2193. IF recordType.isObject THEN
  2194. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2195. ELSE
  2196. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2197. END;
  2198. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2199. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2200. Emit(Br(position,op));
  2201. END;
  2202. IF ~recordType.isObject THEN
  2203. GetRecordTypeName (recordType,name);
  2204. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2205. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2206. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2207. NEW(registerUsageCount);
  2208. usedRegisters := NIL;
  2209. IF dump # NIL THEN dump := section.comments END;
  2210. register := NewRegisterOperand (addressType);
  2211. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2212. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2213. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2214. END;
  2215. Emit (Push(position,register));
  2216. GetRecordTypeName (recordType,name);
  2217. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2218. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2219. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2220. ReleaseIntermediateOperand(register);
  2221. Emit(Exit(position,0,0));
  2222. GetRecordTypeName (recordType,name);
  2223. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2224. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2225. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2226. NEW(registerUsageCount);
  2227. usedRegisters := NIL;
  2228. register := NewRegisterOperand (addressType);
  2229. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2230. Emit(Mov(position, register, parameter1));
  2231. label := NewLabel();
  2232. SetLabel(label);
  2233. Emit(Push(position, register));
  2234. GetRecordTypeName (recordType,name);
  2235. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2236. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2237. Emit(Call(position, op, 0));
  2238. Emit(Pop(position, register));
  2239. Emit(Add(position, register, register, ofs));
  2240. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2241. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2242. Emit(Exit(position,0,0));
  2243. END;
  2244. INC(statCoopTraceMethod, section.pc);
  2245. ReturnToContext(context);
  2246. IF dump # NIL THEN dump := section.comments END;
  2247. END CreateTraceMethod;
  2248. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2249. VAR name: Basic.SegmentedName;
  2250. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2251. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2252. context: Context;
  2253. BEGIN
  2254. IF recordType.isObject THEN RETURN END;
  2255. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2256. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2257. GetRecordTypeName (recordType,name);
  2258. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2259. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2260. IF dump # NIL THEN dump := section.comments END;
  2261. variable := recordType.recordScope.firstVariable;
  2262. WHILE variable # NIL DO
  2263. IF variable.NeedsTrace() THEN
  2264. dst := NewRegisterOperand (addressType);
  2265. Emit (Mov(position, dst, parameter1));
  2266. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2267. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2268. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2269. END;
  2270. CallResetProcedure(dst, nil, variable.type);
  2271. ReleaseIntermediateOperand(dst);
  2272. END;
  2273. variable := variable.nextVariable;
  2274. END;
  2275. recordBase := recordType.GetBaseRecord();
  2276. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2277. GetRecordTypeName (recordBase,name);
  2278. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2279. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2280. Emit(Br(position,op));
  2281. ELSE
  2282. Emit(Exit(position,0,0));
  2283. END;
  2284. GetRecordTypeName (recordType,name);
  2285. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2286. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2287. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2288. NEW(registerUsageCount);
  2289. usedRegisters := NIL;
  2290. dst := NewRegisterOperand (addressType);
  2291. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2292. Emit(Mov(position, dst, parameter1));
  2293. label := NewLabel();
  2294. SetLabel(label);
  2295. Emit(Push(position, dst));
  2296. GetRecordTypeName (recordType,name);
  2297. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2298. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2299. Emit(Call(position, op, 0));
  2300. Emit(Pop(position, dst));
  2301. Emit(Add(position, dst, dst, ofs));
  2302. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2303. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2304. Emit(Exit(position,0,0));
  2305. INC(statCoopResetProcedure, section.pc);
  2306. ReturnToContext(context);
  2307. IF dump # NIL THEN dump := section.comments END;
  2308. END CreateResetProcedure;
  2309. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2310. VAR name: Basic.SegmentedName; context: Context;
  2311. BEGIN
  2312. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2314. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2315. IF dump # NIL THEN dump := section.comments END;
  2316. Emit(Push(position,fp));
  2317. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2318. ResetVariables(scope);
  2319. Emit(Pop(position,fp));
  2320. Emit(Exit(position,0,0));
  2321. ReturnToContext(context);
  2322. IF dump # NIL THEN dump := section.comments END;
  2323. END CreateResetMethod;
  2324. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2325. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2326. BEGIN
  2327. IF SemanticChecker.IsPointerType (type) THEN
  2328. Emit (Push(position, dest));
  2329. CallThis(position,"GarbageCollector","Reset", 1);
  2330. ELSIF type.IsRecordType() THEN
  2331. Emit (Push(position, dest));
  2332. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2333. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2334. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2335. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2336. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2337. size := GetArrayLength(type, tag);
  2338. base := ArrayBaseType(type);
  2339. IF base.IsRecordType() THEN
  2340. Emit (Push(position, size));
  2341. Emit (Push(position, dest));
  2342. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2343. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2344. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2345. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2346. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2347. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2348. Emit (Push(position, size));
  2349. Emit (Push(position, dest));
  2350. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2351. ELSE
  2352. Emit (Push(position, size));
  2353. Emit (Push(position, dest));
  2354. CallThis(position,"GarbageCollector","ResetArray", 2);
  2355. ASSERT(ArrayBaseType(type).IsPointer());
  2356. END;
  2357. ReleaseIntermediateOperand(size);
  2358. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2359. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2360. Emit (Push(position, dest));
  2361. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2362. ELSE HALT(100); (* missing ? *)
  2363. END;
  2364. END CallResetProcedure;
  2365. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2366. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2367. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2368. VAR operand: Operand;
  2369. BEGIN
  2370. Symbol (symbol, operand);
  2371. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2372. ReleaseOperand(operand);
  2373. END Reset;
  2374. BEGIN
  2375. previousScope := currentScope;
  2376. currentScope := scope;
  2377. pc := section.pc;
  2378. variable := scope.firstVariable;
  2379. WHILE variable # NIL DO
  2380. IF variable.NeedsTrace() THEN
  2381. Reset (variable);
  2382. END;
  2383. variable := variable.nextVariable;
  2384. END;
  2385. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2386. WHILE parameter # NIL DO
  2387. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2388. Reset (parameter);
  2389. END;
  2390. parameter := parameter.nextParameter;
  2391. END;
  2392. INC(statCoopResetVariables, section.pc - pc);
  2393. currentScope := previousScope;
  2394. END ResetVariables;
  2395. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2396. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2397. VAR op: IntermediateCode.Operand; context: Context;
  2398. BEGIN
  2399. previousSection := section;
  2400. GetCodeSectionNameForSymbol(procedure, name);
  2401. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2402. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2403. IF dump # NIL THEN dump := section.comments END;
  2404. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2405. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2406. Emit(Data(position,op));
  2407. Emit(Data(position,nil));
  2408. IF HasPointers (procedure.procedureScope) THEN
  2409. GetCodeSectionNameForSymbol(procedure, name);
  2410. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2411. ELSE
  2412. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2413. END;
  2414. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2415. Emit(Data(position,op));
  2416. ReturnToContext(context);
  2417. IF dump # NIL THEN dump := section.comments END;
  2418. END CreateProcedureDescriptor;
  2419. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2420. VAR import: SyntaxTree.Import;
  2421. s: Basic.MessageString;
  2422. selfName: SyntaxTree.IdentifierString;
  2423. BEGIN
  2424. moduleScope.ownerModule.GetName(selfName);
  2425. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2426. module := moduleScope.ownerModule
  2427. ELSE
  2428. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2429. IF import = NIL THEN
  2430. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2431. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2432. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2433. s := "Module ";
  2434. Strings.Append(s,moduleName);
  2435. Strings.Append(s," cannot be imported.");
  2436. IF force THEN
  2437. Error(position,s);
  2438. ELSIF canBeLoaded THEN
  2439. Strings.Append(s, "=> no dynamic linking.");
  2440. Warning(position, s);
  2441. canBeLoaded := FALSE;
  2442. END;
  2443. RETURN FALSE
  2444. ELSE
  2445. SELF.module.imports.AddName(moduleName)
  2446. END;
  2447. ELSIF import.module = NIL THEN (* already tried *)
  2448. RETURN FALSE
  2449. END;
  2450. module := import.module;
  2451. END;
  2452. RETURN TRUE
  2453. END AddImport;
  2454. (* needed for old binary object file format*)
  2455. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2456. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2457. BEGIN
  2458. IF AddImport(moduleName,module,TRUE) THEN
  2459. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2460. IF procedure = NIL THEN
  2461. s := "Instruction not supported on target, emulation procedure ";
  2462. Strings.Append(s,moduleName);
  2463. Strings.Append(s,".");
  2464. Strings.Append(s,procedureName);
  2465. Strings.Append(s," not present");
  2466. Error(position,s);
  2467. ELSE
  2468. StaticCallOperand(r,procedure);
  2469. ReleaseOperand(r);
  2470. fp := GetFingerprint(procedure);
  2471. END;
  2472. END;
  2473. END EnsureSymbol;
  2474. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2475. VAR exit: Label; trueL,falseL: Label;
  2476. BEGIN
  2477. trueL := NewLabel();
  2478. falseL := NewLabel();
  2479. exit := NewLabel();
  2480. Condition(x,trueL,falseL);
  2481. InitOperand(result,ModeValue);
  2482. SetLabel(trueL);
  2483. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2484. Emit(Mov(position,result.op,true));
  2485. BrL(exit);
  2486. SetLabel(falseL);
  2487. Emit(MovReplace(position,result.op,false));
  2488. SetLabel(exit);
  2489. END ConditionToValue;
  2490. PROCEDURE ValueToCondition(VAR op: Operand);
  2491. BEGIN
  2492. LoadValue(op,system.booleanType);
  2493. BrneL(trueLabel,op.op, false);
  2494. ReleaseOperand(op);
  2495. BrL(falseLabel);
  2496. END ValueToCondition;
  2497. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2498. VAR size: LONGINT;
  2499. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2500. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2501. BEGIN
  2502. ASSERT(type.form = SyntaxTree.Open);
  2503. baseType := type.arrayBase.resolved;
  2504. IF IsOpenArray(baseType) THEN
  2505. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2506. ELSE
  2507. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2508. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2509. END;
  2510. len := tag;
  2511. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2512. IntermediateCode.MakeMemory(len,addressType);
  2513. UseIntermediateOperand(len);
  2514. Reuse2(res,sizeOperand,len);
  2515. Emit(Mul(position,res,sizeOperand,len));
  2516. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2517. RETURN res
  2518. END GetArraySize;
  2519. BEGIN
  2520. type := type.resolved;
  2521. IF IsOpenArray(type) THEN
  2522. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2523. RETURN tag
  2524. ELSE
  2525. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2526. END;
  2527. ELSE
  2528. size := ToMemoryUnits(system,system.SizeOf(type));
  2529. RETURN IntermediateCode.Immediate(addressType,size)
  2530. END;
  2531. END GetDynamicSize;
  2532. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2533. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2534. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2535. BEGIN
  2536. ASSERT(type.form = SyntaxTree.Open);
  2537. baseType := type.arrayBase.resolved;
  2538. IF IsOpenArray(baseType) THEN
  2539. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2540. ELSE
  2541. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2542. END;
  2543. len := tag;
  2544. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2545. IntermediateCode.MakeMemory(len,addressType);
  2546. UseIntermediateOperand(len);
  2547. Reuse2(res,sizeOperand,len);
  2548. Emit(Mul(position,res,sizeOperand,len));
  2549. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2550. RETURN res
  2551. END GetLength;
  2552. BEGIN
  2553. type := type.resolved;
  2554. IF IsOpenArray(type) THEN
  2555. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2556. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2557. ELSE
  2558. RETURN IntermediateCode.Immediate(addressType,1)
  2559. END;
  2560. END GetArrayLength;
  2561. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2562. VAR result: IntermediateCode.Operand;
  2563. BEGIN
  2564. IF backend.cooperative THEN
  2565. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2566. ELSE
  2567. MakeMemory(result, tag, addressType, 0);
  2568. END;
  2569. RETURN result
  2570. END GetSizeFromTag;
  2571. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2572. VAR parameter: SyntaxTree.Parameter;
  2573. BEGIN
  2574. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2575. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2576. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2577. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2578. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2579. END;
  2580. RETURN GetDynamicSize(e.type, tag);
  2581. END GetArrayOfBytesSize;
  2582. (*
  2583. to find imported symbol. not needed ?
  2584. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2585. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2586. BEGIN
  2587. IF AddImport(moduleName,importedModule,FALSE) THEN
  2588. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2589. RETURN symbol
  2590. ELSE
  2591. RETURN NIL
  2592. END
  2593. END SymbolByName;
  2594. *)
  2595. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2596. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2597. BEGIN
  2598. IF AddImport(moduleName,runtimeModule,force) THEN
  2599. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2600. IF procedure = NIL THEN
  2601. s := "Procedure ";
  2602. Strings.Append(s,moduleName);
  2603. Strings.Append(s,".");
  2604. Strings.Append(s,procedureName);
  2605. Strings.Append(s," not present");
  2606. Error(position,s);
  2607. RETURN FALSE
  2608. ELSE
  2609. RETURN TRUE
  2610. END;
  2611. ELSE RETURN FALSE
  2612. END;
  2613. END GetRuntimeProcedure;
  2614. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2615. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2616. s: Basic.MessageString;
  2617. BEGIN
  2618. Basic.InitSegmentedName(name);
  2619. name[0] := Basic.MakeString(moduleName);
  2620. name[1] := Basic.MakeString(typeName);
  2621. name[2] := -1;
  2622. IF AddImport(moduleName,importedModule, FALSE) THEN
  2623. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2624. IF symbol = NIL THEN
  2625. s := "type ";
  2626. Strings.Append(s,moduleName);
  2627. Strings.Append(s,".");
  2628. Strings.Append(s,typeName);
  2629. Strings.Append(s," not present");
  2630. Error(position,s);
  2631. END;
  2632. ELSE symbol := NIL;
  2633. END;
  2634. IF importedModule = moduleScope.ownerModule THEN
  2635. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2636. ELSE
  2637. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2638. END;
  2639. RETURN symbol
  2640. END GetTypeDescriptor;
  2641. (* 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. *)
  2642. PROCEDURE CallThisChecked(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2643. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2644. pooledName: Basic.SegmentedName; size: LONGINT;
  2645. BEGIN
  2646. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2647. StaticCallOperand(result,procedure);
  2648. IF numberParameters < 0 THEN
  2649. size := ProcedureParametersSize(system,procedure);
  2650. ELSE
  2651. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2652. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2653. Error(position,"runtime call parameter count mismatch");
  2654. END;
  2655. END;
  2656. Emit(Call(position, result.op, size));
  2657. ReleaseOperand(result);
  2658. ELSE (* only static linking possible *)
  2659. ASSERT(numberParameters >= 0);
  2660. Basic.InitSegmentedName(pooledName);
  2661. pooledName[0] := Basic.MakeString(moduleName);
  2662. pooledName[1] := Basic.MakeString(procedureName);
  2663. pooledName[2] := -1;
  2664. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2665. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2666. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2667. END;
  2668. END CallThisChecked;
  2669. (* 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. *)
  2670. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2671. BEGIN
  2672. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2673. END CallThis;
  2674. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2675. VAR
  2676. left,right: Operand;
  2677. leftSize, rightSize: IntermediateCode.Operand;
  2678. saved: RegisterEntry;
  2679. reg: IntermediateCode.Operand;
  2680. procedureName: SyntaxTree.IdentifierString;
  2681. BEGIN
  2682. procedureName := "CompareString";
  2683. SaveRegisters();ReleaseUsedRegisters(saved);
  2684. Designate(leftExpression,left);
  2685. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2686. Emit(Push(position,leftSize));
  2687. ReleaseIntermediateOperand(leftSize);
  2688. Emit(Push(position,left.op));
  2689. ReleaseOperand(left);
  2690. Designate(rightExpression,right);
  2691. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2692. Emit(Push(position,rightSize));
  2693. ReleaseIntermediateOperand(rightSize);
  2694. Emit(Push(position,right.op));
  2695. ReleaseOperand(right);
  2696. IF backend.cooperative THEN
  2697. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2698. ELSE
  2699. CallThis(position,runtimeModuleName,procedureName, 4);
  2700. END;
  2701. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2702. Emit(Result(position,reg));
  2703. (*
  2704. AcquireThisRegister(int8,IntermediateCode.Result);
  2705. *)
  2706. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2707. (*
  2708. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2709. *)
  2710. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2711. ReleaseIntermediateOperand(reg);
  2712. BrL(falseLabel);
  2713. END CompareString;
  2714. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2715. VAR
  2716. left,right: Operand;
  2717. leftSize, rightSize: IntermediateCode.Operand;
  2718. saved: RegisterEntry;
  2719. procedureName: SyntaxTree.IdentifierString;
  2720. BEGIN
  2721. procedureName := "CopyString";
  2722. SaveRegisters();ReleaseUsedRegisters(saved);
  2723. Designate(leftExpression,left);
  2724. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2725. Emit(Push(position,leftSize));
  2726. ReleaseIntermediateOperand(leftSize);
  2727. Emit(Push(position,left.op));
  2728. ReleaseOperand(left);
  2729. Designate(rightExpression,right);
  2730. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2731. Emit(Push(position,rightSize));
  2732. ReleaseIntermediateOperand(rightSize);
  2733. Emit(Push(position,right.op));
  2734. ReleaseOperand(right);
  2735. IF backend.cooperative THEN
  2736. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2737. ELSE
  2738. CallThis(position,runtimeModuleName,procedureName,4);
  2739. END;
  2740. RestoreRegisters(saved);
  2741. END CopyString;
  2742. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2743. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2744. leftType,rightType: SyntaxTree.Type;
  2745. leftExpression,rightExpression : SyntaxTree.Expression;
  2746. componentType: IntermediateCode.Type;
  2747. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2748. size: LONGINT;
  2749. BEGIN
  2750. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2751. dest := destination; destination := emptyOperand;
  2752. leftType := x.left.type.resolved;
  2753. rightType := x.right.type.resolved;
  2754. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2755. CASE x.operator OF
  2756. Scanner.Or:
  2757. (* shortcut evaluation of left OR right *)
  2758. IF ~conditional THEN ConditionToValue(x);
  2759. ELSE
  2760. next := NewLabel();
  2761. Condition(x.left,trueLabel,next);
  2762. SetLabel(next);
  2763. Condition(x.right,trueLabel,falseLabel);
  2764. END;
  2765. |Scanner.And:
  2766. (* shortcut evaluation of left & right *)
  2767. IF ~conditional THEN ConditionToValue(x);
  2768. ELSE
  2769. next := NewLabel();
  2770. Condition(x.left,next,falseLabel);
  2771. SetLabel(next);
  2772. Condition(x.right,trueLabel,falseLabel);
  2773. END;
  2774. |Scanner.Is:
  2775. IF ~conditional THEN ConditionToValue(x);
  2776. ELSE
  2777. (* get type desc tag *)
  2778. IF IsPointerToRecord(leftType,recordType) THEN
  2779. Evaluate(x.left,left);
  2780. Dereference(left,recordType,IsUnsafePointer(leftType))
  2781. ELSE
  2782. Designate(x.left,left);
  2783. END;
  2784. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2785. ReleaseOperand(left);
  2786. END;
  2787. |Scanner.Plus:
  2788. Evaluate(x.left,left);
  2789. Evaluate(x.right,right);
  2790. IF leftType IS SyntaxTree.SetType THEN
  2791. InitOperand(result,ModeValue);
  2792. Reuse2a(result.op,left.op,right.op,dest);
  2793. Emit(Or(position,result.op,left.op,right.op));
  2794. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2795. InitOperand(result,ModeValue);
  2796. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2797. Reuse2(result.tag,left.tag,right.tag);
  2798. Emit(Add(position,result.op,left.op,right.op));
  2799. Emit(Add(position,result.tag,left.tag,right.tag))
  2800. ELSE
  2801. InitOperand(result,ModeValue);
  2802. Reuse2a(result.op,left.op,right.op,dest);
  2803. Emit(Add(position,result.op,left.op,right.op));
  2804. END;
  2805. ReleaseOperand(left); ReleaseOperand(right);
  2806. |Scanner.Minus:
  2807. Evaluate(x.left,left);
  2808. Evaluate(x.right,right);
  2809. IF leftType IS SyntaxTree.SetType THEN
  2810. InitOperand(result,ModeValue);
  2811. Reuse1(result.op,right.op);
  2812. Emit(Not(position,result.op,right.op));
  2813. ReleaseOperand(right);
  2814. Emit(And(position,result.op,result.op,left.op));
  2815. ReleaseOperand(left);
  2816. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2817. InitOperand(result,ModeValue);
  2818. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2819. Reuse2(result.tag,left.tag,right.tag);
  2820. Emit(Sub(position,result.op,left.op,right.op));
  2821. Emit(Sub(position,result.tag,left.tag,right.tag));
  2822. ReleaseOperand(left); ReleaseOperand(right)
  2823. ELSE
  2824. InitOperand(result,ModeValue);
  2825. Reuse2a(result.op,left.op,right.op,dest);
  2826. Emit(Sub(position,result.op,left.op,right.op));
  2827. ReleaseOperand(left); ReleaseOperand(right);
  2828. END;
  2829. |Scanner.Times:
  2830. Evaluate(x.left,left);
  2831. Evaluate(x.right,right);
  2832. IF leftType IS SyntaxTree.SetType THEN
  2833. InitOperand(result,ModeValue);
  2834. Reuse2a(result.op,left.op,right.op,dest);
  2835. Emit(And(position,result.op,left.op,right.op));
  2836. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2837. InitOperand(result,ModeValue);
  2838. Reuse1a(result.op,left.op,dest);
  2839. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2840. Emit(Shl(position,result.op,left.op,right.op));
  2841. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2842. InitOperand(result, ModeValue);
  2843. componentType := left.op.type;
  2844. (* TODO: review this *)
  2845. (*
  2846. result.op = left.op * right.op - left.tag * right.tag
  2847. result.tag = left.tag * right.op + left.op * right.tag
  2848. *)
  2849. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2850. Emit(Mul(position,result.op, left.op, right.op));
  2851. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2852. Emit(Mul(position,tempReg, left.tag, right.tag));
  2853. Emit(Sub(position,result.op, result.op, tempReg));
  2854. Reuse2(result.tag, left.tag, right.op);
  2855. Emit(Mul(position,result.tag, left.tag, right.op));
  2856. Emit(Mul(position,tempReg, left.op, right.tag));
  2857. Emit(Add(position,result.tag, result.tag, tempReg));
  2858. ReleaseIntermediateOperand(tempReg)
  2859. ELSE
  2860. InitOperand(result,ModeValue);
  2861. Reuse2a(result.op,left.op,right.op,dest);
  2862. Emit(Mul(position,result.op,left.op,right.op));
  2863. END;
  2864. ReleaseOperand(left); ReleaseOperand(right);
  2865. |Scanner.Div:
  2866. Evaluate(x.left,left);
  2867. Evaluate(x.right,right);
  2868. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2869. InitOperand(result,ModeValue);
  2870. Reuse1a(result.op,left.op,dest);
  2871. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2872. Emit(Shr(position,result.op,left.op,right.op));
  2873. ELSE
  2874. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2875. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2876. IF ~isUnchecked THEN
  2877. exit := NewLabel();
  2878. BrltL(exit,zero,right.op);
  2879. EmitTrap(position,NegativeDivisorTrap);
  2880. SetLabel(exit);
  2881. END;
  2882. END;
  2883. InitOperand(result,ModeValue);
  2884. Reuse2a(result.op,left.op,right.op,dest);
  2885. Emit(Div(position,result.op,left.op,right.op));
  2886. END;
  2887. ReleaseOperand(left); ReleaseOperand(right);
  2888. |Scanner.Mod:
  2889. Evaluate(x.left,left);
  2890. Evaluate(x.right,right);
  2891. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2892. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2893. InitOperand(result,ModeValue);
  2894. Reuse1a(result.op,left.op,dest);
  2895. Emit(And(position,result.op,left.op,right.op));
  2896. ELSE
  2897. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2898. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2899. IF ~isUnchecked THEN
  2900. exit := NewLabel();
  2901. BrltL(exit,zero,right.op);
  2902. EmitTrap(position,NegativeDivisorTrap);
  2903. SetLabel(exit);
  2904. END;
  2905. END;
  2906. InitOperand(result,ModeValue);
  2907. Reuse2a(result.op,left.op,right.op,dest);
  2908. Emit(Mod(position,result.op,left.op,right.op));
  2909. END;
  2910. ReleaseOperand(left); ReleaseOperand(right);
  2911. |Scanner.Slash:
  2912. Evaluate(x.left,left);
  2913. Evaluate(x.right,right);
  2914. IF leftType IS SyntaxTree.SetType THEN
  2915. InitOperand(result,ModeValue);
  2916. Reuse2a(result.op,left.op,right.op,dest);
  2917. Emit(Xor(position,result.op,left.op,right.op));
  2918. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2919. InitOperand(result,ModeValue);
  2920. componentType := left.op.type;
  2921. (* review this *)
  2922. (*
  2923. divisor = right.op * right.op + right.tag * right.tag
  2924. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2925. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2926. *)
  2927. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2928. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2929. Emit(Mul(position,tempReg, right.op, right.op));
  2930. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2931. Emit(Add(position,tempReg, tempReg, tempReg2));
  2932. result.op := tempReg2;
  2933. Emit(Mul(position,result.op, left.op, right.op));
  2934. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2935. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2936. Emit(Add(position,result.op, result.op, tempReg2));
  2937. Emit(Div(position,result.op, result.op, tempReg));
  2938. Reuse2(result.tag, left.tag, right.op);
  2939. Emit(Mul(position,result.tag, left.tag, right.op));
  2940. Emit(Mul(position,tempReg2, left.op, right.tag));
  2941. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2942. Emit(Div(position,result.tag, result.tag, tempReg));
  2943. ReleaseIntermediateOperand(tempReg);
  2944. ReleaseIntermediateOperand(tempReg2)
  2945. ELSE
  2946. InitOperand(result,ModeValue);
  2947. Reuse2a(result.op,left.op,right.op,dest);
  2948. Emit(Div(position,result.op,left.op,right.op));
  2949. END;
  2950. ReleaseOperand(left); ReleaseOperand(right);
  2951. |Scanner.Equal:
  2952. IF ~conditional THEN ConditionToValue(x);
  2953. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2954. CompareString(BreqL,x.left,x.right);
  2955. ELSE
  2956. Evaluate(x.left,left);
  2957. Evaluate(x.right,right);
  2958. IF leftType IS SyntaxTree.RangeType THEN
  2959. ASSERT(rightType IS SyntaxTree.RangeType);
  2960. BrneL(falseLabel, left.op, right.op); (* first *)
  2961. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2962. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2963. ReleaseOperand(left); ReleaseOperand(right);
  2964. BrL(trueLabel)
  2965. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2966. BrneL(falseLabel, left.op, right.op); (* first *)
  2967. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2968. ReleaseOperand(left); ReleaseOperand(right);
  2969. BrL(trueLabel)
  2970. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2971. (* TODO: review this *)
  2972. BrneL(falseLabel, left.op, right.op); (* real part *)
  2973. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2974. ReleaseOperand(left); ReleaseOperand(right);
  2975. BrL(trueLabel)
  2976. ELSE
  2977. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2978. ReleaseOperand(left); ReleaseOperand(right);
  2979. BrL(trueLabel);
  2980. END;
  2981. END;
  2982. |Scanner.LessEqual:
  2983. IF ~conditional THEN ConditionToValue(x);
  2984. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2985. CompareString(BrgeL,x.right,x.left);
  2986. ELSE
  2987. Evaluate(x.left,left);
  2988. Evaluate(x.right,right);
  2989. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2990. Reuse1(temp.op,right.op);
  2991. Emit(And(position,temp.op,left.op,right.op));
  2992. ReleaseOperand(right);
  2993. BreqL(trueLabel,temp.op,left.op);
  2994. BrL(falseLabel);
  2995. ReleaseOperand(temp);ReleaseOperand(left);
  2996. ELSE
  2997. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  2998. ReleaseOperand(left); ReleaseOperand(right);
  2999. BrL(trueLabel);
  3000. END;
  3001. END;
  3002. |Scanner.Less:
  3003. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3004. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  3005. leftExpression.SetType(system.booleanType);
  3006. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3007. rightExpression.SetType(system.booleanType);
  3008. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3009. leftExpression.SetType(system.booleanType);
  3010. Expression(leftExpression);
  3011. ELSIF ~conditional THEN ConditionToValue(x);
  3012. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3013. CompareString(BrltL,x.left,x.right);
  3014. ELSE
  3015. Evaluate(x.left,left);
  3016. Evaluate(x.right,right);
  3017. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3018. ReleaseOperand(left); ReleaseOperand(right);
  3019. BrL(trueLabel);
  3020. END;
  3021. |Scanner.Greater:
  3022. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3023. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  3024. leftExpression.SetType(system.booleanType);
  3025. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3026. rightExpression.SetType(system.booleanType);
  3027. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3028. leftExpression.SetType(system.booleanType);
  3029. Expression(leftExpression);
  3030. ELSIF ~conditional THEN ConditionToValue(x);
  3031. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3032. CompareString(BrltL,x.right,x.left);
  3033. ELSE
  3034. Evaluate(x.left,left);
  3035. Evaluate(x.right,right);
  3036. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3037. ReleaseOperand(left); ReleaseOperand(right);
  3038. BrL(trueLabel);
  3039. END;
  3040. |Scanner.GreaterEqual:
  3041. IF ~conditional THEN ConditionToValue(x);
  3042. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3043. CompareString(BrgeL,x.left,x.right);
  3044. ELSE
  3045. Evaluate(x.left,left);
  3046. Evaluate(x.right,right);
  3047. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3048. Reuse1(temp.op,left.op);
  3049. Emit(And(position,temp.op,left.op,right.op));
  3050. ReleaseOperand(left);
  3051. BreqL(trueLabel, temp.op,right.op);
  3052. ReleaseOperand(temp); ReleaseOperand(right);
  3053. BrL(falseLabel);
  3054. ELSE
  3055. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3056. ReleaseOperand(left); ReleaseOperand(right);
  3057. BrL(trueLabel);
  3058. END;
  3059. END;
  3060. |Scanner.Unequal:
  3061. IF ~conditional THEN ConditionToValue(x);
  3062. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3063. CompareString(BrneL,x.left,x.right);
  3064. ELSE
  3065. Evaluate(x.left,left);
  3066. Evaluate(x.right,right);
  3067. IF leftType IS SyntaxTree.RangeType THEN
  3068. ASSERT(rightType IS SyntaxTree.RangeType);
  3069. BrneL(trueLabel, left.op, right.op); (* first *)
  3070. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3071. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3072. ReleaseOperand(left); ReleaseOperand(right);
  3073. BrL(falseLabel)
  3074. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3075. BrneL(trueLabel, left.op, right.op); (* first *)
  3076. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3077. ReleaseOperand(left); ReleaseOperand(right);
  3078. BrL(falseLabel)
  3079. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3080. (* TODO: review this *)
  3081. BrneL(trueLabel, left.op, right.op); (* real part *)
  3082. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3083. ReleaseOperand(left); ReleaseOperand(right);
  3084. BrL(falseLabel)
  3085. ELSE
  3086. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3087. ReleaseOperand(left); ReleaseOperand(right);
  3088. BrL(trueLabel);
  3089. END;
  3090. END;
  3091. |Scanner.In:
  3092. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3093. Evaluate(x.left,left);
  3094. Evaluate(x.right,right);
  3095. Convert(left.op,setType);
  3096. ReuseCopy(temp.op,right.op);
  3097. Emit(Shr(position,temp.op,temp.op,left.op));
  3098. ReleaseOperand(right); ReleaseOperand(left);
  3099. IntermediateCode.InitImmediate(one,setType,1);
  3100. Emit(And(position,temp.op,temp.op,one));
  3101. IF conditional THEN
  3102. IntermediateCode.InitImmediate(zero,setType,0);
  3103. BrneL(trueLabel,temp.op,zero);
  3104. ReleaseOperand(temp);
  3105. BrL(falseLabel);
  3106. ELSE
  3107. Convert(temp.op,bool);
  3108. result.mode := ModeValue;
  3109. result.op := temp.op;
  3110. result.tag := nil; (* may be left over from calls to evaluate *)
  3111. END;
  3112. ELSE
  3113. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3114. IF x.operator = Scanner.Questionmarks THEN
  3115. leftExpression := x.left;
  3116. rightExpression := x.right;
  3117. ELSE
  3118. leftExpression := x.right;
  3119. rightExpression := x.left;
  3120. END;
  3121. Evaluate(leftExpression, left);
  3122. Emit(Push(position,left.op));
  3123. ReleaseOperand(left);
  3124. Designate(rightExpression, right);
  3125. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3126. IF ~backend.cellsAreObjects THEN
  3127. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3128. END;
  3129. Emit(Push(position,right.op));
  3130. ReleaseOperand(right);
  3131. IF backend.cellsAreObjects THEN
  3132. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3133. ELSE
  3134. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3135. END;
  3136. InitOperand(result, ModeValue);
  3137. result.op := NewRegisterOperand(bool);
  3138. Emit(Result(position,result.op));
  3139. IF conditional THEN
  3140. IntermediateCode.InitImmediate(zero,setType,0);
  3141. BrneL(trueLabel,result.op,zero);
  3142. ReleaseOperand(result);
  3143. BrL(falseLabel);
  3144. END;
  3145. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3146. leftExpression := x.left;
  3147. rightExpression := x.right;
  3148. Evaluate(leftExpression, left);
  3149. Emit(Push(position,left.op));
  3150. ReleaseOperand(left);
  3151. Evaluate(rightExpression, right);
  3152. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3153. IF ~backend.cellsAreObjects THEN
  3154. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3155. END;
  3156. Emit(Push(position,right.op));
  3157. ReleaseOperand(right);
  3158. IF backend.cellsAreObjects THEN
  3159. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3160. ELSE
  3161. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3162. END;
  3163. InitOperand(result, ModeValue);
  3164. result.op := NewRegisterOperand(bool);
  3165. Emit(Result(position,result.op));
  3166. IF conditional THEN
  3167. IntermediateCode.InitImmediate(zero,setType,0);
  3168. BrneL(trueLabel,result.op,zero);
  3169. ReleaseOperand(result);
  3170. BrL(falseLabel);
  3171. END;
  3172. ELSE
  3173. HALT(100);
  3174. END;
  3175. END;
  3176. destination := dest;
  3177. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3178. END VisitBinaryExpression;
  3179. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3180. VAR localResult, operand: Operand;
  3181. BEGIN
  3182. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3183. InitOperand(localResult, ModeValue);
  3184. ASSERT(x.first # NIL);
  3185. Evaluate(x.first, operand);
  3186. localResult.op := operand.op;
  3187. ReleaseOperand(operand);
  3188. UseIntermediateOperand(localResult.op);
  3189. ASSERT(x.last # NIL);
  3190. Evaluate(x.last, operand);
  3191. localResult.tag := operand.op;
  3192. ReleaseOperand(operand);
  3193. UseIntermediateOperand(localResult.tag);
  3194. IF x.step # NIL THEN
  3195. Evaluate(x.step, operand);
  3196. localResult.extra := operand.op;
  3197. ReleaseOperand(operand);
  3198. UseIntermediateOperand(localResult.extra);
  3199. END;
  3200. result := localResult;
  3201. IF Trace THEN TraceExit("VisitRangeExpression") END
  3202. END VisitRangeExpression;
  3203. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3204. BEGIN
  3205. HALT(100); (* should never be evaluated *)
  3206. END VisitTensorRangeExpression;
  3207. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3208. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3209. BEGIN
  3210. IF Trace THEN TraceEnter("VisitConversion") END;
  3211. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3212. dest := destination; destination := emptyOperand;
  3213. Evaluate(x.expression,old);
  3214. InitOperand(result,ModeValue);
  3215. result.op := old.op;
  3216. ASSERT(result.op.mode # 0);
  3217. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3218. (* convert TO a complex number *)
  3219. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3220. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3221. ASSERT(result.op.mode # 0);
  3222. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3223. (* convert FROM a complex number TO a complex number*)
  3224. result.tag := old.tag;
  3225. ASSERT(result.tag.mode # 0);
  3226. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3227. ASSERT(result.tag.mode # 0)
  3228. ELSE
  3229. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3230. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3231. END
  3232. ELSE
  3233. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3234. ASSERT(result.op.mode # 0);
  3235. result.tag := old.tag; (*! probably never used *)
  3236. END;
  3237. destination := dest;
  3238. IF Trace THEN TraceExit("VisitConversion") END;
  3239. END VisitConversion;
  3240. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3241. BEGIN
  3242. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3243. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3244. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3245. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3246. END VisitTypeDeclaration;
  3247. (** designators (expressions) *)
  3248. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3249. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3250. BEGIN
  3251. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3252. IF x.left # NIL THEN Expression(x.left) END;
  3253. Symbol(x.symbol,result);
  3254. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3255. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3256. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3257. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3258. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3259. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3260. END;
  3261. END;
  3262. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3263. END VisitSymbolDesignator;
  3264. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3265. BEGIN
  3266. IF isUnchecked THEN RETURN END;
  3267. IF tagsAvailable THEN
  3268. TrapC(BrltL,index,length,IndexCheckTrap);
  3269. END;
  3270. END BoundCheck;
  3271. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3272. VAR d: IntermediateCode.Operand;
  3273. BEGIN
  3274. IF isUnchecked THEN RETURN END;
  3275. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3276. TrapC(op,dim,d,ArraySizeTrap);
  3277. ReleaseIntermediateOperand(d);
  3278. END DimensionCheck;
  3279. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3280. VAR
  3281. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3282. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3283. expression: SyntaxTree.Expression;
  3284. resultingType, leftType, baseType: SyntaxTree.Type;
  3285. skipLabel1: Label;
  3286. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3287. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3288. variableOp: Operand;
  3289. variable: SyntaxTree.Variable;
  3290. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3291. VAR expression: SyntaxTree.Expression;
  3292. BEGIN
  3293. (* count the number of indices, ranges and tensorRanges in the index list *)
  3294. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3295. FOR i := 0 TO parameters.Length() - 1 DO
  3296. expression := parameters.GetExpression(i);
  3297. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3298. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3299. ELSE INC(indexCount)
  3300. END
  3301. END;
  3302. END CountIndices;
  3303. BEGIN
  3304. ASSERT(tagsAvailable);
  3305. resultingType := x.type.resolved; (* resulting type *)
  3306. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3307. InitOperand(localResult, ModeReference);
  3308. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3309. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3310. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3311. (* a globally defined array destination tag is available -> use and invalidate it*)
  3312. localResult.tag := arrayDestinationTag;
  3313. IntermediateCode.InitOperand(arrayDestinationTag)
  3314. ELSE
  3315. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3316. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3317. Symbol(variable, variableOp);
  3318. ReuseCopy(localResult.tag, variableOp.op);
  3319. ReleaseOperand(variableOp);
  3320. END
  3321. END;
  3322. indexListSize := x.parameters.Length();
  3323. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3324. ASSERT(tensorRangeCount <= 1);
  3325. (* designate the array to be indexed over, perform tensor range check if known *)
  3326. Designate(x.left, array);
  3327. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3328. Dereference(array, leftType,FALSE);
  3329. IF tensorRangeCount=0 THEN
  3330. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3331. END
  3332. END;
  3333. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3334. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3335. srcDimOffset := -indexListSize;
  3336. destDimOffset := -rangeCount
  3337. ELSE
  3338. srcDimOffset := 0;
  3339. destDimOffset := 0
  3340. END;
  3341. indexDim := 0;
  3342. (* use address of source array as basis *)
  3343. (*
  3344. ReuseCopy(localResult.op, array.op);
  3345. *)
  3346. localResult.op := array.op;
  3347. UseIntermediateOperand(localResult.op);
  3348. (* go through the index list *)
  3349. FOR i := 0 TO indexListSize - 1 DO
  3350. expression := x.parameters.GetExpression(i);
  3351. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3352. (* nothing to do *)
  3353. ELSE
  3354. (* determine which dimension of source array is currently looked at *)
  3355. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3356. (* get the memory operand pointing to array descriptor dimension *)
  3357. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3358. (* make a reusable register from it *)
  3359. ReuseCopy(srcDim, tmp);
  3360. ReleaseIntermediateOperand(tmp);
  3361. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3362. ELSE
  3363. srcDim := IntermediateCode.Immediate(int32, i)
  3364. END;
  3365. (* get length and increment of source array for current dimension *)
  3366. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3367. Convert(sourceLength.op, sizeType);
  3368. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3369. Convert(sourceIncrement.op, sizeType);
  3370. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3371. ReleaseIntermediateOperand(srcDim);
  3372. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3373. (* SINGLE INDEX *)
  3374. Evaluate(expression, index);
  3375. ReleaseIntermediateOperand(index.tag);
  3376. index.tag := emptyOperand;
  3377. Convert(index.op, sizeType);
  3378. (* lower bound check *)
  3379. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3380. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3381. ELSIF isUnchecked THEN (* do nothing *)
  3382. ELSE
  3383. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3384. END;
  3385. (* upper bound check *)
  3386. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3387. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3388. ELSIF isUnchecked THEN (* do nothing *)
  3389. ELSE
  3390. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3391. END;
  3392. ReleaseOperand(sourceLength);
  3393. Convert(index.op, addressType);
  3394. summand := index.op;
  3395. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3396. (* RANGE OF INDICES *)
  3397. Evaluate(expression, range);
  3398. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3399. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3400. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3401. ReleaseOperand(range);
  3402. Convert(firstIndex, sizeType);
  3403. Convert(lastIndex, sizeType);
  3404. Convert(stepSize, sizeType);
  3405. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3406. if it is 'MAX(LONGINT)' *)
  3407. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3408. TransferToRegister(lastIndex, lastIndex);
  3409. skipLabel1 := NewLabel();
  3410. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3411. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3412. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3413. SetLabel(skipLabel1)
  3414. END;
  3415. (* check if step size is valid *)
  3416. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3417. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3418. ELSIF isUnchecked THEN (* do nothing *)
  3419. ELSE
  3420. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3421. END;
  3422. (* check lower bound check *)
  3423. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3424. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3425. ELSIF isUnchecked THEN (* do nothing *)
  3426. ELSE
  3427. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3428. END;
  3429. (* check upper bound check *)
  3430. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3431. (* statically open range: nothing to do *)
  3432. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3433. ASSERT(staticLastIndex < staticSourceLength)
  3434. ELSIF isUnchecked THEN (* do nothing *)
  3435. ELSE
  3436. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3437. END;
  3438. (* determine length of target array for current dimension *)
  3439. (* 1. incorporate last index: *)
  3440. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3441. (* last index is static *)
  3442. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3443. targetLength := sourceLength.op
  3444. ELSE
  3445. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3446. END;
  3447. UseIntermediateOperand(targetLength);
  3448. ELSE
  3449. (* targetLength := lastIndex + 1
  3450. Reuse1(targetLength, lastIndex);
  3451. *)
  3452. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3453. END;
  3454. ReleaseOperand(sourceLength);
  3455. ReleaseIntermediateOperand(lastIndex);
  3456. (* 2. incorporate first index: *)
  3457. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3458. (* first index and current target length are static *)
  3459. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3460. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3461. (* first index = 0: nothing to do *)
  3462. ELSE
  3463. (* targetLength := targetLength - firstIndex *)
  3464. TransferToRegister(targetLength, targetLength);
  3465. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3466. END;
  3467. (* clip negative lengths to 0 *)
  3468. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3469. IF staticTargetLength < 0 THEN
  3470. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3471. END
  3472. ELSE
  3473. skipLabel1 := NewLabel();
  3474. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3475. TransferToRegister(targetLength, targetLength);
  3476. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3477. SetLabel(skipLabel1)
  3478. END;
  3479. (* 3. incorporate index step size: *)
  3480. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3481. (*step size and current target length are static *)
  3482. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3483. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3484. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3485. (* step size = 1: nothing to do *)
  3486. ELSE
  3487. (* emit code for this:
  3488. targetLength := (targetLength-1) DIV stepSize +1;
  3489. *)
  3490. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3491. DivInt(targetLength, targetLength, stepSize);
  3492. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3493. END;
  3494. (* determine increment of target array for current dimension *)
  3495. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3496. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3497. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3498. (* step size = 1 *)
  3499. targetIncrement := sourceIncrement.op;
  3500. UseIntermediateOperand(targetIncrement)
  3501. ELSE
  3502. (* targetIncrement := sourceIncrement * stepSize *)
  3503. Reuse1(targetIncrement, stepSize);
  3504. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3505. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3506. END;
  3507. ReleaseIntermediateOperand(stepSize);
  3508. (* write length and increment of target array to descriptor *)
  3509. IF destDimOffset < 0 THEN
  3510. (* determine which dimension of target array is currently looked at *)
  3511. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3512. TransferToRegister(destDim, tmp);
  3513. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3514. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3515. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3516. ReleaseIntermediateOperand(destDim)
  3517. ELSE
  3518. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3519. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3520. END;
  3521. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3522. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3523. INC(indexDim);
  3524. Convert(firstIndex, addressType);
  3525. TransferToRegister(summand, firstIndex);
  3526. ELSE HALT(100);
  3527. END;
  3528. (*
  3529. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3530. *)
  3531. Convert(sourceIncrement.op, addressType);
  3532. Convert(summand, addressType);
  3533. MulInt(summand, summand, sourceIncrement.op);
  3534. ReleaseIntermediateOperand(sourceIncrement.op);
  3535. AddInt(localResult.op, localResult.op, summand);
  3536. ReleaseIntermediateOperand(summand);
  3537. END
  3538. END;
  3539. result := localResult;
  3540. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3541. ReleaseIntermediateOperand(result.tag);
  3542. result.tag := TypeDescriptorAdr(resultingType);
  3543. IF ~newObjectFile THEN
  3544. IntermediateCode.MakeMemory(result.tag,addressType);
  3545. END;
  3546. ELSIF IsDelegate(resultingType) THEN
  3547. ReleaseIntermediateOperand(result.tag);
  3548. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3549. UseIntermediateOperand(result.tag);
  3550. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3551. ReleaseIntermediateOperand(result.tag);
  3552. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3553. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3554. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3555. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3556. (* finalize target array descriptor *)
  3557. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3558. (* write lengths and increments of target array for remaining dimensions *)
  3559. i := indexListSize;
  3560. WHILE indexDim < targetArrayDimensionality DO
  3561. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3562. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3563. ReleaseOperand(sourceLength);
  3564. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3565. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3566. ReleaseOperand(sourceIncrement);
  3567. INC(i); INC(indexDim);
  3568. END;
  3569. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3570. tmp := nil;
  3571. ELSE
  3572. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3573. END;
  3574. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3575. ReleaseIntermediateOperand(tmp);
  3576. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3577. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3578. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3579. ELSE
  3580. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3581. END;
  3582. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3583. ReleaseIntermediateOperand(tmp);
  3584. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3585. (* write dimensionality *)
  3586. IF targetArrayDimensionality # 0 THEN
  3587. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3588. END;
  3589. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3590. END;
  3591. ReleaseOperand(array);
  3592. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3593. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3594. END;
  3595. END MathIndexDesignator;
  3596. (* TENTATIVE *)
  3597. PROCEDURE DumpOperand(operand: Operand);
  3598. BEGIN
  3599. D.Log.String(" op = ");
  3600. IntermediateCode.DumpOperand(D.Log, operand.op );
  3601. D.Log.Ln;
  3602. D.Log.String(" tag = ");
  3603. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3604. D.Log.Ln;
  3605. D.Log.String(" extra = ");
  3606. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3607. D.Log.Ln;
  3608. D.Log.Update
  3609. END DumpOperand;
  3610. (* get the length of an array , trying to make use of static information *)
  3611. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3612. VAR res: IntermediateCode.Operand; size: LONGINT;
  3613. BEGIN
  3614. type := type.resolved;
  3615. IF type IS SyntaxTree.ArrayType THEN
  3616. WITH type: SyntaxTree.ArrayType DO
  3617. IF type.form = SyntaxTree.Static THEN
  3618. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3619. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3620. Evaluate(type.length, op);
  3621. ReleaseIntermediateOperand(op.tag);
  3622. RETURN op.op;*)
  3623. ELSE
  3624. res := tag;
  3625. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3626. IntermediateCode.MakeMemory(res,addressType);
  3627. UseIntermediateOperand(res);
  3628. RETURN res
  3629. END
  3630. END;
  3631. ELSE
  3632. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3633. RETURN IntermediateCode.Immediate(addressType,size);
  3634. END;
  3635. END ArrayLength;
  3636. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3637. BEGIN
  3638. ReleaseIntermediateOperand(res);
  3639. IF IsImmediate(x) & IsImmediate(y) THEN
  3640. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3641. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3642. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3643. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3644. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3645. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3646. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3647. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3648. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3649. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3650. UseIntermediateOperand(res);
  3651. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3652. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3653. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3654. UseIntermediateOperand(res);
  3655. ELSE
  3656. IF ~ReusableRegister(res) THEN
  3657. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3658. ELSE
  3659. UseIntermediateOperand(res);
  3660. END;
  3661. IF IsImmediate(x) & (x.intValue = 0) THEN
  3662. Emit(Mov(position,res,y))
  3663. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3664. Emit(Mov(position,res,x))
  3665. ELSE
  3666. Emit(Add(position,res, x, y));
  3667. END;
  3668. END;
  3669. END AddInt;
  3670. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3671. BEGIN
  3672. ReleaseIntermediateOperand(res);
  3673. IF IsImmediate(x) & IsImmediate(y) THEN
  3674. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3675. ELSE
  3676. IF ~ReusableRegister(res) THEN
  3677. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3678. ELSE UseIntermediateOperand(res);
  3679. END;
  3680. IF IsImmediate(x) & (x.intValue = 1) THEN
  3681. Emit(Mov(position,res,y))
  3682. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3683. Emit(Mov(position,res,x))
  3684. ELSE
  3685. Emit(Mul(position,res, x, y));
  3686. END;
  3687. END;
  3688. END MulInt;
  3689. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3690. BEGIN
  3691. ReleaseIntermediateOperand(res);
  3692. IF IsImmediate(x) & IsImmediate(y) THEN
  3693. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3694. ELSE
  3695. IF ~ReusableRegister(res) THEN
  3696. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3697. ELSE UseIntermediateOperand(res);
  3698. END;
  3699. IF IsImmediate(x) & (x.intValue = 1) THEN
  3700. Emit(Mov(position,res,y))
  3701. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3702. Emit(Mov(position,res,x))
  3703. ELSE
  3704. Emit(Div(position,res, x, y));
  3705. END;
  3706. END;
  3707. END DivInt;
  3708. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3709. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3710. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3711. BEGIN
  3712. type := x.left.type.resolved;
  3713. IF type IS SyntaxTree.StringType THEN
  3714. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3715. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3716. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3717. type := atype;
  3718. x.left.SetType(type);
  3719. END;
  3720. IntermediateCode.InitImmediate(res,addressType,0);
  3721. maxDim := x.parameters.Length()-1;
  3722. FOR i := 0 TO maxDim DO
  3723. e := x.parameters.GetExpression(i);
  3724. Evaluate(e,index);
  3725. Convert(index.op,addressType);
  3726. AddInt(res, res, index.op);
  3727. IF i = 0 THEN
  3728. (*
  3729. ReuseCopy(res, index.op);
  3730. *)
  3731. Designate(x.left,array);
  3732. type := x.left.type.resolved;
  3733. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3734. Dereference(array, type, FALSE);
  3735. END;
  3736. (*
  3737. ELSE AddInt(res, res, index.op);
  3738. *)
  3739. END;
  3740. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3741. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3742. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3743. BoundCheck(index.op, length);
  3744. END;
  3745. ReleaseIntermediateOperand(length);
  3746. END;
  3747. ReleaseOperand(index);
  3748. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3749. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3750. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3751. MulInt(res,res,length);
  3752. END;
  3753. ReleaseIntermediateOperand(length);
  3754. END;
  3755. IF (type IS SyntaxTree.ArrayType) THEN
  3756. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3757. size := StaticSize(system, type);
  3758. IF size # 1 THEN
  3759. length := IntermediateCode.Immediate(addressType,size);
  3760. MulInt(res,res,length);
  3761. END;
  3762. ELSE
  3763. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3764. IF size # 1 THEN
  3765. length := IntermediateCode.Immediate(addressType,size);
  3766. MulInt(res,res,length);
  3767. END;
  3768. END;
  3769. END;
  3770. AddInt(res,res,array.op);
  3771. InitOperand(result,ModeReference);
  3772. result.op := res;
  3773. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3774. ReleaseIntermediateOperand(result.tag);
  3775. result.tag := TypeDescriptorAdr(type);
  3776. IF ~newObjectFile THEN
  3777. IntermediateCode.MakeMemory(result.tag,addressType);
  3778. END
  3779. ELSIF IsDelegate(type) THEN
  3780. ReleaseIntermediateOperand(result.tag);
  3781. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3782. UseIntermediateOperand(result.tag);
  3783. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3784. ReleaseIntermediateOperand(result.tag);
  3785. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3786. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3787. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3788. END;
  3789. ReleaseOperand(array);
  3790. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3791. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3792. END;
  3793. END IndexDesignator;
  3794. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3795. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3796. BEGIN
  3797. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3798. dest := destination; destination := emptyOperand;
  3799. type := x.left.type.resolved;
  3800. IF type IS SyntaxTree.MathArrayType THEN
  3801. MathIndexDesignator(x);
  3802. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3803. IndexDesignator(x);
  3804. END;
  3805. destination := dest;
  3806. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3807. END VisitIndexDesignator;
  3808. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3809. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3810. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3811. procedure: SyntaxTree.Procedure;
  3812. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3813. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3814. BEGIN
  3815. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3816. parameters := expression.parameters;
  3817. moduleName := "FoxArrayBase";
  3818. procedureName := "CopyDescriptor";
  3819. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3820. SaveRegisters();ReleaseUsedRegisters(saved);
  3821. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3822. IF procedure = NIL THEN
  3823. s := "procedure ";
  3824. Strings.Append(s,moduleName);
  3825. Strings.Append(s,".");
  3826. Strings.Append(s,procedureName);
  3827. Strings.Append(s," not present");
  3828. Error(position,s);
  3829. ELSE
  3830. (* push address of temporary variable *)
  3831. Symbol(variable,destOperand);
  3832. Emit(Push(position,destOperand.op));
  3833. ReleaseOperand(destOperand);
  3834. (* push src *)
  3835. Evaluate(expression.left,srcOperand);
  3836. (*
  3837. Dereference(srcOperand,expression.type.resolved);
  3838. Emit(Push(position,srcOperand.tag));
  3839. *)
  3840. Emit(Push(position,srcOperand.op));
  3841. ReleaseOperand(srcOperand);
  3842. tensorFound := FALSE;
  3843. FOR i := 0 TO parameters.Length()-1 DO
  3844. e := parameters.GetExpression(i);
  3845. IF e IS SyntaxTree.TensorRangeExpression THEN
  3846. tensorFound := TRUE;
  3847. ELSIF e IS SyntaxTree.RangeExpression THEN
  3848. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3849. ELSE
  3850. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3851. END;
  3852. END;
  3853. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3854. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3855. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3856. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3857. StaticCallOperand(procOp,procedure);
  3858. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3859. ReleaseOperand(procOp);
  3860. END;
  3861. RestoreRegisters(saved);
  3862. END;
  3863. RETURN variable
  3864. END PrepareTensorDescriptor;
  3865. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3866. VAR
  3867. type, descriptorType, baseType: SyntaxTree.Type;
  3868. operand, tmpOperand, variableOp, variable2Op: Operand;
  3869. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3870. variable, variable2: SyntaxTree.Variable;
  3871. dim, i, size: LONGINT;
  3872. (* TODO: needed? *)
  3873. oldArrayDestinationTag: IntermediateCode.Operand;
  3874. oldArrayDestinationDimension: LONGINT;
  3875. position: LONGINT;
  3876. saved: RegisterEntry;
  3877. PROCEDURE Pass(op: IntermediateCode.Operand);
  3878. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3879. BEGIN
  3880. IF numberRegister >= 0 THEN
  3881. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3882. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3883. Emit(Mov(position,parameterRegister, op));
  3884. ELSE
  3885. Emit(Push(position,op))
  3886. END
  3887. END Pass;
  3888. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3889. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3890. BEGIN
  3891. formalType := formalType.resolved; actualType := actualType.resolved;
  3892. IF IsOpenArray(formalType)THEN
  3893. IF actualType IS SyntaxTree.StringType THEN
  3894. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3895. RETURN;
  3896. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3897. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3898. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3899. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3900. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3901. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3902. ELSE
  3903. tmp := baseReg;
  3904. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3905. IntermediateCode.MakeMemory(tmp,addressType);
  3906. Pass((tmp));
  3907. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3908. END;
  3909. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3910. END;
  3911. END PushArrayLens;
  3912. BEGIN
  3913. IF Trace THEN TraceEnter("PushParameter") END;
  3914. position := expression.position;
  3915. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3916. type := expression.type.resolved;
  3917. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3918. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3919. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3920. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3921. );
  3922. (* TODO: needed? *)
  3923. oldArrayDestinationTag := arrayDestinationTag;
  3924. oldArrayDestinationDimension := arrayDestinationDimension;
  3925. IF IsArrayOfSystemByte(parameter.type) THEN
  3926. Designate(expression,operand);
  3927. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3928. ReleaseIntermediateOperand(operand.tag);
  3929. operand.tag := tmp;
  3930. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3931. Pass((operand.tag));
  3932. END;
  3933. Pass((operand.op));
  3934. ELSIF IsOpenArray(parameter.type) THEN
  3935. Designate(expression,operand);
  3936. baseReg := operand.tag;
  3937. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3938. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3939. END;
  3940. Pass((operand.op)); (* address of the array *)
  3941. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3942. (* case 1 *)
  3943. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3944. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3945. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3946. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3947. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3948. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3949. arrayDestinationTag := sp;
  3950. (* case 1b *)
  3951. IF expression IS SyntaxTree.IndexDesignator THEN
  3952. (*
  3953. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3954. descriptorType := GetMathArrayDescriptorType(dim);
  3955. variable := GetTemporaryVariable(descriptorType,expression.position);
  3956. Symbol(variable,variableOp);
  3957. arrayDestinationTag := variableOp.op;
  3958. *)
  3959. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3960. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3961. arrayDestinationDimension := dim;
  3962. Designate(expression,operand);
  3963. (* case 1a *)
  3964. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3965. Designate(expression,operand);
  3966. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3967. i := 0;
  3968. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3969. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3970. INC(i);
  3971. END;
  3972. type := expression.type.resolved;
  3973. WHILE (i<dim) DO (* remaining static dimensions *)
  3974. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3975. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3976. ReleaseOperand(tmpOperand);
  3977. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3978. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3979. ReleaseOperand(tmpOperand);
  3980. INC(i);
  3981. END;
  3982. dimOp := IntermediateCode.Immediate(addressType,dim);
  3983. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3984. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3985. (* case 1d *)
  3986. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3987. Designate(expression,operand);
  3988. Dereference(operand,type.resolved,FALSE);
  3989. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3990. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3991. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3992. (* case 1f *)
  3993. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3994. Designate(expression,operand);
  3995. FOR i := 0 TO dim-1 DO
  3996. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3997. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3998. ReleaseOperand(tmpOperand);
  3999. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4000. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4001. ReleaseOperand(tmpOperand);
  4002. END;
  4003. dimOp := IntermediateCode.Immediate(addressType,dim);
  4004. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4005. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4006. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4007. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4008. baseType := SemanticChecker.ArrayBase(type,dim);
  4009. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4010. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4011. ELSE HALT(100);
  4012. END;
  4013. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4014. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4015. (* case 2b *)
  4016. IF expression IS SyntaxTree.IndexDesignator THEN
  4017. descriptorType := GetMathArrayDescriptorType(dim);
  4018. variable := GetTemporaryVariable(descriptorType, FALSE);
  4019. Symbol(variable,variableOp);
  4020. arrayDestinationTag := variableOp.op;
  4021. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4022. arrayDestinationDimension := dim;
  4023. NeedDescriptor := TRUE;
  4024. Designate(expression,operand);
  4025. Pass((operand.tag));
  4026. NeedDescriptor := FALSE;
  4027. (* case 2a *)
  4028. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4029. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4030. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4031. INC(i);
  4032. END;
  4033. IF i = dim THEN
  4034. Designate(expression,operand);
  4035. Pass((operand.tag));
  4036. ELSE (* open-static *)
  4037. type := expression.type.resolved;
  4038. descriptorType := GetMathArrayDescriptorType(dim);
  4039. variable := GetTemporaryVariable(descriptorType, FALSE);
  4040. Symbol(variable,variableOp);
  4041. arrayDestinationTag := variableOp.op;
  4042. Designate(expression,operand);
  4043. FOR i := 0 TO dim-1 DO
  4044. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4045. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4046. ReleaseOperand(tmpOperand);
  4047. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4048. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4049. ReleaseOperand(tmpOperand);
  4050. END;
  4051. dimOp := IntermediateCode.Immediate(addressType,dim);
  4052. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4053. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4054. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4055. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4056. baseType := SemanticChecker.ArrayBase(type,dim);
  4057. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4058. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4059. Pass((arrayDestinationTag));
  4060. END;
  4061. (* case 2d *)
  4062. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4063. Designate(expression,operand);
  4064. Dereference(operand,type.resolved,FALSE);
  4065. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4066. Pass((operand.tag));
  4067. (* case 2f *)
  4068. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4069. descriptorType := GetMathArrayDescriptorType(dim);
  4070. variable := GetTemporaryVariable(descriptorType, FALSE);
  4071. Symbol(variable,variableOp);
  4072. arrayDestinationTag := variableOp.op;
  4073. Designate(expression,operand);
  4074. FOR i := 0 TO dim-1 DO
  4075. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4076. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4077. ReleaseOperand(tmpOperand);
  4078. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4079. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4080. ReleaseOperand(tmpOperand);
  4081. END;
  4082. dimOp := IntermediateCode.Immediate(addressType,dim);
  4083. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4084. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4085. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4086. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4087. baseType := SemanticChecker.ArrayBase(type,dim);
  4088. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4089. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4090. Pass((arrayDestinationTag));
  4091. ELSE HALT(100);
  4092. END;
  4093. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4094. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4095. (* case 3b *)
  4096. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4097. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4098. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4099. Symbol(variable,variableOp);
  4100. LoadValue(variableOp,system.addressType);
  4101. ELSE
  4102. descriptorType := GetMathArrayDescriptorType(dim);
  4103. variable := GetTemporaryVariable(descriptorType, FALSE);
  4104. Symbol(variable,variableOp);
  4105. END;
  4106. arrayDestinationTag := variableOp.op;
  4107. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4108. arrayDestinationDimension := 0;
  4109. Designate(expression,operand);
  4110. Pass((operand.tag));
  4111. (* case 3a *)
  4112. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4113. i := 0;
  4114. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4115. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4116. INC(i);
  4117. END;
  4118. IF i = dim THEN
  4119. Designate(expression,operand);
  4120. Pass((operand.tag));
  4121. ELSE (* open-static *)
  4122. type := expression.type.resolved;
  4123. descriptorType := GetMathArrayDescriptorType(dim);
  4124. variable := GetTemporaryVariable(descriptorType, FALSE);
  4125. Symbol(variable,variableOp);
  4126. arrayDestinationTag := variableOp.op;
  4127. Designate(expression,operand);
  4128. FOR i := 0 TO dim-1 DO
  4129. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4130. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4131. ReleaseOperand(tmpOperand);
  4132. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4133. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4134. ReleaseOperand(tmpOperand);
  4135. END;
  4136. dimOp := IntermediateCode.Immediate(addressType,dim);
  4137. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4138. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4139. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4140. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4141. baseType := SemanticChecker.ArrayBase(type,dim);
  4142. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4143. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4144. Pass((arrayDestinationTag));
  4145. END;
  4146. (* case 3d *)
  4147. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4148. Designate(expression,operand);
  4149. Dereference(operand,type.resolved,FALSE);
  4150. (*
  4151. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4152. *)
  4153. Pass((operand.tag));
  4154. (* case 3f *)
  4155. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4156. descriptorType := GetMathArrayDescriptorType(dim);
  4157. variable := GetTemporaryVariable(descriptorType, FALSE);
  4158. Symbol(variable,variableOp);
  4159. arrayDestinationTag := variableOp.op;
  4160. Designate(expression,operand);
  4161. IF operand.op.type.length >1 THEN (* vector register *)
  4162. variable2 := GetTemporaryVariable(type, FALSE);
  4163. Symbol(variable2, variable2Op);
  4164. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4165. Emit(Mov(position,tmp, operand.op));
  4166. ReleaseOperand(operand);
  4167. Symbol(variable2, operand);
  4168. END;
  4169. FOR i := 0 TO dim-1 DO
  4170. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4171. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4172. ReleaseOperand(tmpOperand);
  4173. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4174. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4175. ReleaseOperand(tmpOperand);
  4176. END;
  4177. dimOp := IntermediateCode.Immediate(addressType,dim);
  4178. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4179. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4180. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4181. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4182. baseType := SemanticChecker.ArrayBase(type,dim);
  4183. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4184. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4185. Pass((arrayDestinationTag));
  4186. ELSE HALT(100);
  4187. END;
  4188. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4189. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4190. (* case 4b *)
  4191. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4192. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4193. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4194. Symbol(variable,variableOp);
  4195. LoadValue(variableOp,system.addressType);
  4196. ELSE
  4197. descriptorType := GetMathArrayDescriptorType(dim);
  4198. variable := GetTemporaryVariable(descriptorType, FALSE);
  4199. Symbol(variable,variableOp);
  4200. END;
  4201. arrayDestinationTag := variableOp.op;
  4202. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4203. arrayDestinationDimension := 0;
  4204. Designate(expression,operand);
  4205. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4206. Symbol(variable,variableOp);
  4207. ELSE
  4208. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4209. Symbol(variable,variableOp);
  4210. MakeMemory(tmp,variableOp.op,addressType,0);
  4211. Emit(Mov(position,tmp,operand.tag));
  4212. ReleaseIntermediateOperand(tmp);
  4213. END;
  4214. Pass((variableOp.op));
  4215. ReleaseOperand(variableOp);
  4216. (* case 4a *)
  4217. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4218. i := 0;
  4219. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4220. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4221. INC(i);
  4222. END;
  4223. IF i = dim THEN
  4224. Designate(expression,operand);
  4225. arrayDestinationTag := operand.tag;
  4226. ELSE (* open-static *)
  4227. type := expression.type.resolved;
  4228. descriptorType := GetMathArrayDescriptorType(dim);
  4229. variable := GetTemporaryVariable(descriptorType, FALSE);
  4230. Symbol(variable,variableOp);
  4231. arrayDestinationTag := variableOp.op;
  4232. Designate(expression,operand);
  4233. FOR i := 0 TO dim-1 DO
  4234. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4235. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4236. ReleaseOperand(tmpOperand);
  4237. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4238. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4239. ReleaseOperand(tmpOperand);
  4240. END;
  4241. dimOp := IntermediateCode.Immediate(addressType,dim);
  4242. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4243. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4244. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4245. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4246. baseType := SemanticChecker.ArrayBase(type,dim);
  4247. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4248. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4249. END;
  4250. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4251. Symbol(variable,variableOp);
  4252. MakeMemory(tmp,variableOp.op,addressType,0);
  4253. Emit(Mov(position,tmp,arrayDestinationTag));
  4254. ReleaseIntermediateOperand(tmp);
  4255. Pass((variableOp.op));
  4256. ReleaseOperand(variableOp);
  4257. (* case 4d *)
  4258. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4259. Designate(expression,operand);
  4260. (*
  4261. Dereference(operand,type.resolved,FALSE);
  4262. *)
  4263. (*
  4264. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4265. *)
  4266. Pass((operand.op));
  4267. (* case 4f *)
  4268. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4269. descriptorType := GetMathArrayDescriptorType(dim);
  4270. variable := GetTemporaryVariable(descriptorType, FALSE);
  4271. Symbol(variable,variableOp);
  4272. arrayDestinationTag := variableOp.op;
  4273. Designate(expression,operand);
  4274. FOR i := 0 TO dim-1 DO
  4275. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4276. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4277. ReleaseOperand(tmpOperand);
  4278. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4279. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4280. ReleaseOperand(tmpOperand);
  4281. END;
  4282. dimOp := IntermediateCode.Immediate(addressType,dim);
  4283. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4284. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4285. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4286. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4287. baseType := SemanticChecker.ArrayBase(type,dim);
  4288. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4289. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4290. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4291. Symbol(variable,variableOp);
  4292. MakeMemory(tmp,variableOp.op,addressType,0);
  4293. Emit(Mov(position,tmp,arrayDestinationTag));
  4294. ReleaseIntermediateOperand(tmp);
  4295. Pass((variableOp.op));
  4296. ReleaseOperand(variableOp);
  4297. ELSE HALT(100);
  4298. END;
  4299. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4300. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4301. Designate(expression,operand);
  4302. IF operand.op.type.length > 1 THEN
  4303. Emit(Push(position, operand.op));
  4304. ReleaseOperand(operand);
  4305. ELSE
  4306. size := system.SizeOf(type);
  4307. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4308. size := ToMemoryUnits(system,size);
  4309. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4310. arrayDestinationTag := sp;
  4311. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4312. END;
  4313. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4314. Designate(expression,operand);
  4315. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4316. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4317. Symbol(variable,variableOp);
  4318. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4319. Emit(Mov(position,tmp,operand.op));
  4320. Emit(Push(position,variableOp.op));
  4321. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4322. Pass((operand.op));
  4323. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4324. END;
  4325. ELSE HALT(200)
  4326. END;
  4327. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4328. IF parameter.kind = SyntaxTree.VarParameter THEN
  4329. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4330. Designate(expression, operand);
  4331. Pass((operand.op));
  4332. ELSE
  4333. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4334. Evaluate(expression, operand);
  4335. Pass((operand.extra)); (* step *)
  4336. Pass((operand.tag)); (* last *)
  4337. Pass((operand.op)) (* first *)
  4338. END
  4339. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4340. IF parameter.kind = SyntaxTree.VarParameter THEN
  4341. Designate(expression, operand);
  4342. Pass((operand.op));
  4343. ELSE
  4344. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4345. Evaluate(expression, operand);
  4346. Pass((operand.tag)); (* real part *)
  4347. Pass((operand.op)) (* imaginary part *)
  4348. END
  4349. ELSE
  4350. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4351. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4352. Designate(expression,operand);
  4353. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4354. (* stack allocation *)
  4355. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4356. (*! parameter alignment to be discussed ... *)
  4357. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4358. size := type(SyntaxTree.StringType).length;
  4359. END;
  4360. IF backend.cooperative & parameter.NeedsTrace() THEN
  4361. dst := NewRegisterOperand (addressType);
  4362. Emit(Mov(position,dst, sp));
  4363. IntermediateCode.InitImmediate(null, byteType, 0);
  4364. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4365. ReleaseIntermediateOperand(dst);
  4366. SaveRegisters();ReleaseUsedRegisters(saved);
  4367. (* register dst has been freed before SaveRegisters already *)
  4368. CallAssignMethod(dst, operand.op, parameter.type);
  4369. RestoreRegisters(saved);
  4370. END;
  4371. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4372. ELSIF IsOpenArray(parameter.type) THEN
  4373. Designate(expression,operand);
  4374. baseReg := operand.tag;
  4375. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4376. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4377. END;
  4378. Pass((operand.op)); (* address of the array *)
  4379. ELSIF IsDelegate(parameter.type) THEN
  4380. Evaluate(expression,operand);
  4381. IF backend.cooperative & parameter.NeedsTrace() THEN
  4382. Emit(Push(position, nil));
  4383. dst := NewRegisterOperand (addressType);
  4384. Emit(Mov(position,dst, sp));
  4385. ReleaseIntermediateOperand(dst);
  4386. SaveRegisters();ReleaseUsedRegisters(saved);
  4387. Emit(Push(position, dst));
  4388. (* register dst has been freed before SaveRegisters already *)
  4389. Emit(Push(position, operand.tag));
  4390. CallThis(position,"GarbageCollector","Assign",2);
  4391. RestoreRegisters(saved);
  4392. ELSE
  4393. Pass((operand.tag));
  4394. END;
  4395. Pass((operand.op));
  4396. ELSE
  4397. Evaluate(expression,operand);
  4398. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4399. Emit(Push(position, nil));
  4400. dst := NewRegisterOperand (addressType);
  4401. Emit(Mov(position,dst, sp));
  4402. ReleaseIntermediateOperand(dst);
  4403. SaveRegisters();ReleaseUsedRegisters(saved);
  4404. Emit(Push(position, dst));
  4405. (* register dst has been freed before SaveRegisters already *)
  4406. Emit(Push(position, operand.op));
  4407. CallThis(position,"GarbageCollector","Assign",2);
  4408. RestoreRegisters(saved);
  4409. ELSE
  4410. Pass((operand.op));
  4411. END;
  4412. END;
  4413. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4414. Evaluate(expression,operand);
  4415. Pass((operand.op));
  4416. ELSE (* var parameter *)
  4417. Designate(expression,operand);
  4418. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4419. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4420. Pass((operand.tag));
  4421. END;
  4422. END;
  4423. Pass((operand.op));
  4424. END;
  4425. END;
  4426. (* TODO: needed? *)
  4427. arrayDestinationTag := oldArrayDestinationTag;
  4428. arrayDestinationDimension := oldArrayDestinationDimension;
  4429. IF needsParameterBackup THEN
  4430. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4431. ReuseCopy(parameterBackup, operand.op)
  4432. END;
  4433. ReleaseOperand(operand);
  4434. IF Trace THEN TraceExit("PushParameter") END;
  4435. END PushParameter;
  4436. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4437. VAR prevConditional: BOOLEAN;
  4438. BEGIN
  4439. prevConditional := conditional;
  4440. conditional := FALSE;
  4441. 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
  4442. Expression(x.result); (* use register *)
  4443. END;
  4444. Statement(x.statement);
  4445. conditional := prevConditional;
  4446. IF x.result # NIL THEN Expression(x.result) END;
  4447. 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
  4448. ReleaseIntermediateOperand(result.op);
  4449. END;
  4450. END VisitStatementDesignator;
  4451. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4452. VAR
  4453. procedure: SyntaxTree.Procedure;
  4454. procedureType: SyntaxTree.ProcedureType;
  4455. wasInline: BOOLEAN;
  4456. actualParameters: SyntaxTree.ExpressionList;
  4457. formalParameter: SyntaxTree.Parameter;
  4458. actualParameter: SyntaxTree.Expression;
  4459. i: LONGINT;
  4460. localVariable: SyntaxTree.Variable;
  4461. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4462. src, dest: Operand;
  4463. prevInlineExit : Label;
  4464. prevMapper: SymbolMapper;
  4465. tooComplex: BOOLEAN;
  4466. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4467. BEGIN
  4468. IF e = NIL THEN RETURN TRUE
  4469. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4470. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4471. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4472. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4473. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4474. ELSE RETURN FALSE
  4475. END;
  4476. END SimpleExpression;
  4477. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4478. BEGIN
  4479. RETURN checker.CanPassInRegister(type)
  4480. END FitsInRegister;
  4481. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4482. VAR
  4483. variable: SyntaxTree.Variable;
  4484. variableDesignator: SyntaxTree.Designator;
  4485. BEGIN
  4486. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4487. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4488. variableDesignator.SetType(type);
  4489. RETURN variableDesignator
  4490. END GetTemp;
  4491. BEGIN
  4492. wasInline := currentIsInline;
  4493. prevInlineExit := currentInlineExit;
  4494. prevMapper := currentMapper;
  4495. currentInlineExit := NewLabel();
  4496. tooComplex := FALSE;
  4497. NEW(currentMapper);
  4498. currentIsInline := TRUE;
  4499. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4500. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4501. formalParameter := procedureType.firstParameter;
  4502. actualParameters := x.parameters;
  4503. i := 0;
  4504. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4505. actualParameter := actualParameters.GetExpression(i);
  4506. IF actualParameter.resolved # NIL THEN
  4507. actualParameter := actualParameter.resolved
  4508. END;
  4509. (*
  4510. if expression is simple and can be passed immediately
  4511. or if type fits in register then we can proceed
  4512. otherwise we escape to ordinary procedure call.
  4513. *)
  4514. (* cases where the expression can be mapped identically *)
  4515. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4516. currentMapper.Add(formalParameter, actualParameter, NIL);
  4517. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4518. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4519. (*
  4520. Assign(variableDesignator, actualParameter);
  4521. *)
  4522. Evaluate(actualParameter, src);
  4523. Designate(variableDesignator, dest);
  4524. Emit(Mov(x.position, dest.op, src.op));
  4525. ReleaseOperand(dest);
  4526. ReleaseOperand(src);
  4527. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4528. ELSE tooComplex := TRUE
  4529. END;
  4530. (*
  4531. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4532. currentMapper.Add(formalParameter, actualParameter, NIL);
  4533. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4534. variableDesignator := GetTemp(system.addressType, FALSE);
  4535. Designate(actualParameter, src);
  4536. Designate(variableDesignator, dest);
  4537. IntermediateCode.MakeMemory(dest.op,addressType);
  4538. Emit(Mov(x.position, dest.op, src.op));
  4539. ReleaseOperand(dest);
  4540. IF src.tag.mode # IntermediateCode.Undefined THEN
  4541. tagDesignator := GetTemp(system.addressType, FALSE);
  4542. Designate(tagDesignator, dest);
  4543. IntermediateCode.MakeMemory(dest.op,addressType);
  4544. Emit(Mov(x.position, dest.op, src.op));
  4545. END;
  4546. ReleaseOperand(dest); ReleaseOperand(src);
  4547. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4548. END;
  4549. *)
  4550. formalParameter := formalParameter.nextParameter;
  4551. INC(i);
  4552. END;
  4553. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4554. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4555. currentMapper.Add(NIL, returnDesignator, NIL);
  4556. END;
  4557. localVariable := procedure.procedureScope.firstVariable;
  4558. WHILE ~tooComplex & (localVariable # NIL) DO
  4559. variableDesignator := GetTemp(localVariable.type, FALSE);
  4560. currentMapper.Add(localVariable, variableDesignator, NIL);
  4561. localVariable := localVariable.nextVariable;
  4562. END;
  4563. IF ~tooComplex THEN
  4564. VisitStatementBlock(procedure.procedureScope.body);
  4565. SetLabel(currentInlineExit);
  4566. IF procedureType.returnType # NIL THEN
  4567. Designate(returnDesignator, result);
  4568. IF conditional THEN
  4569. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4570. ValueToCondition(result)
  4571. END;
  4572. END;
  4573. END;
  4574. currentMapper := prevMapper;
  4575. currentInlineExit := prevInlineExit;
  4576. currentIsInline := wasInline;
  4577. RETURN ~tooComplex
  4578. END InlineProcedureCall;
  4579. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4580. VAR
  4581. parameters: SyntaxTree.ExpressionList;
  4582. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4583. designator: SyntaxTree.Designator;
  4584. procedureType: SyntaxTree.ProcedureType;
  4585. formalParameter: SyntaxTree.Parameter;
  4586. operand, returnValue: Operand;
  4587. reg, size, mask, dest: IntermediateCode.Operand;
  4588. saved: RegisterEntry;
  4589. symbol: SyntaxTree.Symbol;
  4590. variable: SyntaxTree.Variable;
  4591. i, parametersSize, returnTypeSize : LONGINT;
  4592. structuredReturnType: BOOLEAN;
  4593. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4594. tempVariableDesignator: SyntaxTree.Designator;
  4595. gap, alignment: LONGINT; (*fld*)
  4596. (* TODO: remove unnecessary backup variables *)
  4597. oldResult: Operand;
  4598. oldCurrentScope: SyntaxTree.Scope;
  4599. oldArrayDestinationTag: IntermediateCode.Operand;
  4600. oldArrayDestinationDimension: LONGINT;
  4601. oldConstantDeclaration: SyntaxTree.Symbol;
  4602. oldDestination: IntermediateCode.Operand;
  4603. oldCurrentLoop: Label;
  4604. oldConditional: BOOLEAN;
  4605. oldTrueLabel, oldFalseLabel: Label;
  4606. oldLocked: BOOLEAN;
  4607. usedRegisters,oldUsedRegisters: RegisterEntry;
  4608. return: IntermediateCode.Operand;
  4609. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4610. arg: IntermediateCode.Operand;
  4611. dummy: IntermediateCode.Operand;
  4612. recordType: SyntaxTree.RecordType;
  4613. operatorSelectionProcedureOperand: Operand;
  4614. operatorSelectionProcedure: SyntaxTree.Procedure;
  4615. fingerPrint: SyntaxTree.FingerPrint;
  4616. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4617. identifierNumber: LONGINT;
  4618. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4619. procedure: SyntaxTree.Procedure;
  4620. PROCEDURE BackupGlobalState;
  4621. BEGIN
  4622. oldResult := result;
  4623. oldCurrentScope := currentScope;
  4624. oldArrayDestinationTag := arrayDestinationTag;
  4625. oldArrayDestinationDimension := arrayDestinationDimension;
  4626. oldConstantDeclaration := constantDeclaration;
  4627. oldDestination := destination;
  4628. oldCurrentLoop := currentLoop;
  4629. oldConditional := conditional;
  4630. oldTrueLabel := trueLabel;
  4631. oldFalseLabel := falseLabel;
  4632. oldLocked := locked;
  4633. oldUsedRegisters := usedRegisters
  4634. END BackupGlobalState;
  4635. PROCEDURE RestoreGlobalState;
  4636. BEGIN
  4637. result := oldResult;
  4638. currentScope := oldCurrentScope;
  4639. arrayDestinationTag := oldArrayDestinationTag;
  4640. arrayDestinationDimension := oldArrayDestinationDimension;
  4641. constantDeclaration := oldConstantDeclaration;
  4642. destination := oldDestination;
  4643. currentLoop := oldCurrentLoop;
  4644. conditional := oldConditional;
  4645. trueLabel := oldTrueLabel;
  4646. falseLabel := oldFalseLabel;
  4647. locked := oldLocked;
  4648. usedRegisters := oldUsedRegisters
  4649. END RestoreGlobalState;
  4650. (** do preparations before parameter push for array-structured object types (ASOTs):
  4651. if ASOT is passed as VAR parameter:
  4652. - allocate temporary variable of math array type
  4653. - copy contents of ASOT to be passed to temporary variable
  4654. - use temporary variable as the actual parameter instead
  4655. - 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)
  4656. **)
  4657. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4658. VAR
  4659. expression: SyntaxTree.Expression;
  4660. BEGIN
  4661. IF actualParameter IS SyntaxTree.Designator THEN
  4662. designator := actualParameter(SyntaxTree.Designator);
  4663. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4664. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4665. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4666. ASSERT(checker # NIL);
  4667. checker.SetCurrentScope(currentScope);
  4668. (* allocate temporary variable *)
  4669. ASSERT(actualParameter.type # NIL);
  4670. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4671. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4672. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4673. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4674. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4675. ASSERT(tempVariableDesignator.type # NIL);
  4676. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4677. (* copy math array stored in actual parameter to temporary variable *)
  4678. BackupGlobalState;
  4679. AssignMathArray(tempVariableDesignator, actualParameter);
  4680. RestoreGlobalState;
  4681. (* use temporary variable as actual parameter instead of the original one *)
  4682. actualParameter := tempVariableDesignator;
  4683. (* create write-back call and store it in linked list *)
  4684. (* create new list entry *)
  4685. IF firstWriteBackCall = NIL THEN
  4686. NEW(firstWriteBackCall);
  4687. currentWriteBackCall := firstWriteBackCall
  4688. ELSE
  4689. ASSERT(currentWriteBackCall # NIL);
  4690. NEW(currentWriteBackCall.next);
  4691. currentWriteBackCall := currentWriteBackCall.next
  4692. END;
  4693. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4694. ASSERT(expression.type = NIL);
  4695. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4696. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4697. (* prepare writeback for any other "normal" indexer *)
  4698. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4699. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4700. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4701. Assign(tempVariableDesignator, actualParameter);
  4702. actualParameter := tempVariableDesignator;
  4703. IF firstWriteBackCall = NIL THEN
  4704. NEW(firstWriteBackCall);
  4705. currentWriteBackCall := firstWriteBackCall
  4706. ELSE
  4707. ASSERT(currentWriteBackCall # NIL);
  4708. NEW(currentWriteBackCall.next);
  4709. currentWriteBackCall := currentWriteBackCall.next
  4710. END;
  4711. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4712. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4713. END
  4714. END
  4715. END PrepareParameter;
  4716. BEGIN
  4717. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4718. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4719. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4720. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4721. IF InlineProcedureCall(x) THEN
  4722. RETURN
  4723. ELSE
  4724. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4725. END
  4726. END;
  4727. END;
  4728. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4729. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4730. dest := destination; destination := emptyOperand;
  4731. SaveRegisters();ReleaseUsedRegisters(saved);
  4732. parameters := x.parameters;
  4733. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4734. (* an operator is called *)
  4735. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4736. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4737. (* check if a dynamic operator call should be performed *)
  4738. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4739. ELSE
  4740. isCallOfDynamicOperator := FALSE
  4741. END;
  4742. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4743. Emit(Push(position, ap));
  4744. END;
  4745. alignment := procedureType.stackAlignment;
  4746. IF alignment > 1 THEN
  4747. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4748. Emit(Mov(position,reg, sp));
  4749. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4750. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4751. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4752. Emit(And(position,sp, sp, mask));
  4753. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4754. Emit(Push(position,reg));
  4755. (*
  4756. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4757. Emit(Mov(position,mem,reg));
  4758. *)
  4759. ReleaseIntermediateOperand(reg);
  4760. END;
  4761. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4762. IntermediateCode.InitImmediate(mask,addressType,-16);
  4763. Emit(And(position,sp, sp, mask));
  4764. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4765. IF gap # 0 THEN
  4766. IntermediateCode.InitImmediate(size,addressType,gap);
  4767. Emit(Sub(position,sp,sp,size))
  4768. END;
  4769. END;
  4770. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4771. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4772. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4773. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4774. ELSE
  4775. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4776. END;
  4777. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4778. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4779. (* push ID *)
  4780. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4781. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4782. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4783. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4784. formalParameter := procedureType.firstParameter;
  4785. FOR i := 0 TO parameters.Length() - 1 DO
  4786. IF formalParameter.access # SyntaxTree.Hidden THEN
  4787. ASSERT(i < 2);
  4788. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4789. (* push pointer *)
  4790. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4791. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4792. (* add dereference *)
  4793. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4794. ReleaseIntermediateOperand(parameterBackups[i]);
  4795. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4796. END;
  4797. Emit(Push(position,parameterBackups[i]));
  4798. ReleaseIntermediateOperand(parameterBackups[i]);
  4799. (* push typetag *)
  4800. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4801. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4802. arg := TypeDescriptorAdr(recordType);
  4803. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4804. Emit(Push(position,arg));
  4805. ELSE
  4806. (* push 'NonPointer' *)
  4807. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4808. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4809. (* push fingerprint *)
  4810. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4811. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4812. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4813. END
  4814. END;
  4815. formalParameter := formalParameter.nextParameter
  4816. END;
  4817. (* for unary operators: complete the information for the second parameter *)
  4818. IF procedureType.numberParameters < 2 THEN
  4819. (* push 'NonPointer' *)
  4820. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4821. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4822. (* push 'NoType' *)
  4823. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4824. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4825. END;
  4826. (* call operator selection procedure *)
  4827. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4828. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4829. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4830. ReleaseOperand(operatorSelectionProcedureOperand);
  4831. (* use the address that the operator selection procedure returned as the target address of the call *)
  4832. InitOperand(operand, ModeValue);
  4833. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4834. Emit(Result(position,operand.op))
  4835. END
  4836. ELSE
  4837. Evaluate(x.left, operand)
  4838. END;
  4839. IF symbol IS SyntaxTree.Procedure THEN
  4840. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4841. Emit(Push(position,operand.tag));
  4842. ELSIF (procedureType.isDelegate) THEN
  4843. Emit(Push(position,operand.tag));
  4844. END;
  4845. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4846. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4847. Emit(Push(position,operand.tag));
  4848. END;
  4849. ELSE HALT(200);
  4850. END;
  4851. ReleaseIntermediateOperand(operand.tag);
  4852. operand.tag := emptyOperand;
  4853. (* determine if a structured return type is needed *)
  4854. structuredReturnType := StructuredReturnType(procedureType);
  4855. IF structuredReturnType THEN
  4856. IF resultDesignator # NIL THEN
  4857. d := resultDesignator;
  4858. ELSE
  4859. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4860. variable.SetUntraced(procedureType.hasUntracedReturn);
  4861. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4862. d.SetType(variable.type);
  4863. END;
  4864. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4865. Designate(d,returnValue);
  4866. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4867. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4868. Emit(Push(position,size));
  4869. Emit(Push(position,returnValue.op));
  4870. ReleaseOperand(returnValue);
  4871. ELSE
  4872. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4873. END;
  4874. END;
  4875. firstWriteBackCall := NIL; (* reset write-back call list *)
  4876. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4877. passByRegister := system.registerParameters > 0;
  4878. registerNumber := 0;
  4879. formalParameter := procedureType.lastParameter;
  4880. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4881. actualParameter := parameters.GetExpression(i);
  4882. PrepareParameter(actualParameter, formalParameter);
  4883. IF passByRegister & (i < system.registerParameters) THEN
  4884. IF ~PassInRegister(formalParameter) THEN
  4885. Error(actualParameter.position,"cannot be passed by register")
  4886. ELSE
  4887. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4888. END;
  4889. INC(registerNumber);
  4890. ELSE
  4891. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4892. END;
  4893. formalParameter := formalParameter.prevParameter;
  4894. END;
  4895. IF passByRegister & (registerNumber > 0) THEN
  4896. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4897. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4898. END;
  4899. ELSE
  4900. hasDynamicOperands := FALSE;
  4901. formalParameter := procedureType.firstParameter;
  4902. FOR i := 0 TO parameters.Length() - 1 DO
  4903. actualParameter := parameters.GetExpression(i);
  4904. IF formalParameter # NIL THEN (* TENTATIVE *)
  4905. PrepareParameter(actualParameter, formalParameter);
  4906. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4907. ASSERT(i < 2);
  4908. hasDynamicOperands := TRUE;
  4909. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4910. ELSE
  4911. IF passByRegister & (registerNumber > 0) THEN
  4912. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4913. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4914. END;
  4915. passByRegister := FALSE;
  4916. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4917. END;
  4918. formalParameter := formalParameter.nextParameter;
  4919. END;
  4920. END;
  4921. END;
  4922. IF symbol IS SyntaxTree.Procedure THEN
  4923. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4924. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4925. Emit(Push(position,reg));
  4926. ReleaseIntermediateOperand(reg);
  4927. END;
  4928. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4929. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4930. parametersSize := ParametersSize(system,procedureType,FALSE);
  4931. END;
  4932. ReleaseParameterRegisters();
  4933. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4934. Emit(Call(position,operand.op,0));
  4935. ELSE
  4936. Emit(Call(position,operand.op,parametersSize));
  4937. END;
  4938. ReleaseOperand(operand);
  4939. IF procedureType.noReturn THEN
  4940. EmitTrap(position,NoReturnTrap);
  4941. END;
  4942. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4943. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4944. Emit(Result(position,return));
  4945. END;
  4946. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4947. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4948. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4949. END;
  4950. size := IntermediateCode.Immediate(addressType,parametersSize);
  4951. Emit(Add(position,sp,sp,size));
  4952. END;
  4953. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4954. IF structuredReturnType THEN
  4955. (* stack pointer rewinding done by callee
  4956. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4957. Emit(Add(position,sp,sp,size));
  4958. *)
  4959. RestoreRegisters(saved);
  4960. InitOperand(result,ModeReference);
  4961. Symbol(variable,result);
  4962. ELSE
  4963. RestoreRegisters(saved);
  4964. InitOperand(result,ModeValue);
  4965. result.op := return;
  4966. END;
  4967. END;
  4968. IF alignment > 1 THEN
  4969. Emit(Pop(position,sp));
  4970. END;
  4971. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4972. Emit(Pop(position, ap));
  4973. END;
  4974. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4975. ValueToCondition(result);
  4976. END;
  4977. destination := dest;
  4978. (* perform all write-back calls in the list *)
  4979. BackupGlobalState;
  4980. currentWriteBackCall := firstWriteBackCall;
  4981. WHILE currentWriteBackCall # NIL DO
  4982. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4983. currentWriteBackCall := currentWriteBackCall.next
  4984. END;
  4985. RestoreGlobalState;
  4986. (* TENATIVE *)
  4987. (*
  4988. IF isOperatorCall THEN
  4989. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4990. END;
  4991. *)
  4992. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  4993. END VisitProcedureCallDesignator;
  4994. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  4995. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  4996. td: SyntaxTree.Symbol;
  4997. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  4998. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4999. BEGIN
  5000. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5001. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5002. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5003. variable.SetType(system.anyType);
  5004. scope.AddVariable(variable);
  5005. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5006. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5007. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5008. typeDeclaration.SetScope(module.module.moduleScope);
  5009. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5010. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5011. END;
  5012. RETURN hiddenPointerType;
  5013. END GetHiddenPointerType;
  5014. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5015. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5016. BEGIN
  5017. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5018. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5019. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  5020. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  5021. scope.AddVariable(variable);
  5022. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5023. variable.SetType(system.anyType);
  5024. scope.AddVariable(variable);
  5025. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5026. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  5027. typeDeclaration.SetDeclaredType(delegatePointerType);
  5028. typeDeclaration.SetScope(module.module.moduleScope);
  5029. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5030. delegatePointerType.SetState(SyntaxTree.Resolved);
  5031. END;
  5032. RETURN delegatePointerType
  5033. END GetDelegateType;
  5034. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5035. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5036. such as in VAR a: RECORD ... END;
  5037. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5038. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5039. *)
  5040. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5041. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5042. BEGIN (* no code emission *)
  5043. source := NIL;
  5044. x := x.resolved;
  5045. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5046. x := GetHiddenPointerType();
  5047. ELSIF IsDelegate(x) THEN
  5048. x := GetDelegateType();
  5049. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5050. ELSE HALT(200);
  5051. END;
  5052. td := x.typeDeclaration;
  5053. IF td = NIL THEN
  5054. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5055. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5056. ASSERT(td # NIL);
  5057. END;
  5058. IF newObjectFile THEN
  5059. GetCodeSectionNameForSymbol(td,name);
  5060. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5061. meta.CheckTypeDeclaration(x);
  5062. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5063. ELSE
  5064. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5065. END;
  5066. IF backend.cooperative THEN
  5067. offset := 0;
  5068. ELSE
  5069. IF x IS SyntaxTree.CellType THEN
  5070. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5071. IF baseRecord = NIL THEN
  5072. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5073. ELSE
  5074. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5075. END;
  5076. ELSE
  5077. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5078. END;
  5079. END;
  5080. ELSE
  5081. offset := 0;
  5082. source := module.allSections.FindBySymbol(td); (*TODO*)
  5083. IF source = NIL THEN
  5084. null := 0;
  5085. GetCodeSectionNameForSymbol(td,name);
  5086. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5087. IntermediateCode.InitImmediate(op,addressType,0);
  5088. source(IntermediateCode.Section).Emit(Data(position,op));
  5089. source.SetReferenced(FALSE)
  5090. ELSE
  5091. name := source.name;
  5092. END;
  5093. END;
  5094. RETURN td
  5095. END GetBackendType;
  5096. BEGIN
  5097. (*td := t.typeDeclaration;*)
  5098. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5099. (*
  5100. IF t IS SyntaxTree.PointerType THEN
  5101. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5102. ELSE
  5103. source := GetBackendType(t);
  5104. END;
  5105. *)
  5106. IF newObjectFile THEN
  5107. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5108. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5109. IntermediateCode.SetOffset(res,offset);
  5110. ELSE
  5111. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5112. END;
  5113. (*
  5114. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5115. make memory should be used when tag is used, not earlier
  5116. *)
  5117. RETURN res
  5118. END TypeDescriptorAdr;
  5119. (*
  5120. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5121. VAR result: IntermediateCode.Operand;
  5122. BEGIN
  5123. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5124. operand.tag := TypeDescriptorAdr(operand.type);
  5125. IntermediateCode.MakeMemory(operand.tag,addressType);
  5126. UseIntermediateOperand(operand.tag);
  5127. END;
  5128. END MakeTypeTag;
  5129. *)
  5130. PROCEDURE ProfilerInit;
  5131. VAR reg: IntermediateCode.Operand;
  5132. BEGIN
  5133. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5134. Emit(Call(position,reg,0));
  5135. END ProfilerInit;
  5136. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5137. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5138. BEGIN
  5139. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5140. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5141. THEN
  5142. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5143. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5144. Emit(Push(position,reg));
  5145. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5146. Emit(Push(position,reg));
  5147. StaticCallOperand(result,procedure);
  5148. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5149. ReleaseOperand(result);
  5150. END;
  5151. END ProfilerEnterExit;
  5152. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5153. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5154. BEGIN
  5155. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5156. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5157. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5158. profileInit.Emit(Push(position,reg));
  5159. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5160. profileInit.Emit(Push(position,reg));
  5161. NEW(string, LEN(name)); COPY(name, string^);
  5162. sv := SyntaxTree.NewStringValue(-1,string);
  5163. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5164. sv.SetType(type);
  5165. Designate(sv,result);
  5166. profileInit.Emit(Push(position,result.tag));
  5167. profileInit.Emit(Push(position,result.op));
  5168. StaticCallOperand(result,procedure);
  5169. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5170. ReleaseOperand(result);
  5171. END;
  5172. END ProfilerAddProcedure;
  5173. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5174. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5175. BEGIN
  5176. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5177. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5178. profileInit.Emit(Push(position,reg));
  5179. profileInitPatchPosition := profileInit.pc;
  5180. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5181. NEW(string, LEN(name)); COPY(name, string^);
  5182. sv := SyntaxTree.NewStringValue(-1,string);
  5183. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5184. sv.SetType(type);
  5185. Designate(sv,result);
  5186. profileInit.Emit(Push(position,result.tag));
  5187. profileInit.Emit(Push(position,result.op));
  5188. StaticCallOperand(result,procedure);
  5189. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5190. ReleaseOperand(result);
  5191. END;
  5192. END ProfilerAddModule;
  5193. PROCEDURE ProfilerPatchInit;
  5194. VAR reg: IntermediateCode.Operand;
  5195. BEGIN
  5196. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5197. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5198. EmitLeave(profileInit,position,0);
  5199. profileInit.Emit(Exit(position,0,0));
  5200. END ProfilerPatchInit;
  5201. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5202. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5203. VAR
  5204. id: LONGINT;
  5205. leftType, rightType: SyntaxTree.Type;
  5206. procedureType: SyntaxTree.ProcedureType;
  5207. runtimeProcedure: SyntaxTree.Procedure;
  5208. runtimeProcedureOperand, operatorOperand: Operand;
  5209. kind: SET;
  5210. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5211. VAR
  5212. arg: IntermediateCode.Operand;
  5213. recordType: SyntaxTree.RecordType;
  5214. fingerPrint: SyntaxTree.FingerPrint;
  5215. BEGIN
  5216. IF type = NIL THEN
  5217. (* no type: push 'NoType' *)
  5218. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5219. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5220. ELSIF IsStrictlyPointerToRecord(type) THEN
  5221. (* pointer to record type: push typetag *)
  5222. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5223. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5224. arg := TypeDescriptorAdr(recordType);
  5225. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5226. ELSE
  5227. (* non-pointer to record type: push fingerprint *)
  5228. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5229. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5230. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5231. END;
  5232. operatorInitializationCodeSection.Emit(Push(position,arg))
  5233. END PushTypeInfo;
  5234. BEGIN
  5235. ASSERT(operatorInitializationCodeSection # NIL);
  5236. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5237. procedureType := operator.type(SyntaxTree.ProcedureType);
  5238. (* determine types *)
  5239. leftType := procedureType.firstParameter.type;
  5240. IF procedureType.numberParameters = 2 THEN
  5241. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5242. rightType := procedureType.firstParameter.nextParameter.type;
  5243. ELSE
  5244. rightType := NIL
  5245. END;
  5246. (* determine operator kind *)
  5247. IF IsStrictlyPointerToRecord(leftType) THEN
  5248. kind := {LhsIsPointer}
  5249. ELSE
  5250. kind := {}
  5251. END;
  5252. IF IsStrictlyPointerToRecord(rightType) THEN
  5253. kind := kind + {RhsIsPointer}
  5254. END;
  5255. IF kind # {} THEN (* TODO: to be removed later on *)
  5256. (* at least one of the types is a pointer to record *)
  5257. (* emit a code that registers this specific operator in the runtime *)
  5258. dump := operatorInitializationCodeSection.comments;
  5259. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5260. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5261. (* push ID *)
  5262. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5263. id := Global.GetSymbol(module.module.case, operator.name);
  5264. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5265. (* push kind *)
  5266. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5267. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5268. (* push type infos *)
  5269. PushTypeInfo(leftType);
  5270. PushTypeInfo(rightType);
  5271. (* push operator address *)
  5272. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5273. StaticCallOperand(operatorOperand, operator);
  5274. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5275. ReleaseOperand(operatorOperand);
  5276. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5277. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5278. ReleaseOperand(runtimeProcedureOperand)
  5279. END
  5280. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5281. END
  5282. END RegisterDynamicOperator;
  5283. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5284. VAR
  5285. traceModule: SyntaxTree.Module;
  5286. procedure: SyntaxTree.Procedure;
  5287. procedureVariable: SyntaxTree.Variable;
  5288. s,msg: Basic.MessageString;
  5289. res: Operand;
  5290. i: LONGINT;
  5291. sv: SyntaxTree.StringValue;
  5292. type: SyntaxTree.Type;
  5293. recordType: SyntaxTree.RecordType;
  5294. printout: Printout.Printer;
  5295. stringWriter: Streams.StringWriter;
  5296. expression: SyntaxTree.Expression;
  5297. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5298. BEGIN
  5299. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5300. IF procedure = NIL THEN
  5301. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5302. END;
  5303. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5304. s := "procedure ";
  5305. Strings.Append(s,backend.traceModuleName);
  5306. Strings.Append(s,".");
  5307. Strings.Append(s,procedureName);
  5308. Strings.Append(s," not present");
  5309. Error(position,s);
  5310. RETURN FALSE
  5311. ELSE
  5312. RETURN TRUE
  5313. END;
  5314. END GetProcedure;
  5315. PROCEDURE CallProcedure;
  5316. VAR size: LONGINT;
  5317. BEGIN
  5318. IF procedure # NIL THEN
  5319. StaticCallOperand(result,procedure);
  5320. size := ProcedureParametersSize(system,procedure);
  5321. ELSE
  5322. Symbol(procedureVariable, result);
  5323. LoadValue(result, procedureVariable.type.resolved);
  5324. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5325. END;
  5326. Emit(Call(position,result.op,size));
  5327. END CallProcedure;
  5328. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5329. VAR res: Operand; string: SyntaxTree.String;
  5330. BEGIN
  5331. IF GetProcedure("String") THEN
  5332. NEW(string, LEN(s)); COPY(s, string^);
  5333. sv := SyntaxTree.NewStringValue(-1,string);
  5334. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5335. sv.SetType(type);
  5336. Designate(sv,res);
  5337. Emit(Push(position,res.tag));
  5338. Emit(Push(position,res.op));
  5339. ReleaseOperand(res);
  5340. CallProcedure;
  5341. END;
  5342. END String;
  5343. PROCEDURE Integer(op: IntermediateCode.Operand);
  5344. BEGIN
  5345. IF GetProcedure("Int") THEN
  5346. Emit(Push(position,op));
  5347. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5348. CallProcedure;
  5349. END;
  5350. END Integer;
  5351. PROCEDURE Float(op: IntermediateCode.Operand);
  5352. BEGIN
  5353. IF GetProcedure("HIntHex") THEN
  5354. Emit(Push(position,op));
  5355. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5356. CallProcedure;
  5357. END;
  5358. END Float;
  5359. PROCEDURE Set(op: IntermediateCode.Operand);
  5360. BEGIN
  5361. IF GetProcedure("Set") THEN
  5362. Emit(Push(position,op));
  5363. (*
  5364. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5365. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5366. *)
  5367. CallProcedure;
  5368. END;
  5369. END Set;
  5370. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5371. BEGIN
  5372. IF GetProcedure("Boolean") THEN
  5373. Emit(Push(position,op));
  5374. CallProcedure;
  5375. END;
  5376. END Boolean;
  5377. PROCEDURE Char(op: IntermediateCode.Operand);
  5378. BEGIN
  5379. IF GetProcedure("Char") THEN
  5380. Emit(Push(position,op));
  5381. CallProcedure;
  5382. END;
  5383. END Char;
  5384. PROCEDURE Address(op: IntermediateCode.Operand);
  5385. BEGIN
  5386. IF GetProcedure("Address") THEN
  5387. Emit(Push(position,op));
  5388. CallProcedure;
  5389. END;
  5390. END Address;
  5391. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5392. BEGIN
  5393. IF GetProcedure("String") THEN
  5394. Emit(Push(position,tag));
  5395. Emit(Push(position,op));
  5396. CallProcedure;
  5397. END;
  5398. END StringOperand;
  5399. PROCEDURE Ln;
  5400. BEGIN
  5401. IF GetProcedure("Ln") THEN
  5402. CallProcedure;
  5403. END;
  5404. END Ln;
  5405. BEGIN
  5406. IF backend.traceModuleName = "" THEN RETURN END;
  5407. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5408. IF GetProcedure("Enter") THEN
  5409. CallProcedure
  5410. END;
  5411. NEW(stringWriter,LEN(s));
  5412. FOR i := 0 TO x.Length()-1 DO
  5413. msg := "";
  5414. expression := x.GetExpression(i);
  5415. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5416. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5417. ELSE
  5418. Global.GetModuleName(module.module, s);
  5419. END;
  5420. IF i = 0 THEN
  5421. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5422. stringWriter.String(":");
  5423. END;
  5424. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5425. IF ~(expression IS SyntaxTree.StringValue) THEN
  5426. printout.Expression(expression);
  5427. stringWriter.Get(s);
  5428. Strings.Append(msg,s);
  5429. Strings.Append(msg,"= ");
  5430. ELSE stringWriter.Get(s); (* remove from string writer *)
  5431. Strings.Append(msg, s);
  5432. END;
  5433. String(msg);
  5434. IF SemanticChecker.IsStringType(expression.type) THEN
  5435. Designate(expression,res);
  5436. StringOperand(res.op,res.tag);
  5437. ELSE
  5438. Evaluate(expression,res);
  5439. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5440. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5441. Convert(res.op,int64);
  5442. END;
  5443. Integer(res.op);
  5444. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5445. Boolean(res.op);
  5446. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5447. Set(res.op);
  5448. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5449. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5450. Convert(res.op,float64);
  5451. END;
  5452. Float(res.op);
  5453. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5454. Char(res.op);
  5455. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5456. Address(res.op);String("H");
  5457. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5458. Address(res.op);String("H");
  5459. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5460. Address(res.op);String("H");
  5461. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5462. Address(res.op);String("H");
  5463. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5464. String("NIL");
  5465. ELSE HALT(200);
  5466. END;
  5467. END;
  5468. ReleaseOperand(res);
  5469. String("; ");
  5470. END;
  5471. IF GetProcedure("Exit") THEN
  5472. CallProcedure
  5473. ELSE
  5474. Ln;
  5475. END;
  5476. END;
  5477. END SystemTrace;
  5478. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5479. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5480. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5481. BEGIN
  5482. type := type.resolved;
  5483. IF type IS SyntaxTree.RecordType THEN
  5484. WITH type: SyntaxTree.RecordType DO
  5485. baseType := type.baseType;
  5486. IF baseType # NIL THEN
  5487. baseType := baseType.resolved;
  5488. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5489. InitFields(baseType,adr,offset);
  5490. END;
  5491. variable := type.recordScope.firstVariable;
  5492. WHILE variable # NIL DO
  5493. IF variable.initializer # NIL THEN
  5494. Evaluate(variable.initializer,initializerOp);
  5495. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5496. Emit(Mov(position,mem,initializerOp.op));
  5497. ReleaseOperand(initializerOp);
  5498. ReleaseIntermediateOperand(mem);
  5499. END;
  5500. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5501. variable := variable.nextVariable
  5502. END;
  5503. END;
  5504. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5505. WITH type: SyntaxTree.ArrayType DO
  5506. IF type.form = SyntaxTree.Static THEN
  5507. baseType := type.arrayBase;
  5508. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5509. FOR i := 0 TO type.staticLength-1 DO
  5510. InitFields(baseType,adr,offset+i*size);
  5511. END;
  5512. END;
  5513. END;
  5514. ELSIF type IS SyntaxTree.MathArrayType THEN
  5515. WITH type: SyntaxTree.MathArrayType DO
  5516. IF type.form = SyntaxTree.Open THEN
  5517. dim := DynamicDim(type);
  5518. imm := IntermediateCode.Immediate(addressType,dim);
  5519. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5520. baseType := SemanticChecker.ArrayBase(type,dim);
  5521. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5522. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5523. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5524. ReleaseIntermediateOperand(imm);
  5525. (* flags remain empty (=0) for open array *)
  5526. ELSIF type.form = SyntaxTree.Static THEN
  5527. baseType := type.arrayBase;
  5528. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5529. ASSERT(type.staticLength < 1024*1024*1024);
  5530. FOR i := 0 TO type.staticLength-1 DO
  5531. InitFields(baseType,adr,offset+i*size);
  5532. END;
  5533. END;
  5534. END;
  5535. END;
  5536. END InitFields;
  5537. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5538. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5539. BEGIN
  5540. type := variable.type.resolved;
  5541. IF (type IS SyntaxTree.MathArrayType) THEN
  5542. WITH type: SyntaxTree.MathArrayType DO
  5543. IF type.form = SyntaxTree.Open THEN
  5544. Symbol(variable,operand);
  5545. InitFields(type, operand.tag,0);
  5546. ELSIF type.form = SyntaxTree.Tensor THEN
  5547. Symbol(variable, operand);
  5548. MakeMemory(tmp,operand.op,addressType,0);
  5549. ReleaseOperand(operand);
  5550. Emit(Mov(position,tmp, nil ) );
  5551. ReleaseIntermediateOperand(tmp);
  5552. END;
  5553. END;
  5554. ELSE
  5555. Symbol(variable,operand);
  5556. IF variable.initializer # NIL THEN
  5557. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5558. reference.SetType(variable.type.resolved);
  5559. reference.SetAssignable(TRUE);
  5560. Assign(reference,variable.initializer);
  5561. END;
  5562. InitFields(type, operand.op,0);
  5563. ReleaseOperand(operand);
  5564. END;
  5565. END InitVariable;
  5566. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5567. VAR end: Label;
  5568. BEGIN
  5569. IF type.form = SyntaxTree.Tensor THEN
  5570. InitOperand(result,ModeValue);
  5571. ReuseCopy(result.op,base);
  5572. end := NewLabel();
  5573. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5574. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5575. SetLabel(end);
  5576. Convert(result.op,int32);
  5577. ELSE
  5578. InitOperand(result,ModeValue);
  5579. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5580. END
  5581. END MathArrayDim;
  5582. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5583. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5584. BEGIN
  5585. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5586. MakeMemory(mem,base,addressType,offset);
  5587. Emit(Mov(position,mem,value));
  5588. ReleaseIntermediateOperand(mem);
  5589. END PutMathArrayField;
  5590. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5591. VAR mem: IntermediateCode.Operand;
  5592. BEGIN
  5593. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5594. MakeMemory(mem,base,addressType,offset);
  5595. Emit(Mov(position,mem,value));
  5596. ReleaseIntermediateOperand(mem);
  5597. END PutMathArrayFieldOffset;
  5598. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5599. BEGIN
  5600. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5601. MakeMemory(value,base,addressType,offset);
  5602. END GetMathArrayField;
  5603. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5604. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5605. BEGIN
  5606. IF incr THEN
  5607. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5608. ELSE
  5609. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5610. END;
  5611. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5612. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5613. ELSE
  5614. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5615. Emit(Mov(position,reg,dim));
  5616. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5617. Emit(Add(position,reg,reg,base));
  5618. MakeMemory(mem, reg, addressType, offset);
  5619. ReleaseIntermediateOperand(reg);
  5620. Emit(Mov(position,mem,value));
  5621. ReleaseIntermediateOperand(mem);
  5622. END;
  5623. END PutMathArrayLenOrIncr;
  5624. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5625. BEGIN
  5626. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5627. END PutMathArrayLength;
  5628. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5629. BEGIN
  5630. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5631. END PutMathArrayIncrement;
  5632. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5633. BEGIN
  5634. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5635. END GetMathArrayIncrement;
  5636. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5637. BEGIN
  5638. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5639. END GetMathArrayLength;
  5640. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5641. VAR dimOp: IntermediateCode.Operand;
  5642. BEGIN
  5643. dimOp := IntermediateCode.Immediate(int32, dim);
  5644. GetMathArrayLength(type, operand, dimOp, check, result);
  5645. END GetMathArrayLengthAt;
  5646. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5647. VAR dimOp: IntermediateCode.Operand;
  5648. BEGIN
  5649. dimOp := IntermediateCode.Immediate(int32, dim);
  5650. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5651. END GetMathArrayIncrementAt;
  5652. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5653. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5654. offset: LONGINT;
  5655. BEGIN
  5656. IF increment THEN
  5657. offset := MathIncrOffset;
  5658. ELSE
  5659. offset := MathLenOffset;
  5660. END;
  5661. INC(offset,operand.dimOffset*2);
  5662. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5663. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5664. END;
  5665. (* static dimension *)
  5666. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5667. IF check & (type.form = SyntaxTree.Tensor) THEN
  5668. DimensionCheck(operand.tag,dim,BrltL);
  5669. END;
  5670. val := SHORT(dim.intValue);
  5671. IF type.form # SyntaxTree.Tensor THEN
  5672. t := SemanticChecker.ArrayBase(type,val);
  5673. type := t.resolved(SyntaxTree.MathArrayType);
  5674. IF type.form = SyntaxTree.Static THEN
  5675. IF increment THEN
  5676. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5677. ELSE
  5678. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5679. END;
  5680. InitOperand(result,ModeValue);
  5681. result.op := res;
  5682. RETURN;
  5683. END;
  5684. END;
  5685. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5686. MakeMemory(res,operand.tag,addressType,offset);
  5687. (*
  5688. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5689. *)
  5690. InitOperand(result,ModeValue);
  5691. result.op := res;
  5692. ELSE
  5693. Convert(dim,addressType);
  5694. IF check THEN
  5695. IF type.form = SyntaxTree.Tensor THEN
  5696. DimensionCheck(operand.tag,dim,BrltL);
  5697. ELSIF isUnchecked THEN (* do nothing *)
  5698. ELSE
  5699. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5700. END;
  5701. END;
  5702. end := NewLabel(); next := NIL;
  5703. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5704. Emit(Mov(position,res,dim));
  5705. Convert(res,int32);
  5706. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5707. WHILE t IS SyntaxTree.MathArrayType DO
  5708. type := t(SyntaxTree.MathArrayType);
  5709. IF type.form = SyntaxTree.Static THEN
  5710. imm := IntermediateCode.Immediate(int32,val);
  5711. next := NewLabel();
  5712. BrneL(next,imm,res);
  5713. IF increment THEN
  5714. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5715. ELSE
  5716. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5717. END;
  5718. Emit(MovReplace(position,res,imm));
  5719. BrL(end);
  5720. ELSE hasDynamicPart := TRUE;
  5721. END;
  5722. t := type.arrayBase.resolved;
  5723. val := val + 1;
  5724. IF next # NIL THEN SetLabel(next) END;
  5725. END;
  5726. IF hasDynamicPart THEN
  5727. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5728. Emit(Mov(position,res2,dim));
  5729. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5730. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5731. Emit(Add(position,res2,res2,imm));
  5732. Emit(Add(position,res2,res2,operand.tag));
  5733. IntermediateCode.MakeMemory(res2,int32);
  5734. Emit(MovReplace(position,res,res2));
  5735. ReleaseIntermediateOperand(res2);
  5736. END;
  5737. SetLabel(end);
  5738. Convert(res,int32);
  5739. InitOperand(result,ModeValue);
  5740. result.op := res;
  5741. END;
  5742. END MathArrayLenOrIncr;
  5743. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5744. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5745. offset: LONGINT;
  5746. BEGIN
  5747. offset := operand.dimOffset+DynamicDim(type)-1;
  5748. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5749. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5750. val := SHORT(dim.intValue);
  5751. t := SemanticChecker.ArrayBase(type,val);
  5752. type := t.resolved(SyntaxTree.ArrayType);
  5753. IF type.form = SyntaxTree.Static THEN
  5754. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5755. ELSE
  5756. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5757. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5758. END;
  5759. UseIntermediateOperand(res);
  5760. InitOperand(result,ModeValue);
  5761. result.op := res;
  5762. ELSE
  5763. Convert(dim,addressType);
  5764. IF ~isUnchecked THEN
  5765. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5766. END;
  5767. end := NewLabel(); next := NIL;
  5768. (* ReuseCopy(dim,res); *)
  5769. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5770. Emit(Mov(position,res,dim));
  5771. Convert(res,int32);
  5772. Convert(res,int32);
  5773. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5774. WHILE t IS SyntaxTree.ArrayType DO
  5775. type := t(SyntaxTree.ArrayType);
  5776. IF type.form = SyntaxTree.Static THEN
  5777. imm := IntermediateCode.Immediate(int32,val);
  5778. next := NewLabel();
  5779. BrneL(next,imm,res);
  5780. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5781. Emit(MovReplace(position,res,imm));
  5782. BrL(end);
  5783. ELSE hasDynamicPart := TRUE;
  5784. END;
  5785. t := type.arrayBase.resolved;
  5786. val := val + 1;
  5787. IF next # NIL THEN SetLabel(next) END;
  5788. END;
  5789. IF hasDynamicPart THEN
  5790. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5791. Convert(res2,addressType);
  5792. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5793. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5794. Emit(Sub(position,res2,imm,res2));
  5795. Emit(Add(position,res2,res2,operand.tag));
  5796. IntermediateCode.MakeMemory(res2,int32);
  5797. Emit(MovReplace(position,res,res2));
  5798. ReleaseIntermediateOperand(res2);
  5799. END;
  5800. SetLabel(end);
  5801. Convert(res,int32);
  5802. InitOperand(result,ModeValue);
  5803. result.op := res;
  5804. END;
  5805. END ArrayLen;
  5806. (**
  5807. create a temporary variable in current scope
  5808. **)
  5809. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5810. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5811. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5812. BEGIN
  5813. IF ~register THEN
  5814. variable := temporaries.GetFreeVariable(type, index);
  5815. ELSE
  5816. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5817. END;
  5818. scope := currentScope;
  5819. IF variable = NIL THEN
  5820. COPY("@hiddenIRVar",string);
  5821. Basic.AppendNumber(string,index);
  5822. name := SyntaxTree.NewIdentifier(string);
  5823. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5824. variable.SetType(type);
  5825. variable.SetAccess(SyntaxTree.Hidden);
  5826. IF ~register THEN
  5827. temporaries.AddVariable(variable);
  5828. IF scope.lastVariable # NIL THEN
  5829. offset := scope.lastVariable.offsetInBits;
  5830. ELSE
  5831. offset := 0;
  5832. END;
  5833. DEC(offset,system.SizeOf(variable.type));
  5834. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5835. variable(SyntaxTree.Variable).SetOffset(offset);
  5836. scope.AddVariable(variable(SyntaxTree.Variable));
  5837. scope.EnterSymbol(variable, duplicate);
  5838. ASSERT(~duplicate);
  5839. InitVariable(variable(SyntaxTree.Variable));
  5840. ELSE
  5841. variable.SetUseRegister(TRUE);
  5842. variable(SyntaxTree.Variable).SetOffset(0);
  5843. END;
  5844. ELSE
  5845. InitVariable(variable(SyntaxTree.Variable));
  5846. (*
  5847. ASSERT(variable.type.resolved = type.resolved)
  5848. *)
  5849. END;
  5850. RETURN variable(SyntaxTree.Variable)
  5851. END GetTemporaryVariable;
  5852. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5853. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5854. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5855. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5856. i: LONGINT; duplicate: BOOLEAN;
  5857. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5858. VAR variable: SyntaxTree.Variable;
  5859. BEGIN
  5860. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5861. variable.SetType(type);
  5862. recordScope.AddVariable(variable);
  5863. END AddVariable;
  5864. BEGIN
  5865. name := "@ArrayDescriptor";
  5866. Basic.AppendNumber(name,dimensions);
  5867. identifier := SyntaxTree.NewIdentifier(name);
  5868. parentScope := module.module.moduleScope;
  5869. symbol := parentScope.FindSymbol(identifier);
  5870. IF symbol # NIL THEN
  5871. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5872. type := typeDeclaration.declaredType;
  5873. ELSE
  5874. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5875. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5876. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5877. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5878. recordType.SetTypeDeclaration(typeDeclaration);
  5879. recordType.SetState(SyntaxTree.Resolved);
  5880. typeDeclaration.SetDeclaredType(recordType);
  5881. AddVariable("@ptr",system.anyType);
  5882. AddVariable("@adr",system.addressType);
  5883. AddVariable("@flags",system.addressType);
  5884. AddVariable("@dim",system.addressType);
  5885. AddVariable("@elementSize",system.addressType);
  5886. FOR i := 0 TO dimensions-1 DO
  5887. name := "@len";
  5888. Basic.AppendNumber(name,i);
  5889. AddVariable(name,system.addressType);
  5890. name := "@incr";
  5891. Basic.AppendNumber(name,i);
  5892. AddVariable(name,system.addressType);
  5893. END;
  5894. parentScope.AddTypeDeclaration(typeDeclaration);
  5895. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5896. ASSERT(~duplicate);
  5897. type := recordType;
  5898. END;
  5899. RETURN type
  5900. END GetMathArrayDescriptorType;
  5901. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5902. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5903. BEGIN
  5904. type := GetMathArrayDescriptorType(dimensions);
  5905. Emit(Push(position,op.op));
  5906. (* push type descriptor *)
  5907. reg := TypeDescriptorAdr(type);
  5908. IF ~newObjectFile THEN
  5909. IntermediateCode.MakeMemory(reg,addressType);
  5910. END;
  5911. Emit(Push(position,reg));
  5912. ReleaseIntermediateOperand(reg);
  5913. (* push realtime flag: false by default *)
  5914. Emit(Push(position,false));
  5915. CallThis(position,"Heaps","NewRec",3);
  5916. END NewMathArrayDescriptor;
  5917. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5918. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5919. BEGIN
  5920. NEW(string, LEN(s)); COPY(s, string^);
  5921. sv := SyntaxTree.NewStringValue(-1,string);
  5922. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5923. sv.SetType(type);
  5924. Designate(sv,res);
  5925. Emit(Push(position,res.tag));
  5926. Emit(Push(position,res.op));
  5927. ReleaseOperand(res);
  5928. END PushConstString;
  5929. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5930. BEGIN
  5931. IF b THEN
  5932. Emit(Push(-1, true));
  5933. ELSE
  5934. Emit(Push(-1, false));
  5935. END;
  5936. END PushConstBoolean;
  5937. PROCEDURE PushConstSet(v: SET);
  5938. VAR value: SyntaxTree.Value; op: Operand;
  5939. BEGIN
  5940. value := SyntaxTree.NewSetValue(-1, v);
  5941. value.SetType(system.setType);
  5942. Evaluate(value, op);
  5943. Emit(Push(-1, op.op));
  5944. ReleaseOperand(op);
  5945. END PushConstSet;
  5946. PROCEDURE PushConstInteger(v: LONGINT);
  5947. VAR value: SyntaxTree.Value; op: Operand;
  5948. BEGIN
  5949. value := SyntaxTree.NewIntegerValue(-1, v);
  5950. value.SetType(system.longintType);
  5951. Evaluate(value, op);
  5952. Emit(Push(-1, op.op));
  5953. ReleaseOperand(op);
  5954. END PushConstInteger;
  5955. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5956. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5957. section: IntermediateCode.Section;
  5958. BEGIN
  5959. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5960. Global.GetSymbolSegmentedName(symbol, name);
  5961. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5962. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5963. procedure.SetScope(moduleScope);
  5964. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5965. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5966. procedure.SetAccess(SyntaxTree.Hidden);
  5967. currentScope := procedureScope;
  5968. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5969. EmitEnter(section, -1,NIL,0,0,0);
  5970. RETURN section;
  5971. END OpenInitializer;
  5972. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5973. BEGIN
  5974. EmitLeave(section, 0, 0 );
  5975. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5976. section := prev;
  5977. END CloseInitializer;
  5978. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5979. VAR name: SyntaxTree.IdentifierString;
  5980. variable: SyntaxTree.Variable;
  5981. type: SyntaxTree.Type;
  5982. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5983. BEGIN
  5984. InitOperand(op,ModeReference);
  5985. op.op := fp;
  5986. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5987. Dereference(op, x, FALSE);
  5988. result := op;
  5989. Symbol(symbol, op);
  5990. END Field;
  5991. PROCEDURE Direction(direction: LONGINT): SET;
  5992. BEGIN
  5993. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  5994. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  5995. ELSE HALT(100);
  5996. END;
  5997. END Direction;
  5998. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  5999. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6000. BEGIN
  6001. Field(port, op);
  6002. ToMemory(op.op,addressType,0);
  6003. Emit(Push(-1, op.op));
  6004. ReleaseOperand(op);
  6005. Basic.GetString(modifier.identifier, name);
  6006. PushConstString(name);
  6007. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6008. ASSERT(SemanticChecker.IsStringType(value.type));
  6009. Designate(value, op);
  6010. Emit(Push(modifier.position, op.tag));
  6011. Emit(Push(modifier.position, op.op));
  6012. ReleaseOperand(op);
  6013. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6014. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6015. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6016. Evaluate(value, op);
  6017. Emit(Push(modifier.position, op.op));
  6018. ReleaseOperand(op);
  6019. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6020. ELSE
  6021. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6022. END;
  6023. END AddPortProperty;
  6024. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  6025. VAR modifier: SyntaxTree.Modifier;
  6026. BEGIN
  6027. modifier := variable.modifiers;
  6028. WHILE modifier # NIL DO
  6029. AddPortProperty(variable,modifier, modifier.expression);
  6030. modifier := modifier.nextModifier;
  6031. END;
  6032. END AddPortProperties;
  6033. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6034. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6035. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6036. PROCEDURE PushLens(type: SyntaxTree.Type);
  6037. BEGIN
  6038. IF type IS SyntaxTree.ArrayType THEN
  6039. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6040. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6041. Emit(Push(-1, op.op));
  6042. ReleaseOperand(op);
  6043. INC(dim);
  6044. ELSE
  6045. baseType := type;
  6046. END;
  6047. END PushLens;
  6048. BEGIN
  6049. (* cell *)
  6050. IF variable.type IS SyntaxTree.ArrayType THEN
  6051. type := variable.type;
  6052. dim := 0;
  6053. PushLens(type);
  6054. portType := baseType.resolved(SyntaxTree.PortType);
  6055. ELSE
  6056. portType := variable.type(SyntaxTree.PortType);
  6057. END;
  6058. PushSelfPointer();
  6059. (* port *)
  6060. Field(variable, op);
  6061. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6062. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6063. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6064. Designate(d, op);*)
  6065. Emit(Push(-1, op.op));
  6066. ReleaseOperand(op);
  6067. (* name *)
  6068. PushConstString(name);
  6069. (* inout *)
  6070. PushConstSet(Direction(portType.direction));
  6071. (* width *)
  6072. PushConstInteger(portType.sizeInBits);
  6073. IF variable.type IS SyntaxTree.PortType THEN
  6074. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6075. AddPortProperties(variable);
  6076. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6077. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6078. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6079. Emit(Add(position,reg, sp, size));
  6080. (* dim *)
  6081. PushConstInteger(dim);
  6082. (* len array *)
  6083. Emit(Push(position, reg));
  6084. ReleaseIntermediateOperand(reg);
  6085. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6086. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6087. Emit(Add(position, sp,sp, size));
  6088. END;
  6089. END Variable;
  6090. BEGIN
  6091. IF backend.cellsAreObjects THEN
  6092. variable := x.cellScope.firstVariable;
  6093. WHILE (variable # NIL) DO
  6094. type := variable.type.resolved;
  6095. WHILE (type IS SyntaxTree.ArrayType) DO
  6096. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6097. END;
  6098. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6099. (*!!! check port arrays !! *)
  6100. Global.GetSymbolName(variable,name);
  6101. Variable(name,variable);
  6102. END;
  6103. variable := variable.nextVariable;
  6104. END;
  6105. ELSE HALT(200)
  6106. END;
  6107. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6108. (*
  6109. x.typeDeclaration.GetName(componentName);
  6110. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6111. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6112. instanceType.SetDataMemorySize(dataMemorySize);
  6113. END;
  6114. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6115. instanceType.SetInstructionMemorySize(codeMemorySize)
  6116. END;
  6117. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6118. instanceType.AddCapability(ActiveCells.VectorCapability)
  6119. END;
  6120. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6121. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6122. END;
  6123. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6124. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6125. END;
  6126. AddDevices(instanceType, x);
  6127. *)
  6128. (*
  6129. IF x.isCellNet THEN
  6130. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6131. END;
  6132. *)
  6133. (*currentActiveCellsScope := instanceType;*)
  6134. (*
  6135. parameter := x.firstParameter;
  6136. portIndex := 0;
  6137. WHILE parameter # NIL DO
  6138. parameter.GetName(parameterName);
  6139. parameterType := parameter.type.resolved;
  6140. Parameter(parameterName, parameterType);
  6141. (*
  6142. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6143. ParameterArray(parameterType);
  6144. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6145. FOR i := 0 TO len-1 DO
  6146. COPY(parameterName,name);
  6147. AppendIndex(name,i);
  6148. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6149. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6150. INC(portIndex);
  6151. END;
  6152. ELSE
  6153. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6154. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6155. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6156. INC(portIndex);
  6157. END;
  6158. *)
  6159. parameter := parameter.nextParameter;
  6160. END;
  6161. *)
  6162. (*
  6163. Scope(x.cellScope);
  6164. currentActiveCellsScope := prevActiveCellsScope;
  6165. AddModules(instanceType,x.cellScope);
  6166. *)
  6167. END AddPorts;
  6168. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6169. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6170. BEGIN
  6171. Symbol(cell,op);
  6172. ToMemory(op.op,addressType,0);
  6173. Emit(Push(position,op.op));
  6174. ReleaseOperand(op);
  6175. Basic.GetString(property.name, name);
  6176. PushConstString(name);
  6177. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6178. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6179. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6180. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6181. Designate(d, op);
  6182. IF SemanticChecker.IsStringType(property.type) THEN
  6183. Emit(Push(-1, op.tag))
  6184. END;
  6185. Emit(Push(-1, op.op));
  6186. ReleaseOperand(op);
  6187. END;
  6188. IF SemanticChecker.IsStringType(property.type) THEN
  6189. ASSERT(SemanticChecker.IsStringType(value.type));
  6190. Designate(value, op);
  6191. Emit(Push(property.position, op.tag));
  6192. Emit(Push(property.position, op.op));
  6193. ReleaseOperand(op);
  6194. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6195. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6196. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6197. Evaluate(value, op);
  6198. Emit(Push(property.position, op.op));
  6199. ReleaseOperand(op);
  6200. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6201. ELSE
  6202. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6203. END;
  6204. END AddProperty;
  6205. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6206. VAR symbol: SyntaxTree.Symbol;
  6207. BEGIN
  6208. WHILE modifier # NIL DO
  6209. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6210. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6211. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6212. ELSE
  6213. (*! move this check to checker *)
  6214. Error(modifier.position, "undefined property");
  6215. END;
  6216. modifier := modifier.nextModifier;
  6217. END;
  6218. END AddModifiers;
  6219. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6220. VAR last: SyntaxTree.Modifier;
  6221. BEGIN
  6222. IF to = NIL THEN
  6223. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6224. ELSE
  6225. last := to;
  6226. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6227. last := last.nextModifier;
  6228. END;
  6229. IF last.identifier # this.identifier THEN
  6230. ASSERT(last.nextModifier = NIL);
  6231. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6232. END;
  6233. END;
  6234. END AppendModifier;
  6235. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6236. BEGIN
  6237. WHILE this # NIL DO
  6238. AppendModifier(to, this);
  6239. this := this.nextModifier;
  6240. END;
  6241. END AppendModifiers;
  6242. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6243. VAR op: Operand;
  6244. BEGIN
  6245. Evaluate(p, op);
  6246. Emit(Push(p.position, op.op));
  6247. ReleaseOperand(op);
  6248. (*
  6249. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6250. p := p(SyntaxTree.Designator).left;
  6251. END;
  6252. IF p # NIL THEN
  6253. Evaluate(p, op);
  6254. Emit(Push(p.position, op.op));
  6255. ReleaseOperand(op);
  6256. ELSE
  6257. Emit(Push(-1, nil));
  6258. END;
  6259. *)
  6260. END PushPort;
  6261. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6262. VAR
  6263. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6264. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6265. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6266. tmp:IntermediateCode.Operand;
  6267. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6268. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6269. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6270. dest: IntermediateCode.Operand;
  6271. staticLength: LONGINT; itype: IntermediateCode.Type;
  6272. convert,isTensor: BOOLEAN;
  6273. recordType: SyntaxTree.RecordType;
  6274. baseType: SyntaxTree.Type;
  6275. flags: SET;
  6276. left: SyntaxTree.Expression;
  6277. call: SyntaxTree.Designator;
  6278. procedure: SyntaxTree.Procedure;
  6279. temporaryVariable: SyntaxTree.Variable;
  6280. dummy: IntermediateCode.Operand;
  6281. customBuiltin: SyntaxTree.CustomBuiltin;
  6282. isVarPar: ARRAY 3 OF BOOLEAN;
  6283. callsection: Sections.Section;
  6284. segmentedName: Basic.SegmentedName;
  6285. needsTrace: BOOLEAN;
  6286. n: ARRAY 256 OF CHAR;
  6287. modifier: SyntaxTree.Modifier;
  6288. previous, init: IntermediateCode.Section;
  6289. prevScope: SyntaxTree.Scope;
  6290. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6291. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6292. priority: IntermediateCode.Operand;
  6293. op,callop: Operand;
  6294. BEGIN
  6295. IF type = NIL THEN RETURN END;
  6296. type := type.resolved;
  6297. IF type IS SyntaxTree.PointerType THEN
  6298. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6299. END;
  6300. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6301. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6302. recordScope := type(SyntaxTree.RecordType).recordScope;
  6303. IF recordScope.bodyProcedure # NIL THEN
  6304. procedure := recordScope.bodyProcedure;
  6305. body := procedure.procedureScope.body;
  6306. Emit(Push(position,self));
  6307. IF body.isActive THEN
  6308. StaticCallOperand(callop,procedure);
  6309. Emit(Push(position,callop.op));
  6310. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6311. Convert(priority,sizeType);
  6312. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6313. END;
  6314. Emit(Push(position,priority));
  6315. ReleaseIntermediateOperand(priority);
  6316. IF backend.cooperative THEN
  6317. Emit(Push(position,self));
  6318. CallThis(position,"Activities","Create",3)
  6319. ELSE
  6320. flags := 0;
  6321. IF body.isSafe THEN
  6322. flags := 1;
  6323. END;
  6324. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6325. Emit(Push(position,self));
  6326. CallThis(position,"Objects","CreateProcess",4)
  6327. END;
  6328. ELSE
  6329. Emit(Push(position,self));
  6330. StaticCallOperand(callop,procedure);
  6331. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6332. END;
  6333. Emit(Pop(position,self));
  6334. END;
  6335. END CallBodies;
  6336. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6337. BEGIN
  6338. actualType := actualType.resolved;
  6339. IF actualType IS SyntaxTree.StringType THEN
  6340. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6341. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6342. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6343. ELSE
  6344. tmp := op.tag;
  6345. IntermediateCode.MakeMemory(tmp,addressType);
  6346. Emit(Push(position,tmp));
  6347. END;
  6348. Emit(Push(position,op.op))
  6349. END PushString;
  6350. PROCEDURE PushTD(type: SyntaxTree.Type);
  6351. VAR op: IntermediateCode.Operand;
  6352. BEGIN
  6353. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6354. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6355. ELSE
  6356. IF type.resolved IS SyntaxTree.PointerType THEN
  6357. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6358. END;
  6359. op := TypeDescriptorAdr(type.resolved);
  6360. IF ~newObjectFile THEN
  6361. IntermediateCode.MakeMemory(op,addressType);
  6362. END;
  6363. Emit(Push(position,op));
  6364. END
  6365. END PushTD;
  6366. BEGIN
  6367. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6368. dest := destination; destination := emptyOperand;
  6369. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6370. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6371. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6372. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6373. CASE x.id OF
  6374. (* ---- COPY ----- *)
  6375. |Global.Copy:
  6376. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6377. (* ---- EXCL, INCL----- *)
  6378. |Global.Excl,Global.Incl:
  6379. Evaluate(p0,s0);
  6380. Evaluate(p1,s1);
  6381. Convert(s1.op,setType);
  6382. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6383. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6384. END;
  6385. ReuseCopy(res,s0.op);
  6386. ReleaseOperand(s0);
  6387. Reuse1(tmp,s1.op);
  6388. ReleaseOperand(s1);
  6389. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6390. IF x.id = Global.Excl THEN
  6391. Emit(Not(position,tmp,tmp));
  6392. Emit(And(position,res,res,tmp));
  6393. ELSE
  6394. Emit(Or(position,res,res,tmp));
  6395. END;
  6396. ReleaseIntermediateOperand(tmp);
  6397. Designate(p0,s0);
  6398. ToMemory(s0.op,setType,0);
  6399. Emit(Mov(position,s0.op,res));
  6400. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6401. (* ---- DISPOSE ----- *)
  6402. |Global.Dispose:
  6403. Designate(p0,s0);
  6404. Emit(Push(position,s0.op));
  6405. ReleaseOperand(s0);
  6406. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6407. (* ---- GETPROCEDURE ----- *)
  6408. |Global.GetProcedure:
  6409. Designate(p0,s0);
  6410. PushString(s0,p0.type);
  6411. Designate(p1,s1);
  6412. PushString(s1,p1.type);
  6413. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6414. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6415. ELSE PushTD(procedureType.firstParameter.type)
  6416. END;
  6417. PushTD(procedureType.returnType);
  6418. Designate(p2,s2);
  6419. Emit(Push(position,s2.op));
  6420. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6421. CallThis(position,"Modules","GetProcedure", 7);
  6422. (* ---- ASH, LSH, ROT ----- *)
  6423. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6424. Evaluate(p0,s0);
  6425. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6426. (* make unsigned arguments in order to produced a logical shift *)
  6427. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6428. convert:= TRUE;
  6429. itype := s0.op.type;
  6430. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6431. Convert(s0.op,itype);
  6432. ELSE
  6433. convert := FALSE;
  6434. END;
  6435. END;
  6436. Evaluate(p1,s1);
  6437. IF IsIntegerConstant(p1,hint) THEN
  6438. ReuseCopy(reg,s0.op);
  6439. IF hint > 0 THEN
  6440. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6441. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6442. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6443. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6444. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6445. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6446. END;
  6447. ELSIF hint < 0 THEN
  6448. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6449. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6450. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6451. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6452. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6453. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6454. END;
  6455. END;
  6456. ReleaseOperand(s0); ReleaseOperand(s1);
  6457. ELSE
  6458. exit := NewLabel();
  6459. end := NewLabel();
  6460. ReuseCopy(reg,s0.op);
  6461. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6462. Reuse1(tmp,s1.op);
  6463. Emit(Neg(position,tmp,s1.op));
  6464. Convert(tmp,s1.op.type);
  6465. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6466. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6467. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6468. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6469. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6470. END;
  6471. ReleaseIntermediateOperand(tmp);
  6472. BrL(end);
  6473. SetLabel(exit);
  6474. ReuseCopy(tmp,s1.op);
  6475. Convert(tmp,s1.op.type);
  6476. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6477. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6478. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6479. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6480. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6481. END;
  6482. ReleaseIntermediateOperand(tmp);
  6483. SetLabel(end);
  6484. ReleaseOperand(s0); ReleaseOperand(s1);
  6485. END;
  6486. InitOperand(result,ModeValue);
  6487. IF convert THEN
  6488. itype := reg.type;
  6489. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6490. Convert(reg,itype);
  6491. END;
  6492. result.op := reg;
  6493. (* ---- CAP ----- *)
  6494. |Global.Cap:
  6495. Evaluate(p0,result);
  6496. ReuseCopy(reg,result.op);
  6497. ReleaseIntermediateOperand(result.op);
  6498. ignore := NewLabel();
  6499. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6500. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6501. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6502. SetLabel(ignore);
  6503. result.op := reg;
  6504. (* ---- CHR ----- *)
  6505. |Global.Chr, Global.Chr32:
  6506. Evaluate(p0,result);
  6507. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6508. |Global.Entier, Global.EntierH:
  6509. Evaluate(p0,result);
  6510. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6511. (* ---- MIN and MAX ----- *)
  6512. |Global.Max,Global.Min:
  6513. Evaluate(p0,s0);
  6514. Evaluate(p1,s1);
  6515. Reuse2(res,s0.op,s1.op);
  6516. else := NewLabel();
  6517. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6518. ELSE BrltL(else,s1.op,s0.op) END;
  6519. Emit(Mov(position,res,s0.op));
  6520. ReleaseOperand(s0);
  6521. end := NewLabel();
  6522. BrL(end);
  6523. SetLabel(else);
  6524. Emit(MovReplace(position,res,s1.op));
  6525. SetLabel(end);
  6526. ReleaseOperand(s1);
  6527. InitOperand(result,ModeValue);
  6528. result.op := res;
  6529. (* ---- ODD ----- *)
  6530. |Global.Odd:
  6531. IF ~conditional THEN
  6532. ConditionToValue(x)
  6533. ELSE
  6534. Evaluate(p0,result);
  6535. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6536. Reuse1(res,result.op);
  6537. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6538. ReleaseIntermediateOperand(result.op);
  6539. result.op := res;
  6540. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6541. ReleaseOperand(result);
  6542. BrL(falseLabel);
  6543. END;
  6544. (* ---- ORD ----- *)
  6545. |Global.Ord, Global.Ord32:
  6546. Evaluate(p0,result);
  6547. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6548. (* ---- SHORT, LONG ----- *)
  6549. |Global.Short, Global.Long:
  6550. Evaluate(p0,result);
  6551. IF x.type IS SyntaxTree.ComplexType THEN
  6552. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6553. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6554. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6555. ELSE
  6556. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6557. END
  6558. (* ---- HALT, SYSTEM.HALT----- *)
  6559. |Global.Halt, Global.systemHalt:
  6560. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6561. EmitTrap (position, val);
  6562. (* ---- ASSERT ----- *)
  6563. |Global.Assert:
  6564. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6565. trueL := NewLabel();
  6566. falseL := NewLabel();
  6567. Condition(p0,trueL,falseL);
  6568. IF p1 = NIL THEN val := AssertTrap
  6569. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6570. END;
  6571. SetLabel(falseL);
  6572. EmitTrap(position,val);
  6573. SetLabel(trueL);
  6574. END;
  6575. (*
  6576. Emit(TrapC(result.op,val);
  6577. *)
  6578. (* ---- INC, DEC----- *)
  6579. |Global.Inc,Global.Dec:
  6580. Expression(p0); adr := result.op;
  6581. LoadValue(result,p0.type); l := result;
  6582. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6583. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6584. END;
  6585. IF x.id = Global.Inc THEN
  6586. Emit(Add(position,l.op,l.op,r.op));
  6587. ELSE
  6588. Emit(Sub(position,l.op,l.op,r.op));
  6589. END;
  6590. ReleaseOperand(l); ReleaseOperand(r);
  6591. (* ---- LEN ----- *)
  6592. |Global.Len: (* dynamic length, static length done by checker *)
  6593. Designate(p0,operand);
  6594. IF p1 = NIL THEN
  6595. InitOperand(l,ModeValue);
  6596. l.op := IntermediateCode.Immediate(int32,0);
  6597. ELSE
  6598. Evaluate(p1,l);
  6599. END;
  6600. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6601. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6602. Dereference(operand, p0.type.resolved, FALSE);
  6603. END;
  6604. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6605. ReleaseOperand(operand); ReleaseOperand(l);
  6606. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6607. ASSERT(p1 # NIL);
  6608. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6609. Dereference(operand,p0.type.resolved,FALSE);
  6610. END;
  6611. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6612. ReleaseOperand(operand); ReleaseOperand(l);
  6613. ELSE HALT(100);
  6614. END;
  6615. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6616. (* ---- FIRST ---- *)
  6617. |Global.First:
  6618. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6619. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6620. ELSE
  6621. Designate(p0, result)
  6622. END
  6623. (* ---- LAST ---- *)
  6624. |Global.Last:
  6625. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6626. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6627. ELSE
  6628. Designate(p0, result);
  6629. (* make sure result.op is a register *)
  6630. tmp := result.op;
  6631. ReuseCopy(result.op, result.op);
  6632. ReleaseIntermediateOperand(tmp);
  6633. (* add offset to result.op *)
  6634. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6635. END
  6636. (* ---- STEP ---- *)
  6637. |Global.Step:
  6638. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6639. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6640. ELSE
  6641. Designate(p0, result);
  6642. (* make sure result.op is a register *)
  6643. tmp := result.op;
  6644. ReuseCopy(result.op, result.op);
  6645. ReleaseIntermediateOperand(tmp);
  6646. (* add offset to result.op *)
  6647. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6648. END
  6649. (* ---- RE ---- *)
  6650. |Global.Re:
  6651. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6652. Designate(p0, result)
  6653. ELSE
  6654. Evaluate(p0, result)
  6655. END
  6656. (* ---- IM ---- *)
  6657. |Global.Im:
  6658. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6659. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6660. Designate(p0, result);
  6661. (* make sure result.op is a register *)
  6662. tmp := result.op;
  6663. ReuseCopy(result.op, result.op);
  6664. ReleaseIntermediateOperand(tmp);
  6665. (* add offset to result.op *)
  6666. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6667. (* ---- ABS ----- *)
  6668. |Global.Abs:
  6669. Evaluate(p0,operand);
  6670. type := p0.type.resolved;
  6671. InitOperand(result,ModeValue);
  6672. Reuse1a(result.op,operand.op,dest);
  6673. Emit(Abs(position,result.op,operand.op));
  6674. ReleaseOperand(operand);
  6675. (* ---- WAIT ----- *)
  6676. |Global.Wait:
  6677. Evaluate(p0,operand);
  6678. Emit(Push(position,operand.op));
  6679. ReleaseOperand(operand);
  6680. CallThis(position,"Activities","Wait", 1);
  6681. (* ---- NEW ----- *)
  6682. |Global.New:
  6683. (*! the following code is only correct for "standard" Oberon calling convention *)
  6684. type := p0.type.resolved;
  6685. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6686. THEN
  6687. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6688. IF backend.cooperative THEN
  6689. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6690. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6691. IF recordType.isObject THEN
  6692. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6693. IF recordType.IsActive() THEN
  6694. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6695. END;
  6696. IF recordType.IsProtected() THEN
  6697. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6698. END;
  6699. ELSE
  6700. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6701. END;
  6702. END;
  6703. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6704. CallThis(position,"Runtime","New", 1);
  6705. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6706. Emit(Result(position, pointer));
  6707. exit := NewLabel();
  6708. BreqL(exit,pointer,nil);
  6709. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6710. GetRecordTypeName (recordType,name);
  6711. IF ~recordType.isObject THEN
  6712. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6713. END;
  6714. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6715. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6716. IF recordType.isObject THEN
  6717. IF recordType.IsProtected() THEN
  6718. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6719. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6720. END;
  6721. IF recordType.IsActive() THEN
  6722. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6723. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6724. END;
  6725. END;
  6726. END;
  6727. (* initialize fields *)
  6728. IF type(SyntaxTree.PointerType).isPlain THEN
  6729. size := 0;
  6730. ELSIF recordType.isObject THEN
  6731. size := BaseObjectTypeSize;
  6732. ELSE
  6733. size := BaseRecordTypeSize;
  6734. END;
  6735. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6736. (* call initializer *)
  6737. constructor := GetConstructor(recordType);
  6738. IF constructor # NIL THEN
  6739. (*! should be unified with ProcedureCallDesignator *)
  6740. Emit(Push(position,pointer));
  6741. ReleaseIntermediateOperand(pointer);
  6742. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6743. FOR i := 1 TO x.parameters.Length()-1 DO
  6744. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6745. formalParameter := formalParameter.nextParameter;
  6746. END;
  6747. (* static call of the constructor *)
  6748. GetCodeSectionNameForSymbol(constructor,name);
  6749. ASSERT(~constructor.isInline);
  6750. IF constructor.scope.ownerModule # module.module THEN
  6751. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6752. ELSE
  6753. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6754. END;
  6755. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6756. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6757. Emit(Pop(position,pointer));
  6758. END;
  6759. (* call bodies *)
  6760. CallBodies(pointer,p0.type);
  6761. SetLabel(exit);
  6762. needsTrace := p0.NeedsTrace();
  6763. IF needsTrace THEN ModifyAssignments(true) END;
  6764. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6765. Emit(Push(position, pointer));
  6766. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  6767. Emit(Pop(position, pointer));
  6768. END;
  6769. Designate(p0,l);
  6770. IF needsTrace THEN
  6771. CallAssignPointer(l.op, pointer);
  6772. ELSE
  6773. ToMemory(l.op,addressType,0);
  6774. Emit(Mov(position,l.op,pointer));
  6775. END;
  6776. ReleaseIntermediateOperand(pointer);
  6777. ReleaseOperand(l);
  6778. IF needsTrace THEN ModifyAssignments(false) END;
  6779. ELSE
  6780. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6781. IF temporaryVariable # NIL THEN
  6782. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6783. ELSE
  6784. Designate(p0,l);
  6785. END;
  6786. (* l.op contains address of pointer to record *)
  6787. Emit(Push(position,l.op)); (* address for use after syscall *)
  6788. Emit(Push(position,l.op));
  6789. ReleaseOperand(l);
  6790. (* push type descriptor *)
  6791. reg := TypeDescriptorAdr(recordType);
  6792. IF ~newObjectFile THEN
  6793. IntermediateCode.MakeMemory(reg,addressType);
  6794. END;
  6795. Emit(Push(position,reg));
  6796. ReleaseIntermediateOperand(reg);
  6797. (* push realtime flag *)
  6798. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6799. ELSE Emit(Push(position,false));
  6800. END;
  6801. CallThis(position,"Heaps","NewRec", 3);
  6802. (* check allocation success, if not successful then do not call initializers and bodies *)
  6803. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6804. Emit(Pop(position,pointer));
  6805. MakeMemory(reg,pointer,addressType,0);
  6806. ReleaseIntermediateOperand(pointer);
  6807. pointer := reg;
  6808. exit := NewLabel();
  6809. BreqL(exit,pointer,nil);
  6810. Emit(Push(position,pointer));
  6811. (* initialize fields *)
  6812. InitFields(recordType, pointer,0);
  6813. (* call initializer *)
  6814. constructor := GetConstructor(recordType);
  6815. IF constructor # NIL THEN
  6816. (*! should be unified with ProcedureCallDesignator *)
  6817. Emit(Push(position,pointer));
  6818. ReleaseIntermediateOperand(pointer);
  6819. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6820. FOR i := 1 TO x.parameters.Length()-1 DO
  6821. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6822. formalParameter := formalParameter.nextParameter;
  6823. END;
  6824. (* static call of the constructor *)
  6825. GetCodeSectionNameForSymbol(constructor,name);
  6826. ASSERT(~constructor.isInline);
  6827. IF constructor.scope.ownerModule # module.module THEN
  6828. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6829. ELSE
  6830. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6831. END;
  6832. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6833. ELSE
  6834. ReleaseIntermediateOperand(pointer);
  6835. END;
  6836. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6837. Emit(Pop(position,pointer));
  6838. IF temporaryVariable # NIL THEN
  6839. Designate(p0,l);
  6840. ToMemory(l.op,addressType,0);
  6841. Emit(Mov(position,l.op,pointer));
  6842. ReleaseOperand(l);
  6843. result.tag := emptyOperand;
  6844. END;
  6845. (* call bodies *)
  6846. CallBodies(pointer,p0.type);
  6847. ReleaseIntermediateOperand(pointer);
  6848. IF temporaryVariable # NIL THEN
  6849. end := NewLabel();
  6850. BrL(end);
  6851. SetLabel(exit);
  6852. Designate(p0,l);
  6853. ToMemory(l.op,addressType,0);
  6854. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6855. ReleaseOperand(l);
  6856. SetLabel(end);
  6857. ELSE
  6858. SetLabel(exit);
  6859. END;
  6860. END;
  6861. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6862. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6863. dim := 0;
  6864. IF p1 # NIL THEN
  6865. FOR i := 1 TO x.parameters.Length()-1 DO
  6866. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6867. parameter := x.parameters.GetExpression(i);
  6868. Evaluate(parameter,r);
  6869. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6870. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6871. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6872. END;
  6873. Emit(Push(position,r.op));
  6874. IF i=1 THEN
  6875. ReuseCopy(reg,r.op);
  6876. ELSE
  6877. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6878. END;
  6879. ReleaseOperand(r);
  6880. INC(dim);
  6881. END;
  6882. Convert(reg,addressType);
  6883. ELSE
  6884. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6885. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6886. END;
  6887. openDim := dim;
  6888. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6889. IF backend.cooperative THEN
  6890. size := ToMemoryUnits(system,system.SizeOf(type));
  6891. WHILE type IS SyntaxTree.ArrayType DO
  6892. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6893. END;
  6894. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6895. IF (size # 1) THEN
  6896. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6897. END;
  6898. Emit(Push(position,reg));
  6899. size := ToMemoryUnits(system,system.SizeOf(type));
  6900. IF (size # 1) THEN
  6901. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6902. END;
  6903. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6904. Emit(Push(position,reg));
  6905. ReleaseIntermediateOperand(reg);
  6906. CallThis(position,"Runtime","New", 1);
  6907. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6908. Emit(Result(position, pointer));
  6909. exit := NewLabel();
  6910. else := NewLabel();
  6911. BreqL(else,pointer,nil);
  6912. IF ~type.hasPointers THEN
  6913. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6914. ELSIF type IS SyntaxTree.RecordType THEN
  6915. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6916. ELSIF type IS SyntaxTree.ProcedureType THEN
  6917. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6918. ELSE
  6919. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6920. END;
  6921. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6922. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6923. 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))));
  6924. IF type IS SyntaxTree.RecordType THEN
  6925. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6926. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6927. ELSE
  6928. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6929. END;
  6930. i := openDim;
  6931. WHILE i > 0 DO
  6932. DEC (i);
  6933. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6934. END;
  6935. needsTrace := p0.NeedsTrace();
  6936. IF needsTrace THEN ModifyAssignments(true) END;
  6937. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6938. Emit(Push(position, pointer));
  6939. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  6940. Emit(Pop(position, pointer));
  6941. END;
  6942. Designate(p0,l);
  6943. IF needsTrace THEN
  6944. CallAssignPointer(l.op, pointer);
  6945. ModifyAssignments(false);
  6946. ELSE
  6947. ToMemory(l.op,addressType,0);
  6948. Emit(Mov(position,l.op,pointer));
  6949. END;
  6950. ReleaseIntermediateOperand(pointer);
  6951. ReleaseOperand(l);
  6952. BrL(exit);
  6953. SetLabel(else);
  6954. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6955. Designate(p0,l);
  6956. IF needsTrace THEN
  6957. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6958. ELSE
  6959. ToMemory(l.op,addressType,0);
  6960. Emit(Mov(position,l.op,pointer));
  6961. END;
  6962. ReleaseOperand(l);
  6963. SetLabel(exit);
  6964. ELSE
  6965. (*! the following code is only correct for "standard" Oberon calling convention *)
  6966. IF SemanticChecker.ContainsPointer(type) THEN
  6967. IF type IS SyntaxTree.ArrayType THEN
  6968. staticLength := 1;
  6969. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6970. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6971. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6972. END;
  6973. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6974. Emit(Mul(position,reg,reg,tmp));
  6975. END;
  6976. Designate(p0,l);
  6977. IF openDim > 0 THEN
  6978. Emit(Push(position,l.op)); (* address for use after syscall *)
  6979. END;
  6980. Emit(Push(position,l.op)); (* address *)
  6981. ReleaseOperand(l);
  6982. tmp := TypeDescriptorAdr(type);
  6983. IF ~newObjectFile THEN
  6984. IntermediateCode.MakeMemory(tmp,addressType);
  6985. END;
  6986. Emit(Push(position,tmp)); (* type descriptor *)
  6987. ReleaseIntermediateOperand(tmp);
  6988. Emit(Push(position,reg)); (* number Elements *)
  6989. ReleaseIntermediateOperand(reg);
  6990. tmp := IntermediateCode.Immediate(addressType,dim);
  6991. Emit(Push(position,tmp)); (* dimensions *)
  6992. (* push realtime flag *)
  6993. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6994. ELSE Emit(Push(position,false));
  6995. END;
  6996. CallThis(position,"Heaps","NewArr",5)
  6997. ELSE
  6998. size := ToMemoryUnits(system,system.SizeOf(type));
  6999. IF (size # 1) THEN
  7000. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7001. END;
  7002. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7003. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7004. Emit(Add(position,reg,reg,tmp));
  7005. Designate(p0,l);
  7006. IF openDim >0 THEN
  7007. Emit(Push(position,l.op)); (* address for use after syscall *)
  7008. END;
  7009. Emit(Push(position,l.op)); (* address for syscall *)
  7010. ReleaseOperand(l); (* pointer address *)
  7011. Emit(Push(position,reg)); (* size *)
  7012. ReleaseIntermediateOperand(reg);
  7013. (* push realtime flag *)
  7014. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7015. ELSE Emit(Push(position,false));
  7016. END;
  7017. CallThis(position,"Heaps","NewSys", 3)
  7018. END;
  7019. IF openDim > 0 THEN
  7020. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7021. Emit(Pop(position,adr));
  7022. ToMemory(adr,addressType,0);
  7023. ReuseCopy(tmp,adr);
  7024. ReleaseIntermediateOperand(adr);
  7025. adr := tmp;
  7026. else := NewLabel();
  7027. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7028. i := openDim-1;
  7029. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7030. WHILE (i >= 0) DO
  7031. Emit(Pop(position,reg));
  7032. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7033. Emit(Mov(position,res,reg));
  7034. DEC(i);
  7035. END;
  7036. ReleaseIntermediateOperand(adr);
  7037. ReleaseIntermediateOperand(reg);
  7038. exit := NewLabel();
  7039. BrL(exit);
  7040. SetLabel(else);
  7041. (* else part: array could not be allocated *)
  7042. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7043. Emit(Add(position,sp,sp,tmp));
  7044. SetLabel(exit);
  7045. END;
  7046. END;
  7047. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7048. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7049. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7050. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7051. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7052. left.SetType(procedure.type);
  7053. formalParameter := procedureType.firstParameter;
  7054. (* push array to allocate *)
  7055. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7056. formalParameter :=formalParameter.nextParameter;
  7057. (* push length array *)
  7058. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7059. (* push size *)
  7060. type := t0;
  7061. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7062. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7063. END;
  7064. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7065. Emit(Push(position,tmp));
  7066. (* *)
  7067. IF SemanticChecker.ContainsPointer(type) THEN
  7068. tmp := TypeDescriptorAdr(type);
  7069. IF ~newObjectFile THEN
  7070. IntermediateCode.MakeMemory(tmp,addressType);
  7071. END;
  7072. ELSE
  7073. tmp := IntermediateCode.Immediate(addressType, 0);
  7074. END;
  7075. Emit(Push(position,tmp)); (* type descriptor *)
  7076. StaticCallOperand(result,procedure);
  7077. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7078. ReleaseOperand(result);
  7079. END;
  7080. (*
  7081. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7082. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7083. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7084. *)
  7085. ELSE
  7086. dim := 0;
  7087. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7088. (* generate geometry descriptor *)
  7089. Designate(p0,l);
  7090. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7091. ReleaseOperand(l);
  7092. isTensor := TRUE;
  7093. ELSE
  7094. isTensor := FALSE;
  7095. END;
  7096. FOR i := 1 TO x.parameters.Length()-1 DO
  7097. IF ~isTensor THEN
  7098. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7099. END;
  7100. parameter := x.parameters.GetExpression(i);
  7101. Evaluate(parameter,r);
  7102. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7103. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7104. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7105. END;
  7106. Emit(Push(position,r.op));
  7107. IF i=1 THEN
  7108. ReuseCopy(reg,r.op);
  7109. ELSE
  7110. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7111. END;
  7112. ReleaseOperand(r);
  7113. INC(dim);
  7114. END;
  7115. Convert(reg,addressType);
  7116. openDim := dim;
  7117. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7118. (*! the following code is only correct for "standard" Oberon calling convention *)
  7119. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7120. t := type;
  7121. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7122. staticLength := 1;
  7123. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7124. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7125. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7126. END;
  7127. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7128. Emit(Mul(position,reg,reg,tmp));
  7129. END;
  7130. Designate(p0,l);
  7131. IF isTensor THEN
  7132. Dereference(l,type,FALSE);
  7133. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7134. END;
  7135. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7136. Emit(Push(position,l.tag)); (* address *)
  7137. ReleaseOperand(l);
  7138. tmp := TypeDescriptorAdr(t);
  7139. IF ~newObjectFile THEN
  7140. IntermediateCode.MakeMemory(tmp,addressType);
  7141. END;
  7142. Emit(Push(position,tmp)); (* type descriptor *)
  7143. ReleaseIntermediateOperand(tmp);
  7144. Emit(Push(position,reg)); (* number Elements *)
  7145. ReleaseIntermediateOperand(reg);
  7146. tmp := IntermediateCode.Immediate(addressType,0);
  7147. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7148. (* push realtime flag: false by default *)
  7149. Emit(Push(position,false));
  7150. CallThis(position,"Heaps","NewArr",5);
  7151. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7152. Emit(Pop(position,adr));
  7153. GetMathArrayField(tmp,adr,MathPtrOffset);
  7154. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7155. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7156. PutMathArrayField(adr,reg,MathAdrOffset);
  7157. ReleaseIntermediateOperand(tmp);
  7158. ReleaseIntermediateOperand(reg);
  7159. ELSE
  7160. IF isTensor THEN
  7161. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7162. ELSE
  7163. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7164. END;
  7165. IF (size # 1) THEN
  7166. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7167. END;
  7168. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7169. Emit(Add(position,reg,reg,tmp));
  7170. Designate(p0,l);
  7171. IF isTensor THEN
  7172. Dereference(l,type,FALSE);
  7173. END;
  7174. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7175. Emit(Push(position,l.tag)); (* address for syscall *)
  7176. ReleaseOperand(l); (* pointer address *)
  7177. Emit(Push(position,reg)); (* size *)
  7178. ReleaseIntermediateOperand(reg);
  7179. (* push realtime flag: false by default *)
  7180. Emit(Push(position,false));
  7181. CallThis(position,"Heaps","NewSys",3);
  7182. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7183. Emit(Pop(position,adr));
  7184. GetMathArrayField(tmp,adr,MathPtrOffset);
  7185. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7186. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7187. PutMathArrayField(adr,reg,MathAdrOffset);
  7188. ReleaseIntermediateOperand(tmp);
  7189. ReleaseIntermediateOperand(reg);
  7190. END;
  7191. flags := {};
  7192. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7193. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7194. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7195. PutMathArrayField(adr,tmp,MathDimOffset);
  7196. else := NewLabel();
  7197. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7198. i := openDim-1;
  7199. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7200. IF isTensor THEN
  7201. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7202. ELSE
  7203. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7204. END;
  7205. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7206. WHILE (i >= 0) DO
  7207. Emit(Pop(position,reg));
  7208. PutMathArrayLength(adr,reg,i);
  7209. PutMathArrayIncrement(adr,tmp,i);
  7210. IF i > 0 THEN
  7211. IF i=openDim-1 THEN
  7212. ReuseCopy(tmp,tmp);
  7213. END;
  7214. Emit(Mul(position,tmp,tmp,reg));
  7215. END;
  7216. DEC(i);
  7217. END;
  7218. ReleaseIntermediateOperand(adr);
  7219. ReleaseIntermediateOperand(reg);
  7220. ReleaseIntermediateOperand(tmp);
  7221. exit := NewLabel();
  7222. BrL(exit);
  7223. SetLabel(else);
  7224. (* else part: array could not be allocated *)
  7225. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7226. Emit(Add(position,sp,sp,tmp));
  7227. SetLabel(exit);
  7228. END;
  7229. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7230. THEN
  7231. IF ~backend.cellsAreObjects THEN RETURN END;
  7232. IF InCellScope(currentScope) THEN
  7233. PushSelfPointer()
  7234. ELSE
  7235. Emit(Push(position, nil));
  7236. END;
  7237. (* push temp address *)
  7238. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7239. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7240. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7241. (* l.op contains address of pointer to record *)
  7242. Emit(Push(position,l.op)); (* address for use after syscall *)
  7243. ReleaseOperand(l);
  7244. (* push type descriptor *)
  7245. reg := TypeDescriptorAdr(baseType);
  7246. IF ~newObjectFile THEN
  7247. IntermediateCode.MakeMemory(reg,addressType);
  7248. END;
  7249. Emit(Push(position,reg));
  7250. ReleaseIntermediateOperand(reg);
  7251. (* push name *)
  7252. (*Global.GetSymbolName(p0, n);*)
  7253. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7254. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7255. ELSE
  7256. Global.GetModuleName(module.module, n);
  7257. END;
  7258. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7259. (*type.typeDeclaration.GetName(n);*)
  7260. PushConstString(n);
  7261. (* push cellnet boolean *)
  7262. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7263. (* push engine boolean *)
  7264. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7265. (* allocate *)
  7266. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7267. (* add capabilities *)
  7268. modifier := p0(SyntaxTree.Designator).modifiers;
  7269. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7270. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7271. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7272. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7273. END;
  7274. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7275. (*
  7276. modifier := baseType(SyntaxTree.CellType).modifiers;
  7277. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7278. modifier := p0(SyntaxTree.Designator).modifiers;
  7279. *)
  7280. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7281. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7282. (* l.op contains address of pointer to record *)
  7283. ToMemory(l.op,addressType,0);
  7284. (* l.op contains value of pointer to record *)
  7285. Emit(Push(position,l.op)); (* address for use after syscall *)
  7286. ReleaseOperand(l);
  7287. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7288. prevScope := currentScope;
  7289. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7290. previous := section;
  7291. section := init;
  7292. (* add ports *)
  7293. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7294. CloseInitializer(previous);
  7295. currentScope := prevScope;
  7296. Symbol(temporaryVariable,l);
  7297. ToMemory(l.op,addressType,0);
  7298. Emit(Push(position,l.op));
  7299. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7300. (*
  7301. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7302. IF constructor # NIL THEN
  7303. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7304. FOR i := 1 TO x.parameters.Length()-1 DO
  7305. p := x.parameters.GetExpression(i);
  7306. Global.GetSymbolName(parameter,name);
  7307. Evaluate(p, value);
  7308. ASSERT(value.type # NIL);
  7309. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7310. par := instance.AddParameter(name);
  7311. par.SetInteger(value.integer);
  7312. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7313. par := instance.AddParameter(name);
  7314. par.SetBoolean(value.boolean);
  7315. ELSE Error(x.position,NotYetImplemented)
  7316. END;
  7317. parameter := parameter.nextParameter
  7318. END;
  7319. END;
  7320. *)
  7321. (* call initializer *)
  7322. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7323. IF constructor # NIL THEN
  7324. (*! should be unified with ProcedureCallDesignator *)
  7325. IF backend.cellsAreObjects THEN
  7326. Symbol(temporaryVariable,l);
  7327. ToMemory(l.op,addressType,0);
  7328. Emit(Push(position,l.op));
  7329. ReleaseOperand(l);
  7330. END;
  7331. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7332. FOR i := 1 TO x.parameters.Length()-1 DO
  7333. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7334. formalParameter := formalParameter.nextParameter;
  7335. END;
  7336. (* static call of the constructor *)
  7337. Global.GetSymbolSegmentedName(constructor,name);
  7338. ASSERT(~constructor.isInline);
  7339. IF constructor.scope.ownerModule # module.module THEN
  7340. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7341. ELSE
  7342. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7343. END;
  7344. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7345. (*ELSE
  7346. ReleaseIntermediateOperand(pointer);*)
  7347. END;
  7348. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7349. ToMemory(l.op, addressType, 0);
  7350. Designate(p0,s0);
  7351. ToMemory(s0.op,addressType,0);
  7352. Emit(Mov(position,s0.op,l.op));
  7353. ReleaseOperand(l);
  7354. ReleaseOperand(s0);
  7355. result.tag := emptyOperand;
  7356. (* start *)
  7357. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7358. (* push cell *)
  7359. Symbol(temporaryVariable, l);
  7360. ToMemory(l.op,addressType,0);
  7361. Emit(Push(-1,l.op));
  7362. (* push delegate *)
  7363. Emit(Push(-1,l.op));
  7364. ReleaseOperand(l);
  7365. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7366. Emit(Push(position, s1.op));
  7367. ReleaseOperand(s1);
  7368. CallThis(position,"ActiveCellsRuntime","Start",3);
  7369. END;
  7370. (*IF temporaryVariable # NIL THEN
  7371. end := NewLabel();
  7372. BrL(end);
  7373. SetLabel(exit);
  7374. Designate(p0,l);
  7375. ToMemory(l.op,addressType,0);
  7376. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7377. ReleaseOperand(l);
  7378. SetLabel(end);
  7379. ELSE
  7380. SetLabel(exit);
  7381. END;
  7382. *)
  7383. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7384. ELSE (* no pointer to record, no pointer to array *)
  7385. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7386. (* ignore new statement *)
  7387. Warning(p0.position, "cannot run on final hardware");
  7388. ELSE
  7389. HALT(200);
  7390. END;
  7391. END;
  7392. (* ---- ADDRESSOF----- *)
  7393. |Global.systemAdr:
  7394. Designate(p0,s0);
  7395. s0.mode := ModeValue;
  7396. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7397. ReleaseIntermediateOperand(s0.op);
  7398. s0.op := s0.tag;
  7399. IntermediateCode.InitOperand(s0.tag);
  7400. END;
  7401. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7402. result := s0;
  7403. (* ---- BIT ----- *)
  7404. |Global.systemBit:
  7405. Evaluate(p0,s0);
  7406. ToMemory(s0.op,addressType,0);
  7407. ReuseCopy(res,s0.op);
  7408. ReleaseOperand(s0);
  7409. Evaluate(p1,s1);
  7410. Emit(Ror(position,res,res,s1.op));
  7411. ReleaseOperand(s1);
  7412. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7413. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7414. IF ~conditional THEN
  7415. InitOperand(result,ModeValue); result.op := res;
  7416. ELSE
  7417. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7418. BrL(falseLabel);
  7419. ReleaseIntermediateOperand(res);
  7420. END;
  7421. (* --- MSK ----*)
  7422. |Global.systemMsk:
  7423. Evaluate(p0,s0);
  7424. Evaluate(p1,s1);
  7425. ReuseCopy(res,s0.op);
  7426. ReleaseOperand(s0);
  7427. Emit(And(position,res,res,s1.op));
  7428. ReleaseOperand(s1);
  7429. InitOperand(result,ModeValue);
  7430. result.op := res;
  7431. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7432. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7433. Evaluate(p0,s0);
  7434. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7435. ReleaseOperand(s0);
  7436. InitOperand(result,ModeValue);
  7437. result.op := res;
  7438. (* ---- SYSTEM.GetStackPointer ----- *)
  7439. |Global.systemGetStackPointer:
  7440. InitOperand(result,ModeValue);
  7441. result.op := sp;
  7442. (* ---- SYSTEM.GetFramePointer ----- *)
  7443. |Global.systemGetFramePointer:
  7444. InitOperand(result,ModeValue);
  7445. result.op := fp;
  7446. (* ---- SYSTEM.GetActivity ----- *)
  7447. |Global.systemGetActivity:
  7448. ASSERT(backend.cooperative);
  7449. InitOperand(result,ModeValue);
  7450. result.op := ap;
  7451. (* ---- SYSTEM.SetStackPointer ----- *)
  7452. |Global.systemSetStackPointer:
  7453. Evaluate(p0,s0); (* *)
  7454. Emit(Mov(position,sp,s0.op));
  7455. ReleaseOperand(s0);
  7456. (* ---- SYSTEM.SetFramePointer ----- *)
  7457. |Global.systemSetFramePointer:
  7458. Evaluate(p0,s0); (* *)
  7459. Emit(Mov(position,fp,s0.op));
  7460. ReleaseOperand(s0);
  7461. (* ---- SYSTEM.Activity ----- *)
  7462. |Global.systemSetActivity:
  7463. ASSERT(backend.cooperative);
  7464. Evaluate(p0,s0); (* *)
  7465. Emit(Mov(position,ap,s0.op));
  7466. ReleaseOperand(s0);
  7467. (* ---- SYSTEM.VAL ----- *)
  7468. |Global.systemVal:
  7469. Expression(p1);
  7470. s1 := result;
  7471. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7472. IF s1.mode = ModeReference THEN
  7473. (* nothing to be done if not record type, just take over new type *)
  7474. IF (type IS SyntaxTree.RecordType) THEN
  7475. ReleaseIntermediateOperand(s1.tag);
  7476. s1.tag := TypeDescriptorAdr(type);
  7477. IF ~newObjectFile THEN
  7478. IntermediateCode.MakeMemory(s1.tag,addressType);
  7479. END;
  7480. UseIntermediateOperand(s1.tag);
  7481. END;
  7482. result := s1;
  7483. ELSE (* copy over result to different type, may not use convert *)
  7484. itype := IntermediateCode.GetType(system,type);
  7485. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7486. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7487. Emit(Mov(position,s0.op,s1.op));
  7488. ReleaseOperand(s1);
  7489. InitOperand(result,ModeValue);
  7490. result.op := s0.op;
  7491. ELSE (* different size, must convert *)
  7492. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7493. Convert(s1.op, IntermediateCode.GetType(system,type));
  7494. result := s1;
  7495. END;
  7496. END;
  7497. (* ---- SYSTEM.GET ----- *)
  7498. |Global.systemGet:
  7499. Evaluate(p0,s0); (* adr *)
  7500. Designate(p1,s1); (* variable *)
  7501. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7502. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7503. Emit(Mov(position,s1.op,s0.op));
  7504. ReleaseOperand(s1);
  7505. ReleaseOperand(s0);
  7506. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7507. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7508. Evaluate(p0,s0); (* *)
  7509. Evaluate(p1,s1); (* variable *)
  7510. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7511. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7512. (* real part *)
  7513. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7514. Emit(Mov(position,res, s1.op));
  7515. ReleaseIntermediateOperand(res);
  7516. (* imaginary part *)
  7517. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7518. Emit(Mov(position,res, s1.tag));
  7519. ReleaseIntermediateOperand(res);
  7520. ReleaseOperand(s1);
  7521. ReleaseOperand(s0);
  7522. ELSE
  7523. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7524. ReleaseOperand(s0);
  7525. Emit(Mov(position,res,s1.op));
  7526. ReleaseIntermediateOperand(res);
  7527. ReleaseOperand(s1);
  7528. END;
  7529. (* ---- SYSTEM.MOVE ----- *)
  7530. |Global.systemMove:
  7531. Evaluate(p0,s0);
  7532. Evaluate(p1,s1);
  7533. Evaluate(p2,s2);
  7534. Emit(Copy(position,s1.op,s0.op,s2.op));
  7535. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7536. (* ---- SYSTEM.NEW ----- *)
  7537. |Global.systemNew:
  7538. Designate(p0,s0);
  7539. Emit(Push(position,s0.op));
  7540. ReleaseOperand(s0);
  7541. Evaluate(p1,s1);
  7542. Emit(Push(position,s1.op));
  7543. ReleaseOperand(s1);
  7544. (* push realtime flag: false by default *)
  7545. Emit(Push(position,false));
  7546. CallThis(position,"Heaps","NewSys",3);
  7547. (* ---- SYSTEM.CALL ----- *)
  7548. |Global.systemRef:
  7549. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7550. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7551. s0.mode := ModeValue;
  7552. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7553. result := s0
  7554. (* ---- INCR ----- *)
  7555. |Global.Incr:
  7556. Designate(p0,operand);
  7557. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7558. Dereference(operand,p0.type.resolved,FALSE);
  7559. END;
  7560. ASSERT(p1 # NIL);
  7561. Evaluate(p1,l);
  7562. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7563. ReleaseOperand(operand); ReleaseOperand(l);
  7564. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7565. (* ---- SUM ----- *)
  7566. |Global.Sum: HALT(200);
  7567. (* ---- CAS ----- *)
  7568. |Global.Cas:
  7569. needsTrace := p0.NeedsTrace();
  7570. IF needsTrace THEN ModifyAssignments(true) END;
  7571. Designate(p0,s0);
  7572. Evaluate(p1,s1);
  7573. Evaluate(p2,s2);
  7574. IF needsTrace THEN
  7575. Emit(Push(position, s0.op));
  7576. Emit(Push(position, s1.op));
  7577. Emit(Push(position, s2.op));
  7578. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7579. ELSE
  7580. Emit(Cas(position,s0.op,s1.op,s2.op));
  7581. END;
  7582. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7583. IF needsTrace THEN ModifyAssignments(false) END;
  7584. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7585. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7586. IF conditional THEN
  7587. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7588. BrL(falseLabel);
  7589. ReleaseIntermediateOperand(res);
  7590. END;
  7591. (* ---- DIM ----- *)
  7592. |Global.Dim:
  7593. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7594. Designate(p0,s0);
  7595. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7596. Dereference(s0,p0.type.resolved,FALSE);
  7597. END;
  7598. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7599. ReleaseOperand(s0);
  7600. (* ---- RESHAPE ----- *)
  7601. |Global.Reshape:
  7602. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7603. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7604. left.SetType(procedure.type);
  7605. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7606. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7607. END;
  7608. (* ---- SYSTEM.TYPECODE ----- *)
  7609. |Global.systemTypeCode:
  7610. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7611. IF type.resolved IS SyntaxTree.PointerType THEN
  7612. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7613. END;
  7614. result.op := TypeDescriptorAdr(type);
  7615. IF ~newObjectFile THEN
  7616. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7617. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7618. END;
  7619. result.mode := ModeValue;
  7620. (* ---- SYSTEM.TRACE ----- *)
  7621. |Global.systemTrace:
  7622. SystemTrace(x.parameters, x.position);
  7623. (* ----- CONNECT ------*)
  7624. |Global.Connect:
  7625. IF backend.cellsAreObjects THEN
  7626. PushPort(p0);
  7627. PushPort(p1);
  7628. IF p2 # NIL THEN
  7629. Evaluate(p2, s2);
  7630. Emit(Push(p2.position, s2.op));
  7631. ReleaseOperand(s2);
  7632. ELSE
  7633. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7634. END;
  7635. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7636. ELSE
  7637. Warning(x.position, "cannot run on final hardware");
  7638. END;
  7639. (* ----- DELEGATE ------*)
  7640. |Global.Delegate:
  7641. IF backend.cellsAreObjects THEN
  7642. PushPort(p0);
  7643. PushPort(p1);
  7644. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7645. ELSE
  7646. Warning(x.position, "cannot run on final hardware");
  7647. END;
  7648. (* ----- SEND ------*)
  7649. |Global.Send:
  7650. Evaluate(p0,s0);
  7651. Evaluate(p1,s1);
  7652. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7653. Emit(Push(position,s0.op));
  7654. Emit(Push(position,s1.op));
  7655. (*
  7656. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7657. *)
  7658. IF ~backend.cellsAreObjects THEN
  7659. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7660. END;
  7661. ReleaseOperand(s0);
  7662. ReleaseOperand(s1);
  7663. IF backend.cellsAreObjects THEN
  7664. CallThis(position,"ActiveCellsRuntime","Send",2);
  7665. ELSE
  7666. CallThis(position,ChannelModuleName,"Send",2);
  7667. END;
  7668. (* ----- RECEIVE ------*)
  7669. |Global.Receive:
  7670. Evaluate(p0,s0);
  7671. Emit(Push(position,s0.op));
  7672. Designate(p1,s1);
  7673. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7674. Emit(Push(position,s1.op));
  7675. IF p2 # NIL THEN
  7676. Designate(p2,s2);
  7677. Emit(Push(position,s2.op));
  7678. END;
  7679. (*
  7680. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7681. *)
  7682. IF ~backend.cellsAreObjects THEN
  7683. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7684. END;
  7685. ReleaseOperand(s0);
  7686. ReleaseOperand(s1);
  7687. ReleaseOperand(s2);
  7688. IF backend.cellsAreObjects THEN
  7689. IF p2 = NIL THEN
  7690. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7691. ELSE
  7692. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7693. END;
  7694. ELSE
  7695. IF p2 = NIL THEN
  7696. CallThis(position,ChannelModuleName,"Receive",2)
  7697. ELSE
  7698. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7699. END;
  7700. END;
  7701. | Global.systemSpecial:
  7702. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7703. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7704. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7705. (* determine if parameters are of the VAR kind *)
  7706. ASSERT(x.parameters.Length() <= 3);
  7707. formalParameter := procedureType.firstParameter;
  7708. FOR i := 0 TO x.parameters.Length() - 1 DO
  7709. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7710. formalParameter := formalParameter.nextParameter
  7711. END;
  7712. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7713. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7714. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7715. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7716. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7717. IF procedureType.returnType # NIL THEN
  7718. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7719. Emit(Result(position, res));
  7720. (*InitOperand(result,ModeValue);
  7721. result.op := res;
  7722. *)
  7723. IF ~conditional THEN
  7724. InitOperand(result,ModeValue); result.op := res;
  7725. ELSE
  7726. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7727. BrL(falseLabel);
  7728. ReleaseIntermediateOperand(res);
  7729. END;
  7730. END
  7731. ELSE (* function not yet implemented *)
  7732. Error(position,"not yet implemented");
  7733. END;
  7734. destination := dest;
  7735. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7736. END VisitBuiltinCallDesignator;
  7737. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7738. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7739. BEGIN
  7740. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7741. dest := destination; destination := emptyOperand;
  7742. Expression(x.left);
  7743. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7744. ELSIF isUnchecked THEN (* no check *)
  7745. ELSE
  7746. trueL := NewLabel();
  7747. falseL := NewLabel();
  7748. IF IsPointerToRecord(x.left.type,recordType) THEN
  7749. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7750. Emit(Mov(position,tag, result.op));
  7751. IF result.mode # ModeValue THEN
  7752. ptr := tag;
  7753. IntermediateCode.MakeMemory(ptr,addressType);
  7754. Emit(Mov(position,tag, ptr));
  7755. END;
  7756. IF ~backend.cooperative THEN
  7757. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7758. END;
  7759. IntermediateCode.MakeMemory(tag,addressType);
  7760. ELSE
  7761. tag := result.tag;
  7762. END;
  7763. TypeTest(tag,x.type,trueL,falseL);
  7764. ReleaseIntermediateOperand(tag);
  7765. SetLabel(falseL);
  7766. EmitTrap(position,TypeCheckTrap);
  7767. SetLabel(trueL);
  7768. END;
  7769. destination := dest;
  7770. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7771. END VisitTypeGuardDesignator;
  7772. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7773. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7774. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7775. VAR label: Label; pc: LONGINT;
  7776. BEGIN
  7777. IF backend.cooperative & ~isUnchecked THEN
  7778. pc := section.pc;
  7779. label := NewLabel();
  7780. BrneL(label, operand.op, nil);
  7781. EmitTrap(position, NilPointerTrap);
  7782. SetLabel(label);
  7783. INC(statCoopNilCheck, section.pc - pc);
  7784. END;
  7785. END NilCheck;
  7786. BEGIN
  7787. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7788. ReuseCopy(dereferenced,operand.op);
  7789. ReleaseOperand(operand);
  7790. operand.mode := ModeReference;
  7791. operand.op := dereferenced;
  7792. operand.tag := dereferenced;
  7793. UseIntermediateOperand(operand.tag);
  7794. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7795. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7796. ReleaseIntermediateOperand(operand.tag);
  7797. operand.tag := TypeDescriptorAdr(type);
  7798. IF ~newObjectFile THEN
  7799. IntermediateCode.MakeMemory(operand.tag,addressType);
  7800. END;
  7801. ELSE
  7802. IF ~backend.cooperative THEN
  7803. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7804. END;
  7805. IntermediateCode.MakeMemory(operand.tag,addressType);
  7806. END;
  7807. NilCheck(operand.op);
  7808. ELSIF type IS SyntaxTree.ArrayType THEN
  7809. IF isUnsafe THEN
  7810. NilCheck(operand.op);
  7811. ReleaseIntermediateOperand(operand.tag);
  7812. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7813. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7814. ELSE
  7815. operand.tag := emptyOperand;
  7816. END;
  7817. ELSE
  7818. NilCheck(operand.op);
  7819. IF backend.cooperative THEN
  7820. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7821. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7822. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7823. ELSE
  7824. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7825. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7826. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7827. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7828. END;
  7829. END;
  7830. ELSIF type IS SyntaxTree.MathArrayType THEN
  7831. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7832. IntermediateCode.MakeMemory(operand.op,addressType);
  7833. ELSE HALT(100);
  7834. END;
  7835. END Dereference;
  7836. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7837. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7838. BEGIN
  7839. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7840. dest := destination; destination := emptyOperand;
  7841. Evaluate(x.left,d);
  7842. type := x.type.resolved;
  7843. prevIsUnchecked := isUnchecked;
  7844. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7845. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7846. END;
  7847. Dereference(d,type,IsUnsafePointer(x.left.type));
  7848. isUnchecked := prevIsUnchecked;
  7849. result := d;
  7850. 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
  7851. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7852. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7853. END;
  7854. END;
  7855. destination := dest;
  7856. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7857. END VisitDereferenceDesignator;
  7858. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7859. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7860. BEGIN
  7861. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7862. dest := destination; destination := emptyOperand;
  7863. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7864. tag := result.op;
  7865. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7866. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7867. StaticCallOperand(result,procedure.super);
  7868. ReleaseIntermediateOperand(result.tag);
  7869. UseIntermediateOperand(tag); (* necessary ? *)
  7870. result.tag := tag;
  7871. destination := dest;
  7872. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7873. END VisitSupercallDesignator;
  7874. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7875. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7876. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7877. name: Basic.SegmentedName;
  7878. BEGIN
  7879. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7880. dest := destination; destination := emptyOperand;
  7881. scope := currentScope;
  7882. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7883. scope := scope.outerScope;
  7884. END;
  7885. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7886. IF newObjectFile THEN
  7887. moduleSection := meta.ModuleSection();
  7888. IF backend.cooperative THEN
  7889. moduleOffset := 0;
  7890. ELSE
  7891. moduleOffset := moduleSection.pc;
  7892. END;
  7893. result.mode := ModeValue;
  7894. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7895. ELSE
  7896. Symbol(moduleSelf,result);
  7897. IntermediateCode.MakeMemory(result.op,addressType);
  7898. END
  7899. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7900. result.mode := ModeValue;
  7901. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7902. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7903. ELSE
  7904. GetBaseRegister(basereg,currentScope,scope);
  7905. InitOperand(result,ModeReference);
  7906. result.op := basereg;
  7907. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7908. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7909. IF backend.cooperative THEN
  7910. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7911. END;
  7912. (* tag must be loaded when dereferencing SELF pointer *)
  7913. END;
  7914. destination := dest;
  7915. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7916. END VisitSelfDesignator;
  7917. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7918. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7919. BEGIN
  7920. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7921. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7922. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7923. parameter := procedureType.returnParameter;
  7924. VisitParameter(parameter);
  7925. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7926. END VisitResultDesignator;
  7927. (** values *)
  7928. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7929. BEGIN
  7930. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7931. IF conditional THEN
  7932. IF x.value THEN BrL(trueLabel)
  7933. ELSE BrL(falseLabel)
  7934. END;
  7935. ELSE
  7936. InitOperand(result,ModeValue);
  7937. IF x.value THEN result.op := true ELSE result.op := false END;
  7938. END;
  7939. END VisitBooleanValue;
  7940. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7941. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7942. BEGIN
  7943. Global.GetModuleSegmentedName(module.module, name);
  7944. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7945. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7946. RETURN section
  7947. END GetDataSection;
  7948. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7949. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7950. BEGIN
  7951. type := vop.type;
  7952. data := GetDataSection();
  7953. pc := EnterImmediate(data,vop);
  7954. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7955. IntermediateCode.MakeMemory(vop, type);
  7956. END GetImmediateMem;
  7957. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7958. BEGIN
  7959. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7960. InitOperand(result,ModeValue);
  7961. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7962. IF ~supportedImmediate(result.op) &~inData THEN
  7963. GetImmediateMem(result.op)
  7964. END;
  7965. END VisitIntegerValue;
  7966. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7967. BEGIN
  7968. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7969. InitOperand(result,ModeValue);
  7970. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7971. END VisitCharacterValue;
  7972. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7973. BEGIN
  7974. IF Trace THEN TraceEnter("VisitSetValue") END;
  7975. InitOperand(result,ModeValue);
  7976. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7977. END VisitSetValue;
  7978. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7979. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7980. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7981. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7982. BEGIN
  7983. numberElements := x.elements.Length();
  7984. FOR i := 0 TO numberElements-1 DO
  7985. expression := x.elements.GetExpression(i);
  7986. IF expression IS SyntaxTree.MathArrayExpression THEN
  7987. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7988. ELSE
  7989. inData := TRUE;
  7990. Evaluate(expression,op);
  7991. irv.Emit(Data(position,op.op));
  7992. inData := FALSE;
  7993. ReleaseOperand(op);
  7994. END;
  7995. END;
  7996. END RecursiveData;
  7997. BEGIN
  7998. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  7999. IF ~TryConstantDeclaration() THEN
  8000. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8001. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8002. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8003. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8004. ELSE
  8005. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8006. END;
  8007. RecursiveData(x.array);
  8008. InitOperand(result,ModeReference);
  8009. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8010. END
  8011. END VisitMathArrayValue;
  8012. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8013. VAR constant: Sections.Section;
  8014. BEGIN
  8015. IF constantDeclaration = NIL THEN
  8016. RETURN FALSE
  8017. ELSE
  8018. (* Is a constant in this module: did we generate it already? *)
  8019. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8020. IF constant # NIL THEN
  8021. InitOperand(result,ModeReference);
  8022. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8023. RETURN TRUE;
  8024. END;
  8025. END;
  8026. RETURN FALSE
  8027. END TryConstantDeclaration;
  8028. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8029. BEGIN
  8030. constantDeclaration := x;
  8031. x.value.resolved.Accept(SELF);
  8032. END VisitConstant;
  8033. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8034. BEGIN
  8035. IF Trace THEN TraceEnter("VisitRealValue") END;
  8036. InitOperand(result,ModeValue);
  8037. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8038. END VisitRealValue;
  8039. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8040. VAR
  8041. componentType: SyntaxTree.Type;
  8042. BEGIN
  8043. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8044. ASSERT(x.type IS SyntaxTree.ComplexType);
  8045. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8046. InitOperand(result,ModeValue);
  8047. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8048. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8049. END VisitComplexValue;
  8050. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8051. VAR i: LONGINT; name: Basic.SegmentedName;
  8052. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8053. BEGIN
  8054. IF Trace THEN TraceEnter("VisitStringValue") END;
  8055. IF ~TryConstantDeclaration() THEN
  8056. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8057. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8058. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8059. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8060. ELSE
  8061. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8062. END;
  8063. FOR i := 0 TO x.length-1 DO
  8064. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8065. irv.Emit(Data(position,op));
  8066. END;
  8067. InitOperand(result,ModeReference);
  8068. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8069. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8070. END
  8071. END VisitStringValue;
  8072. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8073. BEGIN
  8074. IF Trace THEN TraceEnter("VisitNilValue") END;
  8075. InitOperand(result,ModeValue);
  8076. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8077. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8078. END VisitNilValue;
  8079. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8080. BEGIN
  8081. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8082. InitOperand(result,ModeValue);
  8083. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8084. END VisitEnumerationValue;
  8085. (** symbols *)
  8086. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8087. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8088. END VisitImport;
  8089. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8090. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8091. BEGIN
  8092. IF scope # baseScope THEN
  8093. (* left := [fp+8] *)
  8094. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8095. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8096. ReuseCopy(left,right);
  8097. ReleaseIntermediateOperand(right);
  8098. scope := scope.outerScope; DEC(level);
  8099. (* { left := [left+8] } *)
  8100. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8101. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8102. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8103. Emit(Mov(position,left,right));
  8104. scope := scope.outerScope; DEC(level);
  8105. END;
  8106. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8107. result := left;
  8108. ELSE
  8109. result := fp;
  8110. END;
  8111. END GetBaseRegister;
  8112. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8113. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8114. BEGIN
  8115. IF Trace THEN TraceEnter("VisitVariable"); END;
  8116. type := x.type.resolved;
  8117. IF (x.useRegister) THEN
  8118. InitOperand(result, ModeValue);
  8119. IF x.registerNumber < 0 THEN
  8120. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8121. reg := x.registerNumber;
  8122. ELSE
  8123. reg := registerUsageCount.Map(x.registerNumber);
  8124. UseRegister(reg);
  8125. END;
  8126. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8127. ELSIF x.externalName # NIL THEN
  8128. InitOperand(result,ModeReference);
  8129. Basic.ToSegmentedName(x.externalName^, name);
  8130. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8131. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8132. InitOperand(result,ModeReference);
  8133. GetBaseRegister(result.op,currentScope,x.scope);
  8134. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8135. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8136. InitOperand(result,ModeReference);
  8137. GetCodeSectionNameForSymbol(x,name);
  8138. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8139. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8140. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8141. InitOperand(result,ModeReference);
  8142. GetCodeSectionNameForSymbol(x,name);
  8143. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8144. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8145. ELSE (* field, left designator must have been emitted *)
  8146. ASSERT(result.mode = ModeReference);
  8147. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8148. ReuseCopy(temp,result.op);
  8149. ReleaseIntermediateOperand(result.op);
  8150. result.op := temp;
  8151. END;
  8152. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8153. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8154. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8155. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8156. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8157. END;
  8158. END;
  8159. END;
  8160. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8161. ValueToCondition(result);
  8162. ELSIF type IS SyntaxTree.ProcedureType THEN
  8163. ReleaseIntermediateOperand(result.tag);
  8164. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8165. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8166. UseIntermediateOperand(result.tag);
  8167. ELSE
  8168. result.tag := nil; (* nil *)
  8169. END;
  8170. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8171. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8172. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8173. ReleaseIntermediateOperand(result.tag);
  8174. Global.GetSymbolSegmentedName(x,name);
  8175. Basic.AppendToSegmentedName(name,"@len");
  8176. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8177. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8178. ELSE
  8179. END;
  8180. ELSE
  8181. ReleaseIntermediateOperand(result.tag);
  8182. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8183. END;
  8184. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8185. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8186. ReleaseIntermediateOperand(result.tag);
  8187. result.tag := result.op;
  8188. UseIntermediateOperand(result.tag);
  8189. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8190. IntermediateCode.MakeMemory(result.op,addressType);
  8191. END;
  8192. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8193. ReleaseIntermediateOperand(result.tag);
  8194. result.tag := TypeDescriptorAdr(type);
  8195. IF ~newObjectFile THEN
  8196. IntermediateCode.MakeMemory(result.tag,addressType);
  8197. END;
  8198. UseIntermediateOperand(result.tag);
  8199. (* tag for pointer type computed not here but during dereferencing *)
  8200. END;
  8201. IF Trace THEN TraceExit("VisitVariable") END;
  8202. END VisitVariable;
  8203. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8204. BEGIN
  8205. VisitVariable(property);
  8206. END VisitProperty;
  8207. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8208. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8209. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8210. BEGIN
  8211. type := x.type.resolved;
  8212. IF Trace THEN TraceEnter("VisitParameter") END;
  8213. IF x.ownerType IS SyntaxTree.CellType THEN
  8214. ptype := x.type.resolved;
  8215. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8216. IF ~(ptype IS SyntaxTree.PortType) THEN
  8217. InitOperand(result,ModeReference);
  8218. GetCodeSectionNameForSymbol(x,name);
  8219. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8220. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8221. RETURN
  8222. ELSE
  8223. InitOperand(result, ModeValue);
  8224. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8225. adr := 0;
  8226. WHILE parameter # x DO
  8227. parameterType := parameter.type;
  8228. inc := 1;
  8229. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8230. INC(adr, inc);
  8231. parameter := parameter.nextParameter
  8232. END;
  8233. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8234. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8235. RETURN
  8236. END;
  8237. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8238. InitOperand(result,ModeReference);
  8239. GetCodeSectionNameForSymbol(x,name);
  8240. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8241. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8242. RETURN
  8243. ELSE
  8244. GetBaseRegister(basereg,currentScope,x.scope);
  8245. InitOperand(result,ModeReference);
  8246. result.op := basereg;
  8247. END;
  8248. IF IsOpenArray(type) THEN
  8249. result.tag := basereg;
  8250. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8251. IntermediateCode.MakeMemory(result.op,addressType);
  8252. IF Global.IsOberonProcedure(x.ownerType) THEN
  8253. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8254. UseIntermediateOperand(result.tag);
  8255. ELSE
  8256. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8257. END;
  8258. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8259. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8260. ELSIF IsStaticArray(type) THEN
  8261. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8262. IntermediateCode.MakeMemory(result.op,addressType);
  8263. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8264. ELSIF type IS SyntaxTree.MathArrayType THEN
  8265. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8266. WITH type: SyntaxTree.MathArrayType DO
  8267. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8268. IF type.form = SyntaxTree.Tensor THEN
  8269. ELSIF type.form = SyntaxTree.Open THEN
  8270. result.tag := result.op;
  8271. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8272. IntermediateCode.MakeMemory(result.op,addressType);
  8273. UseIntermediateOperand(result.tag);
  8274. ELSIF type.form = SyntaxTree.Static THEN
  8275. IF x.kind = SyntaxTree.ConstParameter THEN
  8276. IntermediateCode.MakeMemory(result.op,addressType);
  8277. END;
  8278. ELSE HALT(100)
  8279. END;
  8280. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8281. IF type.form = SyntaxTree.Tensor THEN
  8282. ToMemory(result.op,addressType,0);
  8283. ELSIF type.form = SyntaxTree.Open THEN
  8284. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8285. ReuseCopy(result.tag, mem);
  8286. ReleaseIntermediateOperand(mem);
  8287. ReleaseIntermediateOperand(result.op);
  8288. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8289. ELSIF type.form = SyntaxTree.Static THEN
  8290. IntermediateCode.MakeMemory(result.op,addressType);
  8291. ELSE HALT(100)
  8292. END;
  8293. ELSE HALT(100)
  8294. END;
  8295. END;
  8296. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8297. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8298. IntermediateCode.MakeMemory(result.op,addressType);
  8299. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8300. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8301. END;
  8302. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8303. ValueToCondition(result);
  8304. ELSIF type IS SyntaxTree.ProcedureType THEN
  8305. ReleaseIntermediateOperand(result.tag);
  8306. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8307. IF x.kind = SyntaxTree.VarParameter THEN
  8308. ReuseCopy(result.tag,result.op);
  8309. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8310. IntermediateCode.MakeMemory(result.tag,addressType);
  8311. ELSE
  8312. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8313. UseIntermediateOperand(result.tag);
  8314. END;
  8315. ELSE
  8316. result.tag := nil;
  8317. END;
  8318. (* tag for pointer type computed not here but during dereferencing *)
  8319. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8320. ReleaseIntermediateOperand(result.tag);
  8321. result.tag := basereg;
  8322. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8323. IntermediateCode.MakeMemory(result.tag,addressType);
  8324. UseIntermediateOperand(result.tag);
  8325. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8326. ReleaseIntermediateOperand(result.tag);
  8327. result.tag := TypeDescriptorAdr(type);
  8328. IF ~newObjectFile THEN
  8329. IntermediateCode.MakeMemory(result.tag,addressType);
  8330. END;
  8331. UseIntermediateOperand(result.tag);
  8332. END;
  8333. IF Trace THEN TraceExit("VisitParameter") END;
  8334. END VisitParameter;
  8335. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8336. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8337. BEGIN
  8338. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8339. (* left.p: left already emitted *)
  8340. tag := result.op; (* value of pointer to left *)
  8341. (* get type desc *)
  8342. tmp := result.tag;
  8343. IntermediateCode.MakeMemory(tmp,addressType);
  8344. (* get method adr *)
  8345. Reuse1(reg,tmp);
  8346. ReleaseIntermediateOperand(tmp);
  8347. IF backend.cooperative THEN
  8348. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8349. WHILE recordType.baseType # NIL DO
  8350. recordType := recordType.GetBaseRecord ();
  8351. END;
  8352. GetRecordTypeName (recordType,name);
  8353. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8354. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8355. offset := 0;
  8356. ELSE
  8357. offset := 2;
  8358. END;
  8359. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8360. ELSE
  8361. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8362. END;
  8363. InitOperand(operand,ModeReference);
  8364. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8365. operand.op := reg;
  8366. operand.tag := tag;
  8367. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8368. END DynamicCallOperand;
  8369. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8370. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8371. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8372. BEGIN
  8373. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8374. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8375. tag := operand.op;
  8376. ReleaseIntermediateOperand(operand.tag);
  8377. ELSE tag := nil
  8378. END;
  8379. IF x.isInline THEN
  8380. sectionType := Sections.InlineCodeSection;
  8381. ELSE
  8382. sectionType := Sections.CodeSection;
  8383. END;
  8384. IF x.externalName # NIL THEN
  8385. Basic.ToSegmentedName(x.externalName^, name);
  8386. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8387. ELSE
  8388. GetCodeSectionNameForSymbol(x, name);
  8389. IF (x.scope.ownerModule = module.module) THEN
  8390. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8391. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8392. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8393. IF source.pc = 0 THEN (* no code yet *)
  8394. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8395. END;
  8396. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8397. bits := x.procedureScope.body.code.inlineCode;
  8398. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8399. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8400. binary.CopyBits(bits, 0, bits.GetSize());
  8401. source.SetResolved(binary);
  8402. ELSE
  8403. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8404. END;
  8405. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8406. END;
  8407. InitOperand(operand,ModeValue);
  8408. operand.op := reg;
  8409. operand.tag := tag;
  8410. IF Trace THEN TraceExit("StaticCallOperand") END;
  8411. END StaticCallOperand;
  8412. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8413. (* handle expressions of the form designator.procedure or procedure *)
  8414. BEGIN
  8415. IF Trace THEN TraceEnter("VisitProcedure") END;
  8416. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8417. DynamicCallOperand(result,x);
  8418. ELSIF x.isInline THEN
  8419. StaticCallOperand(result,x);
  8420. ELSE
  8421. StaticCallOperand(result,x);
  8422. END;
  8423. IF Trace THEN TraceExit("VisitProcedure") END;
  8424. END VisitProcedure;
  8425. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8426. BEGIN
  8427. VisitProcedure(x);
  8428. END VisitOperator;
  8429. (** statements *)
  8430. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8431. BEGIN
  8432. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8433. Expression(x.call);
  8434. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8435. ReleaseOperand(result)
  8436. END;
  8437. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8438. END VisitProcedureCallStatement;
  8439. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8440. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8441. leftBase, rightBase: SyntaxTree.Type;
  8442. procedureName,s: SyntaxTree.IdentifierString;
  8443. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8444. size: LONGINT; rightKind: LONGINT;
  8445. dummy: IntermediateCode.Operand;
  8446. CONST moduleName = "FoxArrayBase";
  8447. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8448. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8449. BEGIN
  8450. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8451. IF base IS SyntaxTree.MathArrayType THEN
  8452. base := OpenArray(base(SyntaxTree.MathArrayType));
  8453. END;
  8454. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8455. result.SetArrayBase(base);
  8456. RETURN result
  8457. END OpenArray;
  8458. BEGIN
  8459. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8460. SaveRegisters();ReleaseUsedRegisters(saved);
  8461. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8462. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8463. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8464. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8465. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8466. IF leftType.form = SyntaxTree.Tensor THEN
  8467. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8468. ELSIF leftType.form = SyntaxTree.Open THEN
  8469. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8470. ELSIF leftType.form = SyntaxTree.Static THEN
  8471. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8472. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8473. END;
  8474. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8475. IF procedure = NIL THEN
  8476. s := "Instruction not supported on target, emulation procedure ";
  8477. Strings.Append(s,moduleName);
  8478. Strings.Append(s,".");
  8479. Strings.Append(s,procedureName);
  8480. Strings.Append(s," not present");
  8481. Error(position,s);
  8482. ELSE
  8483. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8484. parameter.SetType(leftType);
  8485. parameter.SetAccess(SyntaxTree.Internal);
  8486. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8487. parameter.SetKind(rightKind);
  8488. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8489. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8490. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8491. StaticCallOperand(result,procedure);
  8492. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8493. ReleaseOperand(result);
  8494. END;
  8495. RestoreRegisters(saved);
  8496. END;
  8497. END AssignMathArray;
  8498. VAR modifyAssignmentCounter := 0: LONGINT;
  8499. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8500. VAR processor,mem,dst: IntermediateCode.Operand;
  8501. BEGIN
  8502. IF value.intValue = true.intValue THEN
  8503. INC(modifyAssignmentCounter);
  8504. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8505. modifyAssignmentsPC := section.pc;
  8506. ELSE
  8507. DEC(modifyAssignmentCounter);
  8508. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8509. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8510. END;
  8511. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8512. dst := NewRegisterOperand (addressType);
  8513. Emit(Mov(position,dst, processor));
  8514. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8515. Emit(Mov(position,mem, value));
  8516. ReleaseIntermediateOperand(dst);
  8517. END ModifyAssignments;
  8518. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8519. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8520. BEGIN
  8521. type := left.type.resolved;
  8522. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8523. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8524. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8525. IF procedureType.returnParameter = parameter THEN
  8526. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8527. END;
  8528. END;
  8529. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8530. END CopySize;
  8531. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8532. VAR
  8533. leftO, rightO: Operand;
  8534. mem, sizeOp: IntermediateCode.Operand;
  8535. leftType, rightType, componentType: SyntaxTree.Type;
  8536. size: LONGINT;
  8537. parameters: SyntaxTree.ExpressionList;
  8538. procedure: SyntaxTree.Procedure;
  8539. call: SyntaxTree.ProcedureCallDesignator;
  8540. designator: SyntaxTree.Designator;
  8541. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8542. VAR procedureType: SyntaxTree.ProcedureType;
  8543. BEGIN
  8544. IF ReturnedAsParameter(right.type) THEN
  8545. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8546. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8547. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8548. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8549. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8550. IF right.id = Global.Reshape THEN RETURN TRUE
  8551. END;
  8552. END;
  8553. END;
  8554. END;
  8555. RETURN FALSE
  8556. END CanPassAsResultParameter;
  8557. BEGIN
  8558. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8559. leftType := left.type.resolved; rightType:= right.type.resolved;
  8560. IF backend.cooperative & left.NeedsTrace() THEN
  8561. ModifyAssignments(true);
  8562. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8563. Designate(right, rightO);
  8564. Designate(left, leftO);
  8565. ASSERT(leftO.mode = ModeReference);
  8566. TransferToRegister(leftO.op, leftO.op);
  8567. TransferToRegister(rightO.op, rightO.op);
  8568. Emit(Push(position, leftO.op));
  8569. Emit(Push(position, rightO.op));
  8570. CallAssignMethod(leftO.op, rightO.op, left.type);
  8571. Emit(Pop(position, rightO.op));
  8572. Emit(Pop(position, leftO.op));
  8573. sizeOp := CopySize(left);
  8574. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8575. ReleaseOperand(leftO);
  8576. ReleaseOperand(rightO);
  8577. ELSE
  8578. Evaluate(right,rightO);
  8579. Designate(left,leftO);
  8580. ASSERT(leftO.mode = ModeReference);
  8581. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8582. (* copy procedure address first *)
  8583. MakeMemory(mem,leftO.op,addressType,0);
  8584. Emit(Mov(position,mem,rightO.op));
  8585. ReleaseIntermediateOperand(mem);
  8586. (* copy pointer address *)
  8587. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8588. CallAssignPointer(leftO.tag, rightO.tag);
  8589. ELSE
  8590. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8591. CallAssignPointer(leftO.op, rightO.op);
  8592. END;
  8593. ReleaseOperand(leftO);
  8594. ReleaseOperand(rightO);
  8595. END;
  8596. ModifyAssignments(false);
  8597. RETURN;
  8598. END;
  8599. IF CanPassAsResultParameter(right) THEN
  8600. procedureResultDesignator := left(SyntaxTree.Designator);
  8601. Expression(right);
  8602. procedureResultDesignator := NIL;
  8603. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8604. (* left <-- ALIAS OF right: zerocopy *)
  8605. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8606. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8607. designator.SetType(procedure.type);
  8608. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8609. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8610. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8611. END;
  8612. ELSIF leftType IS SyntaxTree.RangeType THEN
  8613. (* LHS is of array range type *)
  8614. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8615. Evaluate(right, rightO);
  8616. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8617. (* first *)
  8618. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8619. Emit(Mov(position,mem, rightO.op));
  8620. ReleaseIntermediateOperand(mem);
  8621. (* last *)
  8622. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8623. Emit(Mov(position,mem, rightO.tag));
  8624. ReleaseIntermediateOperand(mem);
  8625. (* step *)
  8626. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8627. Emit(Mov(position,mem, rightO.extra));
  8628. ReleaseIntermediateOperand(mem);
  8629. ReleaseOperand(rightO);
  8630. ReleaseOperand(leftO)
  8631. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8632. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8633. Evaluate(right, rightO);
  8634. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8635. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8636. (* real part *)
  8637. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8638. Emit(Mov(position,mem, rightO.op));
  8639. ReleaseIntermediateOperand(mem);
  8640. (* imaginary part *)
  8641. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8642. Emit(Mov(position,mem, rightO.tag));
  8643. ReleaseIntermediateOperand(mem);
  8644. ReleaseOperand(rightO);
  8645. ReleaseOperand(leftO)
  8646. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8647. OR (leftType IS SyntaxTree.PortType) THEN
  8648. (* rightO := leftO;*)
  8649. Evaluate(right,rightO);
  8650. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8651. Designate(left,leftO);
  8652. IF leftO.mode = ModeReference THEN
  8653. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8654. destination := mem;
  8655. ELSE
  8656. destination := leftO.op;
  8657. END;
  8658. ReleaseOperand(leftO);
  8659. IF destination.mode # IntermediateCode.Undefined THEN
  8660. Emit(Mov(position,destination,rightO.op));
  8661. END;
  8662. ReleaseOperand(rightO);
  8663. ReleaseIntermediateOperand(mem);
  8664. IntermediateCode.InitOperand(destination);
  8665. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8666. Evaluate(right,rightO);
  8667. Designate(left,leftO);
  8668. MakeMemory(mem,leftO.op,addressType,0);
  8669. Emit(Mov(position,mem,rightO.op));
  8670. ReleaseIntermediateOperand(mem);
  8671. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8672. (* delegate *)
  8673. (*
  8674. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8675. *)
  8676. Emit(Mov(position,leftO.tag,rightO.tag));
  8677. END;
  8678. ReleaseOperand(leftO);
  8679. ReleaseOperand(rightO);
  8680. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8681. Designate(right,rightO);
  8682. Designate(left,leftO);
  8683. sizeOp := CopySize(left);
  8684. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8685. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8686. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8687. IF (rightType IS SyntaxTree.StringType) THEN
  8688. CopyString(left,right);
  8689. 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
  8690. Designate(right,rightO);
  8691. Designate(left,leftO);
  8692. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8693. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8694. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8695. ELSE
  8696. HALT(201)
  8697. END;
  8698. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8699. AssignMathArray(left,right);
  8700. ELSE
  8701. HALT(200);
  8702. END;
  8703. END Assign;
  8704. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8705. BEGIN
  8706. IF Trace THEN TraceEnter("VisitAssignment") END;
  8707. Assign(x.left,x.right);
  8708. IF Trace THEN TraceExit("VisitAssignment") END;
  8709. END VisitAssignment;
  8710. PROCEDURE EmitCooperativeSwitch;
  8711. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8712. BEGIN
  8713. ASSERT (cooperativeSwitches);
  8714. pc := section.pc;
  8715. IF lastSwitchPC = section.pc THEN RETURN END;
  8716. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8717. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8718. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8719. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8720. INC(statCoopSwitch, section.pc - pc);
  8721. END EmitCooperativeSwitch;
  8722. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8723. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8724. BEGIN
  8725. p0 := communication.left; p1 := communication.right;
  8726. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8727. Evaluate(p0,s0);
  8728. Evaluate(p1,s1);
  8729. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8730. Emit(Push(position,s0.op));
  8731. Emit(Push(position,s1.op));
  8732. (*
  8733. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8734. *)
  8735. IF ~backend.cellsAreObjects THEN
  8736. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8737. END;
  8738. ReleaseOperand(s0);
  8739. ReleaseOperand(s1);
  8740. IF backend.cellsAreObjects THEN
  8741. CallThis(position,"ActiveCellsRuntime","Send",2);
  8742. ELSE
  8743. CallThis(position,ChannelModuleName,"Send",2);
  8744. END;
  8745. (* ----- RECEIVE ------*)
  8746. ELSE
  8747. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8748. tmp := p0; p0 := p1; p1 := tmp;
  8749. END;
  8750. Evaluate(p0,s0);
  8751. Emit(Push(position,s0.op));
  8752. Designate(p1,s1);
  8753. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8754. Emit(Push(position,s1.op));
  8755. (*
  8756. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8757. *)
  8758. IF ~backend.cellsAreObjects THEN
  8759. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8760. END;
  8761. ReleaseOperand(s0);
  8762. ReleaseOperand(s1);
  8763. IF backend.cellsAreObjects THEN
  8764. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8765. ELSE
  8766. CallThis(position,ChannelModuleName,"Receive",2)
  8767. END;
  8768. END;
  8769. END VisitCommunicationStatement;
  8770. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8771. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8772. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8773. VAR true, false: Label; condition, value: BOOLEAN;
  8774. BEGIN
  8775. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8776. IF condition THEN
  8777. true := NewLabel();
  8778. false := NewLabel();
  8779. Condition(if.condition,true,false);
  8780. SetLabel(true);
  8781. StatementSequence(if.statements);
  8782. BrL(end);
  8783. SetLabel(false);
  8784. ELSE
  8785. IF value THEN (* always true *)
  8786. escape := TRUE;
  8787. StatementSequence(if.statements);
  8788. (* no branch necessary -- rest skipped *)
  8789. END;
  8790. END;
  8791. END IfPart;
  8792. BEGIN
  8793. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8794. end := NewLabel();
  8795. elsifs := x.ElsifParts();
  8796. IfPart(x.ifPart);
  8797. FOR i := 0 TO elsifs-1 DO
  8798. IF ~escape THEN
  8799. elsif := x.GetElsifPart(i);
  8800. IfPart(elsif);
  8801. END;
  8802. END;
  8803. IF (x.elsePart # NIL) & ~escape THEN
  8804. StatementSequence(x.elsePart);
  8805. END;
  8806. SetLabel(end);
  8807. IF Trace THEN TraceExit("VisitIfStatement") END;
  8808. END VisitIfStatement;
  8809. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8810. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8811. BEGIN
  8812. (*IF x.variable.type.resolved = x.type.resolved THEN
  8813. (* always true, do nothing *)
  8814. ELSE*)
  8815. Designate(x.variable,res);
  8816. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8817. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8818. END;
  8819. trueL := NewLabel();
  8820. TypeTest(res.tag,x.type,trueL,falseL);
  8821. ReleaseOperand(res);
  8822. SetLabel(trueL);
  8823. StatementSequence(x.statements);
  8824. BrL(endL);
  8825. END WithPart;
  8826. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8827. VAR endL,falseL: Label;i: LONGINT;
  8828. BEGIN
  8829. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8830. endL := NewLabel();
  8831. FOR i := 0 TO x.WithParts()-1 DO
  8832. falseL := NewLabel();
  8833. WithPart(x.GetWithPart(i),falseL,endL);
  8834. SetLabel(falseL);
  8835. END;
  8836. IF x.elsePart = NIL THEN
  8837. IF ~isUnchecked THEN
  8838. EmitTrap(position,WithTrap);
  8839. END;
  8840. ELSE
  8841. StatementSequence(x.elsePart)
  8842. END;
  8843. SetLabel(endL);
  8844. IF Trace THEN TraceExit("VisitWithStatement") END;
  8845. END VisitWithStatement;
  8846. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8847. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8848. out,else: Label; label: Label;
  8849. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8850. symbol: SyntaxTree.Symbol;
  8851. BEGIN
  8852. (*! split case statement into if-elsif statements for large case label lists *)
  8853. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8854. size := x.max-x.min+1;
  8855. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8856. END;
  8857. Evaluate(x.variable,var);
  8858. ReuseCopy(tmp,var.op);
  8859. ReleaseIntermediateOperand(var.op);
  8860. var.op := tmp;
  8861. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8862. Convert(var.op,addressType);
  8863. size := x.max-x.min+1;
  8864. else := NewLabel();
  8865. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8866. (*
  8867. UniqueId(name,module.module,"case",caseId);
  8868. *)
  8869. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8870. Global.GetModuleSegmentedName(module.module, name);
  8871. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8872. symbol := SyntaxTree.NewSymbol(name[1]);
  8873. symbol.SetScope(moduleScope);
  8874. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8875. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8876. section.isCaseTable := TRUE;
  8877. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8878. ReuseCopy(res,var.op);
  8879. ReleaseOperand(var);
  8880. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8881. Emit(Add(position,res,res,jmp));
  8882. IntermediateCode.MakeMemory(res,addressType);
  8883. Emit(Br(position,res));
  8884. ReleaseIntermediateOperand(res);
  8885. out := NewLabel();
  8886. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8887. part := x.GetCasePart(i);
  8888. constant := part.firstConstant;
  8889. label := NewLabel();
  8890. SetLabel(label);
  8891. WHILE(constant # NIL) DO (* case labels for this case part *)
  8892. FOR j := constant.min TO constant.max DO
  8893. fixups[j-x.min] := label;
  8894. END;
  8895. constant := constant.next;
  8896. END;
  8897. StatementSequence(part.statements);
  8898. BrL(out);
  8899. END;
  8900. SetLabel(else);
  8901. FOR i := 0 TO size-1 DO
  8902. IF fixups[i] = NIL THEN
  8903. fixups[i] := else;
  8904. END;
  8905. END;
  8906. IF x.elsePart # NIL THEN
  8907. StatementSequence(x.elsePart);
  8908. ELSIF ~isUnchecked THEN
  8909. EmitTrap(position,CaseTrap);
  8910. END;
  8911. SetLabel(out);
  8912. FOR i := 0 TO size-1 DO
  8913. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8914. section.Emit(Data(position,op));
  8915. END;
  8916. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8917. END VisitCaseStatement;
  8918. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8919. VAR start: Label; true,false: Label;
  8920. BEGIN
  8921. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8922. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8923. start := NewLabel();
  8924. true := NewLabel();
  8925. false := NewLabel();
  8926. SetLabel(start);
  8927. Condition(x.condition,true,false);
  8928. SetLabel(true);
  8929. StatementSequence(x.statements);
  8930. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8931. BrL(start);
  8932. SetLabel(false);
  8933. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8934. END VisitWhileStatement;
  8935. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8936. VAR false,true: Label;
  8937. BEGIN
  8938. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8939. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8940. true := NewLabel();
  8941. false := NewLabel();
  8942. SetLabel(false);
  8943. StatementSequence(x.statements);
  8944. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8945. Condition(x.condition,true,false);
  8946. SetLabel(true);
  8947. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8948. END VisitRepeatStatement;
  8949. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8950. VAR
  8951. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8952. temporaryVariable: SyntaxTree.Variable;
  8953. temporaryVariableDesignator : SyntaxTree.Designator;
  8954. BEGIN
  8955. IF Trace THEN TraceEnter("VisitForStatement") END;
  8956. true := NewLabel();
  8957. false := NewLabel();
  8958. start := NewLabel();
  8959. Assign(x.variable,x.from);
  8960. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8961. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8962. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8963. Assign(temporaryVariableDesignator,x.to);
  8964. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8965. IF by > 0 THEN
  8966. cmp := Scanner.LessEqual
  8967. ELSE
  8968. cmp := Scanner.GreaterEqual
  8969. END;
  8970. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8971. binary.SetType(system.booleanType);
  8972. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8973. SetLabel(start);
  8974. Condition(binary,true,false);
  8975. SetLabel(true);
  8976. StatementSequence(x.statements);
  8977. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8978. binary.SetType(x.variable.type);
  8979. Assign(x.variable,binary);
  8980. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8981. BrL(start);
  8982. SetLabel(false);
  8983. IF Trace THEN TraceExit("VisitForStatement") END;
  8984. END VisitForStatement;
  8985. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8986. VAR prevLoop: Label;
  8987. BEGIN
  8988. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8989. prevLoop := currentLoop;
  8990. currentLoop := NewLabel();
  8991. StatementSequence(x.statements);
  8992. SetLabel(currentLoop);
  8993. currentLoop := prevLoop;
  8994. IF Trace THEN TraceExit("VisitExitableBlock") END;
  8995. END VisitExitableBlock;
  8996. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  8997. VAR prevLoop,start: Label;
  8998. BEGIN
  8999. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9000. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9001. start := NewLabel();
  9002. prevLoop := currentLoop;
  9003. SetLabel(start);
  9004. currentLoop := NewLabel();
  9005. StatementSequence(x.statements);
  9006. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9007. BrL(start);
  9008. SetLabel(currentLoop);
  9009. currentLoop := prevLoop;
  9010. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9011. END VisitLoopStatement;
  9012. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9013. VAR outer: SyntaxTree.Statement;
  9014. BEGIN
  9015. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9016. IF locked THEN (* r if we jump out of an exclusive block *)
  9017. outer := x.outer;
  9018. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9019. outer := outer.outer;
  9020. END;
  9021. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9022. Lock(FALSE);
  9023. END;
  9024. END;
  9025. BrL(currentLoop);
  9026. IF Trace THEN TraceExit("VisitExitStatement") END;
  9027. END VisitExitStatement;
  9028. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9029. VAR
  9030. expression, parameterDesignator: SyntaxTree.Expression;
  9031. type, componentType: SyntaxTree.Type;
  9032. res, right: Operand;
  9033. left, mem, reg: IntermediateCode.Operand;
  9034. parameter: SyntaxTree.Parameter;
  9035. procedure: SyntaxTree.Procedure;
  9036. procedureType: SyntaxTree.ProcedureType;
  9037. returnTypeOffset: LONGINT;
  9038. delegate: BOOLEAN;
  9039. map: SymbolMap;
  9040. BEGIN
  9041. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9042. expression := x.returnValue;
  9043. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9044. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9045. IF currentIsInline THEN
  9046. IF expression # NIL THEN
  9047. map := currentMapper.Get(NIL);
  9048. IF map # NIL THEN
  9049. Assign(map.to, expression);
  9050. END;
  9051. END;
  9052. BrL(currentInlineExit);
  9053. RETURN;
  9054. END;
  9055. IF expression # NIL THEN
  9056. type := expression.type.resolved;
  9057. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9058. IF locked THEN Lock(FALSE) END;
  9059. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9060. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9061. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9062. (* return without structured return parameter *)
  9063. Evaluate(expression,res);
  9064. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9065. IF locked OR profile THEN
  9066. Emit(Push(position,res.op));
  9067. IF delegate THEN HALT(200) END;
  9068. ReleaseOperand(res);
  9069. IF locked THEN Lock(FALSE) END;
  9070. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9071. reg := NewRegisterOperand(res.op.type);
  9072. Emit(Pop(position,reg));
  9073. Emit(Return(position,reg));
  9074. ReleaseIntermediateOperand(reg);
  9075. ELSE
  9076. Emit(Return(position,res.op));
  9077. ReleaseOperand(res);
  9078. END;
  9079. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9080. THEN
  9081. (* return using structured return parameter *)
  9082. 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)
  9083. OR SemanticChecker.IsPointerType(type));
  9084. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9085. parameter :=procedureType.returnParameter;
  9086. ASSERT(parameter # NIL);
  9087. returnTypeOffset := parameter.offsetInBits;
  9088. (*
  9089. IF parameter# NIL THEN
  9090. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9091. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9092. ELSE
  9093. returnTypeOffset := system.offsetFirstParameter
  9094. END;
  9095. *)
  9096. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9097. IF type IS SyntaxTree.RangeType THEN
  9098. (* array range type *)
  9099. Evaluate(expression, right);
  9100. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9101. Emit(Mov(position,mem, right.op)); (* first *)
  9102. ReleaseIntermediateOperand(mem);
  9103. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9104. Emit(Mov(position,mem, right.tag)); (* last *)
  9105. ReleaseIntermediateOperand(mem);
  9106. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9107. Emit(Mov(position,mem, right.extra)); (* step *)
  9108. ReleaseIntermediateOperand(mem);
  9109. ReleaseOperand(right);
  9110. ELSIF type IS SyntaxTree.ComplexType THEN
  9111. Evaluate(expression, right);
  9112. componentType := type(SyntaxTree.ComplexType).componentType;
  9113. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9114. Emit(Mov(position,mem, right.op)); (* real part *)
  9115. ReleaseIntermediateOperand(mem);
  9116. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9117. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9118. ReleaseIntermediateOperand(mem);
  9119. ReleaseOperand(right);
  9120. ELSE (* covers cases: pointer / record / array *)
  9121. parameter := procedureType.returnParameter;
  9122. checker.SetCurrentScope(currentScope);
  9123. ASSERT(parameter # NIL);
  9124. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9125. Assign(parameterDesignator,expression);
  9126. END;
  9127. ReleaseIntermediateOperand(left);
  9128. IF locked THEN Lock(FALSE) END;
  9129. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9130. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9131. parameter := procedureType.returnParameter;
  9132. checker.SetCurrentScope(currentScope);
  9133. IF parameter = NIL THEN
  9134. Error(procedure.position, "structured return of parameter of procedure not found");
  9135. ELSE
  9136. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9137. Assign(parameterDesignator,expression);
  9138. END;
  9139. IF locked THEN Lock(FALSE) END;
  9140. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9141. ELSE
  9142. HALT(200);
  9143. END;
  9144. ELSE
  9145. IF locked THEN Lock(FALSE) END;
  9146. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9147. END;
  9148. IF backend.cooperative THEN
  9149. BrL(exitLabel);
  9150. ELSE
  9151. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9152. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9153. END;
  9154. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9155. END VisitReturnStatement;
  9156. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9157. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9158. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9159. statements: SyntaxTree.StatementSequence;
  9160. name, suffix: SyntaxTree.IdentifierString;
  9161. BEGIN
  9162. Strings.IntToStr(awaitProcCounter,suffix);
  9163. Strings.Concat("@AwaitProcedure",suffix,name);
  9164. identifier := SyntaxTree.NewIdentifier(name);
  9165. INC(awaitProcCounter);
  9166. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9167. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9168. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9169. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9170. procedure.SetAccess(SyntaxTree.Hidden);
  9171. procedure.SetScope(currentScope);
  9172. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9173. procedureType.SetReturnType(system.booleanType);
  9174. procedure.SetType(procedureType);
  9175. body := SyntaxTree.NewBody(x.position,procedureScope);
  9176. procedureScope.SetBody(body);
  9177. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9178. returnStatement.SetReturnValue(x.condition);
  9179. statements := SyntaxTree.NewStatementSequence();
  9180. statements.AddStatement(returnStatement);
  9181. body.SetStatementSequence(statements);
  9182. currentScope.AddProcedure(procedure);
  9183. RETURN procedure
  9184. END MakeAwaitProcedure;
  9185. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9186. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9187. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9188. BEGIN
  9189. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9190. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9191. IF backend.cooperative THEN
  9192. start := NewLabel();
  9193. true := NewLabel();
  9194. false := NewLabel();
  9195. SetLabel(start);
  9196. Condition(x.condition,true,false);
  9197. SetLabel(false);
  9198. PushSelfPointer();
  9199. CallThis(position,"ExclusiveBlocks","Await",1);
  9200. BrL(start);
  9201. SetLabel(true);
  9202. PushSelfPointer();
  9203. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9204. ELSE
  9205. proc := MakeAwaitProcedure(x);
  9206. Emit(Push(position,fp));
  9207. GetCodeSectionNameForSymbol(proc,name);
  9208. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9209. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9210. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9211. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9212. Emit(Result(position,res));
  9213. (*
  9214. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9215. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9216. *)
  9217. InitOperand(result,ModeValue);
  9218. result.op := res;
  9219. label := NewLabel();
  9220. BreqL(label, result.op, SELF.true);
  9221. ReleaseOperand(result);
  9222. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9223. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9224. Emit(Push(position,res));
  9225. Emit(Push(position,fp));
  9226. PushSelfPointer();
  9227. Emit(Push(position,nil));
  9228. CallThis(position,"Objects","Await",4);
  9229. SetLabel(label);
  9230. END;
  9231. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9232. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9233. END VisitAwaitStatement;
  9234. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9235. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9236. BEGIN
  9237. FOR i := 0 TO x.Length() - 1 DO
  9238. statement := x.GetStatement( i );
  9239. Statement(statement);
  9240. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9241. END;
  9242. END StatementSequence;
  9243. PROCEDURE PushSelfPointer;
  9244. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9245. BEGIN
  9246. scope := currentScope;
  9247. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9248. scope := scope.outerScope;
  9249. END;
  9250. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9251. IF ~newObjectFile THEN
  9252. Symbol(moduleSelf,op);
  9253. IntermediateCode.MakeMemory(op.op,addressType);
  9254. ELSE
  9255. moduleSection := meta.ModuleSection();
  9256. IF backend.cooperative THEN
  9257. moduleOffset := 0;
  9258. ELSE
  9259. moduleOffset := moduleSection.pc;
  9260. END;
  9261. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9262. END;
  9263. ELSE
  9264. GetBaseRegister(op.op,currentScope,scope);
  9265. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9266. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9267. IF backend.cooperative THEN
  9268. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9269. END;
  9270. IntermediateCode.MakeMemory(op.op,addressType);
  9271. END;
  9272. Emit(Push(position,op.op));
  9273. ReleaseOperand(op);
  9274. END PushSelfPointer;
  9275. PROCEDURE Lock(lock: BOOLEAN);
  9276. BEGIN
  9277. IF Trace THEN TraceEnter("Lock") END;
  9278. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9279. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9280. ASSERT(modifyAssignmentCounter = 0);
  9281. IF dump # NIL THEN
  9282. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9283. dump.Ln;dump.Update;
  9284. END;
  9285. PushSelfPointer;
  9286. IF backend.cooperative THEN
  9287. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9288. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9289. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9290. END;
  9291. ELSE
  9292. Emit(Push(position,true));
  9293. IF lock THEN CallThis(position,"Objects","Lock",2)
  9294. ELSE CallThis(position,"Objects","Unlock",2);
  9295. END;
  9296. END;
  9297. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9298. IF Trace THEN TraceExit("Lock") END;
  9299. END Lock;
  9300. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9301. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9302. BEGIN
  9303. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9304. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9305. previouslyUnchecked := isUnchecked;
  9306. isUnchecked := isUnchecked OR x.isUnchecked;
  9307. previouslyCooperativeSwitches := cooperativeSwitches;
  9308. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9309. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9310. IF x.statements # NIL THEN
  9311. StatementSequence(x.statements);
  9312. END;
  9313. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9314. isUnchecked := previouslyUnchecked;
  9315. cooperativeSwitches := previouslyCooperativeSwitches;
  9316. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9317. END VisitStatementBlock;
  9318. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9319. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9320. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9321. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9322. procedure: SyntaxTree.Procedure;
  9323. map: SymbolMap;
  9324. BEGIN
  9325. scope := currentScope;
  9326. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9327. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9328. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9329. return := emptyOperand;
  9330. IF Trace THEN TraceEnter("VisitCode") END;
  9331. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9332. NEW(in, x.inRules.Length());
  9333. FOR i := 0 TO LEN(in)-1 DO
  9334. statement := x.inRules.GetStatement(i);
  9335. WITH statement: SyntaxTree.Assignment DO
  9336. Evaluate(statement.right, operand);
  9337. result := operand.op;
  9338. NEW(str, 64);
  9339. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9340. in[i] := result; IntermediateCode.SetString(in[i], str);
  9341. ReleaseIntermediateOperand(operand.tag);
  9342. END;
  9343. END;
  9344. ELSE in := NIL
  9345. END;
  9346. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9347. NEW(out, x.outRules.Length());
  9348. FOR i := 0 TO LEN(out)-1 DO
  9349. statement := x.outRules.GetStatement(i);
  9350. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9351. WITH statement: SyntaxTree.Assignment DO
  9352. Evaluate(statement.left, operand); (*?? or designate *)
  9353. result := operand.op;
  9354. NEW(str, 64);
  9355. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9356. out[i] := result; IntermediateCode.SetString(out[i], str);
  9357. ReleaseIntermediateOperand(operand.tag);
  9358. |statement: SyntaxTree.ReturnStatement DO
  9359. NEW(str, 64);
  9360. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9361. IF currentIsInline THEN
  9362. map := currentMapper.Get(NIL);
  9363. Evaluate(map.to, operand);
  9364. out[i] := operand.op;
  9365. ELSE
  9366. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9367. END;
  9368. IntermediateCode.SetString(out[i], str);
  9369. ReleaseIntermediateOperand(operand.tag);
  9370. return := out[i];
  9371. ELSE
  9372. END;
  9373. END;
  9374. ELSE out := NIL
  9375. END;
  9376. Emit(Asm(x.position,x.sourceCode, in, out));
  9377. IF in # NIL THEN
  9378. FOR i := 0 TO LEN(in)-1 DO
  9379. ReleaseIntermediateOperand(in[i]);
  9380. END;
  9381. END;
  9382. IF out # NIL THEN
  9383. FOR i := 0 TO LEN(out)-1 DO
  9384. WITH statement: SyntaxTree.Assignment DO
  9385. ReleaseIntermediateOperand(out[i]);
  9386. ELSE
  9387. IF currentIsInline THEN
  9388. ReleaseIntermediateOperand(out[i]);
  9389. END;
  9390. END;
  9391. statement := x.outRules.GetStatement(i);
  9392. END;
  9393. END;
  9394. IF return.mode # IntermediateCode.Undefined THEN
  9395. IF currentIsInline THEN
  9396. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9397. Symbol(procedureType.returnParameter, par);
  9398. MakeMemory(mem, par.op, return.type, 0);
  9399. ReleaseOperand(par);
  9400. Emit(Mov(position, mem, return));
  9401. ReleaseIntermediateOperand(mem);
  9402. ELSE
  9403. Emit(Return(position,return));
  9404. END;
  9405. IF currentIsInline THEN RETURN END;
  9406. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9407. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9408. ReleaseIntermediateOperand(return);
  9409. END;
  9410. IF Trace THEN TraceExit("VisitCode") END;
  9411. END VisitCode;
  9412. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9413. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9414. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9415. const, call: IntermediateCode.Operand;
  9416. parameterDesignator: SyntaxTree.Expression;
  9417. saved: RegisterEntry;
  9418. name: Basic.SegmentedName;
  9419. BEGIN
  9420. IF Trace THEN TraceEnter("ParameterCopies") END;
  9421. parameter := x.firstParameter;
  9422. WHILE parameter # NIL DO
  9423. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9424. type := parameter.type.resolved;
  9425. IF IsOpenArray(type) THEN
  9426. VisitParameter(parameter);
  9427. op := result;
  9428. IF backend.cooperative & parameter.NeedsTrace() THEN
  9429. length := GetArrayLength(type, op.tag);
  9430. size := NewRegisterOperand(addressType);
  9431. base := ArrayBaseType(type);
  9432. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9433. Emit(Mul(position, size, length, const));
  9434. dst := NewRegisterOperand (addressType);
  9435. Emit(Mov(position, dst, size));
  9436. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9437. Emit(Sub(position,dst,sp,dst));
  9438. Emit(And(position,dst,dst,const));
  9439. Emit(Mov(position,sp,dst));
  9440. par := fp;
  9441. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9442. IntermediateCode.InitImmediate(null, byteType, 0);
  9443. Emit(Fill(position, dst, size, null));
  9444. ReleaseIntermediateOperand(dst);
  9445. ReleaseIntermediateOperand(length);
  9446. SaveRegisters();ReleaseUsedRegisters(saved);
  9447. (* register dst has been freed before SaveRegisters already *)
  9448. base := ArrayBaseType(type);
  9449. (* assign method of open array *)
  9450. IF base.IsRecordType() THEN
  9451. Emit (Push(position, length));
  9452. Emit (Push(position, dst));
  9453. Emit (Push(position, op.op));
  9454. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9455. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9456. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9457. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9458. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9459. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9460. Emit (Push(position,length));
  9461. Emit (Push(position, dst));
  9462. Emit (Push(position, length));
  9463. Emit (Push(position, op.op));
  9464. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9465. ELSE
  9466. Emit (Push(position, length));
  9467. Emit (Push(position, dst));
  9468. Emit (Push(position, length));
  9469. Emit (Push(position, op.op));
  9470. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9471. ASSERT(ArrayBaseType(type).IsPointer());
  9472. END;
  9473. RestoreRegisters(saved);
  9474. ELSE
  9475. temp := GetDynamicSize(type,op.tag);
  9476. ReuseCopy(size,temp);
  9477. ReleaseIntermediateOperand(temp);
  9478. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9479. Emit(Sub(position,size,sp,size));
  9480. Emit(And(position,size,size,const));
  9481. Emit(Mov(position,sp,size));
  9482. par := fp;
  9483. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9484. ReleaseIntermediateOperand(size);
  9485. size := GetDynamicSize(type,op.tag);
  9486. END;
  9487. Emit(Copy(position,sp,op.op,size));
  9488. ReleaseIntermediateOperand(size);
  9489. ReleaseOperand(op);
  9490. IntermediateCode.MakeMemory(par,addressType);
  9491. Emit(Mov(position,par,sp));
  9492. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9493. checker.SetCurrentScope(currentScope);
  9494. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9495. Assign(parameterDesignator,parameterDesignator);
  9496. END;
  9497. END;
  9498. parameter := parameter.nextParameter;
  9499. END;
  9500. IF Trace THEN TraceExit("ParameterCopies") END;
  9501. END ParameterCopies;
  9502. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9503. VAR x: SyntaxTree.Variable;
  9504. BEGIN
  9505. x := scope.firstVariable;
  9506. WHILE x # NIL DO
  9507. InitVariable(x);
  9508. x := x.nextVariable;
  9509. END;
  9510. END InitVariables;
  9511. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9512. BEGIN
  9513. IF (symbol # NIL) THEN
  9514. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9515. ELSE
  9516. RETURN 0
  9517. END;
  9518. END GetFingerprint;
  9519. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9520. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9521. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9522. saved: RegisterEntry;
  9523. BEGIN
  9524. IF Trace THEN TraceEnter("Body") END;
  9525. ReleaseUsedRegisters(saved); (* just in case ... *)
  9526. section := ir;
  9527. exitLabel := NewLabel ();
  9528. IF moduleBody THEN moduleBodySection := section END;
  9529. IF ir.comments # NIL THEN
  9530. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9531. commentPrintout.SingleStatement(TRUE);
  9532. dump := ir.comments;
  9533. ELSE
  9534. commentPrintout := NIL;
  9535. dump := NIL;
  9536. END;
  9537. prevScope := currentScope;
  9538. currentScope := scope;
  9539. lastSwitchPC := 0;
  9540. cooperativeSwitches := backend.cooperative;
  9541. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9542. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9543. IF x # NIL THEN
  9544. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9545. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9546. IF moduleBody THEN
  9547. ProfilerInit();
  9548. ELSE
  9549. Basic.SegmentedNameToString(ir.name, string);
  9550. ProfilerAddProcedure(numberProcedures,string);
  9551. ProfilerEnterExit(numberProcedures,TRUE);
  9552. END;
  9553. END;
  9554. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9555. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9556. END;
  9557. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9558. IF moduleBody THEN
  9559. InitVariables(moduleScope)
  9560. END;
  9561. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9562. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9563. IF procedure = cellScope.bodyProcedure THEN
  9564. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9565. StaticCallOperand(op, cellScope.constructor);
  9566. Emit(Call(position,op.op,0));
  9567. END;
  9568. END;
  9569. END;
  9570. ParameterCopies(procedureType);
  9571. InitVariables(scope);
  9572. IF x.code = NIL THEN
  9573. VisitStatementBlock(x);
  9574. ELSE
  9575. VisitCode(x.code)
  9576. END;
  9577. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9578. ir.SetFinally(ir.pc);
  9579. StatementSequence(x.finally)
  9580. END;
  9581. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9582. IF ~backend.cooperative THEN
  9583. ProfilerEnterExit(numberProcedures,FALSE);
  9584. END;
  9585. INC(numberProcedures);
  9586. END;
  9587. END;
  9588. IF backend.cooperative THEN
  9589. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9590. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9591. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9592. (*! not required any more? check and delete!
  9593. PushSelfPointer();
  9594. CallThis(position,"Modules","SetInitialized",1);
  9595. *)
  9596. (*
  9597. SetLabel(end);
  9598. *)
  9599. END;
  9600. IF x # NIL THEN
  9601. SELF.position := x.position;
  9602. END;
  9603. CheckRegistersFree();
  9604. ASSERT(modifyAssignmentCounter = 0);
  9605. currentScope := prevScope;
  9606. IF Trace THEN TraceExit("Body") END;
  9607. END Body;
  9608. END ImplementationVisitor;
  9609. MetaDataGenerator=OBJECT
  9610. VAR
  9611. implementationVisitor: ImplementationVisitor;
  9612. declarationVisitor: DeclarationVisitor;
  9613. module: Sections.Module;
  9614. moduleName: ARRAY 128 OF CHAR;
  9615. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9616. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9617. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9618. TypeTags: LONGINT; (* type extension level support *)
  9619. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9620. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9621. BEGIN
  9622. IF implementationVisitor.backend.cooperative THEN
  9623. TypeTags := MAX(LONGINT); (* only 3 extensions allowed *)
  9624. BaseTypesTableOffset := 0;
  9625. MethodTableOffset := 2;
  9626. TypeRecordBaseOffset := 0;
  9627. ELSIF simple THEN
  9628. TypeTags := 3; (* only 3 extensions allowed *)
  9629. BaseTypesTableOffset := -1;
  9630. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9631. TypeRecordBaseOffset := TypeTags;
  9632. ELSE
  9633. TypeTags := 16;
  9634. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9635. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9636. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9637. END;
  9638. SELF.simple := simple;
  9639. SELF.implementationVisitor := implementationVisitor;
  9640. SELF.declarationVisitor := declarationVisitor;
  9641. implementationVisitor.meta := SELF;
  9642. declarationVisitor.meta := SELF;
  9643. END InitMetaDataGenerator;
  9644. PROCEDURE SetModule(module: Sections.Module);
  9645. BEGIN
  9646. SELF.module := module
  9647. END SetModule;
  9648. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9649. BEGIN
  9650. IF implementationVisitor.backend.cooperative THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9651. END GetTypeRecordBaseOffset;
  9652. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9653. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9654. BEGIN
  9655. INC(dataAdrOffset,6);
  9656. Info(section,"headerAdr");
  9657. Address(section,0);
  9658. Info(section,"typeDesc");
  9659. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9660. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9661. NamedSymbol(section, name, symbol, 0, offset);
  9662. Info(section,"mark: LONGINT;");
  9663. Longint(section,-1);
  9664. Info(section,"dataAdr-: ADDRESS");
  9665. Symbol(section,section, dataAdrOffset,0);
  9666. Info(section,"size-: SIZE");
  9667. Address(section,0);
  9668. Info(section,"nextRealtime: HeapBlock;");
  9669. Address(section,0);
  9670. END HeapBlock;
  9671. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9672. VAR i: LONGINT;
  9673. BEGIN
  9674. INC(dataAdrOffset,14);
  9675. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9676. Info(section,"count*: LONGINT");
  9677. Longint(section,0);
  9678. Info(section,"locked*: BOOLEAN");
  9679. Longint(section,0);
  9680. Info(section,"awaitingLock*: ProcessQueue");
  9681. Address(section,0);
  9682. Address(section,0);
  9683. Info(section,"awaitingCond*: ProcessQueue");
  9684. Address(section,0);
  9685. Address(section,0);
  9686. Info(section,"lockedBy*: ANY");
  9687. Address(section,0);
  9688. Info(section,"lock*: ANY");
  9689. Address(section,0);
  9690. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9691. Longint(section,1);
  9692. FOR i := 2 TO 6 DO
  9693. Longint(section,0);
  9694. END;
  9695. END ProtectedHeapBlock;
  9696. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9697. BEGIN
  9698. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9699. END Info;
  9700. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9701. VAR op: IntermediateCode.Operand;
  9702. BEGIN
  9703. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9704. section.Emit(Data(-11,op));
  9705. END Address;
  9706. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9707. VAR op: IntermediateCode.Operand;
  9708. BEGIN
  9709. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9710. section.Emit(Data(-12,op));
  9711. END Size;
  9712. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9713. VAR op: IntermediateCode.Operand;
  9714. BEGIN
  9715. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9716. section.Emit(Data(-1,op));
  9717. END Set;
  9718. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9719. VAR op: IntermediateCode.Operand;
  9720. BEGIN
  9721. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9722. section.Emit(Data(-1,op));
  9723. END Longint;
  9724. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9725. VAR op,noOperand: IntermediateCode.Operand;
  9726. BEGIN
  9727. IntermediateCode.InitOperand(noOperand);
  9728. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9729. section.PatchOperands(pc,op,noOperand,noOperand);
  9730. END PatchLongint;
  9731. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9732. VAR op, noOperand: IntermediateCode.Operand;
  9733. BEGIN
  9734. IntermediateCode.InitOperand(noOperand);
  9735. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9736. section.PatchOperands(pc,op,noOperand,noOperand);
  9737. END PatchSymbol;
  9738. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9739. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9740. BEGIN
  9741. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9742. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9743. section.Emit(Data(-1,op));
  9744. END Boolean;
  9745. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9746. VAR op: IntermediateCode.Operand;
  9747. BEGIN
  9748. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9749. section.Emit(Data(-1,op));
  9750. END Char;
  9751. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9752. VAR i: LONGINT;
  9753. BEGIN
  9754. Info(section,str);
  9755. i := 0;
  9756. WHILE(str[i] # 0X) DO
  9757. Char(section,str[i]);
  9758. INC(i);
  9759. END;
  9760. Char(section,0X);
  9761. END String;
  9762. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9763. VAR op: IntermediateCode.Operand;
  9764. BEGIN
  9765. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9766. IntermediateCode.SetOffset(op,realOffset);
  9767. section.Emit(Data(-1,op));
  9768. END NamedSymbol;
  9769. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9770. BEGIN
  9771. IF symbol= NIL THEN
  9772. Address( section, realOffset);
  9773. ASSERT(virtualOffset = 0);
  9774. ELSE
  9775. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9776. END;
  9777. END Symbol;
  9778. (* OutPointers delivers
  9779. {pointerOffset}
  9780. *)
  9781. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9782. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9783. BEGIN
  9784. type := type.resolved;
  9785. IF type IS SyntaxTree.AnyType THEN
  9786. Symbol(section, symbol, 0, (offset ));
  9787. INC(numberPointers);
  9788. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9789. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9790. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9791. ELSIF type IS SyntaxTree.PointerType THEN
  9792. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9793. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9794. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9795. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9796. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9797. ELSIF (type IS SyntaxTree.RecordType) THEN
  9798. (* never treat a record like a pointer, even if the pointer field is set! *)
  9799. WITH type: SyntaxTree.RecordType DO
  9800. base := type.GetBaseRecord();
  9801. IF base # NIL THEN
  9802. Pointers(offset,symbol,section, base,numberPointers);
  9803. END;
  9804. variable := type.recordScope.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.CellType) THEN
  9813. WITH type: SyntaxTree.CellType DO
  9814. base := type.GetBaseRecord();
  9815. IF base # NIL THEN
  9816. Pointers(offset,symbol,section, base,numberPointers);
  9817. END;
  9818. variable := type.cellScope.firstVariable;
  9819. WHILE(variable # NIL) DO
  9820. IF ~(variable.untraced) THEN
  9821. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9822. END;
  9823. variable := variable.nextVariable;
  9824. END;
  9825. END;
  9826. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9827. WITH type: SyntaxTree.ArrayType DO
  9828. IF type.form= SyntaxTree.Static THEN
  9829. n := type.staticLength;
  9830. base := type.arrayBase.resolved;
  9831. WHILE(base IS SyntaxTree.ArrayType) DO
  9832. type := base(SyntaxTree.ArrayType);
  9833. n := n* type.staticLength;
  9834. base := type.arrayBase.resolved;
  9835. END;
  9836. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9837. IF SemanticChecker.ContainsPointer(base) THEN
  9838. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9839. FOR i := 0 TO n-1 DO
  9840. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9841. END;
  9842. END;
  9843. ELSE
  9844. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9845. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9846. END;
  9847. END;
  9848. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9849. WITH type: SyntaxTree.MathArrayType DO
  9850. IF type.form = SyntaxTree.Static THEN
  9851. n := type.staticLength;
  9852. base := type.arrayBase.resolved;
  9853. WHILE(base IS SyntaxTree.MathArrayType) DO
  9854. type := base(SyntaxTree.MathArrayType);
  9855. n := n* type.staticLength;
  9856. base := type.arrayBase.resolved;
  9857. END;
  9858. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9859. IF SemanticChecker.ContainsPointer(base) THEN
  9860. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9861. FOR i := 0 TO n-1 DO
  9862. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9863. END;
  9864. END;
  9865. ELSE
  9866. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9867. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9868. END
  9869. END;
  9870. (* ELSE no pointers in type *)
  9871. END;
  9872. END Pointers;
  9873. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9874. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9875. BEGIN
  9876. IF pool.Has(name) THEN
  9877. RETURN pool.GetInt(name)
  9878. ELSE
  9879. position := source.pc;
  9880. pool.PutInt(name, position);
  9881. StringPool.GetString(name, s);
  9882. Info(source, s);
  9883. i := 0;
  9884. REPEAT
  9885. ch := s[i]; INC(i);
  9886. Char( source, ch);
  9887. UNTIL ch = 0X;
  9888. END;
  9889. RETURN position;
  9890. END DynamicName;
  9891. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9892. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9893. BEGIN
  9894. COPY(moduleName,name);
  9895. Strings.Append(name,suffix);
  9896. Basic.ToSegmentedName(name, pooledName);
  9897. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9898. IF implementationVisitor.backend.cooperative THEN
  9899. Info(section, "TypeDescriptor");
  9900. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9901. NamedSymbol(section, pooledName,NIL, 0, 0);
  9902. BasePointer(section);
  9903. offset := 0;
  9904. ELSE
  9905. HeapBlock(mName,typeName,section,2);
  9906. Info(section, "HeapBlock");
  9907. (*
  9908. Symbol(section,section,2,0);
  9909. *)
  9910. Address(section,0); (* empty such that GC does not go on traversing *)
  9911. Info(section, "TypeDescriptor");
  9912. Address(section,0);
  9913. offset := section.pc;
  9914. END;
  9915. RETURN section
  9916. END Block;
  9917. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9918. VAR name: Basic.SegmentedName;
  9919. BEGIN
  9920. Info(source,"ArrayHeader");
  9921. IF implementationVisitor.backend.cooperative THEN
  9922. sizePC := source.pc;
  9923. Address(source,0);
  9924. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9925. IF baseType # "" THEN
  9926. Basic.ToSegmentedName(baseType, name);
  9927. NamedSymbol(source, name,NIL, 0, 0);
  9928. ELSE
  9929. Address(source,0);
  9930. END;
  9931. Address(source,0);
  9932. ELSE
  9933. Address(source,0);
  9934. Address(source,0);
  9935. Address(source,0);
  9936. sizePC := source.pc;
  9937. Address(source,0);
  9938. Info(source,"array data");
  9939. END;
  9940. END Array;
  9941. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9942. BEGIN
  9943. IF implementationVisitor.backend.cooperative THEN
  9944. PatchLongint(section, pc, size);
  9945. PatchLongint(section, pc + 3, size);
  9946. ELSE
  9947. PatchLongint(section, pc, size);
  9948. END;
  9949. END PatchArray;
  9950. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9951. VAR
  9952. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9953. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9954. poolMap: Basic.HashTableInt;
  9955. namePool: IntermediateCode.Section;
  9956. namePoolOffset: LONGINT;
  9957. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9958. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9959. BEGIN
  9960. Basic.SegmentedNameToString(s1.name,n1);
  9961. Basic.SegmentedNameToString(s2.name,n2);
  9962. index := 0;
  9963. ch1 := n1[index];
  9964. ch2 := n2[index];
  9965. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9966. INC(index);
  9967. ch1 := n1[index];
  9968. ch2 := n2[index];
  9969. END;
  9970. RETURN ch1 < ch2;
  9971. END Compare;
  9972. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9973. VAR
  9974. i, j: LONGINT;
  9975. x, t: Sections.Section;
  9976. BEGIN
  9977. IF lo < hi THEN
  9978. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9979. WHILE i <= j DO
  9980. WHILE Compare(list[i], x) DO INC(i) END;
  9981. WHILE Compare(x, list[j]) DO DEC(j) END;
  9982. IF i <= j THEN
  9983. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9984. INC(i); DEC(j)
  9985. END
  9986. END;
  9987. IF lo < j THEN QuickSort(list, lo, j) END;
  9988. IF i < hi THEN QuickSort(list, i, hi) END
  9989. END;
  9990. END QuickSort;
  9991. (*
  9992. ExportDesc* = RECORD
  9993. fp*: ADDRESS;
  9994. name* {UNTRACED}: DynamicName;
  9995. adr*: ADDRESS;
  9996. exports*: LONGINT;
  9997. dsc* {UNTRACED}: ExportArray
  9998. END;
  9999. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10000. *)
  10001. PROCEDURE ExportDesc2(
  10002. source: IntermediateCode.Section;
  10003. namePool: IntermediateCode.Section;
  10004. fingerPrinter: FingerPrinter.FingerPrinter;
  10005. symbol: Sections.Section;
  10006. name: StringPool.Index;
  10007. VAR patchAdr: LONGINT
  10008. ): BOOLEAN;
  10009. VAR fingerPrint: SyntaxTree.FingerPrint;
  10010. BEGIN
  10011. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10012. & (symbol.type # Sections.InlineCodeSection)
  10013. THEN
  10014. *)
  10015. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10016. & (symbol.type # Sections.InlineCodeSection))
  10017. THEN
  10018. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10019. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10020. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10021. Longint(source,fingerPrint.shallow);
  10022. ELSE
  10023. Longint(source, 0);
  10024. END;
  10025. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10026. Symbol(source, symbol,0,0);
  10027. patchAdr := source.pc;
  10028. Address(source,0);
  10029. Address(source,0);
  10030. END;
  10031. RETURN TRUE
  10032. ELSE
  10033. RETURN FALSE
  10034. END;
  10035. END ExportDesc2;
  10036. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10037. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10038. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10039. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10040. TYPE
  10041. Scope = RECORD
  10042. elements: LONGINT;
  10043. gelements: LONGINT;
  10044. section: IntermediateCode.Section;
  10045. patchAdr: LONGINT;
  10046. arraySizePC: LONGINT;
  10047. beginPC: LONGINT; (* current scope start pc *)
  10048. END;
  10049. BEGIN
  10050. Basic.InitSegmentedName(prev);
  10051. olevel := -1;
  10052. scopes[0].section := source;
  10053. FOR s := 0 TO LEN(sections)-1 DO
  10054. symbol := sections[s];
  10055. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10056. this := sections[s].name;
  10057. level := 0;
  10058. WHILE (this[level] > 0) DO
  10059. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10060. INC(level);
  10061. END;
  10062. WHILE level < olevel DO
  10063. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10064. IF olevel > 0 THEN
  10065. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10066. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10067. END;
  10068. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10069. DEC(olevel);
  10070. END;
  10071. IF (this[level] > 0) THEN
  10072. IF level > olevel THEN
  10073. (*TRACE("opening",level); *)
  10074. IF scopes[level].section = NIL THEN
  10075. arrayName := ".@ExportArray";
  10076. Strings.AppendInt(arrayName, level);
  10077. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10078. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10079. END;
  10080. scopes[level].beginPC := scopes[level].section.pc;
  10081. olevel := level;
  10082. scopes[olevel].elements := 0;
  10083. END;
  10084. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10085. sym := sections[s];
  10086. ELSE
  10087. sym := NIL;
  10088. END;
  10089. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10090. THEN
  10091. INC(scopes[olevel].elements);
  10092. END;
  10093. (*StringPool.GetString(this[level], name);*)
  10094. (*TRACE(level, name);*)
  10095. (* enter string in scope *)
  10096. INC(level);
  10097. END;
  10098. END;
  10099. Basic.SegmentedNameToString(this, name);
  10100. (*TRACE(level, "enter", name);*)
  10101. prev := this;
  10102. END;
  10103. END;
  10104. WHILE 0 <= olevel DO
  10105. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10106. IF olevel > 0 THEN
  10107. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10108. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10109. END;
  10110. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10111. DEC(olevel);
  10112. END;
  10113. level := 0;
  10114. WHILE (level < LEN(scopes)) DO
  10115. IF scopes[level].section # NIL THEN
  10116. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10117. END;
  10118. INC(level);
  10119. END;
  10120. END Export;
  10121. BEGIN
  10122. NEW(fingerPrinter, module.system);
  10123. NEW(poolMap, 64);
  10124. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10125. NEW(sectionArray, module.allSections.Length());
  10126. FOR i := 0 TO module.allSections.Length() - 1 DO
  10127. section := module.allSections.GetSection(i);
  10128. sectionArray[i] := section;
  10129. END;
  10130. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10131. Export(sectionArray^);
  10132. END ExportDesc;
  10133. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10134. VAR
  10135. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10136. BEGIN
  10137. Info(source, "exception table offsets array descriptor");
  10138. size := 0;
  10139. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10140. Info(source, "exception table content");
  10141. FOR i := 0 TO module.allSections.Length() - 1 DO
  10142. p := module.allSections.GetSection(i);
  10143. IF p.type = Sections.CodeSection THEN
  10144. finallyPC := p(IntermediateCode.Section).finally;
  10145. IF finallyPC>=0 THEN
  10146. Symbol( source, p, 0,0);
  10147. Symbol( source, p, finallyPC, 0);
  10148. Symbol( source, p, finallyPC,0);
  10149. INC(size);
  10150. END;
  10151. END
  10152. END;
  10153. PatchArray(source,sizePC,size);
  10154. END ExceptionArray;
  10155. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10156. VAR i: LONGINT; ch: CHAR;
  10157. BEGIN
  10158. i := 0;
  10159. REPEAT
  10160. ch := name[i]; INC(i);
  10161. Char( section, ch);
  10162. UNTIL ch = 0X;
  10163. WHILE i < 32 DO
  10164. Char( section, 0X); INC(i);
  10165. END;
  10166. END Name;
  10167. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10168. VAR i: LONGINT; ch: CHAR;
  10169. BEGIN
  10170. i := 0;
  10171. REPEAT
  10172. ch := name[i]; INC(i);
  10173. Char( section, ch);
  10174. UNTIL ch = 0X;
  10175. WHILE i < 128 DO
  10176. Char( section, 0X); INC(i);
  10177. END;
  10178. END LongName;
  10179. PROCEDURE References(section: IntermediateCode.Section);
  10180. CONST
  10181. rfDirect = 1X; rfIndirect = 3X;
  10182. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10183. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10184. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10185. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10186. rfArrayFlag = 80X;
  10187. VAR
  10188. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10189. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10190. VAR char: CHAR;
  10191. BEGIN
  10192. IF type = NIL THEN char := rfLongint
  10193. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10194. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10195. ELSIF type IS SyntaxTree.CharacterType THEN
  10196. IF type.sizeInBits = 8 THEN char := rfChar8
  10197. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10198. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10199. END;
  10200. ELSIF type IS SyntaxTree.IntegerType THEN
  10201. IF type.sizeInBits = 8 THEN char := rfShortint
  10202. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10203. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10204. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10205. END;
  10206. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10207. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10208. ELSIF type IS SyntaxTree.FloatType THEN
  10209. IF type.sizeInBits = 32 THEN char := rfReal
  10210. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10211. END;
  10212. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10213. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10214. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10215. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10216. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10217. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10218. END;
  10219. IF arrayOf THEN
  10220. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10221. ELSE
  10222. Char(section,char)
  10223. END;
  10224. INC(size);
  10225. END BaseType;
  10226. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10227. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10228. BEGIN
  10229. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10230. IF destination = NIL THEN
  10231. (* imported unused record type *)
  10232. Char(section,0X); (* nil type *)
  10233. INC(size);
  10234. type.typeDeclaration.GetName(name);
  10235. (*
  10236. 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
  10237. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10238. *)
  10239. ELSE
  10240. IF type.pointerType # NIL THEN
  10241. Char(section,rfRecordPointer)
  10242. ELSE
  10243. Char(section,rfRecord);
  10244. END;
  10245. INC(size);
  10246. Longint(section,(destination.offset ));
  10247. INC(size,4);
  10248. END;
  10249. END RecordType;
  10250. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10251. BEGIN
  10252. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10253. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10254. INC(size);
  10255. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10256. END;
  10257. IF type.form = SyntaxTree.Static THEN
  10258. Longint(section,type.staticLength)
  10259. ELSE
  10260. Longint(section,0)
  10261. END;
  10262. INC(size,4);
  10263. END ArrayType;
  10264. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10265. BEGIN
  10266. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10267. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType THEN
  10268. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10269. INC(size);
  10270. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10271. END;
  10272. IF type.form = SyntaxTree.Static THEN
  10273. Longint(section,type.staticLength)
  10274. ELSE
  10275. Longint(section,0)
  10276. END;
  10277. INC(size,4);
  10278. END MathArrayType;
  10279. PROCEDURE Type(type: SyntaxTree.Type);
  10280. BEGIN
  10281. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10282. IF type IS SyntaxTree.BasicType THEN
  10283. BaseType(FALSE,type)
  10284. ELSIF type IS SyntaxTree.RecordType THEN
  10285. RecordType(type(SyntaxTree.RecordType));
  10286. ELSIF type IS SyntaxTree.ArrayType THEN
  10287. ArrayType(type(SyntaxTree.ArrayType))
  10288. ELSIF type IS SyntaxTree.EnumerationType THEN
  10289. BaseType(FALSE,module.system.longintType)
  10290. ELSIF type IS SyntaxTree.PointerType THEN
  10291. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10292. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10293. ELSE
  10294. BaseType(FALSE,type)
  10295. END;
  10296. ELSIF type IS SyntaxTree.ProcedureType THEN
  10297. BaseType(FALSE,type);
  10298. ELSIF type IS SyntaxTree.MathArrayType THEN
  10299. MathArrayType(type(SyntaxTree.MathArrayType));
  10300. ELSIF type IS SyntaxTree.CellType THEN
  10301. BaseType(FALSE,module.system.anyType);
  10302. ELSE HALT(200)
  10303. END;
  10304. END Type;
  10305. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10306. VAR name: ARRAY 256 OF CHAR;
  10307. BEGIN
  10308. IF variable.externalName # NIL THEN RETURN END;
  10309. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10310. INC(size);
  10311. variable.GetName(name);
  10312. Type(variable.type);
  10313. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10314. INC(size,4);
  10315. String(section,name);
  10316. INC(size,Strings.Length(name)+1);
  10317. END WriteVariable;
  10318. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10319. VAR name: ARRAY 256 OF CHAR;
  10320. BEGIN
  10321. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10322. INC(size);
  10323. variable.GetName(name);
  10324. Type(variable.type);
  10325. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10326. INC(size,4);
  10327. variable.GetName(name);
  10328. String(section,name);
  10329. INC(size,Strings.Length(name)+1);
  10330. END WriteParameter;
  10331. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10332. BEGIN
  10333. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10334. IF type IS SyntaxTree.ArrayType THEN
  10335. WITH type: SyntaxTree.ArrayType DO
  10336. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10337. ELSE Char(section,rfOpenArray)
  10338. END;
  10339. INC(size);
  10340. END
  10341. ELSIF type IS SyntaxTree.MathArrayType THEN
  10342. WITH type: SyntaxTree.MathArrayType DO
  10343. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10344. ELSE Char(section,rfOpenArray)
  10345. END;
  10346. INC(size);
  10347. END
  10348. ELSIF type IS SyntaxTree.RecordType THEN
  10349. Char(section,rfRecord);
  10350. INC(size);
  10351. ELSE
  10352. BaseType(FALSE,type);
  10353. END;
  10354. END ReturnType;
  10355. PROCEDURE Procedure(s: Sections.Section);
  10356. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10357. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10358. name: ARRAY 256 OF CHAR;
  10359. BEGIN
  10360. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10361. procedure.GetName(name);
  10362. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10363. Char(section,0F9X);
  10364. INC(size);
  10365. Symbol(section,s,0,0);
  10366. INC(size,4);
  10367. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10368. INC(size,4);
  10369. Longint(section,procedureType.numberParameters);
  10370. INC(size,4);
  10371. ReturnType(procedureType.returnType);
  10372. Longint(section,0); (*! level *)
  10373. INC(size,4);
  10374. Longint(section,0);
  10375. INC(size,4);
  10376. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10377. (*
  10378. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10379. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10380. recordName := "";
  10381. IF record.pointerType # NIL THEN
  10382. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10383. ELSE
  10384. DeclarationName(record.typeDeclaration,recordName);
  10385. END;
  10386. i := 0;
  10387. Info(section,recordName);
  10388. WHILE recordName[i] # 0X DO
  10389. Char(section,recordName[i]); INC(i);
  10390. INC(size);
  10391. END;
  10392. Char(section,".");
  10393. INC(size);
  10394. END;
  10395. *)
  10396. String(section,name);
  10397. INC(size,Strings.Length(name)+1);
  10398. parameter := procedureType.firstParameter;
  10399. WHILE(parameter # NIL) DO
  10400. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10401. parameter := parameter.nextParameter;
  10402. END;
  10403. variable := procedure.procedureScope.firstVariable;
  10404. WHILE(variable # NIL) DO
  10405. WriteVariable(variable,FALSE);
  10406. variable := variable.nextVariable;
  10407. END;
  10408. END Procedure;
  10409. PROCEDURE Scope(s: Sections.Section);
  10410. BEGIN
  10411. Char(section,0F8X);
  10412. INC(size);
  10413. Symbol(section,s,0,0); (* start *)
  10414. INC(size,4);
  10415. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10416. INC(size,4);
  10417. String(section,"$$");
  10418. INC(size,3);
  10419. (* removed variables -- wrongly interpreted by Reflection
  10420. variable := module.module.moduleScope.firstVariable;
  10421. WHILE(variable # NIL) DO
  10422. WriteVariable(variable,FALSE);
  10423. variable := variable.nextVariable;
  10424. END;
  10425. *)
  10426. END Scope;
  10427. BEGIN
  10428. Array(section,sizePC,"");
  10429. size := 0;
  10430. Char(section,0FFX); (* sign for trap writer *)
  10431. INC(size);
  10432. FOR i := 0 TO module.allSections.Length() - 1 DO
  10433. s := module.allSections.GetSection(i);
  10434. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10435. Scope(s) (*! must be first procedure in ref section *)
  10436. END
  10437. END;
  10438. FOR i := 0 TO module.allSections.Length() - 1 DO
  10439. s := module.allSections.GetSection(i);
  10440. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10441. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10442. Procedure(s)
  10443. END
  10444. END;
  10445. PatchArray(section,sizePC,size);
  10446. END References;
  10447. (*
  10448. Command* = RECORD
  10449. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10450. name*: Name; (* name of the procedure *)
  10451. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10452. entryAdr* : ADDRESS; (* entry address of procedure *)
  10453. END;
  10454. *)
  10455. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10456. VAR
  10457. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10458. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10459. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10460. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10461. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10462. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10463. BEGIN
  10464. RETURN
  10465. (type = NIL) OR
  10466. (type.resolved IS SyntaxTree.RecordType) OR
  10467. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10468. (type.resolved IS SyntaxTree.AnyType);
  10469. END TypeAllowed;
  10470. BEGIN
  10471. numberParameters := procedureType.numberParameters;
  10472. RETURN
  10473. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10474. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10475. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10476. END GetProcedureAllowed;
  10477. PROCEDURE WriteType(type : SyntaxTree.Type);
  10478. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10479. name: Basic.SegmentedName; offset: LONGINT;
  10480. BEGIN
  10481. IF type = NIL THEN
  10482. Address(source,0);
  10483. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10484. Address(source,1);
  10485. ELSE
  10486. type := type.resolved;
  10487. IF type IS SyntaxTree.PointerType THEN
  10488. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10489. END;
  10490. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10491. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10492. name[0] := typeDeclaration.name; name[1] := -1;
  10493. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10494. ASSERT(section # NIL);
  10495. ELSE
  10496. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10497. (* TODO *)
  10498. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10499. END;
  10500. IF implementationVisitor.backend.cooperative THEN
  10501. offset := 0;
  10502. ELSE
  10503. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10504. END;
  10505. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10506. END;
  10507. END WriteType;
  10508. BEGIN
  10509. Info(source, "command array descriptor");
  10510. Array(source,sizePC,"Modules.Command");
  10511. numberCommands := 0;
  10512. Info(source, "command array content");
  10513. FOR i := 0 TO module.allSections.Length() - 1 DO
  10514. p := module.allSections.GetSection(i);
  10515. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10516. procedure := p.symbol(SyntaxTree.Procedure);
  10517. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10518. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10519. procedure.GetName(name);
  10520. Name(source,name);
  10521. numberParameters := procedureType.numberParameters;
  10522. (* offset of type of first parameter *)
  10523. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10524. ELSE WriteType(procedureType.firstParameter.type)
  10525. END;
  10526. (* offset of type of return parameter *)
  10527. WriteType(procedureType.returnType);
  10528. (* command name *)
  10529. (* command code offset *)
  10530. Symbol(source,p,0,0);
  10531. INC(numberCommands);
  10532. IF Trace THEN
  10533. D.Ln;
  10534. END;
  10535. END;
  10536. END
  10537. END;
  10538. PatchArray(source,sizePC,numberCommands);
  10539. END CommandArray;
  10540. (* to prevent from double import of different module aliases *)
  10541. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10542. VAR i: SyntaxTree.Import;
  10543. BEGIN
  10544. i := module.module.moduleScope.firstImport;
  10545. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10546. i := i.nextImport;
  10547. END;
  10548. RETURN i = import
  10549. END IsFirstDirectOccurence;
  10550. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10551. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10552. BEGIN
  10553. Array(source,pc,"");
  10554. Info(source, "import module array data");
  10555. IF implementationVisitor.backend.cooperative THEN
  10556. offset := 0;
  10557. ELSE
  10558. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10559. END;
  10560. import := module.module.moduleScope.firstImport;
  10561. numberImports := 0;
  10562. WHILE import # NIL DO
  10563. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10564. Global.GetModuleSegmentedName(import.module,name);
  10565. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10566. NamedSymbol(source, name, NIL, 0, offset);
  10567. INC(numberImports);
  10568. END;
  10569. import := import.nextImport
  10570. END;
  10571. PatchArray(source,pc,numberImports);
  10572. END ImportsArray;
  10573. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10574. VAR
  10575. p: Sections.Section; sizePC, size, i: LONGINT;
  10576. BEGIN
  10577. Info(source, "Type info section");
  10578. size := 0;
  10579. Array(source,sizePC,"Modules.TypeDesc");
  10580. FOR i := 0 TO module.allSections.Length() - 1 DO
  10581. p := module.allSections.GetSection(i);
  10582. WITH p: IntermediateCode.Section DO
  10583. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10584. Symbol(source,p,0,0);
  10585. INC(size);
  10586. END;
  10587. END
  10588. END;
  10589. PatchArray(source,sizePC,size);
  10590. END TypeInfoSection;
  10591. (*
  10592. ProcTableEntry* = RECORD
  10593. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10594. noPtr*: LONGINT;
  10595. END;
  10596. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10597. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10598. *)
  10599. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10600. VAR
  10601. destination: Sections.Section;
  10602. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10603. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10604. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10605. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10606. BEGIN
  10607. Info(procArray,"pcFrom");
  10608. Symbol(procArray,destination,0,0);
  10609. Info(procArray,"pcTo");
  10610. Symbol(procArray,destination,destination.pc,0);
  10611. Info(procArray,"pcStatementBegin");
  10612. Symbol(procArray,destination,destination.validPAFEnter,0);
  10613. Info(procArray,"pcStatementEnd");
  10614. Symbol(procArray,destination,destination.validPAFExit,0);
  10615. IF ~implementationVisitor.backend.cooperative THEN
  10616. Basic.SegmentedNameToString(destination.name, string);
  10617. Info(pointerArray,string);
  10618. procedure := destination.symbol(SyntaxTree.Procedure);
  10619. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10620. variable := procedure.procedureScope.firstVariable;
  10621. WHILE(variable # NIL) DO
  10622. IF ~(variable.untraced) THEN
  10623. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10624. END;
  10625. variable := variable.nextVariable
  10626. END;
  10627. parameter := procedureType.firstParameter;
  10628. WHILE(parameter # NIL) DO
  10629. IF ~(parameter.untraced) THEN
  10630. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10631. END;
  10632. parameter := parameter.nextParameter;
  10633. END;
  10634. END;
  10635. Info(procArray,"numberPointers");
  10636. Longint(procArray,numberPointers);
  10637. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10638. INC(pointerArraySize, numberPointers);
  10639. END PointerOffsets;
  10640. BEGIN
  10641. maxPointers := 0;
  10642. Info(procArray, "proc array descriptor");
  10643. Address(procArray,0);
  10644. Address(procArray,0);
  10645. Address(procArray,0);
  10646. procArraySizePC := procArray.pc;
  10647. Address(procArray,0);
  10648. procArraySize := 0;
  10649. IF ~implementationVisitor.backend.cooperative THEN
  10650. Info(pointerArray, "pointer array descriptor");
  10651. Address(pointerArray,0);
  10652. Address(pointerArray,0);
  10653. Address(pointerArray,0);
  10654. pointerArraySizePC := pointerArray.pc;
  10655. Address(pointerArray,0);
  10656. pointerArraySize := 0;
  10657. END;
  10658. procArraySize := 0;
  10659. FOR i := 0 TO module.allSections.Length() - 1 DO
  10660. destination := module.allSections.GetSection(i);
  10661. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10662. PointerOffsets(destination(IntermediateCode.Section));
  10663. INC(procArraySize);
  10664. END
  10665. END;
  10666. PatchLongint(procArray,procArraySizePC,procArraySize);
  10667. IF ~implementationVisitor.backend.cooperative THEN
  10668. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10669. END;
  10670. END PointersInProcTables;
  10671. (*
  10672. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10673. VAR
  10674. next*: Module; (** once a module is published, all fields are read-only *)
  10675. name*: Name;
  10676. init, published: BOOLEAN;
  10677. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10678. sb*: ADDRESS; <- set to beginning of data section by loader
  10679. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10680. command*: POINTER TO ARRAY OF Command;
  10681. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10682. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10683. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10684. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10685. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10686. data*, code*: Bytes;
  10687. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10688. export*: ExportDesc;
  10689. term*: TerminationHandler;
  10690. exTable*: ExceptionTable;
  10691. noProcs*: LONGINT;
  10692. firstProc*: ADDRESS; <- done by loader
  10693. maxPtrs*: LONGINT;
  10694. crc*: LONGINT;
  10695. *)
  10696. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10697. BEGIN
  10698. Info(section, "cycle");
  10699. Size(section,0);
  10700. Info(section, "references");
  10701. Size(section,0);
  10702. Info(section, "nextMarked");
  10703. Address(section,0);
  10704. Info(section, "nextWatched");
  10705. Address(section,0);
  10706. END BasePointer;
  10707. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10708. BEGIN
  10709. BasePointer(section);
  10710. Info(section, "action");
  10711. Address(section,0);
  10712. Info(section, "monitor");
  10713. Address(section,0);
  10714. END BaseObject;
  10715. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10716. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10717. pooledName: Basic.SegmentedName;
  10718. symbol: SyntaxTree.Symbol;
  10719. BEGIN
  10720. Global.GetModuleName(module.module,name);
  10721. Strings.Append(name,".@Module.@Descriptor");
  10722. Basic.ToSegmentedName(name, pooledName);
  10723. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10724. Symbol(section,descriptorSection,0,0);
  10725. Info(descriptorSection, "descriptor");
  10726. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10727. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10728. Address(descriptorSection,0);
  10729. Global.GetModuleName(module.module,name);
  10730. Strings.Append(name,".@Trace");
  10731. Basic.ToSegmentedName(name, pooledName);
  10732. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10733. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10734. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10735. END ModuleDescriptor;
  10736. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10737. VAR name: ARRAY 128 OF CHAR;
  10738. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10739. symbol: SyntaxTree.Symbol;
  10740. BEGIN
  10741. Global.GetModuleName(module.module,name);
  10742. Strings.Append(name,".@Module");
  10743. Basic.ToSegmentedName(name, pooledName);
  10744. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10745. moduleSection.SetExported(TRUE);
  10746. IF moduleSection.pc = 0 THEN
  10747. IF implementationVisitor.backend.cooperative THEN
  10748. Info(moduleSection, "descriptor");
  10749. ModuleDescriptor(moduleSection);
  10750. BaseObject(moduleSection);
  10751. implementationVisitor.CreateTraceModuleMethod(module.module);
  10752. ELSE
  10753. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10754. Info(moduleSection, "HeapBlock");
  10755. Symbol(moduleSection,moduleSection,2,0);
  10756. Info(moduleSection, "TypeDescriptor");
  10757. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10758. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10759. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10760. END;
  10761. END;
  10762. RETURN moduleSection;
  10763. END ModuleSection;
  10764. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10765. VAR
  10766. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10767. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10768. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10769. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10770. referenceSectionOffset : LONGINT;
  10771. BEGIN
  10772. Global.GetModuleName(module.module,moduleName);
  10773. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10774. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10775. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10776. ImportsArray(importSection);
  10777. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10778. CommandArray(commandsSection);
  10779. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10780. ExceptionArray(exceptionSection);
  10781. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10782. TypeInfoSection(typeInfoSection);
  10783. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10784. References(referenceSection);
  10785. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10786. IF ~implementationVisitor.backend.cooperative THEN
  10787. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10788. ELSE
  10789. ptrTableSection := NIL;
  10790. END;
  10791. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10792. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10793. Array(emptyArraySection,temp,"");
  10794. moduleSection := ModuleSection();
  10795. Info(moduleSection, "nextRoot*: RootObject");
  10796. Address(moduleSection,0);
  10797. Info(moduleSection, "next*: Module");
  10798. Address(moduleSection,0);
  10799. Info(moduleSection, "name*: Name");
  10800. Name(moduleSection,moduleName);
  10801. Info(moduleSection, "init, published: BOOLEAN");
  10802. Boolean(moduleSection,FALSE);
  10803. Boolean(moduleSection,FALSE);
  10804. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10805. Boolean(moduleSection,FALSE);
  10806. Boolean(moduleSection,FALSE);
  10807. Info(moduleSection, "refcnt*: LONGINT");
  10808. Longint(moduleSection,0);
  10809. Info(moduleSection, "sb*: ADDRESS");
  10810. Address(moduleSection,0);
  10811. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10812. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10813. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10814. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10815. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10816. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10817. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10818. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10819. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10820. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10821. Info(moduleSection, "procTable*: ProcTable");
  10822. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10823. Info(moduleSection, "ptrTable*: PtrTable");
  10824. IF ~implementationVisitor.backend.cooperative THEN
  10825. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10826. ELSE
  10827. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10828. END;
  10829. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10830. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10831. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10832. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10833. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10834. Info(moduleSection, "export*: ExportDesc");
  10835. ExportDesc(moduleSection);
  10836. Info(moduleSection, "term*: TerminationHandler");
  10837. Address(moduleSection,0);
  10838. Info(moduleSection, "exTable*: ExceptionTable");
  10839. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10840. Info(moduleSection, "noProcs*: LONGINT");
  10841. Longint(moduleSection,numberProcs);
  10842. Info(moduleSection, "firstProc*: ADDRESS");
  10843. Address(moduleSection,0);
  10844. Info(moduleSection, "maxPtrs*: LONGINT");
  10845. Longint(moduleSection,maxPointers);
  10846. Info(moduleSection, "crc*: LONGINT");
  10847. Longint(moduleSection, 0); (*! must be implemented *)
  10848. Info(moduleSection, "body*: ADDRESS");
  10849. Symbol(moduleSection, bodyProc, 0,0);
  10850. IF implementationVisitor.backend.cooperative THEN
  10851. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10852. Info(moduleSection, "monitor.owner");
  10853. Address(moduleSection,0);
  10854. Info(moduleSection, "monitor.nestingLevel");
  10855. Address(moduleSection,0);
  10856. Info(moduleSection, "monitor.blockedQueue");
  10857. Address(moduleSection,0); Address(moduleSection,0);
  10858. Info(moduleSection, "monitor.waitingQueue");
  10859. Address(moduleSection,0); Address(moduleSection,0);
  10860. Info(moduleSection, "monitor.waitingSentinel");
  10861. Address(moduleSection,0);
  10862. END;
  10863. END Module;
  10864. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10865. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10866. BEGIN
  10867. Array(source,pc,"");
  10868. Info(source, "pointer offsets array data");
  10869. IF scope IS SyntaxTree.RecordScope THEN
  10870. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10871. ELSIF scope IS SyntaxTree.CellScope THEN
  10872. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10873. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10874. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10875. WHILE variable # NIL DO
  10876. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10877. symbol := module.allSections.FindBySymbol(variable);
  10878. ASSERT(symbol # NIL);
  10879. Pointers(0,symbol, source,variable.type,numberPointers);
  10880. END;
  10881. variable := variable.nextVariable;
  10882. END;
  10883. END;
  10884. PatchArray(source,pc,numberPointers);
  10885. END PointerArray;
  10886. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10887. VAR recordType: SyntaxTree.RecordType;
  10888. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10889. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10890. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10891. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10892. segmentedName: Basic.SegmentedName;
  10893. td: SyntaxTree.TypeDeclaration;
  10894. BEGIN
  10895. Array(source,pc,"Modules.FieldEntry");
  10896. Info(source, "FieldArray");
  10897. IF x IS SyntaxTree.RecordType THEN
  10898. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10899. ELSE
  10900. variable := NIL;
  10901. END;
  10902. size :=0;
  10903. WHILE variable # NIL DO
  10904. type := variable.type.resolved;
  10905. Info(source,"offset");
  10906. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10907. Info(source,"type class");
  10908. IF type IS SyntaxTree.PointerType THEN
  10909. Size(source, 1);
  10910. ELSIF type IS SyntaxTree.RecordType THEN
  10911. Size(source, 2);
  10912. ELSIF type IS SyntaxTree.NumberType THEN
  10913. Size(source, 3);
  10914. ELSE
  10915. Size(source, 0);
  10916. END;
  10917. Info(source, "type desc");
  10918. IF type IS SyntaxTree.RecordType THEN
  10919. td := type(SyntaxTree.RecordType).typeDeclaration;
  10920. Global.GetSymbolSegmentedName(td,segmentedName);
  10921. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10922. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10923. ELSE
  10924. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10925. END;
  10926. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10927. Symbol(source, tir, 0, offset);
  10928. ELSE
  10929. Address(source, 0);
  10930. END;
  10931. Info(source,"flags");
  10932. Set(source, {});
  10933. Info(source,"name");
  10934. variable.GetName(name);
  10935. LongName(source, name);
  10936. variable := variable.nextVariable;
  10937. INC(size);
  10938. END;
  10939. PatchArray(source,pc,size);
  10940. END FieldArray;
  10941. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10942. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10943. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10944. sectionName: Basic.SectionName;
  10945. CONST MPO=-40000000H;
  10946. BEGIN
  10947. (*
  10948. TypeDesc* = POINTER TO RECORD
  10949. descSize: LONGINT;
  10950. sentinel: LONGINT; (* = MPO-4 *)
  10951. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10952. flags*: SET;
  10953. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10954. name*: Name;
  10955. END;
  10956. *)
  10957. (* source := module.sections.FindByName(...) *)
  10958. Global.GetSymbolSegmentedName(td,name);
  10959. Basic.AppendToSegmentedName(name,"@Info");
  10960. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10961. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10962. Address(source,MPO-4);
  10963. Info(source, "type tag pointer");
  10964. Symbol( source, tag, offset, 0);
  10965. Info(source, "type flags");
  10966. flags := {};
  10967. IF isProtected THEN INCL(flags,31) END;
  10968. Set( source, flags);
  10969. Info(source, "pointer to module");
  10970. moduleSection := ModuleSection();
  10971. Symbol( source, moduleSection, moduleSection.pc,0);
  10972. Info(source, "type name");
  10973. i := 0;
  10974. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10975. (*
  10976. Global.GetSymbolSegmentedName(td,name);
  10977. Basic.SegmentedNameToString(name, sectionName);
  10978. *)
  10979. Name(source,sectionName);
  10980. source.SetReferenced(FALSE);
  10981. Global.GetSymbolSegmentedName(td,name);
  10982. Basic.AppendToSegmentedName(name,"@Fields");
  10983. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10984. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10985. Info(fieldSection, "HeapBlock");
  10986. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10987. Info(fieldSection, "TypeDescriptor");
  10988. Address(fieldSection,0);
  10989. Info(source, "FieldArray ref");
  10990. Symbol(source, fieldSection, fieldSection.pc, 0);
  10991. FieldArray(fieldSection);
  10992. RETURN source;
  10993. END NewTypeDescriptorInfo;
  10994. PROCEDURE NewTypeDescriptor;
  10995. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  10996. procedure: SyntaxTree.Procedure;
  10997. baseRecord: SyntaxTree.RecordType; baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  10998. numberPointers: LONGINT; padding,offset, i: LONGINT;
  10999. CONST MPO=-40000000H;
  11000. PROCEDURE TdTable(size: LONGINT);
  11001. VAR i: LONGINT;
  11002. BEGIN
  11003. Info(source, "tag table");
  11004. baseRecord := recordType;
  11005. i := 0;
  11006. WHILE baseRecord # NIL DO
  11007. INC(i);
  11008. baseRecord := baseRecord.GetBaseRecord();
  11009. END;
  11010. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11011. WHILE i < size DO
  11012. Address(source,0);
  11013. INC(i);
  11014. END;
  11015. baseRecord := recordType;
  11016. WHILE baseRecord # NIL DO
  11017. baseTD := baseRecord.typeDeclaration;
  11018. Global.GetSymbolSegmentedName(baseTD,name);
  11019. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11020. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11021. ELSE
  11022. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11023. END;
  11024. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*module.system.addressSize);
  11025. Symbol(source, tir, 0, offset);
  11026. baseRecord := baseRecord.GetBaseRecord();
  11027. END;
  11028. END TdTable;
  11029. PROCEDURE MethodTable;
  11030. VAR i,methods: LONGINT;
  11031. BEGIN
  11032. Info(source, "method table");
  11033. IF recordType # NIL THEN
  11034. methods := recordType.recordScope.numberMethods;
  11035. FOR i := methods-1 TO 0 BY -1 DO
  11036. procedure := recordType.recordScope.FindMethod(i);
  11037. Global.GetSymbolSegmentedName(procedure, name);
  11038. NamedSymbol(source, name,procedure, 0,0);
  11039. END;
  11040. END;
  11041. END MethodTable;
  11042. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11043. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11044. BEGIN
  11045. Info(source, "method table");
  11046. baseRecord := recordType;
  11047. WHILE baseRecord.baseType # NIL DO
  11048. baseRecord := baseRecord.GetBaseRecord ();
  11049. END;
  11050. GetRecordTypeName (baseRecord, name);
  11051. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11052. IF name = stackFrame THEN
  11053. start := 0;
  11054. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11055. baseRecord := recordType;
  11056. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11057. baseRecord := baseRecord.GetBaseRecord ();
  11058. END;
  11059. IF baseRecord # NIL THEN
  11060. GetRecordTypeName (baseRecord, name);
  11061. IF pointer & ~baseRecord.isObject THEN
  11062. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11063. END;
  11064. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11065. ELSIF recordType.isObject THEN
  11066. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11067. ELSIF pointer THEN
  11068. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11069. ELSE
  11070. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11071. END;
  11072. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11073. Symbol(source, tir, 0, 0);
  11074. start := 0;
  11075. baseRecord := recordType;
  11076. WHILE (baseRecord # NIL) DO
  11077. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11078. baseRecord := baseRecord.GetBaseRecord ();
  11079. END;
  11080. ELSE
  11081. (* explicit trace method: *)
  11082. procedure := recordType.recordScope.FindMethod(0);
  11083. IF ~procedure.isFinalizer THEN
  11084. Global.GetSymbolSegmentedName(procedure, name);
  11085. NamedSymbol(source, name,procedure, 0,0);
  11086. END;
  11087. start := 1;
  11088. END;
  11089. IF (name # stackFrame) & recordType.isObject THEN
  11090. baseRecord := recordType;
  11091. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11092. baseRecord := baseRecord.GetBaseRecord ();
  11093. END;
  11094. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11095. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11096. ELSE
  11097. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11098. END;
  11099. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11100. Symbol(source, tir, 0, 0);
  11101. END;
  11102. methods := recordType.recordScope.numberMethods;
  11103. FOR i := start TO methods-1 DO
  11104. procedure := recordType.recordScope.FindMethod(i);
  11105. IF ~procedure.isFinalizer THEN
  11106. Global.GetSymbolSegmentedName(procedure, name);
  11107. NamedSymbol(source, name,procedure, 0,0);
  11108. END;
  11109. END;
  11110. END CooperativeMethodTable;
  11111. BEGIN
  11112. Global.GetSymbolSegmentedName(td,name);
  11113. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11114. source.SetExported(IsExported(td));
  11115. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11116. IF implementationVisitor.backend.cooperative THEN
  11117. base := NIL;
  11118. baseRecord := recordType.GetBaseRecord();
  11119. IF baseRecord # NIL THEN
  11120. baseTD := baseRecord.typeDeclaration;
  11121. END;
  11122. IF ~recordType.isObject THEN
  11123. Info(source, "parent");
  11124. IF baseRecord # NIL THEN
  11125. Global.GetSymbolSegmentedName(baseTD,name);
  11126. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11127. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11128. ELSE
  11129. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11130. END;
  11131. Symbol(source, tir, 0, 0);
  11132. ELSE
  11133. Address(source,0);
  11134. END;
  11135. Info(source, "record size");
  11136. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11137. source.SetReferenced(FALSE);
  11138. CooperativeMethodTable(FALSE);
  11139. base := source;
  11140. Global.GetSymbolSegmentedName(td,name);
  11141. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11142. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11143. source.SetExported(IsExported(td));
  11144. source.SetReferenced(FALSE);
  11145. END;
  11146. Info(source, "parent");
  11147. IF baseRecord # NIL THEN
  11148. Global.GetSymbolSegmentedName(baseTD,name);
  11149. sym := baseTD;
  11150. IF ~recordType.isObject THEN
  11151. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11152. sym := NIL;
  11153. END;
  11154. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11155. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11156. ELSE
  11157. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11158. END;
  11159. Symbol(source, tir, 0, 0);
  11160. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11161. Address(source,0);
  11162. ELSE
  11163. IF recordType.isObject THEN
  11164. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11165. ELSE
  11166. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11167. END;
  11168. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11169. Symbol(source, tir, 0, 0);
  11170. END;
  11171. Info(source, "base record descriptor");
  11172. Symbol(source, base, 0, 0);
  11173. CooperativeMethodTable(TRUE);
  11174. source.SetReferenced(FALSE);
  11175. IF recordType.hasPointers THEN
  11176. IF ~HasExplicitTraceMethod (recordType) THEN
  11177. implementationVisitor.CreateTraceMethod(recordType);
  11178. END;
  11179. implementationVisitor.CreateResetProcedure(recordType);
  11180. implementationVisitor.CreateAssignProcedure(recordType);
  11181. END;
  11182. ELSIF ~simple THEN
  11183. (*
  11184. MethodEnd = MPO
  11185. ---
  11186. methods (# methods)
  11187. ---
  11188. tags (16)
  11189. ---
  11190. TypeDesc = TypeInfoAdr
  11191. ---
  11192. td adr ---> rec size
  11193. ----
  11194. pointer offsets
  11195. ----
  11196. (padding)
  11197. -----
  11198. empty [2 addresses aligned]
  11199. empty
  11200. empty
  11201. numPtrs
  11202. ---
  11203. pointer offsets
  11204. ---
  11205. *)
  11206. Info(source, "MethodEnd = MPO");
  11207. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11208. source(IntermediateCode.Section).Emit(Data(-1,op));
  11209. MethodTable;
  11210. TdTable(TypeTags);
  11211. Info(source, "type descriptor info pointer");
  11212. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11213. IF (cellType # NIL) THEN
  11214. IF cellType.sizeInBits < 0 THEN
  11215. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11216. END;
  11217. Info(source, "cell size");
  11218. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11219. ELSE
  11220. Info(source, "record size");
  11221. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11222. END;
  11223. Info(source, "pointer offsets pointer");
  11224. padding := 1- source.pc MOD 2;
  11225. Symbol(source, source, source.pc+1+padding,0);
  11226. IF padding >0 THEN
  11227. Info(source, "padding");
  11228. FOR i := 1 TO padding DO Address(source,0) END;
  11229. END;
  11230. IF cellType # NIL THEN
  11231. PointerArray(source, cellType.cellScope, numberPointers);
  11232. ELSE
  11233. PointerArray(source, recordType.recordScope, numberPointers);
  11234. END;
  11235. ELSE
  11236. (*
  11237. simple:
  11238. methods
  11239. tag(2)
  11240. tag(1)
  11241. td adr ---> tag(0)
  11242. td adr ---> size
  11243. *)
  11244. MethodTable;
  11245. TdTable(TypeTags);
  11246. Info(source, "record size");
  11247. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11248. source.SetReferenced(FALSE);
  11249. END;
  11250. END NewTypeDescriptor;
  11251. BEGIN
  11252. x := x.resolved;
  11253. IF (x IS SyntaxTree.PointerType) THEN
  11254. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11255. END;
  11256. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11257. recordType := x(SyntaxTree.RecordType);
  11258. td := x.typeDeclaration;
  11259. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11260. ASSERT(td # NIL);
  11261. section := module.allSections.FindBySymbol(td); (* TODO *)
  11262. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11263. IF implementationVisitor.newObjectFile THEN
  11264. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11265. NewTypeDescriptor
  11266. END;
  11267. ELSE
  11268. (* data section in intermediate code *)
  11269. Global.GetSymbolSegmentedName(td,name);
  11270. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11271. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11272. tir.Emit(Data(-1,op));
  11273. END;
  11274. END;
  11275. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11276. cellType := x(SyntaxTree.CellType);
  11277. td := x.typeDeclaration;
  11278. section := module.allSections.FindBySymbol(td); (* TODO *)
  11279. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11280. IF implementationVisitor.newObjectFile THEN
  11281. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11282. NewTypeDescriptor
  11283. END;
  11284. END;
  11285. END;
  11286. END
  11287. END CheckTypeDeclaration
  11288. END MetaDataGenerator;
  11289. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11290. VAR
  11291. trace-: BOOLEAN;
  11292. traceString-: SyntaxTree.IdentifierString;
  11293. traceModuleName-: SyntaxTree.IdentifierString;
  11294. newObjectFile-: BOOLEAN;
  11295. profile-: BOOLEAN;
  11296. noRuntimeChecks: BOOLEAN;
  11297. simpleMetaData-: BOOLEAN;
  11298. noAsserts: BOOLEAN;
  11299. optimize-: BOOLEAN;
  11300. cooperative-: BOOLEAN;
  11301. preregisterStatic-: BOOLEAN;
  11302. dump-: Basic.Writer;
  11303. cellsAreObjects: BOOLEAN;
  11304. PROCEDURE &InitIntermediateBackend*;
  11305. BEGIN
  11306. simpleMetaData := FALSE;
  11307. newObjectFile := FALSE;
  11308. InitBackend;
  11309. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11310. SetTraceModuleName(DefaultTraceModuleName);
  11311. END InitIntermediateBackend;
  11312. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11313. VAR
  11314. declarationVisitor: DeclarationVisitor;
  11315. implementationVisitor: ImplementationVisitor;
  11316. module: Sections.Module;
  11317. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11318. meta: MetaDataGenerator;
  11319. BEGIN
  11320. ResetError;
  11321. Global.GetSymbolName(x,name);
  11322. IF activeCellsSpecification # NIL THEN
  11323. GetDescription(instructionSet);
  11324. activeCellsSpecification.SetInstructionSet(instructionSet)
  11325. END;
  11326. NEW(module,x,system); (* backend structures *)
  11327. Global.GetModuleName(x, name);
  11328. module.SetModuleName(name);
  11329. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11330. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11331. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11332. declarationVisitor.Module(x,module);
  11333. IF newObjectFile & ~meta.simple THEN
  11334. meta.Module(implementationVisitor.moduleBodySection);
  11335. END;
  11336. GetDescription(platformName);
  11337. module.SetPlatformName(platformName);
  11338. RETURN module
  11339. END GenerateIntermediate;
  11340. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11341. BEGIN RETURN TRUE
  11342. END SupportedImmediate;
  11343. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11344. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11345. END ProcessSyntaxTreeModule;
  11346. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11347. VAR
  11348. result: Sections.Module;
  11349. traceName: Basic.MessageString;
  11350. BEGIN
  11351. ASSERT(intermediateCodeModule IS Sections.Module);
  11352. result := intermediateCodeModule(Sections.Module);
  11353. IF trace THEN
  11354. traceName := "intermediate code trace: ";
  11355. Strings.Append(traceName,traceString);
  11356. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11357. IF (traceString="") OR (traceString="*") THEN
  11358. result.Dump(dump);
  11359. dump.Update
  11360. ELSE
  11361. Sections.DumpFiltered(dump, result, traceString);
  11362. END
  11363. END;
  11364. RETURN result
  11365. END ProcessIntermediateCodeModule;
  11366. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11367. BEGIN instructionSet := "Intermediate";
  11368. END GetDescription;
  11369. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11370. BEGIN
  11371. SELF.newObjectFile := newObjectFile;
  11372. SELF.simpleMetaData := simpleMetaData;
  11373. END SetNewObjectFile;
  11374. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11375. BEGIN COPY(name, traceModuleName)
  11376. END SetTraceModuleName;
  11377. PROCEDURE DefineOptions(options: Options.Options);
  11378. BEGIN
  11379. DefineOptions^(options);
  11380. options.Add(0X,"trace",Options.String);
  11381. options.Add(0X,"runtime",Options.String);
  11382. options.Add(0X,"newObjectFile",Options.Flag);
  11383. options.Add(0X,"traceModule",Options.String);
  11384. options.Add(0X,"profile",Options.Flag);
  11385. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11386. options.Add(0X,"noAsserts",Options.Flag);
  11387. options.Add(0X,"metaData",Options.String);
  11388. options.Add('o',"optimize", Options.Flag);
  11389. options.Add(0X,"preregisterStatic", Options.Flag);
  11390. options.Add(0X,"cellsAreObjects", Options.Flag);
  11391. END DefineOptions;
  11392. PROCEDURE GetOptions(options: Options.Options);
  11393. VAR name,string: SyntaxTree.IdentifierString;
  11394. BEGIN
  11395. GetOptions^(options);
  11396. trace := options.GetString("trace",traceString);
  11397. profile := options.GetFlag("profile");
  11398. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11399. noAsserts := options.GetFlag("noAsserts");
  11400. cooperative := options.GetFlag("cooperative");
  11401. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11402. IF cooperative THEN
  11403. SetRuntimeModuleName("CPU") END
  11404. END;
  11405. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11406. simpleMetaData := TRUE
  11407. END;
  11408. IF options.GetString("metaData",string) THEN
  11409. IF string = "simple" THEN simpleMetaData := TRUE
  11410. ELSIF string ="full" THEN simpleMetaData := FALSE
  11411. END;
  11412. END;
  11413. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11414. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11415. optimize := options.GetFlag("optimize");
  11416. preregisterStatic := options.GetFlag("preregisterStatic");
  11417. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11418. END GetOptions;
  11419. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11420. BEGIN RETURN SymbolFileFormat.Get()
  11421. END DefaultSymbolFileFormat;
  11422. END IntermediateBackend;
  11423. (* ----------------------------------- register allocation ------------------------------------- *)
  11424. (* register mapping scheme
  11425. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11426. spill offset
  11427. part(n) --> ticket <--> physical register
  11428. spill offset
  11429. *)
  11430. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11431. emptyOperand: IntermediateCode.Operand;
  11432. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11433. statCoopResetVariables: LONGINT;
  11434. statCoopModifyAssignments: LONGINT;
  11435. modifyAssignmentsPC : LONGINT;
  11436. statCoopNilCheck: LONGINT;
  11437. statCoopSwitch: LONGINT;
  11438. statCoopAssignProcedure: LONGINT;
  11439. statCoopTraceMethod: LONGINT;
  11440. statCoopResetProcedure: LONGINT;
  11441. statCoopTraceModule: LONGINT;
  11442. PROCEDURE ResetStatistics*;
  11443. BEGIN
  11444. statCoopResetVariables := 0;
  11445. statCoopModifyAssignments := 0;
  11446. statCoopNilCheck:= 0;
  11447. statCoopSwitch:= 0;
  11448. statCoopAssignProcedure:= 0;
  11449. statCoopTraceMethod:= 0;
  11450. statCoopResetProcedure:= 0;
  11451. statCoopTraceModule:= 0;
  11452. END ResetStatistics;
  11453. PROCEDURE Statistics*;
  11454. BEGIN
  11455. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11456. TRACE(statCoopNilCheck, statCoopSwitch);
  11457. TRACE(statCoopAssignProcedure,
  11458. statCoopTraceMethod,
  11459. statCoopResetProcedure,
  11460. statCoopTraceModule)
  11461. END Statistics;
  11462. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  11463. VAR h: LONGINT;
  11464. BEGIN
  11465. WHILE b # 0 DO
  11466. h := a MOD b;
  11467. a := b;
  11468. b := h;
  11469. END;
  11470. RETURN a
  11471. END GCD;
  11472. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  11473. BEGIN
  11474. RETURN a*b DIV GCD(a,b)
  11475. END SCM;
  11476. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  11477. BEGIN
  11478. (*TRACE(a,b);*)
  11479. IF a = 0 THEN RETURN b
  11480. ELSIF b = 0 THEN RETURN a
  11481. ELSE RETURN SCM(a,b)
  11482. END;
  11483. END CommonAlignment;
  11484. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11485. BEGIN
  11486. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11487. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11488. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11489. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11490. 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)
  11491. END
  11492. END PassBySingleReference;
  11493. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11494. BEGIN
  11495. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11496. END PassInRegister;
  11497. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11498. VAR new: RegisterEntry;
  11499. BEGIN
  11500. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11501. IF queue = NIL THEN
  11502. queue := new
  11503. ELSE
  11504. new.next := queue;
  11505. IF queue#NIL THEN queue.prev := new END;
  11506. queue := new
  11507. END;
  11508. END AddRegisterEntry;
  11509. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11510. VAR this: RegisterEntry;
  11511. BEGIN
  11512. this := queue;
  11513. WHILE (this # NIL) & (this.register # register) DO
  11514. this := this.next;
  11515. END;
  11516. IF this = NIL THEN
  11517. RETURN FALSE
  11518. END;
  11519. ASSERT(this # NIL);
  11520. IF this = queue THEN queue := queue.next END;
  11521. IF this.prev # NIL THEN this.prev.next := this.next END;
  11522. IF this.next # NIL THEN this.next.prev := this.prev END;
  11523. RETURN TRUE
  11524. END RemoveRegisterEntry;
  11525. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11526. BEGIN ASSERT(cond);
  11527. END Assert;
  11528. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11529. BEGIN
  11530. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11531. END ReusableRegister;
  11532. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11533. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11534. BEGIN
  11535. procedure := moduleScope.bodyProcedure;
  11536. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11537. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11538. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11539. procedure.SetScope(moduleScope);
  11540. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11541. procedure.SetAccess(SyntaxTree.Hidden);
  11542. moduleScope.SetBodyProcedure(procedure);
  11543. moduleScope.AddProcedure(procedure);
  11544. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11545. END;
  11546. END EnsureBodyProcedure;
  11547. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11548. VAR import: SyntaxTree.Import;
  11549. s: Basic.MessageString;
  11550. selfName: SyntaxTree.IdentifierString;
  11551. module: SyntaxTree.Module;
  11552. BEGIN
  11553. scope.ownerModule.GetName(selfName);
  11554. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11555. module := scope.ownerModule
  11556. ELSE
  11557. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11558. IF import = NIL THEN
  11559. RETURN NIL
  11560. ELSIF import.module = NIL THEN
  11561. RETURN NIL
  11562. ELSE module := import.module
  11563. END;
  11564. END;
  11565. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11566. END GetSymbol;
  11567. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11568. BEGIN
  11569. op.mode := mode;
  11570. IntermediateCode.InitOperand(op.op);
  11571. IntermediateCode.InitOperand(op.tag);
  11572. IntermediateCode.InitOperand(op.extra);
  11573. op.dimOffset := 0;
  11574. END InitOperand;
  11575. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11576. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11577. BEGIN RETURN IntermediateCode.GetType(system, type)
  11578. END GetType;
  11579. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11580. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11581. BEGIN
  11582. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11583. (*
  11584. UniqueId(name,module,name,adr);
  11585. *)
  11586. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11587. constant.SetValue(value);
  11588. module.moduleScope.AddConstant(constant);
  11589. constant.SetScope(module.moduleScope);
  11590. RETURN constant
  11591. END BuildConstant;
  11592. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11593. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11594. BEGIN
  11595. variable := scope.firstVariable;
  11596. WHILE variable # NIL DO
  11597. IF variable.NeedsTrace() THEN
  11598. RETURN TRUE;
  11599. END;
  11600. variable := variable.nextVariable;
  11601. END;
  11602. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11603. WHILE parameter # NIL DO
  11604. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11605. RETURN TRUE;
  11606. END;
  11607. parameter := parameter.nextParameter;
  11608. END;
  11609. RETURN FALSE;
  11610. END HasPointers;
  11611. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11612. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11613. END IsVariableParameter;
  11614. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11615. VAR parameter: SyntaxTree.Parameter;
  11616. BEGIN
  11617. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11618. WHILE parameter # NIL DO
  11619. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11620. IF parameter.movable THEN RETURN TRUE END;
  11621. parameter := parameter.nextParameter;
  11622. END;
  11623. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11624. END HasVariableParameters;
  11625. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11626. BEGIN
  11627. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11628. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11629. END HasExplicitTraceMethod;
  11630. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11631. BEGIN
  11632. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11633. IF (expression IS SyntaxTree.IntegerValue) THEN
  11634. val := expression(SyntaxTree.IntegerValue).value;
  11635. RETURN TRUE
  11636. ELSE
  11637. RETURN FALSE
  11638. END;
  11639. END IsIntegerConstant;
  11640. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11641. BEGIN
  11642. IF val <= 0 THEN RETURN FALSE END;
  11643. exp := 0;
  11644. WHILE ~ODD(val) DO
  11645. val := val DIV 2;
  11646. INC(exp)
  11647. END;
  11648. RETURN val = 1
  11649. END PowerOf2;
  11650. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11651. VAR procedure: SyntaxTree.Procedure;
  11652. BEGIN
  11653. procedure := record.recordScope.constructor;
  11654. IF procedure = NIL THEN
  11655. record := record.GetBaseRecord();
  11656. IF record # NIL THEN
  11657. procedure := GetConstructor(record)
  11658. END;
  11659. END;
  11660. RETURN procedure;
  11661. END GetConstructor;
  11662. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11663. BEGIN
  11664. value := SHORT(op.intValue);
  11665. RETURN op.mode = IntermediateCode.ModeImmediate;
  11666. END IsIntegerImmediate;
  11667. (** whether a type strictily is a pointer to record or object type
  11668. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11669. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11670. BEGIN
  11671. IF type = NIL THEN
  11672. RETURN FALSE
  11673. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11674. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11675. ELSE
  11676. RETURN FALSE
  11677. END
  11678. END IsStrictlyPointerToRecord;
  11679. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11680. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11681. END IsUnsafePointer;
  11682. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11683. BEGIN type := type.resolved;
  11684. IF type IS SyntaxTree.PointerType THEN
  11685. type := type(SyntaxTree.PointerType).pointerBase;
  11686. type := type.resolved;
  11687. IF type IS SyntaxTree.RecordType THEN
  11688. recordType := type(SyntaxTree.RecordType);
  11689. RETURN TRUE
  11690. ELSE
  11691. RETURN FALSE
  11692. END
  11693. ELSIF type IS SyntaxTree.RecordType THEN
  11694. recordType := type(SyntaxTree.RecordType);
  11695. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11696. ELSIF type IS SyntaxTree.ObjectType THEN
  11697. RETURN TRUE
  11698. ELSIF type IS SyntaxTree.AnyType THEN
  11699. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11700. ELSE
  11701. RETURN FALSE
  11702. END;
  11703. END IsPointerToRecord;
  11704. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11705. BEGIN
  11706. type := type.resolved;
  11707. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11708. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11709. END IsArrayOfSystemByte;
  11710. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11711. BEGIN
  11712. IF type = NIL THEN RETURN FALSE END;
  11713. type := type.resolved;
  11714. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11715. END IsOpenArray;
  11716. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11717. BEGIN
  11718. IF type = NIL THEN RETURN FALSE END;
  11719. type := type.resolved;
  11720. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11721. END IsStaticArray;
  11722. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11723. VAR size: LONGINT;
  11724. BEGIN
  11725. size := 1;
  11726. WHILE (IsStaticArray(type)) DO
  11727. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11728. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11729. END;
  11730. RETURN size;
  11731. END StaticArrayNumElements;
  11732. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11733. BEGIN
  11734. WHILE (IsStaticArray(type)) DO
  11735. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11736. END;
  11737. RETURN type;
  11738. END StaticArrayBaseType;
  11739. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11740. BEGIN
  11741. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11742. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11743. END;
  11744. RETURN type;
  11745. END ArrayBaseType;
  11746. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11747. BEGIN
  11748. IF type = NIL THEN RETURN FALSE END;
  11749. type := type.resolved;
  11750. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11751. END IsDelegate;
  11752. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11753. VAR i: LONGINT;
  11754. BEGIN
  11755. i := 0; type := type.resolved;
  11756. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11757. INC(i);
  11758. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11759. END;
  11760. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11761. INC(i);
  11762. type := type(SyntaxTree.MathArrayType).arrayBase;
  11763. IF type # NIL THEN type := type.resolved END;
  11764. END;
  11765. RETURN i
  11766. END DynamicDim;
  11767. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11768. BEGIN
  11769. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11770. type := type(SyntaxTree.ArrayType).arrayBase;
  11771. END;
  11772. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11773. type := type(SyntaxTree.MathArrayType).arrayBase;
  11774. END;
  11775. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11776. END StaticSize;
  11777. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11778. BEGIN
  11779. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11780. END IsImmediate;
  11781. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11782. BEGIN
  11783. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11784. END IsAddress;
  11785. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11786. BEGIN
  11787. RETURN (x.mode = IntermediateCode.ModeRegister);
  11788. END IsRegister;
  11789. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11790. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11791. BEGIN
  11792. typeDeclaration := recordType.typeDeclaration;
  11793. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11794. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11795. END GetRecordTypeName;
  11796. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11797. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11798. BEGIN
  11799. parSize := 0;
  11800. IF StructuredReturnType(procedureType) THEN
  11801. parameter := procedureType.returnParameter;
  11802. INC(parSize,system.SizeOfParameter(parameter));
  11803. parSize := parSize + (-parSize) MOD system.addressSize;
  11804. END;
  11805. parameter :=procedureType.lastParameter;
  11806. WHILE (parameter # NIL) DO
  11807. INC(parSize,system.SizeOfParameter(parameter));
  11808. parSize := parSize + (-parSize) MOD system.addressSize;
  11809. parameter := parameter.prevParameter;
  11810. END;
  11811. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11812. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11813. RETURN ToMemoryUnits(system,parSize)
  11814. END ParametersSize;
  11815. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11816. BEGIN
  11817. IF type = NIL THEN RETURN FALSE
  11818. ELSE
  11819. type := type.resolved;
  11820. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11821. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11822. END
  11823. END ReturnedAsParameter;
  11824. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11825. BEGIN
  11826. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11827. END StructuredReturnType;
  11828. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11829. BEGIN
  11830. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11831. END IsNested;
  11832. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11833. BEGIN
  11834. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11835. scope := scope.outerScope;
  11836. END;
  11837. RETURN scope # NIL;
  11838. END InCellScope;
  11839. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11840. BEGIN
  11841. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11842. RETURN 0
  11843. ELSE
  11844. *)
  11845. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11846. (*END;*)
  11847. END ProcedureParametersSize;
  11848. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11849. VAR dataUnit: LONGINT;
  11850. BEGIN dataUnit := system.dataUnit;
  11851. ASSERT(size MOD system.dataUnit = 0);
  11852. RETURN size DIV system.dataUnit
  11853. END ToMemoryUnits;
  11854. PROCEDURE Get*(): Backend.Backend;
  11855. VAR backend: IntermediateBackend;
  11856. BEGIN NEW(backend); RETURN backend
  11857. END Get;
  11858. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11859. VAR instruction: IntermediateCode.Instruction;
  11860. BEGIN
  11861. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11862. RETURN instruction
  11863. END Nop;
  11864. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11865. VAR instruction: IntermediateCode.Instruction;
  11866. BEGIN
  11867. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11868. RETURN instruction
  11869. END Mov;
  11870. (* like Mov but ensures that no new register will be allocated for dest *)
  11871. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11872. VAR instruction: IntermediateCode.Instruction;
  11873. BEGIN
  11874. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11875. RETURN instruction
  11876. END MovReplace;
  11877. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11878. VAR instruction: IntermediateCode.Instruction;
  11879. BEGIN
  11880. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11881. RETURN instruction
  11882. END Conv;
  11883. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11884. VAR instruction: IntermediateCode.Instruction;
  11885. BEGIN
  11886. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11887. RETURN instruction
  11888. END Call;
  11889. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11890. VAR op1, op2: IntermediateCode.Operand;
  11891. VAR instruction: IntermediateCode.Instruction;
  11892. BEGIN
  11893. IntermediateCode.InitNumber(op1,pcOffset);
  11894. IntermediateCode.InitNumber(op2,callingConvention);
  11895. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11896. RETURN instruction
  11897. END Exit;
  11898. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11899. VAR instruction: IntermediateCode.Instruction;
  11900. BEGIN
  11901. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11902. RETURN instruction
  11903. END Return;
  11904. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11905. VAR instruction: IntermediateCode.Instruction;
  11906. BEGIN
  11907. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11908. RETURN instruction
  11909. END Result;
  11910. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11911. VAR op1: IntermediateCode.Operand;
  11912. VAR instruction: IntermediateCode.Instruction;
  11913. BEGIN
  11914. IntermediateCode.InitNumber(op1,nr);
  11915. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11916. RETURN instruction
  11917. END Trap;
  11918. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11919. VAR instruction: IntermediateCode.Instruction;
  11920. BEGIN
  11921. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11922. RETURN instruction
  11923. END Br;
  11924. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11925. VAR instruction: IntermediateCode.Instruction;
  11926. BEGIN
  11927. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11928. RETURN instruction
  11929. END Breq;
  11930. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11931. VAR instruction: IntermediateCode.Instruction;
  11932. BEGIN
  11933. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11934. RETURN instruction
  11935. END Brne;
  11936. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11937. VAR instruction: IntermediateCode.Instruction;
  11938. BEGIN
  11939. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11940. RETURN instruction
  11941. END Brge;
  11942. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11943. VAR instruction: IntermediateCode.Instruction;
  11944. BEGIN
  11945. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11946. RETURN instruction
  11947. END Brlt;
  11948. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11949. VAR instruction: IntermediateCode.Instruction;
  11950. BEGIN
  11951. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11952. RETURN instruction
  11953. END Pop;
  11954. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11955. VAR instruction: IntermediateCode.Instruction;
  11956. BEGIN
  11957. ASSERT(op.mode # IntermediateCode.Undefined);
  11958. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11959. RETURN instruction
  11960. END Push;
  11961. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11962. VAR instruction: IntermediateCode.Instruction;
  11963. BEGIN
  11964. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11965. RETURN instruction
  11966. END Neg;
  11967. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11968. VAR instruction: IntermediateCode.Instruction;
  11969. BEGIN
  11970. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11971. RETURN instruction
  11972. END Not;
  11973. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11974. VAR instruction: IntermediateCode.Instruction;
  11975. BEGIN
  11976. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11977. RETURN instruction
  11978. END Abs;
  11979. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11980. VAR instruction: IntermediateCode.Instruction;
  11981. BEGIN
  11982. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  11983. RETURN instruction
  11984. END Mul;
  11985. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11986. VAR instruction: IntermediateCode.Instruction;
  11987. BEGIN
  11988. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  11989. RETURN instruction
  11990. END Div;
  11991. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11992. VAR instruction: IntermediateCode.Instruction;
  11993. BEGIN
  11994. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  11995. RETURN instruction
  11996. END Mod;
  11997. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11998. VAR instruction: IntermediateCode.Instruction;
  11999. BEGIN
  12000. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12001. RETURN instruction
  12002. END Sub;
  12003. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12004. VAR instruction: IntermediateCode.Instruction;
  12005. BEGIN
  12006. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12007. RETURN instruction
  12008. END Add;
  12009. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12010. VAR instruction: IntermediateCode.Instruction;
  12011. BEGIN
  12012. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12013. RETURN instruction
  12014. END And;
  12015. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12016. VAR instruction: IntermediateCode.Instruction;
  12017. BEGIN
  12018. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12019. RETURN instruction
  12020. END Or;
  12021. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12022. VAR instruction: IntermediateCode.Instruction;
  12023. BEGIN
  12024. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12025. RETURN instruction
  12026. END Xor;
  12027. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12028. VAR instruction: IntermediateCode.Instruction;
  12029. BEGIN
  12030. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12031. RETURN instruction
  12032. END Shl;
  12033. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12034. VAR instruction: IntermediateCode.Instruction;
  12035. BEGIN
  12036. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12037. RETURN instruction
  12038. END Shr;
  12039. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12040. VAR instruction: IntermediateCode.Instruction;
  12041. BEGIN
  12042. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12043. RETURN instruction
  12044. END Rol;
  12045. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12046. VAR instruction: IntermediateCode.Instruction;
  12047. BEGIN
  12048. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12049. RETURN instruction
  12050. END Ror;
  12051. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12052. VAR instruction: IntermediateCode.Instruction;
  12053. BEGIN
  12054. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12055. RETURN instruction
  12056. END Cas;
  12057. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12058. VAR instruction: IntermediateCode.Instruction;
  12059. BEGIN
  12060. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12061. RETURN instruction
  12062. END Copy;
  12063. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12064. VAR instruction: IntermediateCode.Instruction;
  12065. BEGIN
  12066. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12067. RETURN instruction
  12068. END Fill;
  12069. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12070. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12071. BEGIN
  12072. string := IntermediateCode.String(s);
  12073. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12074. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12075. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12076. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12077. RETURN instruction
  12078. END Asm;
  12079. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12080. VAR instruction: IntermediateCode.Instruction;
  12081. BEGIN
  12082. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12083. RETURN instruction
  12084. END Data;
  12085. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12086. VAR instruction: IntermediateCode.Instruction;
  12087. BEGIN
  12088. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12089. IntermediateCode.SetSubType(instruction, subtype);
  12090. RETURN instruction
  12091. END SpecialInstruction;
  12092. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12093. VAR op1: IntermediateCode.Operand;
  12094. VAR instruction: IntermediateCode.Instruction;
  12095. BEGIN
  12096. (*! generate a warning if size exceeds a certain limit *)
  12097. (*
  12098. ASSERT(bytes < 1000000); (* sanity check *)
  12099. *)
  12100. ASSERT(0 <= units); (* sanity check *)
  12101. IntermediateCode.InitNumber(op1,units);
  12102. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12103. RETURN instruction
  12104. END Reserve;
  12105. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12106. VAR op1: IntermediateCode.Operand;
  12107. VAR instruction: IntermediateCode.Instruction;
  12108. BEGIN
  12109. IntermediateCode.InitNumber(op1,position);
  12110. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12111. RETURN instruction
  12112. END LabelInstruction;
  12113. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12114. VAR pc: LONGINT;
  12115. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12116. BEGIN
  12117. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12118. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12119. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12120. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12121. IF instr.op1.type.form = IntermediateCode.Float THEN
  12122. RETURN instr.op1.floatValue = op.floatValue
  12123. ELSE
  12124. RETURN instr.op1.intValue = op.intValue
  12125. END;
  12126. END ProvidesValue;
  12127. BEGIN
  12128. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12129. pc := 0;
  12130. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12131. INC(pc);
  12132. END;
  12133. IF pc = data.pc THEN
  12134. data.Emit(Data(-1,vop));
  12135. END;
  12136. RETURN pc
  12137. END EnterImmediate;
  12138. PROCEDURE Init;
  12139. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12140. BEGIN
  12141. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12142. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12143. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12144. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12145. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12146. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12147. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12148. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12149. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12150. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12151. IntermediateCode.InitOperand(emptyOperand);
  12152. FOR i := 0 TO NumberSystemCalls-1 DO
  12153. name := "@SystemCall";
  12154. Basic.AppendNumber(name,i);
  12155. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12156. END;
  12157. END Init;
  12158. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12159. BEGIN
  12160. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12161. END IsExported;
  12162. BEGIN
  12163. Init;
  12164. END FoxIntermediateBackend.
  12165. Compiler.Compile FoxIntermediateBackend.Mod ~