FoxIntermediateBackend.Mod 558 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231
  1. MODULE FoxIntermediateBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, Backend := FoxBackend, Global := FoxGlobal,
  3. Scanner := FoxScanner, IntermediateCode := FoxIntermediateCode, Sections := FoxSections, BinaryCode := FoxBinaryCode, Printout := FoxPrintout,
  4. SYSTEM, Diagnostics, Strings, Options, Streams, Compiler, Formats := FoxFormats, SymbolFileFormat := FoxTextualSymbolFile, D := Debugging,
  5. FingerPrinter := FoxFingerPrinter, StringPool;
  6. CONST
  7. (* operand modes *)
  8. ModeUndefined = 0;
  9. ModeReference = 1;
  10. ModeValue = 2;
  11. (* heap data offsets *)
  12. ArrayDimTable = 3; (* dimension table in dyn arrays *)
  13. (* math array offsets *)
  14. MathPtrOffset=0;
  15. MathAdrOffset=1;
  16. MathFlagsOffset=2;
  17. MathDimOffset=3;
  18. MathElementSizeOffset=4;
  19. MathLenOffset=5;
  20. MathIncrOffset=6;
  21. SysDataArrayOffset* = 0; (* array offset in system bl ock, for 32 byte alignment *)
  22. ArrDataArrayOffset*= 16*8; (* 16 bytes array offset in array block, to be compatible with the GC scheme of POINTER TO ARRAY OF ... *)
  23. TensorFlag* = 0; (* flag indicating a tensor array *)
  24. RangeFlag* = 1; (* flag indicating a range, e.g. an array derived from A[..,..] *)
  25. StackFlag* = 2; (* flag indicates temporary result *)
  26. StaticFlag* = 1; (* flag indicating a static array, may not be reallocated *)
  27. (** compiler generated traps *)
  28. WithTrap* = 1; (* generated when a WITH statement fails *)
  29. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  30. ReturnTrap* = 3;
  31. TypeEqualTrap* = 5;
  32. TypeCheckTrap* = 6;
  33. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  34. AssertTrap* = 8; (* generated when an assert fails *)
  35. ArraySizeTrap* = 9;
  36. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  37. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  38. NegativeDivisorTrap*=12;
  39. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  40. NilPointerTrap*=17; (* indicates that a nil pointer was being dereferenced *)
  41. Trace = FALSE;
  42. TraceRegisterUsageCount=TRUE;
  43. ArrayAlignment = 8*8; (* first array element of ArrayBlock and first data element of SystemBlock must be aligned to 0 MOD ArrayAlignment *)
  44. (** system call numbers *)
  45. NumberSystemCalls* = 12;
  46. SysNewRec* = 0; SysNewArr* = 1; SysNewSys* = 2; SysCaseTable* = 3; SysProcAddr* = 4;
  47. SysLock* = 5; SysUnlock* = 6; SysStart* = 7; SysAwait* = 8; SysInterfaceLookup* = 9;
  48. SysRegisterInterface* = 10; SysGetProcedure* = 11;
  49. DefaultRuntimeModuleName ="Runtime";
  50. DefaultTraceModuleName ="KernelLog";
  51. ChannelModuleName = "Channels";
  52. NonPointer = -1; (* special pointer values *)
  53. NoType = 0; (* special type info values *)
  54. LhsIsPointer = 0; (* for the operator kind *)
  55. RhsIsPointer = 1;
  56. (* priority values, lower means higher priority *)
  57. EntryPriority=-4;
  58. FirstPriority=-3;
  59. InitPriority=-2;
  60. ExitPriority=-1;
  61. BasePointerTypeSize = 5;
  62. BaseArrayTypeSize = BasePointerTypeSize + 3;
  63. LengthOffset = BasePointerTypeSize + 0;
  64. DataOffset = BasePointerTypeSize + 1;
  65. DescriptorOffset = BasePointerTypeSize + 2;
  66. BaseRecordTypeSize = BasePointerTypeSize + 2;
  67. ActionOffset = BasePointerTypeSize + 0;
  68. MonitorOffset = BasePointerTypeSize + 1;
  69. BaseObjectTypeSize = BaseRecordTypeSize;
  70. ActionTypeSize = 3;
  71. MonitorTypeSize = 7;
  72. ProcessorOffset = BaseObjectTypeSize + 1;
  73. StackLimitOffset* = BaseObjectTypeSize + 3;
  74. QuantumOffset = BaseObjectTypeSize + 4;
  75. (* flags for optimizations with small matricies and vectors (Alexey Morozov) *)
  76. SmallMatrixFlag = 3; (* flag for identification of a small matrix *)
  77. SmallVectorFlag = 3; (* flag for identification of a small vector *)
  78. Size2Flag = 4; (* size = 2 *)
  79. Size3Flag = 5; (* size = 3 *)
  80. Size4Flag = 6; (* size = 4 *)
  81. Size5Flag = 7; (* size = 5 *)
  82. Size6Flag = 8; (* size = 6 *)
  83. Size7Flag = 9; (* size = 7 *)
  84. Size8Flag = 10; (* size = 8 *)
  85. ReflectionSupport = TRUE;
  86. (* Solution for identifying procedure descriptors on the stack and for being able to differentiate "old school" stack frames from the underlying operating system stack frames:
  87. push a procedure desriptor plus one to where the BP pointer would be located. The misalignment of the procedure descriptor makes it possible to identify that it is not
  88. a base pointer but a procedure descriptor. The base pointer itself is in such cases located at BP + address size.
  89. *)
  90. TYPE
  91. Position=SyntaxTree.Position;
  92. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  93. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  94. Operand = RECORD
  95. mode: SHORTINT;
  96. op: IntermediateCode.Operand;
  97. tag: IntermediateCode.Operand;
  98. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  99. dimOffset: LONGINT;
  100. END;
  101. Fixup= POINTER TO RECORD
  102. pc: LONGINT;
  103. nextFixup: Fixup;
  104. END;
  105. WriteBackCall = POINTER TO RECORD
  106. call: SyntaxTree.ProcedureCallDesignator;
  107. next: WriteBackCall;
  108. END;
  109. Label= OBJECT
  110. VAR
  111. fixups: Fixup;
  112. section: IntermediateCode.Section;
  113. pc: LONGINT;
  114. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  115. BEGIN
  116. SELF.section := section; pc := -1;
  117. END InitLabel;
  118. PROCEDURE Resolve(pc: LONGINT);
  119. VAR at: LONGINT;
  120. BEGIN
  121. SELF.pc := pc;
  122. WHILE(fixups # NIL) DO
  123. at := fixups.pc;
  124. section.PatchAddress(at,pc);
  125. fixups := fixups.nextFixup;
  126. END;
  127. END Resolve;
  128. PROCEDURE AddFixup(at: LONGINT);
  129. VAR fixup: Fixup;
  130. BEGIN
  131. ASSERT(pc=-1);
  132. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  133. END AddFixup;
  134. END Label;
  135. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  136. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  137. VAR
  138. backend: IntermediateBackend;
  139. implementationVisitor: ImplementationVisitor;
  140. meta: MetaDataGenerator;
  141. system: Global.System;
  142. currentScope: SyntaxTree.Scope;
  143. module: Sections.Module;
  144. moduleSelf: SyntaxTree.Variable;
  145. dump: BOOLEAN;
  146. forceModuleBody: BOOLEAN;
  147. addressType: IntermediateCode.Type;
  148. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  149. BEGIN
  150. currentScope := NIL; module := NIL; moduleSelf := NIL;
  151. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  152. SELF.dump := dump;
  153. SELF.backend := backend;
  154. SELF.forceModuleBody := forceModuleBody;
  155. addressType := IntermediateCode.GetType(system,system.addressType)
  156. END Init;
  157. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  158. BEGIN
  159. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  160. END Error;
  161. PROCEDURE Type(x: SyntaxTree.Type);
  162. BEGIN
  163. x.Accept(SELF);
  164. END Type;
  165. (** types **)
  166. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  167. BEGIN (* no code emission *) END VisitBasicType;
  168. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  169. BEGIN (* no code emission *) END VisitCharacterType;
  170. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  171. BEGIN (* no code emission *) END VisitIntegerType;
  172. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  173. BEGIN (* no code emission *) END VisitFloatType;
  174. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  175. BEGIN (* no code emission *) END VisitComplexType;
  176. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  177. VAR type: SyntaxTree.Type;
  178. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  179. type := x.resolved;
  180. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  181. meta.CheckTypeDeclaration(type);
  182. END;
  183. END VisitQualifiedType;
  184. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  185. BEGIN (* no code emission *) END VisitStringType;
  186. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  187. BEGIN (* no code emission *)
  188. END VisitArrayRangeType;
  189. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  190. BEGIN (* no code emission *) END VisitArrayType;
  191. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  192. BEGIN (* no code emission *) END VisitPortType;
  193. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  194. BEGIN
  195. meta.CheckTypeDeclaration(x);
  196. END VisitMathArrayType;
  197. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  198. BEGIN
  199. meta.CheckTypeDeclaration(x);
  200. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  201. END VisitPointerType;
  202. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  203. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  204. BEGIN (* no code emission *)
  205. meta.CheckTypeDeclaration(x);
  206. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  207. IF x.pointerType.typeDeclaration # NIL THEN
  208. td := x.pointerType.typeDeclaration
  209. ELSE
  210. td := x.typeDeclaration
  211. END;
  212. Global.GetSymbolName(td,name);
  213. (* code section for object *)
  214. END;
  215. Scope(x.recordScope);
  216. END VisitRecordType;
  217. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  218. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  219. BEGIN
  220. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  221. WHILE (this # NIL) & (this.identifier# id) DO
  222. this := this.nextModifier;
  223. END;
  224. RETURN this # NIL
  225. END HasFlag;
  226. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  227. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  228. BEGIN
  229. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  230. capabilities := {};
  231. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  232. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  233. backend.SetCapabilities(capabilities);
  234. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  235. Scope(x.cellScope);
  236. END;
  237. END VisitCellType;
  238. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  239. BEGIN (* no code emission *) END VisitProcedureType;
  240. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  241. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  242. END VisitEnumerationType;
  243. (* symbols *)
  244. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  245. BEGIN
  246. Procedure(x);
  247. END VisitProcedure;
  248. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  249. BEGIN
  250. Procedure(x);
  251. END VisitOperator;
  252. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  253. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  254. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  255. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  256. BEGIN
  257. type := type.resolved;
  258. IF type IS SyntaxTree.RecordType THEN
  259. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  260. ELSIF (type IS SyntaxTree.ArrayType) THEN
  261. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  262. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  263. END;
  264. ELSIF type IS SyntaxTree.MathArrayType THEN
  265. WITH type: SyntaxTree.MathArrayType DO
  266. IF type.form = SyntaxTree.Open THEN
  267. RETURN TRUE
  268. ELSIF type.form = SyntaxTree.Static THEN
  269. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  270. END;
  271. END;
  272. END;
  273. RETURN FALSE
  274. END TypeNeedsInitialization;
  275. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  276. VAR variable: SyntaxTree.Variable;
  277. BEGIN
  278. variable := scope.firstVariable;
  279. WHILE variable # NIL DO
  280. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  281. IF variable.initializer # NIL THEN RETURN TRUE END;
  282. variable := variable.nextVariable;
  283. END;
  284. RETURN FALSE
  285. END ScopeNeedsInitialization;
  286. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  287. BEGIN
  288. size := offset - lastUpdated;
  289. IF size > 0 THEN
  290. irv.Emit(Reserve(x.position,size));
  291. END;
  292. irv.Emit(Data(x.position, op));
  293. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  294. END SingleInitialize;
  295. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  296. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  297. BEGIN
  298. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  299. WITH type: SyntaxTree.RecordType DO
  300. baseType := type.baseType;
  301. IF baseType # NIL THEN
  302. baseType := baseType.resolved;
  303. IF baseType IS SyntaxTree.PointerType THEN
  304. baseType := baseType(SyntaxTree.PointerType).pointerBase
  305. END;
  306. Initialize(baseType,NIL, offset);
  307. END;
  308. variable := type.recordScope.firstVariable;
  309. WHILE variable # NIL DO
  310. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  311. variable := variable.nextVariable
  312. END;
  313. | type: SyntaxTree.ArrayType DO
  314. IF type.form = SyntaxTree.Static THEN
  315. baseType := type.arrayBase;
  316. IF TypeNeedsInitialization(baseType) THEN
  317. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  318. FOR i := 0 TO type.staticLength-1 DO
  319. Initialize(baseType,NIL,offset+i*size);
  320. END;
  321. END;
  322. END;
  323. | type: SyntaxTree.MathArrayType DO
  324. IF type.form = SyntaxTree.Open THEN
  325. dim := DynamicDim(type);
  326. baseType := SemanticChecker.ArrayBase(type,dim);
  327. imm := IntermediateCode.Immediate(addressType,dim);
  328. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  329. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  330. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  331. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  332. (* flags remain empty (=0) for open array *)
  333. ELSIF type.form = SyntaxTree.Static THEN
  334. baseType := type.arrayBase;
  335. IF TypeNeedsInitialization(baseType) THEN
  336. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  337. ASSERT(type.staticLength < 1024*1024*1024);
  338. FOR i := 0 TO type.staticLength-1 DO
  339. Initialize(baseType,NIL,offset+i*size);
  340. END;
  341. END;
  342. END;
  343. ELSE
  344. IF initializer # NIL THEN
  345. implementationVisitor.Evaluate(initializer, op);
  346. SingleInitialize(op.op, offset);
  347. END;
  348. END;
  349. END Initialize;
  350. BEGIN
  351. IF x.externalName # NIL THEN RETURN END;
  352. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  353. (* code section for variable *)
  354. Global.GetSymbolSegmentedName(x,name);
  355. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  356. irv.SetExported(IsExported(x));
  357. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  358. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  359. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  360. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  361. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  362. FOR i := 0 TO DynamicDim(x.type)-1 DO
  363. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  364. END;
  365. ELSE
  366. lastUpdated:= 0;
  367. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  368. Initialize(x.type, x.initializer, 0);
  369. END;
  370. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  371. IF size > 0 THEN
  372. irv.Emit(Reserve(x.position,size));
  373. END;
  374. IF ~x.fixed THEN
  375. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  376. ELSE
  377. align := x.alignment;
  378. END;
  379. irv.SetPositionOrAlignment(x.fixed, align);
  380. meta.CheckTypeDeclaration(x.type);
  381. END;
  382. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  383. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  384. END;
  385. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  386. END VisitVariable;
  387. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  388. BEGIN
  389. VisitVariable(x)
  390. END VisitProperty;
  391. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  392. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  393. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  394. BEGIN
  395. ASSERT(currentScope IS SyntaxTree.CellScope);
  396. Global.GetSymbolSegmentedName(x,name);
  397. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  398. irv.SetExported(IsExported(x));
  399. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  400. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  401. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  403. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  404. FOR i := 0 TO DynamicDim(x.type)-1 DO
  405. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  406. END;
  407. ELSE
  408. lastUpdated:= 0;
  409. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  410. IF size > 0 THEN
  411. irv.Emit(Reserve(x.position,size));
  412. END;
  413. IF ~x.fixed THEN
  414. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  415. ELSE
  416. align := x.alignment;
  417. END;
  418. irv.SetPositionOrAlignment(x.fixed, align);
  419. meta.CheckTypeDeclaration(x.type);
  420. END;
  421. END VisitParameter;
  422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  423. BEGIN
  424. Type(x.declaredType); (* => code in objects *)
  425. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  426. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  427. END;
  428. END VisitTypeDeclaration;
  429. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  430. BEGIN
  431. IF (SyntaxTree.Public * x.access # {}) THEN
  432. implementationVisitor.VisitConstant(x);
  433. END;
  434. END VisitConstant;
  435. PROCEDURE Scope(x: SyntaxTree.Scope);
  436. VAR procedure: SyntaxTree.Procedure;
  437. constant: SyntaxTree.Constant;
  438. variable: SyntaxTree.Variable;
  439. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  440. cell: SyntaxTree.CellType;
  441. parameter: SyntaxTree.Parameter;
  442. property: SyntaxTree.Property;
  443. BEGIN
  444. prevScope := currentScope;
  445. currentScope := x;
  446. (* constants treated in implementation visitor *)
  447. WITH x: SyntaxTree.CellScope DO
  448. cell := x.ownerCell;
  449. parameter := cell.firstParameter;
  450. WHILE parameter # NIL DO
  451. VisitParameter(parameter);
  452. parameter := parameter.nextParameter;
  453. END;
  454. property := cell.firstProperty;
  455. WHILE property # NIL DO
  456. VisitProperty(property);
  457. property := property.nextProperty;
  458. END;
  459. ELSE
  460. END;
  461. typeDeclaration := x.firstTypeDeclaration;
  462. WHILE typeDeclaration # NIL DO
  463. VisitTypeDeclaration(typeDeclaration);
  464. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  465. END;
  466. variable := x.firstVariable;
  467. WHILE variable # NIL DO
  468. VisitVariable(variable);
  469. variable := variable.nextVariable;
  470. END;
  471. procedure := x.firstProcedure;
  472. WHILE procedure # NIL DO
  473. VisitProcedure(procedure);
  474. procedure := procedure.nextProcedure;
  475. END;
  476. constant := x.firstConstant;
  477. WHILE constant # NIL DO
  478. VisitConstant(constant);
  479. constant := constant.nextConstant;
  480. END;
  481. currentScope := prevScope;
  482. END Scope;
  483. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  484. VAR parameter: SyntaxTree.Parameter;
  485. BEGIN
  486. parameter := first;
  487. WHILE parameter # NIL DO
  488. VisitParameter(parameter);
  489. parameter := parameter.nextParameter;
  490. END;
  491. END Parameters;
  492. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  493. VAR scope: SyntaxTree.ProcedureScope;
  494. prevScope: SyntaxTree.Scope;
  495. inline, finalizer: BOOLEAN;
  496. procedureType: SyntaxTree.ProcedureType;
  497. pc: LONGINT;
  498. stackSize: LONGINT;
  499. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  500. null,size,src,dest,fp,res: IntermediateCode.Operand;
  501. cc: LONGINT;
  502. cellType: SyntaxTree.CellType;
  503. registerNumber: LONGINT;
  504. registerParameter: Backend.Registers;
  505. registerParameters: LONGINT;
  506. registerClass: IntermediateCode.RegisterClass;
  507. type: IntermediateCode.Type;
  508. formalParameter: SyntaxTree.Parameter;
  509. recordType: SyntaxTree.RecordType;
  510. isModuleBody: BOOLEAN;
  511. parametersSize: LONGINT;
  512. PROCEDURE Signature;
  513. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  514. BEGIN
  515. procedureType := x.type(SyntaxTree.ProcedureType);
  516. returnType := procedureType.returnType;
  517. IF returnType # NIL THEN
  518. meta.CheckTypeDeclaration(returnType)
  519. END;
  520. parameter := procedureType.firstParameter;
  521. WHILE parameter # NIL DO
  522. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  523. parameter := parameter.nextParameter;
  524. END;
  525. END Signature;
  526. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  527. VAR result: BOOLEAN;
  528. BEGIN
  529. result := FALSE;
  530. IF x = SyntaxTree.invalidExpression THEN
  531. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  532. result := TRUE;
  533. value := x.resolved(SyntaxTree.IntegerValue).value;
  534. ELSE
  535. Error(x.position,"expression is not an integer constant");
  536. END;
  537. RETURN result;
  538. END CheckIntegerValue;
  539. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  540. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  541. BEGIN
  542. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  543. WHILE (this # NIL) & (this.identifier # id) DO
  544. this := this.nextModifier;
  545. END;
  546. IF this # NIL THEN
  547. IF this.expression = NIL THEN
  548. Error(this.position,"expected expression value");
  549. ELSIF CheckIntegerValue(this.expression,value) THEN
  550. END;
  551. RETURN TRUE
  552. ELSE RETURN FALSE
  553. END;
  554. END HasValue;
  555. CONST DefaultDataMemorySize=512;
  556. BEGIN
  557. IF x.externalName # NIL THEN RETURN END;
  558. (*
  559. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  560. *)
  561. (* code section for this procedure *)
  562. scope := x.procedureScope;
  563. prevScope := currentScope;
  564. currentScope := scope;
  565. procedureType := x.type(SyntaxTree.ProcedureType);
  566. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  567. implementationVisitor.temporaries.Init;
  568. implementationVisitor.usedRegisters := NIL;
  569. implementationVisitor.registerUsageCount.Init;
  570. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  571. IF (scope.body # NIL) & (x.isInline) THEN
  572. inline := TRUE;
  573. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  574. ir.SetExported(IsExported(x));
  575. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  576. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  577. IF backend.cellsAreObjects THEN
  578. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  579. ir.SetExported(IsExported(x));
  580. ELSE
  581. RETURN; (* cellnet cannot be compiled for final static hardware *)
  582. END;
  583. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  584. inline := FALSE;
  585. AddBodyCallStub(x);
  586. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  587. ir.SetExported(IsExported(x));
  588. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  589. inline := FALSE;
  590. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  591. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  592. AddBodyCallStub(x);
  593. AddStackAllocation(x,stackSize);
  594. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  595. ir.SetExported(IsExported(x));
  596. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  597. inline := FALSE;
  598. Parameters(procedureType.firstParameter);
  599. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  600. ir.SetExported(IsExported(x));
  601. ELSE
  602. inline := FALSE;
  603. IF x.isEntry OR x.isExit THEN
  604. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  605. ir.SetExported(TRUE);
  606. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  607. ELSE
  608. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  609. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  610. END;
  611. END;
  612. cc := procedureType.callingConvention;
  613. IF cc = SyntaxTree.WinAPICallingConvention THEN
  614. parametersSize := ProcedureParametersSize(backend.system,x);
  615. ELSE
  616. parametersSize := 0;
  617. END;
  618. IF scope.body # NIL THEN
  619. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  620. registerNumber := 0;
  621. IF ~inline THEN
  622. IF scope.lastVariable = NIL THEN
  623. stackSize := 0
  624. ELSE
  625. stackSize := scope.lastVariable.offsetInBits;
  626. IF stackSize <0 THEN stackSize := -stackSize END;
  627. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  628. END;
  629. (*
  630. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  632. *)
  633. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  634. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  635. END;
  636. pc := ir.pc-1;
  637. (*
  638. ir.Emit(Nop(position)); (* placeholder for fill *)
  639. *)
  640. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  641. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  642. IF registerParameter = NIL THEN registerParameters := 0
  643. ELSE registerParameters := LEN(registerParameter)
  644. END;
  645. formalParameter := procedureType.lastParameter;
  646. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  647. IF ~PassInRegister(formalParameter) THEN
  648. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  649. ELSE
  650. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  651. type := GetType(system, formalParameter.type);
  652. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  653. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  654. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  655. implementationVisitor.ReleaseIntermediateOperand(src);
  656. INC(registerNumber);
  657. formalParameter := formalParameter.prevParameter;
  658. END;
  659. END;
  660. END;
  661. END;
  662. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  663. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  664. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  665. IF scope.lastVariable # NIL THEN
  666. stackSize := scope.lastVariable.offsetInBits;
  667. IF stackSize <0 THEN stackSize := -stackSize END;
  668. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  669. END;
  670. END;
  671. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  672. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  673. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  674. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  675. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  676. ir.EmitAt(pc,Push(size));
  677. implementationVisitor.StaticCallOperand(result,procedure);
  678. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  679. END;
  680. *)
  681. END;
  682. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  683. IF stackSize > 0 THEN
  684. IF (stackSize MOD system.addressSize = 0) THEN
  685. null := IntermediateCode.Immediate(addressType,0);
  686. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  687. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  688. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  689. ELSE
  690. null := IntermediateCode.Immediate(int8,0);
  691. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  692. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  693. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  694. END;
  695. (*! should potentially be called by backend -- enter might initialize
  696. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  697. *)
  698. END;
  699. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  700. parametersSize := ProcedureParametersSize(backend.system,x);
  701. ELSE
  702. parametersSize := 0;
  703. END;
  704. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  705. finalizer := FALSE;
  706. IF backend.cooperative & x.isFinalizer THEN
  707. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  708. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  709. GetRecordTypeName(recordType,name);
  710. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  711. END;
  712. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  713. IF backend.cooperative THEN
  714. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  715. IF implementationVisitor.profile & ~isModuleBody THEN
  716. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  717. END;
  718. END;
  719. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  720. IF finalizer THEN
  721. IF backend.hasLinkRegister THEN
  722. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  723. END;
  724. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  725. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  726. ir.Emit(Br(x.position,dest));
  727. ELSE
  728. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  729. END;
  730. ELSE
  731. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  732. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  733. END;
  734. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  735. IF backend.cooperative THEN
  736. IF HasPointers (scope) THEN
  737. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  738. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  739. ir.Emit(Result(x.position, res));
  740. ir.Emit(Push(x.position, res));
  741. implementationVisitor.ResetVariables(scope);
  742. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  743. ir.Emit(Pop(x.position, res));
  744. ir.Emit(Return(x.position, res));
  745. ELSE
  746. implementationVisitor.ResetVariables(scope);
  747. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  748. END;
  749. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  750. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  751. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  752. ir.Emit(Result(x.position, res));
  753. ir.Emit(Push(x.position, res));
  754. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  755. ir.Emit(Pop(x.position, res));
  756. ir.Emit(Return(x.position, res));
  757. ELSE
  758. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  759. END;
  760. END;
  761. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  762. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  763. ELSE
  764. ir.Emit(Nop(x.position));
  765. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  766. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  767. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  768. END;
  769. END;
  770. END
  771. END;
  772. ELSE (* force body for procedures *)
  773. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  774. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  775. (*IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;*)
  776. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  777. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  778. END;
  779. Scope(scope);
  780. Signature;
  781. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  782. currentScope := prevScope;
  783. END Procedure;
  784. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  785. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  786. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  787. BEGIN
  788. ASSERT (bodyProcedure # NIL);
  789. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  790. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  791. procedure.SetScope(bodyProcedure.scope);
  792. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  793. procedure.SetAccess(SyntaxTree.Hidden);
  794. Global.GetSymbolSegmentedName (procedure,name);
  795. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  796. ir.SetExported(TRUE);
  797. ir.SetPriority(InitPriority);
  798. Global.GetSymbolSegmentedName (bodyProcedure,name);
  799. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  800. implementationVisitor.currentScope := module.module.moduleScope;
  801. implementationVisitor.section := ir;
  802. implementationVisitor.PushSelfPointer();
  803. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  804. ELSIF backend.preregisterStatic THEN
  805. implementationVisitor.currentScope := module.module.moduleScope;
  806. implementationVisitor.section := ir;
  807. implementationVisitor.PushSelfPointer();
  808. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  809. ELSE
  810. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  811. ir.Emit(Call(bodyProcedure.position,op, 0));
  812. END;
  813. END AddBodyCallStub;
  814. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  815. VAR name: Basic.SegmentedName;
  816. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  817. BEGIN
  818. Global.GetSymbolSegmentedName (symbol,name);
  819. Basic.RemoveSuffix(name);
  820. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  821. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  822. ir.SetExported(TRUE);
  823. ir.SetPriority(FirstPriority);
  824. IntermediateCode.InitImmediate(op,addressType,initStack);
  825. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  826. END AddStackAllocation;
  827. (** entry function to visit a complete module *)
  828. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  829. VAR
  830. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  831. hasDynamicOperatorDeclarations: BOOLEAN;
  832. operator: SyntaxTree.Operator;
  833. import: SyntaxTree.Import;
  834. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  835. BEGIN
  836. type := type.resolved;
  837. IF type IS SyntaxTree.RecordType THEN
  838. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  839. ELSIF (type IS SyntaxTree.ArrayType) THEN
  840. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  841. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  842. END;
  843. ELSIF type IS SyntaxTree.MathArrayType THEN
  844. WITH type: SyntaxTree.MathArrayType DO
  845. IF type.form = SyntaxTree.Open THEN
  846. RETURN TRUE
  847. ELSIF type.form = SyntaxTree.Static THEN
  848. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  849. END;
  850. END;
  851. END;
  852. RETURN FALSE
  853. END TypeNeedsInitialization;
  854. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  855. VAR variable: SyntaxTree.Variable;
  856. BEGIN
  857. variable := scope.firstVariable;
  858. WHILE variable # NIL DO
  859. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  860. IF variable.initializer # NIL THEN RETURN TRUE END;
  861. variable := variable.nextVariable;
  862. END;
  863. RETURN FALSE
  864. END ScopeNeedsInitialization;
  865. BEGIN
  866. ASSERT(x # NIL); ASSERT(module # NIL);
  867. SELF.module := module;
  868. (* add import names to the generated Sections.Module *)
  869. import := x.moduleScope.firstImport;
  870. WHILE import # NIL DO
  871. import.module.GetName(idstr);
  872. module.imports.AddName(idstr);
  873. import := import.nextImport
  874. END;
  875. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  876. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  877. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  878. moduleSelf.SetType(system.anyType);
  879. moduleSelf.SetScope(x.moduleScope);
  880. moduleSelf.SetUntraced(TRUE);
  881. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  882. ir.SetExported(TRUE);
  883. IntermediateCode.InitImmediate(op,addressType,0);
  884. ir.Emit(Data(Basic.invalidPosition,op));
  885. END;
  886. implementationVisitor.module := module;
  887. implementationVisitor.moduleScope := x.moduleScope;
  888. implementationVisitor.moduleSelf := moduleSelf;
  889. implementationVisitor.canBeLoaded := TRUE;
  890. meta.SetModule(module);
  891. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  892. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  893. END;
  894. IF backend.profile THEN
  895. EnsureBodyProcedure(x.moduleScope);
  896. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  897. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  898. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  899. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  900. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  901. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  902. Global.GetModuleName(module.module,idstr);
  903. implementationVisitor.ProfilerAddModule(idstr);
  904. implementationVisitor.numberProcedures := 0;
  905. END;
  906. implementationVisitor.profile := backend.profile;
  907. (* check if there is at least one dynamic operator locally defined *)
  908. hasDynamicOperatorDeclarations := FALSE;
  909. operator := x.moduleScope.firstOperator;
  910. WHILE operator # NIL DO
  911. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  912. operator := operator.nextOperator
  913. END;
  914. (* add operator initialization code section *)
  915. IF hasDynamicOperatorDeclarations THEN
  916. EnsureBodyProcedure(x.moduleScope);
  917. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  918. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  919. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  920. END;
  921. Scope(x.moduleScope);
  922. IF hasDynamicOperatorDeclarations THEN
  923. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  924. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  925. END;
  926. IF backend.profile THEN
  927. implementationVisitor.ProfilerPatchInit;
  928. END;
  929. END Module;
  930. END DeclarationVisitor;
  931. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  932. RegisterUsageCount*=OBJECT
  933. VAR used: UsedArray; count: LONGINT;
  934. PROCEDURE &Init;
  935. VAR i: LONGINT;
  936. BEGIN
  937. count := 0;
  938. IF used = NIL THEN NEW(used,64); END;
  939. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  940. END Init;
  941. PROCEDURE Grow;
  942. VAR new: UsedArray; size,i: LONGINT;
  943. BEGIN
  944. size := LEN(used)*2;
  945. NEW(new,size);
  946. FOR i := 0 TO LEN(used)-1 DO
  947. new[i].count := used[i].count;
  948. new[i].type := used[i].type;
  949. new[i].map := used[i].map
  950. END;
  951. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  952. used := new
  953. END Grow;
  954. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  955. BEGIN
  956. INC(count);
  957. IF count = LEN(used) THEN Grow END;
  958. used[count].type := type;
  959. used[count].class := class;
  960. used[count].map := count;
  961. RETURN count;
  962. END Next;
  963. PROCEDURE IncUse(register: LONGINT);
  964. BEGIN
  965. INC(used[register].count);
  966. (*
  967. IF (register = 1) & (count > 30) THEN
  968. D.TraceBack;
  969. END;
  970. *)
  971. END IncUse;
  972. PROCEDURE DecUse(register: LONGINT);
  973. BEGIN
  974. DEC(used[register].count);
  975. END DecUse;
  976. PROCEDURE Map(register: LONGINT): LONGINT;
  977. VAR map : LONGINT;
  978. BEGIN
  979. IF register > 0 THEN
  980. map := used[register].map;
  981. WHILE register # map DO register := map; map := used[register].map END;
  982. END;
  983. RETURN register
  984. END Map;
  985. PROCEDURE Use(register: LONGINT): LONGINT;
  986. BEGIN
  987. IF register< LEN(used) THEN
  988. RETURN used[register].count
  989. ELSE
  990. RETURN 0
  991. END
  992. END Use;
  993. END RegisterUsageCount;
  994. RegisterEntry = POINTER TO RECORD
  995. prev,next: RegisterEntry;
  996. register: LONGINT;
  997. registerClass: IntermediateCode.RegisterClass;
  998. type: IntermediateCode.Type;
  999. END;
  1000. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1001. Variables = OBJECT (Basic.List)
  1002. VAR
  1003. inUse: VariableUse;
  1004. registerIndex: LONGINT;
  1005. PROCEDURE & Init;
  1006. VAR i: LONGINT;
  1007. BEGIN
  1008. InitList(16);
  1009. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1010. registerIndex := 1024;
  1011. END Init;
  1012. PROCEDURE GetUsage(VAR use: VariableUse);
  1013. BEGIN
  1014. use := inUse;
  1015. END GetUsage;
  1016. PROCEDURE SetUsage(CONST use: VariableUse);
  1017. BEGIN
  1018. inUse := use;
  1019. END SetUsage;
  1020. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1021. VAR any: ANY;
  1022. BEGIN
  1023. any := Get(i);;
  1024. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1025. END GetVariable;
  1026. PROCEDURE Occupy(pos: LONGINT);
  1027. BEGIN
  1028. INCL(inUse[pos DIV 32], pos MOD 32);
  1029. END Occupy;
  1030. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1031. BEGIN
  1032. Occupy(Length());
  1033. Add(v);
  1034. END AddVariable;
  1035. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1036. VAR var : SyntaxTree.Variable;
  1037. BEGIN
  1038. FOR pos := 0 TO Length()-1 DO
  1039. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1040. var := GetVariable(pos);
  1041. IF type.SameType(var.type) THEN
  1042. Occupy(pos); RETURN var
  1043. END;
  1044. END;
  1045. END;
  1046. pos := Length();
  1047. RETURN NIL
  1048. END GetFreeVariable;
  1049. END Variables;
  1050. SymbolMap = POINTER TO RECORD
  1051. this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap;
  1052. isAddress: BOOLEAN;
  1053. END;
  1054. SymbolMapper = OBJECT
  1055. VAR
  1056. first: SymbolMap;
  1057. PROCEDURE & Init;
  1058. BEGIN
  1059. first := NIL;
  1060. END Init;
  1061. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; isAddress: BOOLEAN);
  1062. VAR new: SymbolMap;
  1063. BEGIN
  1064. NEW(new); new.this := this; new.to := to; new.tag := tag; new.isAddress := isAddress;
  1065. new.next := first; first := new;
  1066. END Add;
  1067. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1068. VAR s: SymbolMap;
  1069. BEGIN
  1070. s := first;
  1071. WHILE (s # NIL) & (s.this # this) DO
  1072. s := s.next
  1073. END;
  1074. RETURN s
  1075. END Get;
  1076. END SymbolMapper;
  1077. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1078. VAR
  1079. system: Global.System;
  1080. section: IntermediateCode.Section;
  1081. module: Sections.Module;
  1082. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1083. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1084. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1085. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1086. checker: SemanticChecker.Checker;
  1087. backend: IntermediateBackend;
  1088. meta: MetaDataGenerator;
  1089. position: Position;
  1090. moduleSelf: SyntaxTree.Variable;
  1091. (* variables for hand over of variables / temporary state *)
  1092. currentScope: SyntaxTree.Scope;
  1093. constantDeclaration : SyntaxTree.Symbol;
  1094. result: Operand; (* result of the most recent expression / statement *)
  1095. destination: IntermediateCode.Operand;
  1096. arrayDestinationTag: IntermediateCode.Operand;
  1097. arrayDestinationDimension:LONGINT;
  1098. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1099. conditional: BOOLEAN;
  1100. trueLabel, falseLabel, exitLabel: Label;
  1101. locked: BOOLEAN;
  1102. (*
  1103. usedRegisters: Registers;
  1104. *)
  1105. registerUsageCount: RegisterUsageCount;
  1106. usedRegisters: RegisterEntry;
  1107. (* useful operands and types *)
  1108. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1109. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1110. commentPrintout: Printout.Printer;
  1111. dump: Streams.Writer;
  1112. tagsAvailable : BOOLEAN;
  1113. supportedInstruction: SupportedInstructionProcedure;
  1114. supportedImmediate: SupportedImmediateProcedure;
  1115. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1116. emitLabels: BOOLEAN;
  1117. runtimeModuleName : SyntaxTree.IdentifierString;
  1118. newObjectFile: BOOLEAN;
  1119. indexCounter: LONGINT;
  1120. profile: BOOLEAN;
  1121. profileId, profileInit: IntermediateCode.Section;
  1122. profileInitPatchPosition: LONGINT;
  1123. numberProcedures: LONGINT;
  1124. procedureResultDesignator : SyntaxTree.Designator;
  1125. operatorInitializationCodeSection: IntermediateCode.Section;
  1126. fingerPrinter: FingerPrinter.FingerPrinter;
  1127. temporaries: Variables;
  1128. canBeLoaded : BOOLEAN;
  1129. currentIsInline: BOOLEAN;
  1130. currentMapper: SymbolMapper;
  1131. currentInlineExit: Label;
  1132. moduleBodySection: IntermediateCode.Section;
  1133. NeedDescriptor : BOOLEAN;
  1134. cooperativeSwitches: BOOLEAN;
  1135. lastSwitchPC: LONGINT;
  1136. isUnchecked: BOOLEAN;
  1137. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1138. newObjectFile: BOOLEAN);
  1139. BEGIN
  1140. SELF.system := system;
  1141. SELF.runtimeModuleName := runtime;
  1142. currentScope := NIL;
  1143. hiddenPointerType := NIL;
  1144. delegatePointerType := NIL;
  1145. awaitProcCounter := 0;
  1146. labelId := 0; constId := 0; labelId := 0;
  1147. SELF.checker := checker;
  1148. SELF.backend := backend;
  1149. position := Basic.invalidPosition;
  1150. conditional := FALSE;
  1151. locked := FALSE;
  1152. InitOperand(result,ModeUndefined);
  1153. addressType := IntermediateCode.GetType(system,system.addressType);
  1154. setType := IntermediateCode.GetType(system,system.setType);
  1155. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1156. byteType := IntermediateCode.GetType(system, system.byteType);
  1157. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1158. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1159. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1160. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1161. nil := IntermediateCode.Immediate(addressType,0);
  1162. one := IntermediateCode.Immediate(addressType,1);
  1163. IntermediateCode.InitOperand(destination);
  1164. tagsAvailable := TRUE;
  1165. supportedInstruction := supportedInstructionProcedure;
  1166. supportedImmediate := supportedImmediateProcedure;
  1167. inData := FALSE;
  1168. SELF.emitLabels := emitLabels;
  1169. IntermediateCode.InitOperand(arrayDestinationTag);
  1170. bool := IntermediateCode.GetType(system,system.booleanType);
  1171. IntermediateCode.InitImmediate(false,bool,0);
  1172. IntermediateCode.InitImmediate(true,bool,1);
  1173. SELF.newObjectFile := newObjectFile;
  1174. indexCounter := 0;
  1175. NEW(registerUsageCount);
  1176. usedRegisters := NIL;
  1177. procedureResultDesignator := NIL;
  1178. NEW(fingerPrinter);
  1179. NEW(temporaries);
  1180. currentIsInline := FALSE;
  1181. NeedDescriptor := FALSE;
  1182. isUnchecked := backend.noRuntimeChecks;
  1183. END Init;
  1184. TYPE Context = RECORD
  1185. section: IntermediateCode.Section;
  1186. registerUsageCount: RegisterUsageCount;
  1187. usedRegisters: RegisterEntry;
  1188. END;
  1189. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1190. VAR context: Context;
  1191. BEGIN
  1192. context.section := section;
  1193. context.registerUsageCount := registerUsageCount;
  1194. context.usedRegisters := usedRegisters;
  1195. section := new;
  1196. NEW(registerUsageCount);
  1197. usedRegisters := NIL;
  1198. RETURN context;
  1199. END SwitchContext;
  1200. PROCEDURE ReturnToContext(context: Context);
  1201. BEGIN
  1202. section := context.section;
  1203. registerUsageCount := context.registerUsageCount;
  1204. usedRegisters := context.usedRegisters;
  1205. END ReturnToContext;
  1206. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1207. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1208. BEGIN
  1209. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1210. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1211. END;
  1212. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1213. section.SetExported(IsExported(syntaxTreeSymbol));
  1214. RETURN section
  1215. END NewSection;
  1216. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1217. VAR new: LONGINT;
  1218. BEGIN
  1219. new := registerUsageCount.Next(type,class);
  1220. UseRegister(new);
  1221. RETURN new
  1222. END AcquireRegister;
  1223. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1224. VAR
  1225. fingerPrint: SyntaxTree.FingerPrint;
  1226. fingerPrintString: ARRAY 32 OF CHAR;
  1227. BEGIN
  1228. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1229. string := "[";
  1230. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1231. Strings.Append(string, fingerPrintString);
  1232. Strings.Append(string, "]");
  1233. END GetFingerprintString;
  1234. (** get the name for the code section that represens a certain symbol
  1235. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1236. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1237. VAR string: ARRAY 32 OF CHAR;
  1238. BEGIN
  1239. Global.GetSymbolSegmentedName(symbol, name);
  1240. (* if the symbol is an operator, then append the fingerprint to the name *)
  1241. IF symbol IS SyntaxTree.Operator THEN
  1242. GetFingerprintString(symbol, string);
  1243. Basic.AppendToSegmentedName(name,string);
  1244. END
  1245. END GetCodeSectionNameForSymbol;
  1246. (** get the name for the code section that represens a certain symbol
  1247. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1248. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1249. VAR string: ARRAY 32 OF CHAR;
  1250. BEGIN
  1251. Global.GetSymbolNameInScope(symbol, scope, name);
  1252. (* if the symbol is an operator, then append the fingerprint to the name *)
  1253. IF symbol IS SyntaxTree.Operator THEN
  1254. GetFingerprintString(symbol, string);
  1255. Strings.Append(name, string);
  1256. END
  1257. END GetCodeSectionNameForSymbolInScope;
  1258. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1259. BEGIN
  1260. IF dump # NIL THEN
  1261. dump.String("enter "); dump.String(s); dump.Ln;
  1262. END;
  1263. END TraceEnter;
  1264. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1265. BEGIN
  1266. IF dump # NIL THEN
  1267. dump.String("exit "); dump.String(s); dump.Ln;
  1268. END;
  1269. END TraceExit;
  1270. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1271. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1272. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1273. VAR i: LONGINT;
  1274. BEGIN
  1275. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1276. IF op.rule # NIL THEN
  1277. FOR i := 0 TO LEN(op.rule)-1 DO
  1278. CheckRegister(op.rule[i])
  1279. END;
  1280. END;
  1281. END CheckRegister;
  1282. BEGIN
  1283. CheckRegister(instruction.op1);
  1284. CheckRegister(instruction.op2);
  1285. CheckRegister(instruction.op3);
  1286. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1287. ELSE section.Emit(instruction);
  1288. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1289. END;
  1290. END Emit;
  1291. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1292. BEGIN
  1293. IF backend.cooperative THEN
  1294. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1295. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1296. ELSE
  1297. Emit(Trap(position,trapNo));
  1298. END;
  1299. END EmitTrap;
  1300. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1301. VAR name: Basic.SegmentedName;
  1302. VAR op1, op2, reg: IntermediateCode.Operand;
  1303. VAR call, nocall: Label;
  1304. VAR parametersSize: LONGINT;
  1305. VAR prevSection: IntermediateCode.Section;
  1306. VAR prevDump: Streams.Writer;
  1307. VAR body: SyntaxTree.Body;
  1308. VAR procedureType: SyntaxTree.ProcedureType;
  1309. BEGIN
  1310. IF procedure # NIL THEN
  1311. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1312. ELSE procedureType := NIL;
  1313. END;
  1314. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1315. prevSection := SELF.section;
  1316. SELF.section := section;
  1317. prevDump := dump;
  1318. dump := section.comments;
  1319. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1320. IF backend.hasLinkRegister THEN
  1321. Emit(Push(Basic.invalidPosition, lr));
  1322. END;
  1323. Emit(Push(Basic.invalidPosition,fp));
  1324. END;
  1325. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1326. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1327. GetCodeSectionNameForSymbol(procedure, name);
  1328. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1329. ELSE
  1330. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1331. END;
  1332. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1333. Emit(Push(Basic.invalidPosition,op1));
  1334. Emit(Mov(Basic.invalidPosition,fp, sp));
  1335. body := procedure.procedureScope.body;
  1336. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1337. NEW(call, section);
  1338. NEW(nocall, section);
  1339. reg := NewRegisterOperand(addressType);
  1340. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1341. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1342. BrltL(call, sp, reg);
  1343. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1344. BrgeL(nocall, sp, op2);
  1345. call.Resolve(section.pc);
  1346. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1347. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1348. Emit(Push(Basic.invalidPosition, op2));
  1349. Emit(Push(Basic.invalidPosition, reg));
  1350. ReleaseIntermediateOperand(reg);
  1351. CallThis(position, "Activities","ExpandStack",2);
  1352. Emit(Result(Basic.invalidPosition, sp));
  1353. nocall.Resolve(section.pc);
  1354. END;
  1355. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1356. IF procedure # NIL THEN
  1357. body := procedure.procedureScope.body;
  1358. ELSE
  1359. body := NIL;
  1360. END;
  1361. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1362. Emit(Push(Basic.invalidPosition, one)) ;
  1363. procedureType.SetParametersOffset(1);
  1364. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1365. END;
  1366. Emit(Mov(Basic.invalidPosition, fp, sp));
  1367. END;
  1368. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1369. SELF.section := prevSection;
  1370. dump := prevDump;
  1371. END EmitEnter;
  1372. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1373. VAR op1,op2: IntermediateCode.Operand;
  1374. VAR instruction: IntermediateCode.Instruction;
  1375. BEGIN
  1376. IntermediateCode.InitNumber(op1,callconv);
  1377. IntermediateCode.InitNumber(op2,varSize);
  1378. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1379. RETURN instruction
  1380. END Enter;
  1381. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1382. VAR op1: IntermediateCode.Operand;
  1383. VAR instruction: IntermediateCode.Instruction;
  1384. BEGIN
  1385. IntermediateCode.InitNumber(op1,callconv);
  1386. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1387. RETURN instruction
  1388. END Leave;
  1389. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1390. VAR prevSection: IntermediateCode.Section;
  1391. VAR op2, size: IntermediateCode.Operand;
  1392. VAR body: SyntaxTree.Body;
  1393. BEGIN
  1394. prevSection := SELF.section;
  1395. SELF.section := section;
  1396. Emit(Leave(position, callconv));
  1397. IF procedure # NIL THEN
  1398. body := procedure.procedureScope.body;
  1399. ELSE
  1400. body := NIL;
  1401. END;
  1402. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1403. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1404. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1405. Emit(Add(position, sp, fp, op2));
  1406. ELSE
  1407. Emit(Mov(position, sp, fp));
  1408. END;
  1409. Emit(Pop(position, fp));
  1410. END;
  1411. SELF.section := prevSection;
  1412. END EmitLeave;
  1413. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1414. VAR m: SymbolMap;
  1415. BEGIN
  1416. position := x.position;
  1417. IF currentIsInline THEN
  1418. m := currentMapper.Get(x);
  1419. IF m # NIL THEN
  1420. (*
  1421. Printout.Info("mapping from", x);
  1422. Printout.Info("mapping to ", m.to);
  1423. *)
  1424. m.to.Accept(SELF);
  1425. op := result;
  1426. IF m.tag # NIL THEN
  1427. ReleaseIntermediateOperand(result.tag);
  1428. m.tag.Accept(SELF);
  1429. op.tag := result.op;
  1430. ReleaseIntermediateOperand(result.tag);
  1431. END;
  1432. RETURN
  1433. END;
  1434. END;
  1435. x.Accept(SELF);
  1436. op := result;
  1437. END Symbol;
  1438. PROCEDURE Expression(x: SyntaxTree.Expression);
  1439. BEGIN
  1440. position := x.position;
  1441. constantDeclaration := NIL;
  1442. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1443. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1444. END;
  1445. IF x.resolved # NIL THEN
  1446. x.resolved.Accept(SELF)
  1447. ELSE
  1448. x.Accept(SELF)
  1449. END;
  1450. (* check this, was commented out in ActiveCells3 *)
  1451. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1452. Error(x.position, "unsupported modifier");
  1453. END;
  1454. END Expression;
  1455. (*
  1456. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1457. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1458. BEGIN
  1459. temporaries.GetUsage(current);
  1460. FOR i := 0 TO LEN(current)-1 DO
  1461. set := current[i] - previous[i];
  1462. IF set # {} THEN
  1463. FOR j := 0 TO MAX(SET)-1 DO
  1464. IF j IN set THEN
  1465. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1466. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1467. Symbol(variable, op);
  1468. MakeMemory(tmp,op.op,addressType,0);
  1469. ReleaseOperand(op);
  1470. Emit(Mov(position,tmp, nil ) );
  1471. ReleaseIntermediateOperand(tmp);
  1472. END;
  1473. END;
  1474. END;
  1475. END;
  1476. END;
  1477. END ResetUsedTemporaries;
  1478. *)
  1479. PROCEDURE Statement(x: SyntaxTree.Statement);
  1480. VAR use: VariableUse;
  1481. BEGIN
  1482. temporaries.GetUsage(use);
  1483. position := x.position;
  1484. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1485. IF commentPrintout # NIL THEN
  1486. commentPrintout.Statement(x);
  1487. dump.Ln;
  1488. (*dump.Update;*)
  1489. END;
  1490. x.Accept(SELF);
  1491. (*
  1492. CheckRegistersFree();
  1493. *)
  1494. (*ResetUsedTemporaries(use);*)
  1495. temporaries.SetUsage(use);
  1496. END Statement;
  1497. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1498. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1499. *)
  1500. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1501. BEGIN
  1502. ASSERT(op.mode # IntermediateCode.Undefined);
  1503. IF op.mode = IntermediateCode.ModeMemory THEN
  1504. ReuseCopy(res,op);
  1505. ELSE
  1506. res := op;
  1507. UseIntermediateOperand(res);
  1508. END;
  1509. IntermediateCode.AddOffset(res,offset);
  1510. IntermediateCode.MakeMemory(res,type);
  1511. END MakeMemory;
  1512. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1513. VAR mem: IntermediateCode.Operand;
  1514. BEGIN
  1515. MakeMemory(mem,res,type,offset);
  1516. ReleaseIntermediateOperand(res);
  1517. res := mem;
  1518. END ToMemory;
  1519. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1520. VAR mem: IntermediateCode.Operand;
  1521. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1522. componentType: SyntaxTree.Type;
  1523. BEGIN
  1524. type := type.resolved;
  1525. IF operand.mode = ModeReference THEN
  1526. IF type IS SyntaxTree.RangeType THEN
  1527. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1528. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1529. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1530. ReleaseOperand(operand);
  1531. operand.op := firstOp;
  1532. operand.tag := lastOp;
  1533. operand.extra := stepOp;
  1534. ELSIF type IS SyntaxTree.ComplexType THEN
  1535. componentType := type(SyntaxTree.ComplexType).componentType;
  1536. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1537. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1538. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1539. ReleaseOperand(operand);
  1540. operand.op := firstOp;
  1541. operand.tag := lastOp
  1542. ELSE
  1543. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1544. ReleaseIntermediateOperand(operand.op);
  1545. operand.op := mem;
  1546. END;
  1547. operand.mode := ModeValue;
  1548. END;
  1549. ASSERT(operand.mode = ModeValue);
  1550. END LoadValue;
  1551. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1552. VAR prevConditional: BOOLEAN;
  1553. BEGIN
  1554. prevConditional := conditional;
  1555. conditional := FALSE;
  1556. InitOperand(result, ModeUndefined);
  1557. Expression(x);
  1558. op := result;
  1559. LoadValue(op,x.type.resolved);
  1560. conditional := prevConditional;
  1561. END Evaluate;
  1562. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1563. VAR prevConditional: BOOLEAN;
  1564. BEGIN
  1565. prevConditional := conditional;
  1566. conditional := FALSE;
  1567. InitOperand(result,ModeUndefined);
  1568. Expression(x);
  1569. op := result;
  1570. (*
  1571. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1572. *)
  1573. conditional := prevConditional;
  1574. END Designate;
  1575. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1576. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1577. BEGIN
  1578. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1579. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1580. conditional := TRUE;
  1581. trueLabel := trueL; falseLabel := falseL;
  1582. Expression(x);
  1583. trueL := trueLabel; falseL := falseLabel;
  1584. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1585. END Condition;
  1586. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1587. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1588. BEGIN
  1589. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1590. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1591. RETURN op
  1592. END NewRegisterOperand;
  1593. PROCEDURE UnuseRegister(register: LONGINT);
  1594. BEGIN
  1595. IF (register > 0) THEN
  1596. register := registerUsageCount.Map(register);
  1597. registerUsageCount.DecUse(register);
  1598. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1599. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1600. END;
  1601. IF registerUsageCount.Use(register)=0 THEN
  1602. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1603. Warning(position, "register cannot be removed");
  1604. END;
  1605. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1606. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1607. END;
  1608. ELSIF registerUsageCount.Use(register)<0 THEN
  1609. Warning(position, "register removed too often");
  1610. IF dump # NIL THEN
  1611. dump.String("register removed too often"); dump.Ln; dump.Update;
  1612. END;
  1613. END;
  1614. END;
  1615. END UnuseRegister;
  1616. PROCEDURE UseRegister(register: LONGINT);
  1617. BEGIN
  1618. IF (register > 0) THEN
  1619. register := registerUsageCount.Map(register);
  1620. registerUsageCount.IncUse(register);
  1621. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1622. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1623. END;
  1624. IF registerUsageCount.Use(register)=1 THEN
  1625. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1626. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1627. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1628. END;
  1629. END;
  1630. END;
  1631. END UseRegister;
  1632. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1633. BEGIN
  1634. UnuseRegister(op.register)
  1635. END ReleaseIntermediateOperand;
  1636. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1637. BEGIN
  1638. UseRegister(op.register)
  1639. END UseIntermediateOperand;
  1640. PROCEDURE ReleaseOperand(CONST op: Operand);
  1641. BEGIN
  1642. UnuseRegister(op.op.register);
  1643. UnuseRegister(op.tag.register);
  1644. UnuseRegister(op.extra.register);
  1645. END ReleaseOperand;
  1646. (* save registers marked in array "markedRegisters" to the stack
  1647. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1648. *)
  1649. PROCEDURE SaveRegisters();
  1650. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1651. BEGIN
  1652. entry := usedRegisters;
  1653. WHILE entry # NIL DO
  1654. type := registerUsageCount.used[entry.register].type;
  1655. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1656. Emit(Push(position,op));
  1657. entry := entry.next;
  1658. END;
  1659. END SaveRegisters;
  1660. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1661. BEGIN
  1662. saved := usedRegisters;
  1663. usedRegisters := NIL;
  1664. END ReleaseUsedRegisters;
  1665. (** remove parameter registers from used queue *)
  1666. PROCEDURE ReleaseParameterRegisters;
  1667. VAR entry,prev,next: RegisterEntry;
  1668. BEGIN
  1669. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1670. WHILE entry # NIL DO
  1671. next := entry.next;
  1672. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1673. registerUsageCount.DecUse(entry.register);
  1674. ASSERT(registerUsageCount.Use(entry.register)=0);
  1675. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1676. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1677. END;
  1678. ELSIF prev = NIL THEN
  1679. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1680. ELSE
  1681. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1682. END;
  1683. entry := next;
  1684. END;
  1685. END ReleaseParameterRegisters;
  1686. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1687. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1688. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1689. BEGIN
  1690. entry := saved;
  1691. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1692. entry := prev;
  1693. WHILE entry # NIL DO
  1694. prev := entry.prev;
  1695. type := entry.type;
  1696. class := entry.registerClass;
  1697. IntermediateCode.InitRegister(op,type,class,entry.register);
  1698. (*
  1699. new := registerUsageCount.Next(type,class);
  1700. registerUsageCount.Remap(entry.register,new);
  1701. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1702. dump.String("remap register "); dump.Int(entry.register,1);
  1703. dump.String("to "); dump.Int(new,1);
  1704. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1705. END;
  1706. entry.register := new;
  1707. *)
  1708. Emit(Pop(position,op));
  1709. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1710. entry := prev;
  1711. END;
  1712. (*
  1713. usedRegisters := saved;
  1714. *)
  1715. END RestoreRegisters;
  1716. PROCEDURE CheckRegistersFree;
  1717. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1718. BEGIN
  1719. IF usedRegisters # NIL THEN
  1720. r := usedRegisters;
  1721. WHILE r # NIL DO
  1722. warning := "register ";
  1723. Strings.AppendInt(warning, r.register);
  1724. Strings.Append(warning, " not released.");
  1725. Warning(position,warning);
  1726. r := r .next;
  1727. END;
  1728. END;
  1729. FOR i := 0 TO registerUsageCount.count-1 DO
  1730. IF registerUsageCount.used[i].count < 0 THEN
  1731. warning := "register ";
  1732. Strings.AppendInt(warning, i);
  1733. Strings.Append(warning, " unused too often.");
  1734. Warning(position,warning);
  1735. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1736. warning := "register ";
  1737. Strings.AppendInt(warning, i);
  1738. Strings.Append(warning, " not unused often enough.");
  1739. Warning(position,warning);
  1740. END;
  1741. END;
  1742. END CheckRegistersFree;
  1743. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1744. Otherwise allocate a new register.
  1745. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1746. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1747. BEGIN
  1748. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1749. UseIntermediateOperand(result);
  1750. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1751. UseIntermediateOperand(result);
  1752. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1753. END;
  1754. END Reuse2;
  1755. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1756. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1757. If not then allocate a new register.
  1758. Does NOT necessarily keep the content of src1 or src2 in result!
  1759. *)
  1760. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1761. BEGIN
  1762. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1765. UseIntermediateOperand(result);
  1766. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1767. result := alternative; alternative := emptyOperand;
  1768. UseIntermediateOperand(result);
  1769. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1770. END;
  1771. END Reuse2a;
  1772. (* like reuse2 but only one source *)
  1773. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1774. BEGIN
  1775. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1776. UseIntermediateOperand(result);
  1777. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1778. END;
  1779. END Reuse1;
  1780. (* like reuse2a but only one source *)
  1781. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1782. BEGIN
  1783. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1784. UseIntermediateOperand(result);
  1785. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1786. UseIntermediateOperand(result);
  1787. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1788. END;
  1789. END Reuse1a;
  1790. (* like reuse1 but guarantees that content of src1 is in result *)
  1791. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1792. BEGIN
  1793. IF ReusableRegister(src1) THEN
  1794. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1795. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1796. UseIntermediateOperand(result);
  1797. ELSE
  1798. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1799. Emit(Mov(position,result,src1));
  1800. END
  1801. END ReuseCopy;
  1802. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1803. BEGIN
  1804. IF ReusableRegister(src) THEN
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1806. ELSE
  1807. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1808. Emit(Mov(position,result,src));
  1809. ReleaseIntermediateOperand(src);
  1810. END
  1811. END TransferToRegister;
  1812. (** labels and branches **)
  1813. PROCEDURE NewLabel(): Label;
  1814. VAR label: Label;
  1815. BEGIN
  1816. NEW(label,section); RETURN label;
  1817. END NewLabel;
  1818. PROCEDURE SetLabel(label: Label);
  1819. BEGIN label.Resolve(section.pc);
  1820. END SetLabel;
  1821. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1822. BEGIN
  1823. ASSERT(label # NIL);
  1824. IF label.pc < 0 THEN (* label not yet set *)
  1825. label.AddFixup(section.pc);
  1826. END;
  1827. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1828. END LabelOperand;
  1829. PROCEDURE BrL(label: Label);
  1830. BEGIN
  1831. Emit(Br(position,LabelOperand(label)));
  1832. END BrL;
  1833. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1834. BEGIN
  1835. Emit(Brge(position,LabelOperand(label),left,right));
  1836. END BrgeL;
  1837. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1838. BEGIN
  1839. Emit(Brlt(position,LabelOperand(label),left,right));
  1840. END BrltL;
  1841. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1842. BEGIN
  1843. Emit(Breq(position,LabelOperand(label),left,right));
  1844. END BreqL;
  1845. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1846. BEGIN
  1847. Emit(Brne(position,LabelOperand(label),left,right));
  1848. END BrneL;
  1849. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1850. VAR new: IntermediateCode.Operand;
  1851. BEGIN
  1852. IF Trace THEN TraceEnter("Convert") END;
  1853. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1854. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1855. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1856. & (operand.offset = 0)
  1857. THEN
  1858. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1859. Emit(Conv(position,new,operand));
  1860. ELSE
  1861. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1862. Emit(Conv(position,new,operand));
  1863. ReleaseIntermediateOperand(operand);
  1864. END;
  1865. operand := new;
  1866. ELSIF (operand.mode = IntermediateCode.ModeImmediate) & (operand.symbol.name = "") & (operand.type.sizeInBits <= type.sizeInBits) & (operand.type.form IN IntermediateCode.Integer) & (type.form IN IntermediateCode.Integer) THEN
  1867. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1868. ELSE
  1869. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1870. Emit(Conv(position,new,operand));
  1871. ReleaseIntermediateOperand(operand);
  1872. operand := new;
  1873. END;
  1874. IF Trace THEN TraceExit("Convert") END;
  1875. END Convert;
  1876. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1877. VAR exit: Label;
  1878. BEGIN
  1879. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1880. exit := NewLabel();
  1881. br(exit,left,right);
  1882. EmitTrap(position,trapNo);
  1883. SetLabel(exit);
  1884. END TrapC;
  1885. (** expressions *)
  1886. (** emit necessary runtime check for set elements **)
  1887. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1888. VAR max: IntermediateCode.Operand;
  1889. BEGIN
  1890. IF isUnchecked THEN RETURN END;
  1891. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1892. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1893. TrapC(BrgeL, max, o, SetElementTrap);
  1894. END;
  1895. END CheckSetElement;
  1896. (** the set that a range represents **)
  1897. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1898. VAR
  1899. operand: Operand;
  1900. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1901. BEGIN
  1902. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1903. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1904. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1905. one := IntermediateCode.Immediate(setType, 1);
  1906. Evaluate(x, operand);
  1907. Convert(operand.op, setType);
  1908. Convert(operand.tag, setType);
  1909. CheckSetElement(operand.op);
  1910. CheckSetElement(operand.tag);
  1911. (* create mask for lower bound
  1912. i.e. shift 11111111 to the left by the value of the lower bound
  1913. *)
  1914. Reuse1(temp, operand.op);
  1915. Emit(Shl(position,temp, allBits, operand.op));
  1916. ReleaseIntermediateOperand(operand.op);
  1917. operand.op := temp;
  1918. (* create mask for upper bound
  1919. i.e. shift 11111111 to the right by the difference between the
  1920. upper bound and the maximum number of set elements
  1921. *)
  1922. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1923. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1924. Reuse1(temp, operand.tag);
  1925. ELSE
  1926. Reuse1(temp, operand.tag);
  1927. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1928. Emit(Sub(position,temp, size, operand.tag));
  1929. END;
  1930. Emit(Shr(position,temp, allBits, operand.tag));
  1931. ReleaseIntermediateOperand(operand.tag);
  1932. operand.tag := temp;
  1933. Reuse2(resultingSet, operand.op, operand.tag);
  1934. (* intersect the two masks *)
  1935. Emit(And(position,resultingSet, operand.op, operand.tag));
  1936. ReleaseOperand(operand);
  1937. RETURN resultingSet
  1938. END SetFromRange;
  1939. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1940. VAR
  1941. res, operand: Operand;
  1942. temp, one, noBits, dest: IntermediateCode.Operand;
  1943. expression: SyntaxTree.Expression;
  1944. i: LONGINT;
  1945. BEGIN
  1946. IF Trace THEN TraceEnter("VisitSet") END;
  1947. dest := destination;
  1948. destination := emptyOperand;
  1949. noBits := IntermediateCode.Immediate(setType, 0);
  1950. one := IntermediateCode.Immediate(setType, 1);
  1951. (* start off with the empty set *)
  1952. InitOperand(res, ModeValue);
  1953. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1954. Emit(Mov(position,res.op, noBits));
  1955. FOR i := 0 TO x.elements.Length() - 1 DO
  1956. expression := x.elements.GetExpression(i);
  1957. IF expression IS SyntaxTree.RangeExpression THEN
  1958. (* range of set elements *)
  1959. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1960. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1961. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1962. ReleaseIntermediateOperand(temp)
  1963. ELSE
  1964. (* singelton element *)
  1965. Evaluate(expression, operand);
  1966. Convert(operand.op, setType);
  1967. CheckSetElement(operand.op);
  1968. (* create subset containing single element *)
  1969. Reuse1(temp, operand.op);
  1970. Emit(Shl(position,temp, one, operand.op));
  1971. ReleaseOperand(operand);
  1972. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1973. ReleaseIntermediateOperand(temp);
  1974. END
  1975. END;
  1976. result := res;
  1977. destination := dest;
  1978. IF Trace THEN TraceExit("VisitSet") END;
  1979. END VisitSet;
  1980. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1981. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1982. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1983. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1984. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1985. element: SyntaxTree.IntegerValue;
  1986. BEGIN
  1987. numberElements := x.elements.Length();
  1988. expression := index.parameters.GetExpression(dim);
  1989. element := expression(SyntaxTree.IntegerValue);
  1990. FOR i := 0 TO numberElements-1 DO
  1991. expression := x.elements.GetExpression(i);
  1992. element.SetValue(i);
  1993. IF expression IS SyntaxTree.MathArrayExpression THEN
  1994. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1995. ELSE
  1996. Assign(index,expression);
  1997. END;
  1998. END;
  1999. END RecursiveAssignment;
  2000. BEGIN
  2001. variable := GetTemporaryVariable(x.type, FALSE);
  2002. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2003. designator.SetType(variable.type);
  2004. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2005. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2006. FOR i := 0 TO dim-1 DO
  2007. element := SyntaxTree.NewIntegerValue(x.position,0);
  2008. element.SetType(system.longintType);
  2009. index.parameters.AddExpression(element);
  2010. END;
  2011. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2012. RecursiveAssignment(x,0);
  2013. Expression(designator);
  2014. END VisitMathArrayExpression;
  2015. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2016. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2017. BEGIN
  2018. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2019. dest := destination; destination := emptyOperand;
  2020. IF x.operator = Scanner.Not THEN
  2021. IF conditional THEN
  2022. Condition(x.left,falseLabel,trueLabel)
  2023. ELSE
  2024. Evaluate(x.left,operand);
  2025. InitOperand(result,ModeValue);
  2026. Reuse1a(result.op,operand.op,dest);
  2027. Emit(Xor(position,result.op,operand.op,true));
  2028. ReleaseOperand(operand);
  2029. END;
  2030. ELSIF x.operator = Scanner.Minus THEN
  2031. Evaluate(x.left,operand);
  2032. InitOperand(result,ModeValue);
  2033. Reuse1a(result.op,operand.op,dest);
  2034. type := x.left.type.resolved;
  2035. IF type IS SyntaxTree.SetType THEN
  2036. Emit(Not(position,result.op,operand.op));
  2037. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2038. Reuse1(result.tag,operand.tag);
  2039. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2040. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2041. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2042. Emit(Neg(position,result.op,operand.op));
  2043. ELSE HALT(200)
  2044. END;
  2045. ReleaseOperand(operand);
  2046. ELSIF x.operator = Scanner.Address THEN
  2047. Designate(x.left,operand);
  2048. operand.mode := ModeValue;
  2049. t0 := x.left.type.resolved;
  2050. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2051. ReleaseIntermediateOperand(operand.op);
  2052. operand.op := operand.tag;
  2053. IntermediateCode.InitOperand(operand.tag);
  2054. END;
  2055. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2056. result := operand;
  2057. ELSE HALT(100)
  2058. END;
  2059. destination := dest;
  2060. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2061. END VisitUnaryExpression;
  2062. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2063. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2064. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2065. BEGIN
  2066. type := type.resolved;
  2067. originalType := type;
  2068. IF type IS SyntaxTree.PointerType THEN
  2069. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2070. END;
  2071. IF type IS SyntaxTree.ObjectType THEN
  2072. BrL(trueL);
  2073. ELSE
  2074. ASSERT(type IS SyntaxTree.RecordType);
  2075. (*
  2076. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2077. *)
  2078. ReuseCopy(left,tag);
  2079. right := TypeDescriptorAdr(type);
  2080. IF ~newObjectFile THEN
  2081. IntermediateCode.MakeMemory(right,addressType);
  2082. END;
  2083. IF backend.cooperative THEN
  2084. repeatL := NewLabel();
  2085. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2086. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2087. END;
  2088. SetLabel(repeatL);
  2089. BreqL(trueL,left,right);
  2090. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2091. BrneL(repeatL,left,nil);
  2092. ELSIF meta.simple THEN
  2093. level := type(SyntaxTree.RecordType).Level();
  2094. (* get type desc tag of level relative to base tag *)
  2095. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2096. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2097. IntermediateCode.MakeMemory(left,addressType);
  2098. BreqL(trueL,left,right);
  2099. ELSE
  2100. level := type(SyntaxTree.RecordType).Level();
  2101. (* get type desc tag of level relative to base tag *)
  2102. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2103. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2104. IntermediateCode.MakeMemory(left,addressType);
  2105. BreqL(trueL,left,right);
  2106. END;
  2107. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2108. BrL(falseL);
  2109. END;
  2110. END TypeTest;
  2111. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2112. BEGIN
  2113. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2114. IF dump # NIL THEN
  2115. dump.String(s); dump.Ln;
  2116. END;
  2117. END Error;
  2118. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2119. BEGIN
  2120. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2121. IF dump # NIL THEN
  2122. dump.String(s); dump.Ln; dump.Update;
  2123. END;
  2124. END Warning;
  2125. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2126. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2127. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2128. operand:Operand;
  2129. BEGIN
  2130. previousSection := section;
  2131. Global.GetModuleName(mod,name);
  2132. Strings.Append(name,".@Trace");
  2133. Basic.ToSegmentedName(name, pooledName);
  2134. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2135. IF dump # NIL THEN dump := section.comments END;
  2136. IF backend.hasLinkRegister THEN
  2137. Emit(Push(Basic.invalidPosition, lr));
  2138. END;
  2139. variable := mod.moduleScope.firstVariable;
  2140. WHILE variable # NIL DO
  2141. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2142. Symbol(variable, operand);
  2143. register := operand.op;
  2144. CallTraceMethod(register, variable.type);
  2145. ReleaseIntermediateOperand(register);
  2146. END;
  2147. variable := variable.nextVariable;
  2148. END;
  2149. IF backend.hasLinkRegister THEN
  2150. Emit(Pop(Basic.invalidPosition, lr));
  2151. END;
  2152. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2153. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2154. Emit(Br(position,op));
  2155. INC(statCoopTraceModule, section.pc);
  2156. section := previousSection;
  2157. IF dump # NIL THEN dump := section.comments END;
  2158. END CreateTraceModuleMethod;
  2159. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2160. BEGIN
  2161. Emit (Push(position, dst));
  2162. Emit (Push(position, src));
  2163. CallThis(position,"GarbageCollector","Assign", 2);
  2164. END CallAssignPointer;
  2165. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2166. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2167. BEGIN
  2168. IF SemanticChecker.IsPointerType (type) THEN
  2169. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2170. ELSIF type.IsRecordType() THEN
  2171. Emit (Push(position,dst));
  2172. Emit (Push(position,src));
  2173. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2174. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2175. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2176. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2177. ELSIF IsStaticArray(type) THEN
  2178. size := StaticArrayNumElements(type);
  2179. base := StaticArrayBaseType(type);
  2180. IF base.IsRecordType() THEN
  2181. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2182. Emit (Push(position, dst));
  2183. Emit (Push(position, src));
  2184. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2185. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2187. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2188. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2189. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, dst));
  2192. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2193. Emit (Push(position, src));
  2194. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2195. ELSE
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, dst));
  2198. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2199. Emit (Push(position, src));
  2200. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2201. ASSERT(StaticArrayBaseType(type).IsPointer());
  2202. END;
  2203. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2204. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2205. Emit (Push(position, dst));
  2206. Emit (Push(position, src));
  2207. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2208. ELSE HALT(100); (* missing ? *)
  2209. END;
  2210. END CallAssignMethod;
  2211. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2212. VAR name: Basic.SegmentedName;
  2213. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2214. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2215. context: Context;
  2216. BEGIN
  2217. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2218. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2219. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2220. GetRecordTypeName (recordType,name);
  2221. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2222. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2223. IF dump # NIL THEN dump := section.comments END;
  2224. IF backend.hasLinkRegister THEN
  2225. Emit(Push(Basic.invalidPosition, lr));
  2226. END;
  2227. variable := recordType.recordScope.firstVariable;
  2228. WHILE variable # NIL DO
  2229. IF variable.NeedsTrace() THEN
  2230. dst := NewRegisterOperand (addressType);
  2231. src := NewRegisterOperand (addressType);
  2232. Emit (Mov(position, dst, parameter1));
  2233. Emit (Mov(position, src, parameter2));
  2234. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2235. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2236. CallAssignMethod(dst, src, variable.type);
  2237. ReleaseIntermediateOperand(src);
  2238. ReleaseIntermediateOperand(dst);
  2239. END;
  2240. variable := variable.nextVariable;
  2241. END;
  2242. recordBase := recordType.GetBaseRecord();
  2243. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2244. IF backend.hasLinkRegister THEN
  2245. Emit(Pop(Basic.invalidPosition, lr));
  2246. END;
  2247. GetRecordTypeName (recordBase,name);
  2248. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2249. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2250. Emit(Br(position,op));
  2251. ELSE
  2252. Emit(Exit(position,0,0, 0));
  2253. END;
  2254. IF ~recordType.isObject THEN
  2255. GetRecordTypeName (recordType,name);
  2256. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2257. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2258. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2259. NEW(registerUsageCount);
  2260. usedRegisters := NIL;
  2261. dst := NewRegisterOperand (addressType);
  2262. src := NewRegisterOperand (addressType);
  2263. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2264. Emit(Mov(position, dst, parameter1));
  2265. Emit(Mov(position, src, parameter2));
  2266. label := NewLabel();
  2267. SetLabel(label);
  2268. Emit(Push(position, dst));
  2269. Emit(Push(position, src));
  2270. GetRecordTypeName (recordType,name);
  2271. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2272. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2273. Emit(Call(position, op, 0));
  2274. Emit(Pop(position, src));
  2275. Emit(Pop(position, dst));
  2276. Emit(Add(position, dst, dst, ofs));
  2277. Emit(Add(position, src, src, ofs));
  2278. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2279. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2280. Emit(Exit(position,0,0, 0));
  2281. END;
  2282. INC(statCoopAssignProcedure, section.pc);
  2283. ReturnToContext(context);
  2284. IF dump # NIL THEN dump := section.comments END;
  2285. END CreateAssignProcedure;
  2286. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2287. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2288. BEGIN
  2289. IF IsUnsafePointer (type) THEN
  2290. skip := NewLabel();
  2291. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2292. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2293. BreqL (skip, op, nil);
  2294. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2295. SetLabel (skip);
  2296. ELSIF SemanticChecker.IsPointerType (type) THEN
  2297. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2298. CallThis(position,"GarbageCollector","Mark", 1);
  2299. ELSIF type.IsRecordType() THEN
  2300. Emit (Push(position,register));
  2301. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2302. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2303. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2304. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2305. ELSIF IsStaticArray(type) THEN
  2306. size := StaticArrayNumElements(type);
  2307. base := StaticArrayBaseType(type);
  2308. IF base.IsRecordType() THEN
  2309. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2310. Emit (Push(position, register));
  2311. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2312. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2314. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2315. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2316. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2317. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2318. Emit (Push(position, register));
  2319. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2320. ELSE
  2321. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2322. Emit (Push(position, register));
  2323. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2324. ASSERT(base.IsPointer());
  2325. END;
  2326. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2327. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2328. CallThis(position,"GarbageCollector","Mark", 1);
  2329. ELSE HALT(100); (* missing ? *)
  2330. END;
  2331. END CallTraceMethod;
  2332. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2333. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2334. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2335. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2336. BEGIN
  2337. previousSection := section;
  2338. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2339. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2340. GetRecordTypeName (recordType,name);
  2341. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2342. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2343. IF dump # NIL THEN dump := section.comments END;
  2344. IF backend.hasLinkRegister THEN
  2345. Emit(Push(Basic.invalidPosition, lr));
  2346. END;
  2347. variable := recordType.recordScope.firstVariable;
  2348. WHILE variable # NIL DO
  2349. IF variable.NeedsTrace() THEN
  2350. register := NewRegisterOperand (addressType);
  2351. Emit (Mov(position,register,parameter1));
  2352. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2353. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2354. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2355. END;
  2356. CallTraceMethod(register, variable.type);
  2357. ReleaseIntermediateOperand(register);
  2358. END;
  2359. variable := variable.nextVariable;
  2360. END;
  2361. recordBase := recordType.GetBaseRecord();
  2362. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2363. recordBase := recordBase.GetBaseRecord();
  2364. END;
  2365. IF recordBase # NIL THEN
  2366. IF backend.hasLinkRegister THEN
  2367. Emit(Pop(Basic.invalidPosition, lr));
  2368. END;
  2369. GetRecordTypeName (recordBase,name);
  2370. IF HasExplicitTraceMethod (recordBase) THEN
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2372. ELSE
  2373. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2374. END;
  2375. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2376. Emit(Br(position,op));
  2377. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2378. Emit(Exit(position,0,0,0));
  2379. ELSE
  2380. IF backend.hasLinkRegister THEN
  2381. Emit(Pop(Basic.invalidPosition, lr));
  2382. END;
  2383. IF recordType.isObject THEN
  2384. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2385. ELSE
  2386. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2387. END;
  2388. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2389. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2390. Emit(Br(position,op));
  2391. END;
  2392. IF ~recordType.isObject THEN
  2393. GetRecordTypeName (recordType,name);
  2394. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2395. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2396. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2397. NEW(registerUsageCount);
  2398. usedRegisters := NIL;
  2399. IF dump # NIL THEN dump := section.comments END;
  2400. register := NewRegisterOperand (addressType);
  2401. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2402. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2403. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2404. END;
  2405. Emit (Push(position,register));
  2406. GetRecordTypeName (recordType,name);
  2407. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2408. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2409. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2410. ReleaseIntermediateOperand(register);
  2411. Emit(Exit(position,0,0,0));
  2412. GetRecordTypeName (recordType,name);
  2413. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2414. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2415. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2416. NEW(registerUsageCount);
  2417. usedRegisters := NIL;
  2418. register := NewRegisterOperand (addressType);
  2419. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2420. Emit(Mov(position, register, parameter1));
  2421. label := NewLabel();
  2422. SetLabel(label);
  2423. Emit(Push(position, register));
  2424. GetRecordTypeName (recordType,name);
  2425. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2426. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2427. Emit(Call(position, op, 0));
  2428. Emit(Pop(position, register));
  2429. Emit(Add(position, register, register, ofs));
  2430. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2431. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2432. Emit(Exit(position,0,0,0));
  2433. END;
  2434. INC(statCoopTraceMethod, section.pc);
  2435. ReturnToContext(context);
  2436. IF dump # NIL THEN dump := section.comments END;
  2437. END CreateTraceMethod;
  2438. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2439. VAR name: Basic.SegmentedName;
  2440. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2441. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2442. context: Context;
  2443. BEGIN
  2444. IF recordType.isObject THEN RETURN END;
  2445. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2446. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2447. GetRecordTypeName (recordType,name);
  2448. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2449. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2450. IF dump # NIL THEN dump := section.comments END;
  2451. IF backend.hasLinkRegister THEN
  2452. Emit(Push(Basic.invalidPosition, lr));
  2453. END;
  2454. variable := recordType.recordScope.firstVariable;
  2455. WHILE variable # NIL DO
  2456. IF variable.NeedsTrace() THEN
  2457. dst := NewRegisterOperand (addressType);
  2458. Emit (Mov(position, dst, parameter1));
  2459. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2460. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2461. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2462. END;
  2463. CallResetProcedure(dst, nil, variable.type);
  2464. ReleaseIntermediateOperand(dst);
  2465. END;
  2466. variable := variable.nextVariable;
  2467. END;
  2468. recordBase := recordType.GetBaseRecord();
  2469. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2470. IF backend.hasLinkRegister THEN
  2471. Emit(Pop(Basic.invalidPosition, lr));
  2472. END;
  2473. GetRecordTypeName (recordBase,name);
  2474. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2475. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2476. Emit(Br(position,op));
  2477. ELSE
  2478. Emit(Exit(position,0,0, 0));
  2479. END;
  2480. GetRecordTypeName (recordType,name);
  2481. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2482. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2483. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2484. NEW(registerUsageCount);
  2485. usedRegisters := NIL;
  2486. dst := NewRegisterOperand (addressType);
  2487. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2488. Emit(Mov(position, dst, parameter1));
  2489. label := NewLabel();
  2490. SetLabel(label);
  2491. Emit(Push(position, dst));
  2492. GetRecordTypeName (recordType,name);
  2493. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2494. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2495. Emit(Call(position, op, 0));
  2496. Emit(Pop(position, dst));
  2497. Emit(Add(position, dst, dst, ofs));
  2498. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2499. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2500. Emit(Exit(position,0,0, 0));
  2501. INC(statCoopResetProcedure, section.pc);
  2502. ReturnToContext(context);
  2503. IF dump # NIL THEN dump := section.comments END;
  2504. END CreateResetProcedure;
  2505. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2506. VAR name: Basic.SegmentedName; context: Context;
  2507. BEGIN
  2508. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2509. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2510. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2511. IF dump # NIL THEN dump := section.comments END;
  2512. IF backend.hasLinkRegister THEN
  2513. Emit(Push(Basic.invalidPosition, lr));
  2514. END;
  2515. Emit(Push(position,fp));
  2516. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2517. ResetVariables(scope);
  2518. Emit(Pop(position,fp));
  2519. Emit(Exit(position,0,0, 0));
  2520. ReturnToContext(context);
  2521. IF dump # NIL THEN dump := section.comments END;
  2522. END CreateResetMethod;
  2523. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2524. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2525. BEGIN
  2526. IF SemanticChecker.IsPointerType (type) THEN
  2527. Emit (Push(position, dest));
  2528. CallThis(position,"GarbageCollector","Reset", 1);
  2529. ELSIF type.IsRecordType() THEN
  2530. Emit (Push(position, dest));
  2531. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2532. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2533. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2534. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2535. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2536. size := GetArrayLength(type, tag);
  2537. base := ArrayBaseType(type);
  2538. IF base.IsRecordType() THEN
  2539. Emit (Push(position, size));
  2540. Emit (Push(position, dest));
  2541. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2542. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2543. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2544. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2545. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2546. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2547. Emit (Push(position, size));
  2548. Emit (Push(position, dest));
  2549. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2550. ELSE
  2551. Emit (Push(position, size));
  2552. Emit (Push(position, dest));
  2553. CallThis(position,"GarbageCollector","ResetArray", 2);
  2554. ASSERT(ArrayBaseType(type).IsPointer());
  2555. END;
  2556. ReleaseIntermediateOperand(size);
  2557. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2558. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2559. Emit (Push(position, dest));
  2560. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2561. ELSE HALT(100); (* missing ? *)
  2562. END;
  2563. END CallResetProcedure;
  2564. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2565. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2566. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2567. VAR operand: Operand;
  2568. BEGIN
  2569. Symbol (symbol, operand);
  2570. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2571. ReleaseOperand(operand);
  2572. END Reset;
  2573. BEGIN
  2574. previousScope := currentScope;
  2575. currentScope := scope;
  2576. pc := section.pc;
  2577. variable := scope.firstVariable;
  2578. WHILE variable # NIL DO
  2579. IF variable.NeedsTrace() THEN
  2580. Reset (variable);
  2581. END;
  2582. variable := variable.nextVariable;
  2583. END;
  2584. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2585. WHILE parameter # NIL DO
  2586. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2587. Reset (parameter);
  2588. END;
  2589. parameter := parameter.nextParameter;
  2590. END;
  2591. INC(statCoopResetVariables, section.pc - pc);
  2592. currentScope := previousScope;
  2593. END ResetVariables;
  2594. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2595. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2596. VAR op: IntermediateCode.Operand; context: Context;
  2597. BEGIN
  2598. previousSection := section;
  2599. GetCodeSectionNameForSymbol(procedure, name);
  2600. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2601. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2602. IF dump # NIL THEN dump := section.comments END;
  2603. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2604. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2605. Emit(Data(position,op));
  2606. Emit(Data(position,nil));
  2607. IF HasPointers (procedure.procedureScope) THEN
  2608. GetCodeSectionNameForSymbol(procedure, name);
  2609. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2610. ELSE
  2611. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2612. END;
  2613. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2614. Emit(Data(position,op));
  2615. ReturnToContext(context);
  2616. IF dump # NIL THEN dump := section.comments END;
  2617. END CreateProcedureDescriptor;
  2618. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2619. VAR import: SyntaxTree.Import;
  2620. s: Basic.MessageString;
  2621. selfName: SyntaxTree.IdentifierString;
  2622. BEGIN
  2623. moduleScope.ownerModule.GetName(selfName);
  2624. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2625. module := moduleScope.ownerModule
  2626. ELSE
  2627. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2628. IF import = NIL THEN
  2629. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2630. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2631. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2632. s := "Module ";
  2633. Strings.Append(s,moduleName);
  2634. Strings.Append(s," cannot be imported.");
  2635. IF force THEN
  2636. Error(position,s);
  2637. ELSIF canBeLoaded THEN
  2638. Strings.Append(s, "=> no dynamic linking.");
  2639. Warning(position, s);
  2640. canBeLoaded := FALSE;
  2641. END;
  2642. RETURN FALSE
  2643. ELSE
  2644. SELF.module.imports.AddName(moduleName)
  2645. END;
  2646. ELSIF import.module = NIL THEN (* already tried *)
  2647. RETURN FALSE
  2648. END;
  2649. module := import.module;
  2650. END;
  2651. RETURN TRUE
  2652. END AddImport;
  2653. (* needed for old binary object file format*)
  2654. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2655. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2656. BEGIN
  2657. IF AddImport(moduleName,module,TRUE) THEN
  2658. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2659. IF procedure = NIL THEN
  2660. s := "Instruction not supported on target, emulation procedure ";
  2661. Strings.Append(s,moduleName);
  2662. Strings.Append(s,".");
  2663. Strings.Append(s,procedureName);
  2664. Strings.Append(s," not present");
  2665. Error(position,s);
  2666. ELSE
  2667. StaticCallOperand(r,procedure);
  2668. ReleaseOperand(r);
  2669. fp := GetFingerprint(procedure);
  2670. END;
  2671. END;
  2672. END EnsureSymbol;
  2673. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2674. VAR exit: Label; trueL,falseL: Label;
  2675. BEGIN
  2676. trueL := NewLabel();
  2677. falseL := NewLabel();
  2678. exit := NewLabel();
  2679. Condition(x,trueL,falseL);
  2680. InitOperand(result,ModeValue);
  2681. SetLabel(trueL);
  2682. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2683. Emit(Mov(position,result.op,true));
  2684. BrL(exit);
  2685. SetLabel(falseL);
  2686. Emit(MovReplace(position,result.op,false));
  2687. SetLabel(exit);
  2688. END ConditionToValue;
  2689. PROCEDURE ValueToCondition(VAR op: Operand);
  2690. BEGIN
  2691. LoadValue(op,system.booleanType);
  2692. BrneL(trueLabel,op.op, false);
  2693. ReleaseOperand(op);
  2694. BrL(falseLabel);
  2695. END ValueToCondition;
  2696. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2697. VAR size: LONGINT;
  2698. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2699. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2700. BEGIN
  2701. ASSERT(type.form = SyntaxTree.Open);
  2702. baseType := type.arrayBase.resolved;
  2703. IF IsOpenArray(baseType) THEN
  2704. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2705. ELSE
  2706. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2707. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2708. END;
  2709. len := tag;
  2710. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2711. IntermediateCode.MakeMemory(len,addressType);
  2712. UseIntermediateOperand(len);
  2713. Reuse2(res,sizeOperand,len);
  2714. Emit(Mul(position,res,sizeOperand,len));
  2715. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2716. RETURN res
  2717. END GetArraySize;
  2718. BEGIN
  2719. type := type.resolved;
  2720. IF IsOpenArray(type) THEN
  2721. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2722. RETURN tag
  2723. ELSE
  2724. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2725. END;
  2726. ELSE
  2727. size := ToMemoryUnits(system,system.SizeOf(type));
  2728. RETURN IntermediateCode.Immediate(addressType,size)
  2729. END;
  2730. END GetDynamicSize;
  2731. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2732. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2733. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2734. BEGIN
  2735. ASSERT(type.form = SyntaxTree.Open);
  2736. baseType := type.arrayBase.resolved;
  2737. IF IsOpenArray(baseType) THEN
  2738. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2739. ELSE
  2740. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2741. END;
  2742. len := tag;
  2743. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2744. IntermediateCode.MakeMemory(len,addressType);
  2745. UseIntermediateOperand(len);
  2746. Reuse2(res,sizeOperand,len);
  2747. Emit(Mul(position,res,sizeOperand,len));
  2748. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2749. RETURN res
  2750. END GetLength;
  2751. BEGIN
  2752. type := type.resolved;
  2753. IF IsOpenArray(type) THEN
  2754. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2755. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2756. ELSIF type IS SyntaxTree.StringType THEN
  2757. RETURN tag;
  2758. ELSE
  2759. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2760. END;
  2761. END GetArrayLength;
  2762. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2763. VAR result: IntermediateCode.Operand;
  2764. BEGIN
  2765. IF backend.cooperative THEN
  2766. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2767. ELSE
  2768. MakeMemory(result, tag, addressType, 0);
  2769. END;
  2770. RETURN result
  2771. END GetSizeFromTag;
  2772. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2773. VAR parameter: SyntaxTree.Parameter;
  2774. BEGIN
  2775. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2776. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2777. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2779. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2780. END;
  2781. RETURN GetDynamicSize(e.type, tag);
  2782. END GetArrayOfBytesSize;
  2783. (*
  2784. to find imported symbol. not needed ?
  2785. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2786. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2787. BEGIN
  2788. IF AddImport(moduleName,importedModule,FALSE) THEN
  2789. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2790. RETURN symbol
  2791. ELSE
  2792. RETURN NIL
  2793. END
  2794. END SymbolByName;
  2795. *)
  2796. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2797. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2798. BEGIN
  2799. IF AddImport(moduleName,runtimeModule,force) THEN
  2800. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2801. IF procedure = NIL THEN
  2802. s := "Procedure ";
  2803. Strings.Append(s,moduleName);
  2804. Strings.Append(s,".");
  2805. Strings.Append(s,procedureName);
  2806. Strings.Append(s," not present");
  2807. Error(position,s);
  2808. RETURN FALSE
  2809. ELSE
  2810. RETURN TRUE
  2811. END;
  2812. ELSE RETURN FALSE
  2813. END;
  2814. END GetRuntimeProcedure;
  2815. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2816. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2817. s: Basic.MessageString;
  2818. BEGIN
  2819. Basic.InitSegmentedName(name);
  2820. name[0] := Basic.MakeString(moduleName);
  2821. name[1] := Basic.MakeString(typeName);
  2822. name[2] := -1;
  2823. IF AddImport(moduleName,importedModule, FALSE) THEN
  2824. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2825. IF symbol = NIL THEN
  2826. s := "type ";
  2827. Strings.Append(s,moduleName);
  2828. Strings.Append(s,".");
  2829. Strings.Append(s,typeName);
  2830. Strings.Append(s," not present");
  2831. Error(position,s);
  2832. END;
  2833. ELSE symbol := NIL;
  2834. END;
  2835. IF importedModule = moduleScope.ownerModule THEN
  2836. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. ELSE
  2838. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2839. END;
  2840. RETURN symbol
  2841. END GetTypeDescriptor;
  2842. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2843. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2844. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2845. pooledName: Basic.SegmentedName; size: LONGINT;
  2846. BEGIN
  2847. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2848. StaticCallOperand(result,procedure);
  2849. IF numberParameters < 0 THEN
  2850. size := ProcedureParametersSize(system,procedure);
  2851. ELSE
  2852. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2853. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2854. Error(position,"runtime call parameter count mismatch");
  2855. END;
  2856. END;
  2857. Emit(Call(position, result.op, size));
  2858. ReleaseOperand(result);
  2859. ELSE (* only static linking possible *)
  2860. ASSERT(numberParameters >= 0);
  2861. Basic.InitSegmentedName(pooledName);
  2862. pooledName[0] := Basic.MakeString(moduleName);
  2863. pooledName[1] := Basic.MakeString(procedureName);
  2864. pooledName[2] := -1;
  2865. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2866. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2867. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2868. END;
  2869. END CallThisChecked;
  2870. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2871. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2872. BEGIN
  2873. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2874. END CallThis;
  2875. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2876. VAR
  2877. left,right: Operand;
  2878. leftSize, rightSize: IntermediateCode.Operand;
  2879. saved: RegisterEntry;
  2880. reg: IntermediateCode.Operand;
  2881. procedureName: SyntaxTree.IdentifierString;
  2882. BEGIN
  2883. procedureName := "CompareString";
  2884. SaveRegisters();ReleaseUsedRegisters(saved);
  2885. Designate(leftExpression,left);
  2886. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2887. Emit(Push(position,leftSize));
  2888. ReleaseIntermediateOperand(leftSize);
  2889. Emit(Push(position,left.op));
  2890. ReleaseOperand(left);
  2891. Designate(rightExpression,right);
  2892. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2893. Emit(Push(position,rightSize));
  2894. ReleaseIntermediateOperand(rightSize);
  2895. Emit(Push(position,right.op));
  2896. ReleaseOperand(right);
  2897. IF backend.cooperative THEN
  2898. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2899. ELSE
  2900. CallThis(position,runtimeModuleName,procedureName, 4);
  2901. END;
  2902. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2903. Emit(Result(position,reg));
  2904. (*
  2905. AcquireThisRegister(int8,IntermediateCode.Result);
  2906. *)
  2907. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2908. (*
  2909. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2910. *)
  2911. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2912. ReleaseIntermediateOperand(reg);
  2913. BrL(falseLabel);
  2914. END CompareString;
  2915. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2916. VAR
  2917. left,right: Operand;
  2918. leftSize, rightSize: IntermediateCode.Operand;
  2919. saved: RegisterEntry;
  2920. procedureName: SyntaxTree.IdentifierString;
  2921. BEGIN
  2922. procedureName := "CopyString";
  2923. SaveRegisters();ReleaseUsedRegisters(saved);
  2924. Designate(leftExpression,left);
  2925. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2926. Emit(Push(position,leftSize));
  2927. ReleaseIntermediateOperand(leftSize);
  2928. Emit(Push(position,left.op));
  2929. ReleaseOperand(left);
  2930. Designate(rightExpression,right);
  2931. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2932. Emit(Push(position,rightSize));
  2933. ReleaseIntermediateOperand(rightSize);
  2934. Emit(Push(position,right.op));
  2935. ReleaseOperand(right);
  2936. IF backend.cooperative THEN
  2937. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2938. ELSE
  2939. CallThis(position,runtimeModuleName,procedureName,4);
  2940. END;
  2941. RestoreRegisters(saved);
  2942. END CopyString;
  2943. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2944. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2945. leftType,rightType: SyntaxTree.Type;
  2946. leftExpression,rightExpression : SyntaxTree.Expression;
  2947. componentType: IntermediateCode.Type;
  2948. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2949. size: LONGINT;
  2950. BEGIN
  2951. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2952. dest := destination; destination := emptyOperand;
  2953. leftType := x.left.type.resolved;
  2954. rightType := x.right.type.resolved;
  2955. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2956. CASE x.operator OF
  2957. Scanner.Or:
  2958. (* shortcut evaluation of left OR right *)
  2959. IF ~conditional THEN ConditionToValue(x);
  2960. ELSE
  2961. next := NewLabel();
  2962. Condition(x.left,trueLabel,next);
  2963. SetLabel(next);
  2964. Condition(x.right,trueLabel,falseLabel);
  2965. END;
  2966. |Scanner.And:
  2967. (* shortcut evaluation of left & right *)
  2968. IF ~conditional THEN ConditionToValue(x);
  2969. ELSE
  2970. next := NewLabel();
  2971. Condition(x.left,next,falseLabel);
  2972. SetLabel(next);
  2973. Condition(x.right,trueLabel,falseLabel);
  2974. END;
  2975. |Scanner.Is:
  2976. IF ~conditional THEN ConditionToValue(x);
  2977. ELSE
  2978. (* get type desc tag *)
  2979. IF IsPointerToRecord(leftType,recordType) THEN
  2980. Evaluate(x.left,left);
  2981. Dereference(left,recordType,IsUnsafePointer(leftType))
  2982. ELSE
  2983. Designate(x.left,left);
  2984. END;
  2985. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2986. ReleaseOperand(left);
  2987. END;
  2988. |Scanner.Plus:
  2989. Evaluate(x.left,left);
  2990. Evaluate(x.right,right);
  2991. IF leftType IS SyntaxTree.SetType THEN
  2992. InitOperand(result,ModeValue);
  2993. Reuse2a(result.op,left.op,right.op,dest);
  2994. Emit(Or(position,result.op,left.op,right.op));
  2995. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2996. InitOperand(result,ModeValue);
  2997. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2998. Reuse2(result.tag,left.tag,right.tag);
  2999. Emit(Add(position,result.op,left.op,right.op));
  3000. Emit(Add(position,result.tag,left.tag,right.tag))
  3001. ELSE
  3002. InitOperand(result,ModeValue);
  3003. (*! IF SemanticChecker.IsIntegerType(leftType) THEN
  3004. AddInt(result.op, left.op, right.op) ;
  3005. ELSE
  3006. *)
  3007. Reuse2a(result.op,left.op,right.op,dest);
  3008. Emit(Add(position,result.op,left.op,right.op));
  3009. (*
  3010. END;
  3011. *)
  3012. END;
  3013. ReleaseOperand(left); ReleaseOperand(right);
  3014. |Scanner.Minus:
  3015. Evaluate(x.left,left);
  3016. Evaluate(x.right,right);
  3017. IF leftType IS SyntaxTree.SetType THEN
  3018. InitOperand(result,ModeValue);
  3019. Reuse1(result.op,right.op);
  3020. Emit(Not(position,result.op,right.op));
  3021. ReleaseOperand(right);
  3022. Emit(And(position,result.op,result.op,left.op));
  3023. ReleaseOperand(left);
  3024. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3025. InitOperand(result,ModeValue);
  3026. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3027. Reuse2(result.tag,left.tag,right.tag);
  3028. Emit(Sub(position,result.op,left.op,right.op));
  3029. Emit(Sub(position,result.tag,left.tag,right.tag));
  3030. ReleaseOperand(left); ReleaseOperand(right)
  3031. ELSE
  3032. InitOperand(result,ModeValue);
  3033. Reuse2a(result.op,left.op,right.op,dest);
  3034. Emit(Sub(position,result.op,left.op,right.op));
  3035. ReleaseOperand(left); ReleaseOperand(right);
  3036. END;
  3037. |Scanner.Times:
  3038. Evaluate(x.left,left);
  3039. Evaluate(x.right,right);
  3040. IF leftType IS SyntaxTree.SetType THEN
  3041. InitOperand(result,ModeValue);
  3042. Reuse2a(result.op,left.op,right.op,dest);
  3043. Emit(And(position,result.op,left.op,right.op));
  3044. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3045. InitOperand(result, ModeValue);
  3046. componentType := left.op.type;
  3047. (* TODO: review this *)
  3048. (*
  3049. result.op = left.op * right.op - left.tag * right.tag
  3050. result.tag = left.tag * right.op + left.op * right.tag
  3051. *)
  3052. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3053. Emit(Mul(position,result.op, left.op, right.op));
  3054. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3055. Emit(Mul(position,tempReg, left.tag, right.tag));
  3056. Emit(Sub(position,result.op, result.op, tempReg));
  3057. Reuse2(result.tag, left.tag, right.op);
  3058. Emit(Mul(position,result.tag, left.tag, right.op));
  3059. Emit(Mul(position,tempReg, left.op, right.tag));
  3060. Emit(Add(position,result.tag, result.tag, tempReg));
  3061. ReleaseIntermediateOperand(tempReg)
  3062. ELSE
  3063. InitOperand(result,ModeValue);
  3064. Reuse2a(result.op,left.op,right.op,dest);
  3065. Emit(Mul(position,result.op,left.op,right.op));
  3066. END;
  3067. ReleaseOperand(left); ReleaseOperand(right);
  3068. |Scanner.Div:
  3069. Evaluate(x.left,left);
  3070. Evaluate(x.right,right);
  3071. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3072. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3073. IF ~isUnchecked THEN
  3074. exit := NewLabel();
  3075. BrltL(exit,zero,right.op);
  3076. EmitTrap(position,NegativeDivisorTrap);
  3077. SetLabel(exit);
  3078. END;
  3079. END;
  3080. InitOperand(result,ModeValue);
  3081. Reuse2a(result.op,left.op,right.op,dest);
  3082. Emit(Div(position,result.op,left.op,right.op));
  3083. ReleaseOperand(left); ReleaseOperand(right);
  3084. |Scanner.Mod:
  3085. Evaluate(x.left,left);
  3086. Evaluate(x.right,right);
  3087. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3088. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3089. IF ~isUnchecked THEN
  3090. exit := NewLabel();
  3091. BrltL(exit,zero,right.op);
  3092. EmitTrap(position,NegativeDivisorTrap);
  3093. SetLabel(exit);
  3094. END;
  3095. END;
  3096. InitOperand(result,ModeValue);
  3097. Reuse2a(result.op,left.op,right.op,dest);
  3098. Emit(Mod(position,result.op,left.op,right.op));
  3099. ReleaseOperand(left); ReleaseOperand(right);
  3100. |Scanner.Slash:
  3101. Evaluate(x.left,left);
  3102. Evaluate(x.right,right);
  3103. IF leftType IS SyntaxTree.SetType THEN
  3104. InitOperand(result,ModeValue);
  3105. Reuse2a(result.op,left.op,right.op,dest);
  3106. Emit(Xor(position,result.op,left.op,right.op));
  3107. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3108. InitOperand(result,ModeValue);
  3109. componentType := left.op.type;
  3110. (* review this *)
  3111. (*
  3112. divisor = right.op * right.op + right.tag * right.tag
  3113. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3114. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3115. *)
  3116. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3117. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3118. Emit(Mul(position,tempReg, right.op, right.op));
  3119. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3120. Emit(Add(position,tempReg, tempReg, tempReg2));
  3121. result.op := tempReg2;
  3122. Emit(Mul(position,result.op, left.op, right.op));
  3123. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3124. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3125. Emit(Add(position,result.op, result.op, tempReg2));
  3126. Emit(Div(position,result.op, result.op, tempReg));
  3127. Reuse2(result.tag, left.tag, right.op);
  3128. Emit(Mul(position,result.tag, left.tag, right.op));
  3129. Emit(Mul(position,tempReg2, left.op, right.tag));
  3130. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3131. Emit(Div(position,result.tag, result.tag, tempReg));
  3132. ReleaseIntermediateOperand(tempReg);
  3133. ReleaseIntermediateOperand(tempReg2)
  3134. ELSE
  3135. InitOperand(result,ModeValue);
  3136. Reuse2a(result.op,left.op,right.op,dest);
  3137. Emit(Div(position,result.op,left.op,right.op));
  3138. END;
  3139. ReleaseOperand(left); ReleaseOperand(right);
  3140. |Scanner.Equal:
  3141. IF ~conditional THEN ConditionToValue(x);
  3142. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3143. CompareString(BreqL,x.left,x.right);
  3144. ELSE
  3145. Evaluate(x.left,left);
  3146. Evaluate(x.right,right);
  3147. IF leftType IS SyntaxTree.RangeType THEN
  3148. ASSERT(rightType IS SyntaxTree.RangeType);
  3149. BrneL(falseLabel, left.op, right.op); (* first *)
  3150. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3151. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3152. ReleaseOperand(left); ReleaseOperand(right);
  3153. BrL(trueLabel)
  3154. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3155. BrneL(falseLabel, left.op, right.op); (* first *)
  3156. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3157. ReleaseOperand(left); ReleaseOperand(right);
  3158. BrL(trueLabel)
  3159. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3160. (* TODO: review this *)
  3161. BrneL(falseLabel, left.op, right.op); (* real part *)
  3162. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3163. ReleaseOperand(left); ReleaseOperand(right);
  3164. BrL(trueLabel)
  3165. ELSE
  3166. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3167. ReleaseOperand(left); ReleaseOperand(right);
  3168. BrL(trueLabel);
  3169. END;
  3170. END;
  3171. |Scanner.LessEqual:
  3172. IF ~conditional THEN ConditionToValue(x);
  3173. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3174. CompareString(BrgeL,x.right,x.left);
  3175. ELSE
  3176. Evaluate(x.left,left);
  3177. Evaluate(x.right,right);
  3178. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3179. Reuse1(temp.op,right.op);
  3180. Emit(And(position,temp.op,left.op,right.op));
  3181. ReleaseOperand(right);
  3182. BreqL(trueLabel,temp.op,left.op);
  3183. BrL(falseLabel);
  3184. ReleaseOperand(temp);ReleaseOperand(left);
  3185. ELSE
  3186. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3187. ReleaseOperand(left); ReleaseOperand(right);
  3188. BrL(trueLabel);
  3189. END;
  3190. END;
  3191. |Scanner.Less:
  3192. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3193. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3194. leftExpression.SetType(system.booleanType);
  3195. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3196. rightExpression.SetType(system.booleanType);
  3197. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3198. leftExpression.SetType(system.booleanType);
  3199. Expression(leftExpression);
  3200. ELSIF ~conditional THEN ConditionToValue(x);
  3201. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3202. CompareString(BrltL,x.left,x.right);
  3203. ELSE
  3204. Evaluate(x.left,left);
  3205. Evaluate(x.right,right);
  3206. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3207. ReleaseOperand(left); ReleaseOperand(right);
  3208. BrL(trueLabel);
  3209. END;
  3210. |Scanner.Greater:
  3211. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3212. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3213. leftExpression.SetType(system.booleanType);
  3214. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3215. rightExpression.SetType(system.booleanType);
  3216. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3217. leftExpression.SetType(system.booleanType);
  3218. Expression(leftExpression);
  3219. ELSIF ~conditional THEN ConditionToValue(x);
  3220. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3221. CompareString(BrltL,x.right,x.left);
  3222. ELSE
  3223. Evaluate(x.left,left);
  3224. Evaluate(x.right,right);
  3225. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3226. ReleaseOperand(left); ReleaseOperand(right);
  3227. BrL(trueLabel);
  3228. END;
  3229. |Scanner.GreaterEqual:
  3230. IF ~conditional THEN ConditionToValue(x);
  3231. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3232. CompareString(BrgeL,x.left,x.right);
  3233. ELSE
  3234. Evaluate(x.left,left);
  3235. Evaluate(x.right,right);
  3236. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3237. Reuse1(temp.op,left.op);
  3238. Emit(And(position,temp.op,left.op,right.op));
  3239. ReleaseOperand(left);
  3240. BreqL(trueLabel, temp.op,right.op);
  3241. ReleaseOperand(temp); ReleaseOperand(right);
  3242. BrL(falseLabel);
  3243. ELSE
  3244. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3245. ReleaseOperand(left); ReleaseOperand(right);
  3246. BrL(trueLabel);
  3247. END;
  3248. END;
  3249. |Scanner.Unequal:
  3250. IF ~conditional THEN ConditionToValue(x);
  3251. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3252. CompareString(BrneL,x.left,x.right);
  3253. ELSE
  3254. Evaluate(x.left,left);
  3255. Evaluate(x.right,right);
  3256. IF leftType IS SyntaxTree.RangeType THEN
  3257. ASSERT(rightType IS SyntaxTree.RangeType);
  3258. BrneL(trueLabel, left.op, right.op); (* first *)
  3259. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3260. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3261. ReleaseOperand(left); ReleaseOperand(right);
  3262. BrL(falseLabel)
  3263. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3264. BrneL(trueLabel, left.op, right.op); (* first *)
  3265. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3266. ReleaseOperand(left); ReleaseOperand(right);
  3267. BrL(falseLabel)
  3268. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3269. (* TODO: review this *)
  3270. BrneL(trueLabel, left.op, right.op); (* real part *)
  3271. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3272. ReleaseOperand(left); ReleaseOperand(right);
  3273. BrL(falseLabel)
  3274. ELSE
  3275. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3276. ReleaseOperand(left); ReleaseOperand(right);
  3277. BrL(trueLabel);
  3278. END;
  3279. END;
  3280. |Scanner.In:
  3281. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3282. Evaluate(x.left,left);
  3283. Evaluate(x.right,right);
  3284. Convert(left.op,setType);
  3285. ReuseCopy(temp.op,right.op);
  3286. Emit(Shr(position,temp.op,temp.op,left.op));
  3287. ReleaseOperand(right); ReleaseOperand(left);
  3288. IntermediateCode.InitImmediate(one,setType,1);
  3289. Emit(And(position,temp.op,temp.op,one));
  3290. IF conditional THEN
  3291. IntermediateCode.InitImmediate(zero,setType,0);
  3292. BrneL(trueLabel,temp.op,zero);
  3293. ReleaseOperand(temp);
  3294. BrL(falseLabel);
  3295. ELSE
  3296. Convert(temp.op,bool);
  3297. result.mode := ModeValue;
  3298. result.op := temp.op;
  3299. result.tag := nil; (* may be left over from calls to evaluate *)
  3300. END;
  3301. ELSE
  3302. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3303. IF x.operator = Scanner.Questionmarks THEN
  3304. leftExpression := x.left;
  3305. rightExpression := x.right;
  3306. ELSE
  3307. leftExpression := x.right;
  3308. rightExpression := x.left;
  3309. END;
  3310. Evaluate(leftExpression, left);
  3311. Emit(Push(position,left.op));
  3312. ReleaseOperand(left);
  3313. Designate(rightExpression, right);
  3314. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3315. IF ~backend.cellsAreObjects THEN
  3316. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3317. END;
  3318. Emit(Push(position,right.op));
  3319. ReleaseOperand(right);
  3320. IF backend.cellsAreObjects THEN
  3321. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3322. ELSE
  3323. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3324. END;
  3325. InitOperand(result, ModeValue);
  3326. result.op := NewRegisterOperand(bool);
  3327. Emit(Result(position,result.op));
  3328. IF conditional THEN
  3329. IntermediateCode.InitImmediate(zero,setType,0);
  3330. BrneL(trueLabel,result.op,zero);
  3331. ReleaseOperand(result);
  3332. BrL(falseLabel);
  3333. END;
  3334. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3335. leftExpression := x.left;
  3336. rightExpression := x.right;
  3337. Evaluate(leftExpression, left);
  3338. Emit(Push(position,left.op));
  3339. ReleaseOperand(left);
  3340. Evaluate(rightExpression, right);
  3341. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3342. IF ~backend.cellsAreObjects THEN
  3343. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3344. END;
  3345. Emit(Push(position,right.op));
  3346. ReleaseOperand(right);
  3347. IF backend.cellsAreObjects THEN
  3348. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3349. ELSE
  3350. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3351. END;
  3352. InitOperand(result, ModeValue);
  3353. result.op := NewRegisterOperand(bool);
  3354. Emit(Result(position,result.op));
  3355. IF conditional THEN
  3356. IntermediateCode.InitImmediate(zero,setType,0);
  3357. BrneL(trueLabel,result.op,zero);
  3358. ReleaseOperand(result);
  3359. BrL(falseLabel);
  3360. END;
  3361. ELSE
  3362. HALT(100);
  3363. END;
  3364. END;
  3365. destination := dest;
  3366. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3367. END VisitBinaryExpression;
  3368. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3369. VAR localResult, operand: Operand;
  3370. BEGIN
  3371. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3372. InitOperand(localResult, ModeValue);
  3373. ASSERT(x.first # NIL);
  3374. Evaluate(x.first, operand);
  3375. localResult.op := operand.op;
  3376. ReleaseOperand(operand);
  3377. UseIntermediateOperand(localResult.op);
  3378. ASSERT(x.last # NIL);
  3379. Evaluate(x.last, operand);
  3380. localResult.tag := operand.op;
  3381. ReleaseOperand(operand);
  3382. UseIntermediateOperand(localResult.tag);
  3383. IF x.step # NIL THEN
  3384. Evaluate(x.step, operand);
  3385. localResult.extra := operand.op;
  3386. ReleaseOperand(operand);
  3387. UseIntermediateOperand(localResult.extra);
  3388. END;
  3389. result := localResult;
  3390. IF Trace THEN TraceExit("VisitRangeExpression") END
  3391. END VisitRangeExpression;
  3392. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3393. BEGIN
  3394. HALT(100); (* should never be evaluated *)
  3395. END VisitTensorRangeExpression;
  3396. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3397. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3398. BEGIN
  3399. IF Trace THEN TraceEnter("VisitConversion") END;
  3400. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3401. dest := destination; destination := emptyOperand;
  3402. Evaluate(x.expression,old);
  3403. InitOperand(result,ModeValue);
  3404. result.op := old.op;
  3405. ASSERT(result.op.mode # 0);
  3406. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3407. (* convert TO a complex number *)
  3408. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3409. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3410. ASSERT(result.op.mode # 0);
  3411. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3412. (* convert FROM a complex number TO a complex number*)
  3413. result.tag := old.tag;
  3414. ASSERT(result.tag.mode # 0);
  3415. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3416. ASSERT(result.tag.mode # 0)
  3417. ELSE
  3418. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3419. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3420. END
  3421. ELSE
  3422. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3423. ASSERT(result.op.mode # 0);
  3424. result.tag := old.tag; (*! probably never used *)
  3425. END;
  3426. destination := dest;
  3427. IF Trace THEN TraceExit("VisitConversion") END;
  3428. END VisitConversion;
  3429. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3430. BEGIN
  3431. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3432. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3433. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3434. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3435. END VisitTypeDeclaration;
  3436. (** designators (expressions) *)
  3437. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3438. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3439. BEGIN
  3440. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3441. IF x.left # NIL THEN Expression(x.left) END;
  3442. Symbol(x.symbol,result);
  3443. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3444. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3445. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3446. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3447. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3448. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3449. END;
  3450. END;
  3451. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3452. END VisitSymbolDesignator;
  3453. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3454. BEGIN
  3455. IF isUnchecked THEN RETURN END;
  3456. IF tagsAvailable THEN
  3457. TrapC(BrltL,index,length,IndexCheckTrap);
  3458. END;
  3459. END BoundCheck;
  3460. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3461. VAR d: IntermediateCode.Operand;
  3462. BEGIN
  3463. IF isUnchecked THEN RETURN END;
  3464. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3465. TrapC(op,dim,d,ArraySizeTrap);
  3466. ReleaseIntermediateOperand(d);
  3467. END DimensionCheck;
  3468. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3469. VAR
  3470. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3471. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3472. expression: SyntaxTree.Expression;
  3473. resultingType, leftType, baseType: SyntaxTree.Type;
  3474. skipLabel1: Label;
  3475. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3476. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3477. variableOp: Operand;
  3478. variable: SyntaxTree.Variable;
  3479. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3480. VAR expression: SyntaxTree.Expression;
  3481. BEGIN
  3482. (* count the number of indices, ranges and tensorRanges in the index list *)
  3483. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3484. FOR i := 0 TO parameters.Length() - 1 DO
  3485. expression := parameters.GetExpression(i);
  3486. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3487. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3488. ELSE INC(indexCount)
  3489. END
  3490. END;
  3491. END CountIndices;
  3492. BEGIN
  3493. ASSERT(tagsAvailable);
  3494. resultingType := x.type.resolved; (* resulting type *)
  3495. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3496. InitOperand(localResult, ModeReference);
  3497. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3498. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3499. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3500. (* a globally defined array destination tag is available -> use and invalidate it*)
  3501. localResult.tag := arrayDestinationTag;
  3502. IntermediateCode.InitOperand(arrayDestinationTag)
  3503. ELSE
  3504. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3505. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3506. Symbol(variable, variableOp);
  3507. ReuseCopy(localResult.tag, variableOp.op);
  3508. ReleaseOperand(variableOp);
  3509. END
  3510. END;
  3511. indexListSize := x.parameters.Length();
  3512. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3513. ASSERT(tensorRangeCount <= 1);
  3514. (* designate the array to be indexed over, perform tensor range check if known *)
  3515. Designate(x.left, array);
  3516. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3517. Dereference(array, leftType,FALSE);
  3518. IF tensorRangeCount=0 THEN
  3519. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3520. END
  3521. END;
  3522. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3523. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3524. srcDimOffset := -indexListSize;
  3525. destDimOffset := -rangeCount
  3526. ELSE
  3527. srcDimOffset := 0;
  3528. destDimOffset := 0
  3529. END;
  3530. indexDim := 0;
  3531. (* use address of source array as basis *)
  3532. (*
  3533. ReuseCopy(localResult.op, array.op);
  3534. *)
  3535. localResult.op := array.op;
  3536. UseIntermediateOperand(localResult.op);
  3537. (* go through the index list *)
  3538. FOR i := 0 TO indexListSize - 1 DO
  3539. expression := x.parameters.GetExpression(i);
  3540. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3541. (* nothing to do *)
  3542. ELSE
  3543. (* determine which dimension of source array is currently looked at *)
  3544. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3545. (* get the memory operand pointing to array descriptor dimension *)
  3546. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3547. (* make a reusable register from it *)
  3548. ReuseCopy(srcDim, tmp);
  3549. ReleaseIntermediateOperand(tmp);
  3550. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3551. ELSE
  3552. srcDim := IntermediateCode.Immediate(int32, i)
  3553. END;
  3554. (* get length and increment of source array for current dimension *)
  3555. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3556. Convert(sourceLength.op, sizeType);
  3557. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3558. Convert(sourceIncrement.op, sizeType);
  3559. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3560. ReleaseIntermediateOperand(srcDim);
  3561. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3562. (* SINGLE INDEX *)
  3563. Evaluate(expression, index);
  3564. ReleaseIntermediateOperand(index.tag);
  3565. index.tag := emptyOperand;
  3566. Convert(index.op, sizeType);
  3567. (* lower bound check *)
  3568. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3569. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3570. ELSIF isUnchecked THEN (* do nothing *)
  3571. ELSE
  3572. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3573. END;
  3574. (* upper bound check *)
  3575. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3576. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3577. ELSIF isUnchecked THEN (* do nothing *)
  3578. ELSE
  3579. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3580. END;
  3581. ReleaseOperand(sourceLength);
  3582. Convert(index.op, addressType);
  3583. summand := index.op;
  3584. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3585. (* RANGE OF INDICES *)
  3586. Evaluate(expression, range);
  3587. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3588. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3589. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3590. ReleaseOperand(range);
  3591. Convert(firstIndex, sizeType);
  3592. Convert(lastIndex, sizeType);
  3593. Convert(stepSize, sizeType);
  3594. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3595. if it is 'MAX(LONGINT)' *)
  3596. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3597. TransferToRegister(lastIndex, lastIndex);
  3598. skipLabel1 := NewLabel();
  3599. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3600. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3601. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3602. SetLabel(skipLabel1)
  3603. END;
  3604. (* check if step size is valid *)
  3605. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3606. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3607. ELSIF isUnchecked THEN (* do nothing *)
  3608. ELSE
  3609. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3610. END;
  3611. (* check lower bound check *)
  3612. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3613. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3614. ELSIF isUnchecked THEN (* do nothing *)
  3615. ELSE
  3616. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3617. END;
  3618. (* check upper bound check *)
  3619. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3620. (* statically open range: nothing to do *)
  3621. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3622. ASSERT(staticLastIndex < staticSourceLength)
  3623. ELSIF isUnchecked THEN (* do nothing *)
  3624. ELSE
  3625. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3626. END;
  3627. (* determine length of target array for current dimension *)
  3628. (* 1. incorporate last index: *)
  3629. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3630. (* last index is static *)
  3631. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3632. targetLength := sourceLength.op
  3633. ELSE
  3634. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3635. END;
  3636. UseIntermediateOperand(targetLength);
  3637. ELSE
  3638. (* targetLength := lastIndex + 1
  3639. Reuse1(targetLength, lastIndex);
  3640. *)
  3641. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3642. END;
  3643. ReleaseOperand(sourceLength);
  3644. ReleaseIntermediateOperand(lastIndex);
  3645. (* 2. incorporate first index: *)
  3646. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3647. (* first index and current target length are static *)
  3648. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3649. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3650. (* first index = 0: nothing to do *)
  3651. ELSE
  3652. (* targetLength := targetLength - firstIndex *)
  3653. TransferToRegister(targetLength, targetLength);
  3654. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3655. END;
  3656. (* clip negative lengths to 0 *)
  3657. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3658. IF staticTargetLength < 0 THEN
  3659. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3660. END
  3661. ELSE
  3662. skipLabel1 := NewLabel();
  3663. TransferToRegister(targetLength, targetLength);
  3664. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3665. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3666. SetLabel(skipLabel1)
  3667. END;
  3668. (* 3. incorporate index step size: *)
  3669. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3670. (*step size and current target length are static *)
  3671. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3672. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3673. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3674. (* step size = 1: nothing to do *)
  3675. ELSE
  3676. (* emit code for this:
  3677. targetLength := (targetLength-1) DIV stepSize +1;
  3678. *)
  3679. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3680. DivInt(targetLength, targetLength, stepSize);
  3681. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3682. END;
  3683. (* determine increment of target array for current dimension *)
  3684. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3685. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3686. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3687. (* step size = 1 *)
  3688. targetIncrement := sourceIncrement.op;
  3689. UseIntermediateOperand(targetIncrement)
  3690. ELSE
  3691. (* targetIncrement := sourceIncrement * stepSize *)
  3692. Reuse1(targetIncrement, stepSize);
  3693. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3694. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3695. END;
  3696. ReleaseIntermediateOperand(stepSize);
  3697. (* write length and increment of target array to descriptor *)
  3698. IF destDimOffset < 0 THEN
  3699. (* determine which dimension of target array is currently looked at *)
  3700. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3701. TransferToRegister(destDim, tmp);
  3702. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3703. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3704. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3705. ReleaseIntermediateOperand(destDim)
  3706. ELSE
  3707. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3708. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3709. END;
  3710. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3711. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3712. INC(indexDim);
  3713. Convert(firstIndex, addressType);
  3714. TransferToRegister(summand, firstIndex);
  3715. ELSE HALT(100);
  3716. END;
  3717. (*
  3718. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3719. *)
  3720. Convert(sourceIncrement.op, addressType);
  3721. Convert(summand, addressType);
  3722. MulInt(summand, summand, sourceIncrement.op);
  3723. ReleaseIntermediateOperand(sourceIncrement.op);
  3724. AddInt(localResult.op, localResult.op, summand);
  3725. ReleaseIntermediateOperand(summand);
  3726. END
  3727. END;
  3728. result := localResult;
  3729. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3730. ReleaseIntermediateOperand(result.tag);
  3731. result.tag := TypeDescriptorAdr(resultingType);
  3732. IF ~newObjectFile THEN
  3733. IntermediateCode.MakeMemory(result.tag,addressType);
  3734. END;
  3735. ELSIF IsDelegate(resultingType) THEN
  3736. ReleaseIntermediateOperand(result.tag);
  3737. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3738. UseIntermediateOperand(result.tag);
  3739. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3740. ReleaseIntermediateOperand(result.tag);
  3741. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3742. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3743. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3744. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3745. (* finalize target array descriptor *)
  3746. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3747. (* write lengths and increments of target array for remaining dimensions *)
  3748. i := indexListSize;
  3749. WHILE indexDim < targetArrayDimensionality DO
  3750. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3751. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3752. ReleaseOperand(sourceLength);
  3753. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3754. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3755. ReleaseOperand(sourceIncrement);
  3756. INC(i); INC(indexDim);
  3757. END;
  3758. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3759. tmp := nil;
  3760. ELSE
  3761. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3762. END;
  3763. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3764. ReleaseIntermediateOperand(tmp);
  3765. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3766. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3767. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3768. ELSE
  3769. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3770. END;
  3771. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3772. ReleaseIntermediateOperand(tmp);
  3773. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3774. (* write dimensionality *)
  3775. IF targetArrayDimensionality # 0 THEN
  3776. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3777. END;
  3778. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3779. END;
  3780. ReleaseOperand(array);
  3781. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3782. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3783. END;
  3784. END MathIndexDesignator;
  3785. (* get the length of an array , trying to make use of static information *)
  3786. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3787. VAR res: IntermediateCode.Operand; size: LONGINT;
  3788. BEGIN
  3789. type := type.resolved;
  3790. IF type IS SyntaxTree.ArrayType THEN
  3791. WITH type: SyntaxTree.ArrayType DO
  3792. IF type.form = SyntaxTree.Static THEN
  3793. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3794. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3795. Evaluate(type.length, op);
  3796. ReleaseIntermediateOperand(op.tag);
  3797. RETURN op.op;*)
  3798. ELSE
  3799. res := tag;
  3800. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3801. IntermediateCode.MakeMemory(res,addressType);
  3802. UseIntermediateOperand(res);
  3803. RETURN res
  3804. END
  3805. END;
  3806. ELSE
  3807. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3808. RETURN IntermediateCode.Immediate(addressType,size);
  3809. END;
  3810. END ArrayLength;
  3811. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3812. BEGIN
  3813. IF IsImmediate(x) THEN
  3814. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3815. ELSE
  3816. IF ~ReusableRegister(res) THEN
  3817. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3818. ELSE
  3819. UseIntermediateOperand(res);
  3820. END;
  3821. Emit(Mov(position,res,x))
  3822. END;
  3823. END CopyInt;
  3824. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3825. BEGIN
  3826. ReleaseIntermediateOperand(res);
  3827. IF IsImmediate(x) & IsImmediate(y) THEN
  3828. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3829. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3830. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3831. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3832. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3833. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3834. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3835. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3836. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3837. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3838. UseIntermediateOperand(res);
  3839. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3840. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3841. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3842. UseIntermediateOperand(res);
  3843. ELSE
  3844. IF ~ReusableRegister(res) THEN
  3845. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3846. ELSE
  3847. UseIntermediateOperand(res);
  3848. END;
  3849. IF IsImmediate(x) & (x.intValue = 0) THEN
  3850. Emit(Mov(position,res,y))
  3851. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3852. Emit(Mov(position,res,x))
  3853. ELSE
  3854. Emit(Add(position,res, x, y));
  3855. END;
  3856. END;
  3857. END AddInt;
  3858. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3859. BEGIN
  3860. ReleaseIntermediateOperand(res);
  3861. IF IsImmediate(x) & IsImmediate(y) THEN
  3862. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3863. ELSE
  3864. IF ~ReusableRegister(res) THEN
  3865. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3866. ELSE UseIntermediateOperand(res);
  3867. END;
  3868. IF IsImmediate(x) & (x.intValue = 1) THEN
  3869. Emit(Mov(position,res,y))
  3870. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3871. Emit(Mov(position,res,x))
  3872. ELSE
  3873. Emit(Mul(position,res, x, y));
  3874. END;
  3875. END;
  3876. END MulInt;
  3877. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3878. BEGIN
  3879. ReleaseIntermediateOperand(res);
  3880. IF IsImmediate(x) & IsImmediate(y) THEN
  3881. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3882. ELSE
  3883. IF ~ReusableRegister(res) THEN
  3884. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3885. ELSE UseIntermediateOperand(res);
  3886. END;
  3887. IF IsImmediate(x) & (x.intValue = 1) THEN
  3888. Emit(Mov(position,res,y))
  3889. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3890. Emit(Mov(position,res,x))
  3891. ELSE
  3892. Emit(Div(position,res, x, y));
  3893. END;
  3894. END;
  3895. END DivInt;
  3896. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3897. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3898. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3899. BEGIN
  3900. type := x.left.type.resolved;
  3901. IF type IS SyntaxTree.StringType THEN
  3902. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3903. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3904. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3905. type := atype;
  3906. x.left.SetType(type);
  3907. END;
  3908. IntermediateCode.InitImmediate(res,addressType,0);
  3909. maxDim := x.parameters.Length()-1;
  3910. (*
  3911. computation rule:
  3912. a: ARRAY X,Y,Z OF Element with size S
  3913. a[i,j,k] -->
  3914. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3915. *)
  3916. FOR i := 0 TO maxDim DO
  3917. e := x.parameters.GetExpression(i);
  3918. Evaluate(e,index);
  3919. Convert(index.op,addressType);
  3920. AddInt(res, res, index.op);
  3921. IF i = 0 THEN
  3922. (*
  3923. ReuseCopy(res, index.op);
  3924. *)
  3925. Designate(x.left,array);
  3926. type := x.left.type.resolved;
  3927. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3928. Dereference(array, type, FALSE);
  3929. END;
  3930. (*
  3931. ELSE AddInt(res, res, index.op);
  3932. *)
  3933. END;
  3934. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3935. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3936. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3937. BoundCheck(index.op, length);
  3938. END;
  3939. ReleaseIntermediateOperand(length);
  3940. END;
  3941. ReleaseOperand(index);
  3942. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3943. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3944. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3945. MulInt(res,res,length);
  3946. END;
  3947. ReleaseIntermediateOperand(length);
  3948. END;
  3949. (* remaining open dimensions -- compute address *)
  3950. i := maxDim+1;
  3951. IF type IS SyntaxTree.ArrayType THEN
  3952. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3953. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3954. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3955. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3956. MulInt(res,res,length);
  3957. END;
  3958. ReleaseIntermediateOperand(length);
  3959. INC(i);
  3960. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3961. END;
  3962. END;
  3963. IF (type IS SyntaxTree.ArrayType) THEN
  3964. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3965. size := StaticSize(system, type);
  3966. IF size # 1 THEN
  3967. length := IntermediateCode.Immediate(addressType,size);
  3968. MulInt(res,res,length);
  3969. END;
  3970. ELSE
  3971. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3972. IF size # 1 THEN
  3973. length := IntermediateCode.Immediate(addressType,size);
  3974. MulInt(res,res,length);
  3975. END;
  3976. END;
  3977. END;
  3978. AddInt(res,res,array.op);
  3979. InitOperand(result,ModeReference);
  3980. result.op := res;
  3981. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3982. ReleaseIntermediateOperand(result.tag);
  3983. result.tag := TypeDescriptorAdr(type);
  3984. IF ~newObjectFile THEN
  3985. IntermediateCode.MakeMemory(result.tag,addressType);
  3986. END
  3987. ELSIF IsDelegate(type) THEN
  3988. ReleaseIntermediateOperand(result.tag);
  3989. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3990. UseIntermediateOperand(result.tag);
  3991. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3992. ReleaseIntermediateOperand(result.tag);
  3993. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3994. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3995. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3996. END;
  3997. ReleaseOperand(array);
  3998. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3999. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  4000. END;
  4001. END IndexDesignator;
  4002. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  4003. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  4004. BEGIN
  4005. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  4006. dest := destination; destination := emptyOperand;
  4007. type := x.left.type.resolved;
  4008. IF type IS SyntaxTree.MathArrayType THEN
  4009. MathIndexDesignator(x);
  4010. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4011. IndexDesignator(x);
  4012. END;
  4013. destination := dest;
  4014. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4015. END VisitIndexDesignator;
  4016. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4017. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4018. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4019. procedure: SyntaxTree.Procedure;
  4020. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4021. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4022. BEGIN
  4023. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4024. parameters := expression.parameters;
  4025. moduleName := "FoxArrayBase";
  4026. procedureName := "CopyDescriptor";
  4027. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4028. SaveRegisters();ReleaseUsedRegisters(saved);
  4029. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4030. IF procedure = NIL THEN
  4031. s := "procedure ";
  4032. Strings.Append(s,moduleName);
  4033. Strings.Append(s,".");
  4034. Strings.Append(s,procedureName);
  4035. Strings.Append(s," not present");
  4036. Error(position,s);
  4037. ELSE
  4038. (* push address of temporary variable *)
  4039. Symbol(variable,destOperand);
  4040. Emit(Push(position,destOperand.op));
  4041. ReleaseOperand(destOperand);
  4042. (* push src *)
  4043. Evaluate(expression.left,srcOperand);
  4044. (*
  4045. Dereference(srcOperand,expression.type.resolved);
  4046. Emit(Push(position,srcOperand.tag));
  4047. *)
  4048. Emit(Push(position,srcOperand.op));
  4049. ReleaseOperand(srcOperand);
  4050. tensorFound := FALSE;
  4051. FOR i := 0 TO parameters.Length()-1 DO
  4052. e := parameters.GetExpression(i);
  4053. IF e IS SyntaxTree.TensorRangeExpression THEN
  4054. tensorFound := TRUE;
  4055. ELSIF e IS SyntaxTree.RangeExpression THEN
  4056. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4057. ELSE
  4058. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4059. END;
  4060. END;
  4061. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4062. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4063. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4064. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4065. StaticCallOperand(procOp,procedure);
  4066. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4067. ReleaseOperand(procOp);
  4068. END;
  4069. RestoreRegisters(saved);
  4070. END;
  4071. RETURN variable
  4072. END PrepareTensorDescriptor;
  4073. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4074. VAR
  4075. type, descriptorType, baseType: SyntaxTree.Type;
  4076. operand, tmpOperand, variableOp, variable2Op: Operand;
  4077. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4078. variable, variable2: SyntaxTree.Variable;
  4079. dim, i, size: LONGINT;
  4080. (* TODO: needed? *)
  4081. oldArrayDestinationTag: IntermediateCode.Operand;
  4082. oldArrayDestinationDimension: LONGINT;
  4083. position: Position;
  4084. saved: RegisterEntry;
  4085. arrayFlags: SET;
  4086. m, n: LONGINT;
  4087. PROCEDURE Pass(op: IntermediateCode.Operand);
  4088. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4089. BEGIN
  4090. IF numberRegister >= 0 THEN
  4091. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4092. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4093. Emit(Mov(position,parameterRegister, op));
  4094. ELSE
  4095. Emit(Push(position,op))
  4096. END
  4097. END Pass;
  4098. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4099. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4100. BEGIN
  4101. formalType := formalType.resolved; actualType := actualType.resolved;
  4102. IF IsOpenArray(formalType)THEN
  4103. IF actualType IS SyntaxTree.StringType THEN
  4104. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4105. RETURN;
  4106. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4107. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4108. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4109. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4110. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4111. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4112. ELSE
  4113. tmp := baseReg;
  4114. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4115. IntermediateCode.MakeMemory(tmp,addressType);
  4116. Pass((tmp));
  4117. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4118. END;
  4119. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4120. END;
  4121. END PushArrayLens;
  4122. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4123. BEGIN
  4124. CASE size OF
  4125. |2: INCL(flags,Size2Flag);
  4126. |3: INCL(flags,Size3Flag);
  4127. |4: INCL(flags,Size4Flag);
  4128. |5: INCL(flags,Size5Flag);
  4129. |6: INCL(flags,Size6Flag);
  4130. |7: INCL(flags,Size7Flag);
  4131. |8: INCL(flags,Size8Flag);
  4132. END;
  4133. END SetSmallArraySizeFlag;
  4134. BEGIN
  4135. IF Trace THEN TraceEnter("PushParameter") END;
  4136. position := expression.position;
  4137. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4138. type := expression.type.resolved;
  4139. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4140. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4141. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4142. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4143. );
  4144. (* TODO: needed? *)
  4145. oldArrayDestinationTag := arrayDestinationTag;
  4146. oldArrayDestinationDimension := arrayDestinationDimension;
  4147. IF IsArrayOfSystemByte(parameter.type) THEN
  4148. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4149. Designate(expression,operand);
  4150. ELSE
  4151. Evaluate(expression, tmpOperand);
  4152. variable := GetTemporaryVariable(expression.type, FALSE);
  4153. Symbol(variable, operand);
  4154. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4155. Emit(Mov(position,tmp, tmpOperand.op));
  4156. ReleaseOperand(tmpOperand);
  4157. END;
  4158. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4159. ReleaseIntermediateOperand(operand.tag);
  4160. operand.tag := tmp;
  4161. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4162. Pass((operand.tag));
  4163. END;
  4164. Pass((operand.op));
  4165. ELSIF IsOpenArray(parameter.type) THEN
  4166. Designate(expression,operand);
  4167. baseReg := operand.tag;
  4168. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4169. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4170. END;
  4171. Pass((operand.op)); (* address of the array *)
  4172. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4173. (* case 1 *)
  4174. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4175. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4176. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4177. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4178. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4179. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4180. arrayDestinationTag := sp;
  4181. (* case 1b *)
  4182. IF expression IS SyntaxTree.IndexDesignator THEN
  4183. (*
  4184. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4185. descriptorType := GetMathArrayDescriptorType(dim);
  4186. variable := GetTemporaryVariable(descriptorType,expression.position);
  4187. Symbol(variable,variableOp);
  4188. arrayDestinationTag := variableOp.op;
  4189. *)
  4190. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4191. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4192. arrayDestinationDimension := dim;
  4193. Designate(expression,operand);
  4194. (* case 1a *)
  4195. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4196. Designate(expression,operand);
  4197. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4198. i := 0;
  4199. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4200. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4201. INC(i);
  4202. END;
  4203. type := expression.type.resolved;
  4204. WHILE (i<dim) DO (* remaining static dimensions *)
  4205. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4206. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4207. ReleaseOperand(tmpOperand);
  4208. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4209. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4210. ReleaseOperand(tmpOperand);
  4211. INC(i);
  4212. END;
  4213. dimOp := IntermediateCode.Immediate(addressType,dim);
  4214. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4215. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4216. (* case 1d *)
  4217. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4218. Designate(expression,operand);
  4219. Dereference(operand,type.resolved,FALSE);
  4220. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4221. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4222. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4223. (* case 1f *)
  4224. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4225. Designate(expression,operand);
  4226. FOR i := 0 TO dim-1 DO
  4227. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4228. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4229. ReleaseOperand(tmpOperand);
  4230. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4231. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4232. ReleaseOperand(tmpOperand);
  4233. END;
  4234. (*******
  4235. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4236. *)
  4237. arrayFlags := {StaticFlag};
  4238. IF dim = 1 THEN
  4239. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4240. ReleaseOperand(tmpOperand);
  4241. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4242. m := LONGINT(tmpOperand.op.intValue);
  4243. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4244. ELSIF dim = 2 THEN
  4245. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4246. ReleaseOperand(tmpOperand);
  4247. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4248. m := LONGINT(tmpOperand.op.intValue);
  4249. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4250. ReleaseOperand(tmpOperand);
  4251. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4252. n := LONGINT(tmpOperand.op.intValue);
  4253. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4254. INCL(arrayFlags,SmallMatrixFlag);
  4255. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4256. END;
  4257. END;
  4258. (*******)
  4259. dimOp := IntermediateCode.Immediate(addressType,dim);
  4260. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4261. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4262. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4263. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4264. baseType := SemanticChecker.ArrayBase(type,dim);
  4265. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4266. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4267. ELSE HALT(100);
  4268. END;
  4269. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4270. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4271. (* case 2b *)
  4272. IF expression IS SyntaxTree.IndexDesignator THEN
  4273. descriptorType := GetMathArrayDescriptorType(dim);
  4274. variable := GetTemporaryVariable(descriptorType, FALSE);
  4275. Symbol(variable,variableOp);
  4276. arrayDestinationTag := variableOp.op;
  4277. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4278. arrayDestinationDimension := dim;
  4279. NeedDescriptor := TRUE;
  4280. Designate(expression,operand);
  4281. Pass((operand.tag));
  4282. NeedDescriptor := FALSE;
  4283. (* case 2a *)
  4284. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4285. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4286. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4287. INC(i);
  4288. END;
  4289. IF i = dim THEN
  4290. Designate(expression,operand);
  4291. Pass((operand.tag));
  4292. ELSE (* open-static *)
  4293. type := expression.type.resolved;
  4294. descriptorType := GetMathArrayDescriptorType(dim);
  4295. variable := GetTemporaryVariable(descriptorType, FALSE);
  4296. Symbol(variable,variableOp);
  4297. arrayDestinationTag := variableOp.op;
  4298. Designate(expression,operand);
  4299. FOR i := 0 TO dim-1 DO
  4300. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4301. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4302. ReleaseOperand(tmpOperand);
  4303. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4304. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4305. ReleaseOperand(tmpOperand);
  4306. END;
  4307. dimOp := IntermediateCode.Immediate(addressType,dim);
  4308. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4309. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4310. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4311. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4312. baseType := SemanticChecker.ArrayBase(type,dim);
  4313. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4314. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4315. Pass((arrayDestinationTag));
  4316. END;
  4317. (* case 2d *)
  4318. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4319. Designate(expression,operand);
  4320. Dereference(operand,type.resolved,FALSE);
  4321. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4322. Pass((operand.tag));
  4323. (* case 2f *)
  4324. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4325. descriptorType := GetMathArrayDescriptorType(dim);
  4326. variable := GetTemporaryVariable(descriptorType, FALSE);
  4327. Symbol(variable,variableOp);
  4328. arrayDestinationTag := variableOp.op;
  4329. Designate(expression,operand);
  4330. FOR i := 0 TO dim-1 DO
  4331. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4332. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4333. ReleaseOperand(tmpOperand);
  4334. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4335. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4336. ReleaseOperand(tmpOperand);
  4337. END;
  4338. (*
  4339. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4340. *)
  4341. arrayFlags := {StaticFlag};
  4342. IF dim = 1 THEN
  4343. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4344. ReleaseOperand(tmpOperand);
  4345. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4346. m := LONGINT(tmpOperand.op.intValue);
  4347. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4348. ELSIF dim = 2 THEN
  4349. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4350. ReleaseOperand(tmpOperand);
  4351. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4352. m := LONGINT(tmpOperand.op.intValue);
  4353. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4354. ReleaseOperand(tmpOperand);
  4355. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4356. n := LONGINT(tmpOperand.op.intValue);
  4357. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4358. INCL(arrayFlags,SmallMatrixFlag);
  4359. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4360. END;
  4361. END;
  4362. (*******)
  4363. dimOp := IntermediateCode.Immediate(addressType,dim);
  4364. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4365. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4366. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4367. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4368. baseType := SemanticChecker.ArrayBase(type,dim);
  4369. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4370. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4371. Pass((arrayDestinationTag));
  4372. ELSE HALT(100);
  4373. END;
  4374. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4375. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4376. (* case 3b *)
  4377. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4378. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4379. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4380. Symbol(variable,variableOp);
  4381. LoadValue(variableOp,system.addressType);
  4382. ELSE
  4383. descriptorType := GetMathArrayDescriptorType(dim);
  4384. variable := GetTemporaryVariable(descriptorType, FALSE);
  4385. Symbol(variable,variableOp);
  4386. END;
  4387. arrayDestinationTag := variableOp.op;
  4388. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4389. arrayDestinationDimension := 0;
  4390. Designate(expression,operand);
  4391. Pass((operand.tag));
  4392. (* case 3a *)
  4393. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4394. i := 0;
  4395. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4396. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4397. INC(i);
  4398. END;
  4399. IF i = dim THEN
  4400. Designate(expression,operand);
  4401. Pass((operand.tag));
  4402. ELSE (* open-static *)
  4403. type := expression.type.resolved;
  4404. descriptorType := GetMathArrayDescriptorType(dim);
  4405. variable := GetTemporaryVariable(descriptorType, FALSE);
  4406. Symbol(variable,variableOp);
  4407. arrayDestinationTag := variableOp.op;
  4408. Designate(expression,operand);
  4409. FOR i := 0 TO dim-1 DO
  4410. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4411. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4412. ReleaseOperand(tmpOperand);
  4413. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4414. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4415. ReleaseOperand(tmpOperand);
  4416. END;
  4417. dimOp := IntermediateCode.Immediate(addressType,dim);
  4418. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4419. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4420. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4421. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4422. baseType := SemanticChecker.ArrayBase(type,dim);
  4423. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4424. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4425. Pass((arrayDestinationTag));
  4426. END;
  4427. (* case 3d *)
  4428. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4429. Designate(expression,operand);
  4430. Dereference(operand,type.resolved,FALSE);
  4431. (*
  4432. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4433. *)
  4434. Pass((operand.tag));
  4435. (* case 3f *)
  4436. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4437. descriptorType := GetMathArrayDescriptorType(dim);
  4438. variable := GetTemporaryVariable(descriptorType, FALSE);
  4439. Symbol(variable,variableOp);
  4440. arrayDestinationTag := variableOp.op;
  4441. Designate(expression,operand);
  4442. IF operand.op.type.length >1 THEN (* vector register *)
  4443. variable2 := GetTemporaryVariable(type, FALSE);
  4444. Symbol(variable2, variable2Op);
  4445. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4446. Emit(Mov(position,tmp, operand.op));
  4447. ReleaseOperand(operand);
  4448. Symbol(variable2, operand);
  4449. END;
  4450. FOR i := 0 TO dim-1 DO
  4451. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4452. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4453. ReleaseOperand(tmpOperand);
  4454. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4455. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4456. ReleaseOperand(tmpOperand);
  4457. END;
  4458. dimOp := IntermediateCode.Immediate(addressType,dim);
  4459. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4460. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4461. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4462. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4463. baseType := SemanticChecker.ArrayBase(type,dim);
  4464. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4465. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4466. Pass((arrayDestinationTag));
  4467. ELSE HALT(100);
  4468. END;
  4469. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4470. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4471. (* case 4b *)
  4472. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4473. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4474. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4475. Symbol(variable,variableOp);
  4476. LoadValue(variableOp,system.addressType);
  4477. ELSE
  4478. descriptorType := GetMathArrayDescriptorType(dim);
  4479. variable := GetTemporaryVariable(descriptorType, FALSE);
  4480. Symbol(variable,variableOp);
  4481. END;
  4482. arrayDestinationTag := variableOp.op;
  4483. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4484. arrayDestinationDimension := 0;
  4485. Designate(expression,operand);
  4486. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4487. Symbol(variable,variableOp);
  4488. ELSE
  4489. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4490. Symbol(variable,variableOp);
  4491. MakeMemory(tmp,variableOp.op,addressType,0);
  4492. Emit(Mov(position,tmp,operand.tag));
  4493. ReleaseIntermediateOperand(tmp);
  4494. END;
  4495. Pass((variableOp.op));
  4496. ReleaseOperand(variableOp);
  4497. (* case 4a *)
  4498. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4499. i := 0;
  4500. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4501. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4502. INC(i);
  4503. END;
  4504. IF i = dim THEN
  4505. Designate(expression,operand);
  4506. arrayDestinationTag := operand.tag;
  4507. ELSE (* open-static *)
  4508. type := expression.type.resolved;
  4509. descriptorType := GetMathArrayDescriptorType(dim);
  4510. variable := GetTemporaryVariable(descriptorType, FALSE);
  4511. Symbol(variable,variableOp);
  4512. arrayDestinationTag := variableOp.op;
  4513. Designate(expression,operand);
  4514. FOR i := 0 TO dim-1 DO
  4515. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4516. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4517. ReleaseOperand(tmpOperand);
  4518. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4519. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4520. ReleaseOperand(tmpOperand);
  4521. END;
  4522. dimOp := IntermediateCode.Immediate(addressType,dim);
  4523. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4524. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4525. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4526. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4527. baseType := SemanticChecker.ArrayBase(type,dim);
  4528. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4529. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4530. END;
  4531. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4532. Symbol(variable,variableOp);
  4533. MakeMemory(tmp,variableOp.op,addressType,0);
  4534. Emit(Mov(position,tmp,arrayDestinationTag));
  4535. ReleaseIntermediateOperand(tmp);
  4536. Pass((variableOp.op));
  4537. ReleaseOperand(variableOp);
  4538. (* case 4d *)
  4539. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4540. Designate(expression,operand);
  4541. (*
  4542. Dereference(operand,type.resolved,FALSE);
  4543. *)
  4544. (*
  4545. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4546. *)
  4547. Pass((operand.op));
  4548. (* case 4f *)
  4549. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4550. descriptorType := GetMathArrayDescriptorType(dim);
  4551. variable := GetTemporaryVariable(descriptorType, FALSE);
  4552. Symbol(variable,variableOp);
  4553. arrayDestinationTag := variableOp.op;
  4554. Designate(expression,operand);
  4555. FOR i := 0 TO dim-1 DO
  4556. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4557. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4558. ReleaseOperand(tmpOperand);
  4559. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4560. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4561. ReleaseOperand(tmpOperand);
  4562. END;
  4563. dimOp := IntermediateCode.Immediate(addressType,dim);
  4564. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4565. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4566. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4567. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4568. baseType := SemanticChecker.ArrayBase(type,dim);
  4569. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4570. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4571. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4572. Symbol(variable,variableOp);
  4573. MakeMemory(tmp,variableOp.op,addressType,0);
  4574. Emit(Mov(position,tmp,arrayDestinationTag));
  4575. ReleaseIntermediateOperand(tmp);
  4576. Pass((variableOp.op));
  4577. ReleaseOperand(variableOp);
  4578. ELSE HALT(100);
  4579. END;
  4580. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4581. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4582. Designate(expression,operand);
  4583. IF operand.op.type.length > 1 THEN
  4584. Emit(Push(position, operand.op));
  4585. ELSE
  4586. size := system.SizeOf(type);
  4587. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4588. size := ToMemoryUnits(system,size);
  4589. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4590. arrayDestinationTag := sp;
  4591. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4592. END;
  4593. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4594. Designate(expression,operand);
  4595. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4596. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4597. Symbol(variable,variableOp);
  4598. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4599. Emit(Mov(position,tmp,operand.op));
  4600. Emit(Push(position,variableOp.op));
  4601. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4602. Pass((operand.op));
  4603. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4604. END;
  4605. ELSE HALT(200)
  4606. END;
  4607. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4608. IF parameter.kind = SyntaxTree.VarParameter THEN
  4609. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4610. Designate(expression, operand);
  4611. Pass((operand.op));
  4612. ELSE
  4613. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4614. Evaluate(expression, operand);
  4615. Pass((operand.extra)); (* step *)
  4616. Pass((operand.tag)); (* last *)
  4617. Pass((operand.op)) (* first *)
  4618. END
  4619. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4620. IF parameter.kind = SyntaxTree.VarParameter THEN
  4621. Designate(expression, operand);
  4622. Pass((operand.op));
  4623. ELSE
  4624. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4625. Evaluate(expression, operand);
  4626. Pass((operand.tag)); (* real part *)
  4627. Pass((operand.op)) (* imaginary part *)
  4628. END
  4629. ELSE
  4630. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4631. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4632. Designate(expression,operand);
  4633. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4634. (* stack allocation *)
  4635. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4636. (*! parameter alignment to be discussed ... *)
  4637. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4638. size := type(SyntaxTree.StringType).length;
  4639. END;
  4640. IF backend.cooperative & parameter.NeedsTrace() THEN
  4641. dst := NewRegisterOperand (addressType);
  4642. Emit(Mov(position,dst, sp));
  4643. IntermediateCode.InitImmediate(null, byteType, 0);
  4644. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4645. ReleaseIntermediateOperand(dst);
  4646. SaveRegisters();ReleaseUsedRegisters(saved);
  4647. (* register dst has been freed before SaveRegisters already *)
  4648. CallAssignMethod(dst, operand.op, parameter.type);
  4649. RestoreRegisters(saved);
  4650. END;
  4651. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4652. ELSIF IsOpenArray(parameter.type) THEN
  4653. Designate(expression,operand);
  4654. baseReg := operand.tag;
  4655. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4656. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4657. END;
  4658. Pass((operand.op)); (* address of the array *)
  4659. ELSIF IsDelegate(parameter.type) THEN
  4660. Evaluate(expression,operand);
  4661. IF backend.cooperative & parameter.NeedsTrace() THEN
  4662. Emit(Push(position, nil));
  4663. dst := NewRegisterOperand (addressType);
  4664. Emit(Mov(position,dst, sp));
  4665. ReleaseIntermediateOperand(dst);
  4666. SaveRegisters();ReleaseUsedRegisters(saved);
  4667. Emit(Push(position, dst));
  4668. (* register dst has been freed before SaveRegisters already *)
  4669. Emit(Push(position, operand.tag));
  4670. CallThis(position,"GarbageCollector","Assign",2);
  4671. RestoreRegisters(saved);
  4672. ELSE
  4673. Pass((operand.tag));
  4674. END;
  4675. Pass((operand.op));
  4676. ELSE
  4677. Evaluate(expression,operand);
  4678. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4679. Emit(Push(position, nil));
  4680. dst := NewRegisterOperand (addressType);
  4681. Emit(Mov(position,dst, sp));
  4682. ReleaseIntermediateOperand(dst);
  4683. SaveRegisters();ReleaseUsedRegisters(saved);
  4684. Emit(Push(position, dst));
  4685. (* register dst has been freed before SaveRegisters already *)
  4686. Emit(Push(position, operand.op));
  4687. CallThis(position,"GarbageCollector","Assign",2);
  4688. RestoreRegisters(saved);
  4689. ELSE
  4690. Pass((operand.op));
  4691. END;
  4692. END;
  4693. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4694. Evaluate(expression,operand);
  4695. Pass((operand.op));
  4696. ELSE (* var parameter *)
  4697. Designate(expression,operand);
  4698. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4699. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4700. Pass((operand.tag));
  4701. END;
  4702. END;
  4703. Pass((operand.op));
  4704. END;
  4705. END;
  4706. (* TODO: needed? *)
  4707. arrayDestinationTag := oldArrayDestinationTag;
  4708. arrayDestinationDimension := oldArrayDestinationDimension;
  4709. IF needsParameterBackup THEN
  4710. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4711. ReuseCopy(parameterBackup, operand.op)
  4712. END;
  4713. ReleaseOperand(operand);
  4714. IF Trace THEN TraceExit("PushParameter") END;
  4715. END PushParameter;
  4716. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4717. VAR prevConditional: BOOLEAN;
  4718. BEGIN
  4719. prevConditional := conditional;
  4720. conditional := FALSE;
  4721. 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
  4722. Expression(x.result); (* use register *)
  4723. END;
  4724. Statement(x.statement);
  4725. conditional := prevConditional;
  4726. IF x.result # NIL THEN Expression(x.result) END;
  4727. 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
  4728. ReleaseIntermediateOperand(result.op);
  4729. END;
  4730. END VisitStatementDesignator;
  4731. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4732. VAR
  4733. procedure: SyntaxTree.Procedure;
  4734. procedureType: SyntaxTree.ProcedureType;
  4735. wasInline: BOOLEAN;
  4736. actualParameters: SyntaxTree.ExpressionList;
  4737. formalParameter: SyntaxTree.Parameter;
  4738. actualParameter: SyntaxTree.Expression;
  4739. i: LONGINT;
  4740. localVariable: SyntaxTree.Variable;
  4741. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4742. src, dest: Operand;
  4743. prevInlineExit : Label;
  4744. prevMapper: SymbolMapper;
  4745. tooComplex: BOOLEAN;
  4746. resultDesignator: SyntaxTree.Expression;
  4747. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4748. BEGIN
  4749. IF e = NIL THEN RETURN TRUE
  4750. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4751. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4752. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4753. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4754. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4755. ELSE RETURN FALSE
  4756. END;
  4757. END SimpleExpression;
  4758. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4759. BEGIN
  4760. RETURN checker.CanPassInRegister(type)
  4761. END FitsInRegister;
  4762. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4763. VAR
  4764. variable: SyntaxTree.Variable;
  4765. variableDesignator: SyntaxTree.Designator;
  4766. BEGIN
  4767. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4768. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4769. variableDesignator.SetType(type);
  4770. RETURN variableDesignator
  4771. END GetTemp;
  4772. BEGIN
  4773. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4774. wasInline := currentIsInline;
  4775. prevInlineExit := currentInlineExit;
  4776. prevMapper := currentMapper;
  4777. currentInlineExit := NewLabel();
  4778. tooComplex := FALSE;
  4779. NEW(currentMapper);
  4780. currentIsInline := TRUE;
  4781. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4782. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4783. formalParameter := procedureType.firstParameter;
  4784. actualParameters := x.parameters;
  4785. i := 0;
  4786. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4787. actualParameter := actualParameters.GetExpression(i);
  4788. IF actualParameter.resolved # NIL THEN
  4789. actualParameter := actualParameter.resolved
  4790. END;
  4791. (*
  4792. if expression is simple and can be passed immediately
  4793. or if type fits in register then we can proceed
  4794. otherwise we escape to ordinary procedure call.
  4795. *)
  4796. (* cases where the expression can be mapped identically *)
  4797. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4798. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4799. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4800. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4801. (*
  4802. Assign(variableDesignator, actualParameter);
  4803. *)
  4804. Evaluate(actualParameter, src);
  4805. Designate(variableDesignator, dest);
  4806. Emit(Mov(x.position, dest.op, src.op));
  4807. ReleaseOperand(dest);
  4808. ReleaseOperand(src);
  4809. (* the src operand should now have been completely released ! *)
  4810. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4811. ELSE tooComplex := TRUE
  4812. END;
  4813. (*
  4814. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4815. currentMapper.Add(formalParameter, actualParameter, NIL);
  4816. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4817. variableDesignator := GetTemp(system.addressType, FALSE);
  4818. Designate(actualParameter, src);
  4819. Designate(variableDesignator, dest);
  4820. IntermediateCode.MakeMemory(dest.op,addressType);
  4821. Emit(Mov(x.position, dest.op, src.op));
  4822. ReleaseOperand(dest);
  4823. IF src.tag.mode # IntermediateCode.Undefined THEN
  4824. tagDesignator := GetTemp(system.addressType, FALSE);
  4825. Designate(tagDesignator, dest);
  4826. IntermediateCode.MakeMemory(dest.op,addressType);
  4827. Emit(Mov(x.position, dest.op, src.op));
  4828. END;
  4829. ReleaseOperand(dest); ReleaseOperand(src);
  4830. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4831. END;
  4832. *)
  4833. formalParameter := formalParameter.nextParameter;
  4834. INC(i);
  4835. END;
  4836. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4837. IF resultDesignator # NIL THEN
  4838. returnDesignator := resultDesignator
  4839. ELSE
  4840. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4841. END;
  4842. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4843. END;
  4844. localVariable := procedure.procedureScope.firstVariable;
  4845. WHILE ~tooComplex & (localVariable # NIL) DO
  4846. variableDesignator := GetTemp(localVariable.type, FALSE);
  4847. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4848. localVariable := localVariable.nextVariable;
  4849. END;
  4850. IF ~tooComplex THEN
  4851. VisitStatementBlock(procedure.procedureScope.body);
  4852. SetLabel(currentInlineExit);
  4853. IF procedureType.returnType # NIL THEN
  4854. Designate(returnDesignator, result);
  4855. IF conditional THEN
  4856. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4857. ValueToCondition(result)
  4858. END;
  4859. END;
  4860. END;
  4861. currentMapper := prevMapper;
  4862. currentInlineExit := prevInlineExit;
  4863. currentIsInline := wasInline;
  4864. RETURN ~tooComplex
  4865. END InlineProcedureCall;
  4866. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4867. VAR
  4868. parameters: SyntaxTree.ExpressionList;
  4869. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4870. designator: SyntaxTree.Designator;
  4871. procedureType: SyntaxTree.ProcedureType;
  4872. formalParameter: SyntaxTree.Parameter;
  4873. operand, returnValue: Operand;
  4874. reg, size, mask, dest: IntermediateCode.Operand;
  4875. saved: RegisterEntry;
  4876. symbol: SyntaxTree.Symbol;
  4877. variable: SyntaxTree.Variable;
  4878. i, parametersSize, returnTypeSize : LONGINT;
  4879. structuredReturnType: BOOLEAN;
  4880. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4881. tempVariableDesignator: SyntaxTree.Designator;
  4882. gap, alignment: LONGINT; (*fld*)
  4883. (* TODO: remove unnecessary backup variables *)
  4884. oldResult: Operand;
  4885. oldCurrentScope: SyntaxTree.Scope;
  4886. oldArrayDestinationTag: IntermediateCode.Operand;
  4887. oldArrayDestinationDimension: LONGINT;
  4888. oldConstantDeclaration: SyntaxTree.Symbol;
  4889. oldDestination: IntermediateCode.Operand;
  4890. oldCurrentLoop: Label;
  4891. oldConditional: BOOLEAN;
  4892. oldTrueLabel, oldFalseLabel: Label;
  4893. oldLocked: BOOLEAN;
  4894. usedRegisters,oldUsedRegisters: RegisterEntry;
  4895. return: IntermediateCode.Operand;
  4896. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4897. arg: IntermediateCode.Operand;
  4898. dummy: IntermediateCode.Operand;
  4899. recordType: SyntaxTree.RecordType;
  4900. operatorSelectionProcedureOperand: Operand;
  4901. operatorSelectionProcedure: SyntaxTree.Procedure;
  4902. fingerPrint: SyntaxTree.FingerPrint;
  4903. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4904. identifierNumber: LONGINT;
  4905. parameterRegister: Backend.Registers;
  4906. parameterRegisters: LONGINT;
  4907. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4908. procedure: SyntaxTree.Procedure;
  4909. PROCEDURE BackupGlobalState;
  4910. BEGIN
  4911. oldResult := result;
  4912. oldCurrentScope := currentScope;
  4913. oldArrayDestinationTag := arrayDestinationTag;
  4914. oldArrayDestinationDimension := arrayDestinationDimension;
  4915. oldConstantDeclaration := constantDeclaration;
  4916. oldDestination := destination;
  4917. oldCurrentLoop := currentLoop;
  4918. oldConditional := conditional;
  4919. oldTrueLabel := trueLabel;
  4920. oldFalseLabel := falseLabel;
  4921. oldLocked := locked;
  4922. oldUsedRegisters := usedRegisters
  4923. END BackupGlobalState;
  4924. PROCEDURE RestoreGlobalState;
  4925. BEGIN
  4926. result := oldResult;
  4927. currentScope := oldCurrentScope;
  4928. arrayDestinationTag := oldArrayDestinationTag;
  4929. arrayDestinationDimension := oldArrayDestinationDimension;
  4930. constantDeclaration := oldConstantDeclaration;
  4931. destination := oldDestination;
  4932. currentLoop := oldCurrentLoop;
  4933. conditional := oldConditional;
  4934. trueLabel := oldTrueLabel;
  4935. falseLabel := oldFalseLabel;
  4936. locked := oldLocked;
  4937. usedRegisters := oldUsedRegisters
  4938. END RestoreGlobalState;
  4939. (** do preparations before parameter push for array-structured object types (ASOTs):
  4940. if ASOT is passed as VAR parameter:
  4941. - allocate temporary variable of math array type
  4942. - copy contents of ASOT to be passed to temporary variable
  4943. - use temporary variable as the actual parameter instead
  4944. - 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)
  4945. **)
  4946. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4947. VAR
  4948. expression: SyntaxTree.Expression;
  4949. BEGIN
  4950. IF actualParameter IS SyntaxTree.Designator THEN
  4951. designator := actualParameter(SyntaxTree.Designator);
  4952. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4953. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4954. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4955. ASSERT(checker # NIL);
  4956. checker.SetCurrentScope(currentScope);
  4957. (* allocate temporary variable *)
  4958. ASSERT(actualParameter.type # NIL);
  4959. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4960. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4961. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4962. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4963. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4964. ASSERT(tempVariableDesignator.type # NIL);
  4965. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4966. (* copy math array stored in actual parameter to temporary variable *)
  4967. BackupGlobalState;
  4968. AssignMathArray(tempVariableDesignator, actualParameter);
  4969. RestoreGlobalState;
  4970. (* use temporary variable as actual parameter instead of the original one *)
  4971. actualParameter := tempVariableDesignator;
  4972. (* create write-back call and store it in linked list *)
  4973. (* create new list entry *)
  4974. IF firstWriteBackCall = NIL THEN
  4975. NEW(firstWriteBackCall);
  4976. currentWriteBackCall := firstWriteBackCall
  4977. ELSE
  4978. ASSERT(currentWriteBackCall # NIL);
  4979. NEW(currentWriteBackCall.next);
  4980. currentWriteBackCall := currentWriteBackCall.next
  4981. END;
  4982. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4983. ASSERT(expression.type = NIL);
  4984. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4985. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4986. (* prepare writeback for any other "normal" indexer *)
  4987. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4988. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4989. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4990. Assign(tempVariableDesignator, actualParameter);
  4991. actualParameter := tempVariableDesignator;
  4992. IF firstWriteBackCall = NIL THEN
  4993. NEW(firstWriteBackCall);
  4994. currentWriteBackCall := firstWriteBackCall
  4995. ELSE
  4996. ASSERT(currentWriteBackCall # NIL);
  4997. NEW(currentWriteBackCall.next);
  4998. currentWriteBackCall := currentWriteBackCall.next
  4999. END;
  5000. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  5001. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  5002. END
  5003. END
  5004. END PrepareParameter;
  5005. BEGIN
  5006. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5007. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5008. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5009. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5010. IF InlineProcedureCall(x) THEN
  5011. RETURN
  5012. ELSE
  5013. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5014. END
  5015. END;
  5016. END;
  5017. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5018. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5019. dest := destination; destination := emptyOperand;
  5020. SaveRegisters();ReleaseUsedRegisters(saved);
  5021. parameters := x.parameters;
  5022. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5023. (* an operator is called *)
  5024. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5025. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5026. (* check if a dynamic operator call should be performed *)
  5027. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5028. ELSE
  5029. isCallOfDynamicOperator := FALSE
  5030. END;
  5031. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5032. Emit(Push(position, ap));
  5033. END;
  5034. alignment := procedureType.stackAlignment;
  5035. IF alignment > 1 THEN
  5036. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5037. Emit(Mov(position,reg, sp));
  5038. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5039. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5040. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5041. Emit(And(position,sp, sp, mask));
  5042. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5043. Emit(Push(position,reg));
  5044. (*
  5045. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5046. Emit(Mov(position,mem,reg));
  5047. *)
  5048. ReleaseIntermediateOperand(reg);
  5049. END;
  5050. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5051. (* align stack to 16-byte boundary *)
  5052. IntermediateCode.InitImmediate(mask,addressType,-16);
  5053. Emit(And(position,sp, sp, mask));
  5054. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5055. IF gap # 0 THEN
  5056. IntermediateCode.InitImmediate(size,addressType,gap);
  5057. Emit(Sub(position,sp,sp,size))
  5058. END;
  5059. END;
  5060. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5061. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5062. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5063. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5064. ELSE
  5065. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5066. END;
  5067. Evaluate(x.left, operand);
  5068. IF symbol IS SyntaxTree.Procedure THEN
  5069. IF (procedureType.selfParameter # NIL) THEN
  5070. Emit(Push(position,operand.tag));
  5071. ELSIF x.left IS SyntaxTree.SupercallDesignator THEN
  5072. Emit(Push(position,operand.tag));
  5073. ELSIF (procedureType.isDelegate) THEN
  5074. Emit(Push(position,operand.tag));
  5075. END;
  5076. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5077. IF (procedureType.selfParameter # NIL) THEN
  5078. Emit(Push(position,operand.tag));
  5079. ELSIF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5080. Emit(Push(position,operand.tag));
  5081. END;
  5082. ELSE HALT(200);
  5083. END;
  5084. ReleaseIntermediateOperand(operand.tag);
  5085. operand.tag := emptyOperand;
  5086. (* determine if a structured return type is needed *)
  5087. structuredReturnType := SemanticChecker.StructuredReturnType(procedureType);
  5088. IF structuredReturnType THEN
  5089. IF resultDesignator # NIL THEN
  5090. d := resultDesignator;
  5091. ELSE
  5092. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5093. variable.SetUntraced(procedureType.hasUntracedReturn);
  5094. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5095. d.SetType(variable.type);
  5096. END;
  5097. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5098. Designate(d,returnValue);
  5099. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5100. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5101. Emit(Push(position,size));
  5102. Emit(Push(position,returnValue.op));
  5103. ReleaseOperand(returnValue);
  5104. ELSE
  5105. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5106. END;
  5107. END;
  5108. firstWriteBackCall := NIL; (* reset write-back call list *)
  5109. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5110. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5111. IF parameterRegister = NIL THEN parameterRegisters := 0
  5112. ELSE parameterRegisters := LEN(parameterRegister)
  5113. END;
  5114. passByRegister := parameterRegisters > 0;
  5115. registerNumber := 0;
  5116. formalParameter := procedureType.lastParameter;
  5117. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5118. actualParameter := parameters.GetExpression(i);
  5119. PrepareParameter(actualParameter, formalParameter);
  5120. IF passByRegister & (i < parameterRegisters) THEN
  5121. IF ~PassInRegister(formalParameter) THEN
  5122. Error(actualParameter.position,"cannot be passed by register")
  5123. ELSE
  5124. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5125. END;
  5126. INC(registerNumber);
  5127. ELSE
  5128. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5129. END;
  5130. formalParameter := formalParameter.prevParameter;
  5131. END;
  5132. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5133. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5134. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5135. END;
  5136. ELSE
  5137. hasDynamicOperands := FALSE;
  5138. formalParameter := procedureType.firstParameter;
  5139. FOR i := 0 TO parameters.Length() - 1 DO
  5140. actualParameter := parameters.GetExpression(i);
  5141. IF formalParameter # NIL THEN (* TENTATIVE *)
  5142. PrepareParameter(actualParameter, formalParameter);
  5143. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5144. ASSERT(i < 2);
  5145. hasDynamicOperands := TRUE;
  5146. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5147. ELSE
  5148. IF passByRegister & (registerNumber > 0) THEN
  5149. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5150. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5151. END;
  5152. passByRegister := FALSE;
  5153. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5154. END;
  5155. formalParameter := formalParameter.nextParameter;
  5156. END;
  5157. END;
  5158. END;
  5159. IF symbol IS SyntaxTree.Procedure THEN
  5160. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5161. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5162. Emit(Push(position,reg));
  5163. ReleaseIntermediateOperand(reg);
  5164. END;
  5165. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5166. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5167. parametersSize := ParametersSize(system,procedureType,FALSE);
  5168. END;
  5169. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5170. (*
  5171. dynamic operator overloading:
  5172. first push parameters, regularly:
  5173. [self]
  5174. par1
  5175. par2
  5176. then push parameters for GetOperator
  5177. identifier
  5178. ptr1
  5179. tag
  5180. ptr2
  5181. tag
  5182. call GetOperatorRuntimeProc
  5183. call Operator
  5184. *)
  5185. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5186. (* push ID *)
  5187. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5188. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5189. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5190. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5191. formalParameter := procedureType.firstParameter;
  5192. FOR i := 0 TO parameters.Length() - 1 DO
  5193. IF formalParameter.access # SyntaxTree.Hidden THEN
  5194. ASSERT(i < 2);
  5195. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5196. (* push pointer *)
  5197. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5198. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5199. (* add dereference *)
  5200. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5201. (*! better: do refer to stack above than using parameter backups !!*)
  5202. ReleaseIntermediateOperand(parameterBackups[i]);
  5203. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5204. END;
  5205. Emit(Push(position,parameterBackups[i]));
  5206. ReleaseIntermediateOperand(parameterBackups[i]);
  5207. (* push typetag *)
  5208. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5209. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5210. arg := TypeDescriptorAdr(recordType);
  5211. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5212. Emit(Push(position,arg));
  5213. ELSE
  5214. (* push 'NonPointer' *)
  5215. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5216. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5217. (* push fingerprint *)
  5218. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5219. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5220. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5221. END
  5222. END;
  5223. formalParameter := formalParameter.nextParameter
  5224. END;
  5225. (* for unary operators: complete the information for the second parameter *)
  5226. IF procedureType.numberParameters < 2 THEN
  5227. (* push 'NonPointer' *)
  5228. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5229. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5230. (* push 'NoType' *)
  5231. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5232. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5233. END;
  5234. (* call operator selection procedure *)
  5235. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5236. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5237. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5238. ReleaseOperand(operatorSelectionProcedureOperand);
  5239. (* use the address that the operator selection procedure returned as the target address of the call *)
  5240. InitOperand(operand, ModeValue);
  5241. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5242. Emit(Result(position,operand.op))
  5243. END
  5244. END;
  5245. ReleaseParameterRegisters();
  5246. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5247. Emit(Call(position,operand.op,0));
  5248. ELSE
  5249. Emit(Call(position,operand.op,parametersSize));
  5250. END;
  5251. ReleaseOperand(operand);
  5252. IF procedureType.noReturn THEN
  5253. EmitTrap(position,NoReturnTrap);
  5254. END;
  5255. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5256. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5257. Emit(Result(position,return));
  5258. END;
  5259. (* === return parameter space === *)
  5260. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5261. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5262. IF parametersSize < 32 THEN
  5263. (* allocated space for all parameter registers *)
  5264. parametersSize := 32
  5265. END;
  5266. size := IntermediateCode.Immediate(addressType,parametersSize);
  5267. Emit(Add(position,sp,sp,size))
  5268. END;
  5269. IF SysvABI(procedureType.callingConvention) THEN
  5270. IF passByRegister THEN
  5271. IF parameters.Length() > parameterRegisters THEN
  5272. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5273. ELSE
  5274. parametersSize := 0
  5275. END;
  5276. ELSE
  5277. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5278. INC( parametersSize, (-parametersSize) MOD 16 )
  5279. END;
  5280. IF parametersSize > 0 THEN
  5281. size := IntermediateCode.Immediate(addressType,parametersSize);
  5282. Emit(Add(position,sp,sp,size))
  5283. END;
  5284. END;
  5285. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5286. IF structuredReturnType THEN
  5287. (* stack pointer rewinding done by callee
  5288. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5289. Emit(Add(position,sp,sp,size));
  5290. *)
  5291. RestoreRegisters(saved);
  5292. InitOperand(result,ModeReference);
  5293. Symbol(variable,result);
  5294. ELSE
  5295. RestoreRegisters(saved);
  5296. InitOperand(result,ModeValue);
  5297. result.op := return;
  5298. END;
  5299. END;
  5300. IF alignment > 1 THEN
  5301. Emit(Pop(position,sp));
  5302. END;
  5303. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5304. Emit(Pop(position, ap));
  5305. END;
  5306. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5307. ValueToCondition(result);
  5308. END;
  5309. destination := dest;
  5310. (* perform all write-back calls in the list *)
  5311. BackupGlobalState;
  5312. currentWriteBackCall := firstWriteBackCall;
  5313. WHILE currentWriteBackCall # NIL DO
  5314. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5315. currentWriteBackCall := currentWriteBackCall.next
  5316. END;
  5317. RestoreGlobalState;
  5318. (* TENATIVE *)
  5319. (*
  5320. IF isOperatorCall THEN
  5321. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5322. END;
  5323. *)
  5324. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5325. END VisitProcedureCallDesignator;
  5326. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5327. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5328. td: SyntaxTree.Symbol;
  5329. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5330. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5331. BEGIN
  5332. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5333. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5334. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5335. variable.SetType(system.anyType);
  5336. scope.AddVariable(variable);
  5337. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5338. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5339. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5340. typeDeclaration.SetScope(module.module.moduleScope);
  5341. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5342. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5343. END;
  5344. RETURN hiddenPointerType;
  5345. END GetHiddenPointerType;
  5346. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5347. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5348. BEGIN
  5349. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5350. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5351. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5352. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5353. scope.AddVariable(variable);
  5354. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5355. variable.SetType(system.anyType);
  5356. scope.AddVariable(variable);
  5357. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5358. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5359. typeDeclaration.SetDeclaredType(delegatePointerType);
  5360. typeDeclaration.SetScope(module.module.moduleScope);
  5361. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5362. delegatePointerType.SetState(SyntaxTree.Resolved);
  5363. END;
  5364. RETURN delegatePointerType
  5365. END GetDelegateType;
  5366. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5367. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5368. such as in VAR a: RECORD ... END;
  5369. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5370. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5371. *)
  5372. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5373. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5374. BEGIN (* no code emission *)
  5375. source := NIL;
  5376. x := x.resolved;
  5377. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5378. x := GetHiddenPointerType();
  5379. ELSIF IsDelegate(x) THEN
  5380. x := GetDelegateType();
  5381. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5382. ELSE HALT(200);
  5383. END;
  5384. td := x.typeDeclaration;
  5385. IF td = NIL THEN
  5386. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5387. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5388. ASSERT(td # NIL);
  5389. END;
  5390. IF newObjectFile THEN
  5391. GetCodeSectionNameForSymbol(td,name);
  5392. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5393. meta.CheckTypeDeclaration(x);
  5394. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5395. ELSE
  5396. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5397. END;
  5398. IF backend.cooperative OR meta.simple THEN
  5399. offset := 0;
  5400. ELSE
  5401. IF x IS SyntaxTree.CellType THEN
  5402. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5403. IF baseRecord = NIL THEN
  5404. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5405. ELSE
  5406. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5407. END;
  5408. ELSE
  5409. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5410. END;
  5411. END;
  5412. ELSE
  5413. offset := 0;
  5414. source := module.allSections.FindBySymbol(td); (*TODO*)
  5415. IF source = NIL THEN
  5416. null := 0;
  5417. GetCodeSectionNameForSymbol(td,name);
  5418. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5419. Basic.SuffixSegmentedName (name, module.module.name);
  5420. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5421. IntermediateCode.InitImmediate(op,addressType,0);
  5422. source(IntermediateCode.Section).Emit(Data(position,op));
  5423. source.SetReferenced(FALSE)
  5424. ELSE
  5425. name := source.name;
  5426. END;
  5427. END;
  5428. RETURN td
  5429. END GetBackendType;
  5430. BEGIN
  5431. (*td := t.typeDeclaration;*)
  5432. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5433. (*
  5434. IF t IS SyntaxTree.PointerType THEN
  5435. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5436. ELSE
  5437. source := GetBackendType(t);
  5438. END;
  5439. *)
  5440. IF newObjectFile THEN
  5441. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5442. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5443. IntermediateCode.SetOffset(res,offset);
  5444. ELSE
  5445. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5446. END;
  5447. (*
  5448. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5449. make memory should be used when tag is used, not earlier
  5450. *)
  5451. RETURN res
  5452. END TypeDescriptorAdr;
  5453. (*
  5454. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5455. VAR result: IntermediateCode.Operand;
  5456. BEGIN
  5457. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5458. operand.tag := TypeDescriptorAdr(operand.type);
  5459. IntermediateCode.MakeMemory(operand.tag,addressType);
  5460. UseIntermediateOperand(operand.tag);
  5461. END;
  5462. END MakeTypeTag;
  5463. *)
  5464. PROCEDURE ProfilerInit;
  5465. VAR reg: IntermediateCode.Operand;
  5466. BEGIN
  5467. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5468. Emit(Call(position,reg,0));
  5469. END ProfilerInit;
  5470. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5471. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5472. BEGIN
  5473. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5474. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5475. THEN
  5476. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5477. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5478. Emit(Push(position,reg));
  5479. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5480. Emit(Push(position,reg));
  5481. StaticCallOperand(result,procedure);
  5482. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5483. ReleaseOperand(result);
  5484. END;
  5485. END ProfilerEnterExit;
  5486. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5487. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5488. BEGIN
  5489. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5490. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5491. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5492. profileInit.Emit(Push(position,reg));
  5493. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5494. profileInit.Emit(Push(position,reg));
  5495. NEW(string, LEN(name)); COPY(name, string^);
  5496. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5497. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5498. sv.SetType(type);
  5499. Designate(sv,result);
  5500. profileInit.Emit(Push(position,result.tag));
  5501. profileInit.Emit(Push(position,result.op));
  5502. StaticCallOperand(result,procedure);
  5503. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5504. ReleaseOperand(result);
  5505. END;
  5506. END ProfilerAddProcedure;
  5507. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5508. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5509. BEGIN
  5510. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5511. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5512. profileInit.Emit(Push(position,reg));
  5513. profileInitPatchPosition := profileInit.pc;
  5514. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5515. NEW(string, LEN(name)); COPY(name, string^);
  5516. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5517. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5518. sv.SetType(type);
  5519. Designate(sv,result);
  5520. profileInit.Emit(Push(position,result.tag));
  5521. profileInit.Emit(Push(position,result.op));
  5522. StaticCallOperand(result,procedure);
  5523. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5524. ReleaseOperand(result);
  5525. END;
  5526. END ProfilerAddModule;
  5527. PROCEDURE ProfilerPatchInit;
  5528. VAR reg: IntermediateCode.Operand;
  5529. BEGIN
  5530. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5531. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5532. EmitLeave(profileInit,position,NIL,0);
  5533. profileInit.Emit(Exit(position,0,0,0));
  5534. END ProfilerPatchInit;
  5535. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5536. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5537. VAR
  5538. id: LONGINT;
  5539. leftType, rightType: SyntaxTree.Type;
  5540. procedureType: SyntaxTree.ProcedureType;
  5541. runtimeProcedure: SyntaxTree.Procedure;
  5542. runtimeProcedureOperand, operatorOperand: Operand;
  5543. kind: SET;
  5544. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5545. VAR
  5546. arg: IntermediateCode.Operand;
  5547. recordType: SyntaxTree.RecordType;
  5548. fingerPrint: SyntaxTree.FingerPrint;
  5549. BEGIN
  5550. IF type = NIL THEN
  5551. (* no type: push 'NoType' *)
  5552. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5553. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5554. ELSIF IsStrictlyPointerToRecord(type) THEN
  5555. (* pointer to record type: push typetag *)
  5556. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5557. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5558. arg := TypeDescriptorAdr(recordType);
  5559. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5560. ELSE
  5561. (* non-pointer to record type: push fingerprint *)
  5562. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5563. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5564. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5565. END;
  5566. operatorInitializationCodeSection.Emit(Push(position,arg))
  5567. END PushTypeInfo;
  5568. BEGIN
  5569. ASSERT(operatorInitializationCodeSection # NIL);
  5570. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5571. procedureType := operator.type(SyntaxTree.ProcedureType);
  5572. (* determine types *)
  5573. leftType := procedureType.firstParameter.type;
  5574. IF procedureType.numberParameters = 2 THEN
  5575. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5576. rightType := procedureType.firstParameter.nextParameter.type;
  5577. ELSE
  5578. rightType := NIL
  5579. END;
  5580. (* determine operator kind *)
  5581. IF IsStrictlyPointerToRecord(leftType) THEN
  5582. kind := {LhsIsPointer}
  5583. ELSE
  5584. kind := {}
  5585. END;
  5586. IF IsStrictlyPointerToRecord(rightType) THEN
  5587. kind := kind + {RhsIsPointer}
  5588. END;
  5589. IF kind # {} THEN (* TODO: to be removed later on *)
  5590. (* at least one of the types is a pointer to record *)
  5591. (* emit a code that registers this specific operator in the runtime *)
  5592. dump := operatorInitializationCodeSection.comments;
  5593. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5594. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5595. (* push ID *)
  5596. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5597. id := Global.GetSymbol(module.module.case, operator.name);
  5598. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5599. (* push kind *)
  5600. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5601. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5602. (* push type infos *)
  5603. PushTypeInfo(leftType);
  5604. PushTypeInfo(rightType);
  5605. (* push operator address *)
  5606. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5607. StaticCallOperand(operatorOperand, operator);
  5608. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5609. ReleaseOperand(operatorOperand);
  5610. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5611. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5612. ReleaseOperand(runtimeProcedureOperand)
  5613. END
  5614. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5615. END
  5616. END RegisterDynamicOperator;
  5617. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5618. VAR
  5619. traceModule: SyntaxTree.Module;
  5620. procedure: SyntaxTree.Procedure;
  5621. procedureVariable: SyntaxTree.Variable;
  5622. s,msg: Basic.MessageString;
  5623. res: Operand;
  5624. i: LONGINT;
  5625. sv: SyntaxTree.StringValue;
  5626. type: SyntaxTree.Type;
  5627. recordType: SyntaxTree.RecordType;
  5628. printout: Printout.Printer;
  5629. stringWriter: Streams.StringWriter;
  5630. expression: SyntaxTree.Expression;
  5631. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5632. BEGIN
  5633. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5634. IF procedure = NIL THEN
  5635. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5636. END;
  5637. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5638. s := "procedure ";
  5639. Strings.Append(s,backend.traceModuleName);
  5640. Strings.Append(s,".");
  5641. Strings.Append(s,procedureName);
  5642. Strings.Append(s," not present");
  5643. Error(position,s);
  5644. RETURN FALSE
  5645. ELSE
  5646. RETURN TRUE
  5647. END;
  5648. END GetProcedure;
  5649. PROCEDURE CallProcedure;
  5650. VAR size: LONGINT;
  5651. BEGIN
  5652. IF procedure # NIL THEN
  5653. StaticCallOperand(result,procedure);
  5654. size := ProcedureParametersSize(system,procedure);
  5655. ELSE
  5656. Symbol(procedureVariable, result);
  5657. LoadValue(result, procedureVariable.type.resolved);
  5658. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5659. END;
  5660. Emit(Call(position,result.op,size));
  5661. END CallProcedure;
  5662. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5663. VAR res: Operand; string: SyntaxTree.String;
  5664. BEGIN
  5665. IF GetProcedure("String") THEN
  5666. NEW(string, LEN(s)); COPY(s, string^);
  5667. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5668. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5669. sv.SetType(type);
  5670. Designate(sv,res);
  5671. Emit(Push(position,res.tag));
  5672. Emit(Push(position,res.op));
  5673. ReleaseOperand(res);
  5674. CallProcedure;
  5675. END;
  5676. END String;
  5677. PROCEDURE Integer(op: IntermediateCode.Operand);
  5678. BEGIN
  5679. IF GetProcedure("Int") THEN
  5680. Emit(Push(position,op));
  5681. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5682. CallProcedure;
  5683. END;
  5684. END Integer;
  5685. PROCEDURE Float(op: IntermediateCode.Operand);
  5686. BEGIN
  5687. IF GetProcedure("HIntHex") THEN
  5688. Emit(Push(position,op));
  5689. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5690. CallProcedure;
  5691. END;
  5692. END Float;
  5693. PROCEDURE Set(op: IntermediateCode.Operand);
  5694. BEGIN
  5695. IF GetProcedure("Set") THEN
  5696. Emit(Push(position,op));
  5697. (*
  5698. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5699. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5700. *)
  5701. CallProcedure;
  5702. END;
  5703. END Set;
  5704. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5705. BEGIN
  5706. IF GetProcedure("Boolean") THEN
  5707. Emit(Push(position,op));
  5708. CallProcedure;
  5709. END;
  5710. END Boolean;
  5711. PROCEDURE Char(op: IntermediateCode.Operand);
  5712. BEGIN
  5713. IF GetProcedure("Char") THEN
  5714. Emit(Push(position,op));
  5715. CallProcedure;
  5716. END;
  5717. END Char;
  5718. PROCEDURE Address(op: IntermediateCode.Operand);
  5719. BEGIN
  5720. IF GetProcedure("Address") THEN
  5721. Emit(Push(position,op));
  5722. CallProcedure;
  5723. END;
  5724. END Address;
  5725. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5726. VAR len: IntermediateCode.Operand;
  5727. BEGIN
  5728. IF GetProcedure("String") THEN
  5729. len := GetArrayLength(type, op.tag);
  5730. Emit(Push(position,len));
  5731. ReleaseIntermediateOperand(len);
  5732. Emit(Push(position,op.op));
  5733. CallProcedure;
  5734. END;
  5735. END StringOperand;
  5736. PROCEDURE Ln;
  5737. BEGIN
  5738. IF GetProcedure("Ln") THEN
  5739. CallProcedure;
  5740. END;
  5741. END Ln;
  5742. BEGIN
  5743. IF backend.traceModuleName = "" THEN RETURN END;
  5744. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5745. IF GetProcedure("Enter") THEN
  5746. CallProcedure
  5747. END;
  5748. NEW(stringWriter,LEN(s));
  5749. FOR i := 0 TO x.Length()-1 DO
  5750. msg := "";
  5751. expression := x.GetExpression(i);
  5752. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5753. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5754. ELSE
  5755. Global.GetModuleName(module.module, s);
  5756. END;
  5757. IF i = 0 THEN
  5758. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5759. stringWriter.String(":");
  5760. END;
  5761. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5762. IF ~(expression IS SyntaxTree.StringValue) THEN
  5763. printout.Expression(expression);
  5764. stringWriter.Get(s);
  5765. Strings.Append(msg,s);
  5766. Strings.Append(msg,"= ");
  5767. ELSE stringWriter.Get(s); (* remove from string writer *)
  5768. Strings.Append(msg, s);
  5769. END;
  5770. String(msg);
  5771. IF SemanticChecker.IsStringType(expression.type) THEN
  5772. Designate(expression,res);
  5773. StringOperand(res, expression.type);
  5774. ELSE
  5775. Evaluate(expression,res);
  5776. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5777. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5778. Convert(res.op,int64);
  5779. END;
  5780. Integer(res.op);
  5781. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5782. Boolean(res.op);
  5783. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5784. Set(res.op);
  5785. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5786. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5787. Convert(res.op,float64);
  5788. END;
  5789. Float(res.op);
  5790. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5791. Char(res.op);
  5792. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5793. Address(res.op);String("H");
  5794. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5795. Address(res.op);String("H");
  5796. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5797. Address(res.op);String("H");
  5798. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5799. Address(res.op);String("H");
  5800. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5801. String("NIL");
  5802. ELSE HALT(200);
  5803. END;
  5804. END;
  5805. ReleaseOperand(res);
  5806. String("; ");
  5807. END;
  5808. IF GetProcedure("Exit") THEN
  5809. CallProcedure
  5810. ELSE
  5811. Ln;
  5812. END;
  5813. END;
  5814. END SystemTrace;
  5815. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5816. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5817. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5818. BEGIN
  5819. type := type.resolved;
  5820. IF type IS SyntaxTree.RecordType THEN
  5821. WITH type: SyntaxTree.RecordType DO
  5822. baseType := type.baseType;
  5823. IF baseType # NIL THEN
  5824. baseType := baseType.resolved;
  5825. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5826. InitFields(baseType,adr,offset);
  5827. END;
  5828. variable := type.recordScope.firstVariable;
  5829. WHILE variable # NIL DO
  5830. IF variable.initializer # NIL THEN
  5831. Evaluate(variable.initializer,initializerOp);
  5832. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5833. Emit(Mov(position,mem,initializerOp.op));
  5834. ReleaseOperand(initializerOp);
  5835. ReleaseIntermediateOperand(mem);
  5836. END;
  5837. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5838. variable := variable.nextVariable
  5839. END;
  5840. END;
  5841. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5842. WITH type: SyntaxTree.ArrayType DO
  5843. IF type.form = SyntaxTree.Static THEN
  5844. baseType := type.arrayBase;
  5845. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5846. FOR i := 0 TO type.staticLength-1 DO
  5847. InitFields(baseType,adr,offset+i*size);
  5848. END;
  5849. END;
  5850. END;
  5851. ELSIF type IS SyntaxTree.MathArrayType THEN
  5852. WITH type: SyntaxTree.MathArrayType DO
  5853. IF type.form = SyntaxTree.Open THEN
  5854. dim := DynamicDim(type);
  5855. imm := IntermediateCode.Immediate(addressType,dim);
  5856. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5857. baseType := SemanticChecker.ArrayBase(type,dim);
  5858. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5859. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5860. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5861. ReleaseIntermediateOperand(imm);
  5862. (* flags remain empty (=0) for open array *)
  5863. ELSIF type.form = SyntaxTree.Static THEN
  5864. baseType := type.arrayBase;
  5865. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5866. ASSERT(type.staticLength < 1024*1024*1024);
  5867. FOR i := 0 TO type.staticLength-1 DO
  5868. InitFields(baseType,adr,offset+i*size);
  5869. END;
  5870. END;
  5871. END;
  5872. END;
  5873. END InitFields;
  5874. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5875. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5876. BEGIN
  5877. type := variable.type.resolved;
  5878. IF (type IS SyntaxTree.MathArrayType) THEN
  5879. WITH type: SyntaxTree.MathArrayType DO
  5880. IF type.form = SyntaxTree.Open THEN
  5881. Symbol(variable,operand);
  5882. InitFields(type, operand.tag,0);
  5883. ELSIF type.form = SyntaxTree.Tensor THEN
  5884. Symbol(variable, operand);
  5885. MakeMemory(tmp,operand.op,addressType,0);
  5886. ReleaseOperand(operand);
  5887. Emit(Mov(position,tmp, nil ) );
  5888. ReleaseIntermediateOperand(tmp);
  5889. END;
  5890. END;
  5891. ELSE
  5892. Symbol(variable,operand);
  5893. IF variable.initializer # NIL THEN
  5894. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5895. reference.SetType(variable.type.resolved);
  5896. reference.SetAssignable(TRUE);
  5897. Assign(reference,variable.initializer);
  5898. END;
  5899. InitFields(type, operand.op,0);
  5900. ReleaseOperand(operand);
  5901. END;
  5902. END InitVariable;
  5903. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5904. VAR end: Label;
  5905. BEGIN
  5906. IF type.form = SyntaxTree.Tensor THEN
  5907. InitOperand(result,ModeValue);
  5908. ReuseCopy(result.op,base);
  5909. end := NewLabel();
  5910. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5911. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5912. SetLabel(end);
  5913. Convert(result.op,int32);
  5914. ELSE
  5915. InitOperand(result,ModeValue);
  5916. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5917. END
  5918. END MathArrayDim;
  5919. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5920. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5921. BEGIN
  5922. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5923. MakeMemory(mem,base,addressType,offset);
  5924. Emit(Mov(position,mem,value));
  5925. ReleaseIntermediateOperand(mem);
  5926. END PutMathArrayField;
  5927. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5928. VAR mem: IntermediateCode.Operand;
  5929. BEGIN
  5930. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5931. MakeMemory(mem,base,addressType,offset);
  5932. Emit(Mov(position,mem,value));
  5933. ReleaseIntermediateOperand(mem);
  5934. END PutMathArrayFieldOffset;
  5935. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5936. BEGIN
  5937. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5938. MakeMemory(value,base,addressType,offset);
  5939. END GetMathArrayField;
  5940. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5941. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5942. BEGIN
  5943. IF incr THEN
  5944. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5945. ELSE
  5946. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5947. END;
  5948. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5949. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5950. ELSE
  5951. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5952. Emit(Mov(position,reg,dim));
  5953. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5954. Emit(Add(position,reg,reg,base));
  5955. MakeMemory(mem, reg, addressType, offset);
  5956. ReleaseIntermediateOperand(reg);
  5957. Emit(Mov(position,mem,value));
  5958. ReleaseIntermediateOperand(mem);
  5959. END;
  5960. END PutMathArrayLenOrIncr;
  5961. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5962. BEGIN
  5963. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5964. END PutMathArrayLength;
  5965. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5966. BEGIN
  5967. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5968. END PutMathArrayIncrement;
  5969. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5970. BEGIN
  5971. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5972. END GetMathArrayIncrement;
  5973. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5974. BEGIN
  5975. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5976. END GetMathArrayLength;
  5977. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5978. VAR dimOp: IntermediateCode.Operand;
  5979. BEGIN
  5980. dimOp := IntermediateCode.Immediate(int32, dim);
  5981. GetMathArrayLength(type, operand, dimOp, check, result);
  5982. END GetMathArrayLengthAt;
  5983. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5984. VAR dimOp: IntermediateCode.Operand;
  5985. BEGIN
  5986. dimOp := IntermediateCode.Immediate(int32, dim);
  5987. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5988. END GetMathArrayIncrementAt;
  5989. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5990. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5991. offset: LONGINT;
  5992. BEGIN
  5993. IF increment THEN
  5994. offset := MathIncrOffset;
  5995. ELSE
  5996. offset := MathLenOffset;
  5997. END;
  5998. INC(offset,operand.dimOffset*2);
  5999. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  6000. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  6001. END;
  6002. (* static dimension *)
  6003. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6004. IF check & (type.form = SyntaxTree.Tensor) THEN
  6005. DimensionCheck(operand.tag,dim,BrltL);
  6006. END;
  6007. val := SHORT(dim.intValue);
  6008. IF type.form # SyntaxTree.Tensor THEN
  6009. t := SemanticChecker.ArrayBase(type,val);
  6010. type := t.resolved(SyntaxTree.MathArrayType);
  6011. IF type.form = SyntaxTree.Static THEN
  6012. IF increment THEN
  6013. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6014. ELSE
  6015. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6016. END;
  6017. InitOperand(result,ModeValue);
  6018. result.op := res;
  6019. RETURN;
  6020. END;
  6021. END;
  6022. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6023. MakeMemory(res,operand.tag,addressType,offset);
  6024. (*
  6025. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6026. *)
  6027. InitOperand(result,ModeValue);
  6028. result.op := res;
  6029. ELSE
  6030. Convert(dim,addressType);
  6031. IF check THEN
  6032. IF type.form = SyntaxTree.Tensor THEN
  6033. DimensionCheck(operand.tag,dim,BrltL);
  6034. ELSIF isUnchecked THEN (* do nothing *)
  6035. ELSE
  6036. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6037. END;
  6038. END;
  6039. end := NewLabel(); next := NIL;
  6040. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6041. Emit(Mov(position,res,dim));
  6042. Convert(res,int32);
  6043. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6044. WHILE t IS SyntaxTree.MathArrayType DO
  6045. type := t(SyntaxTree.MathArrayType);
  6046. IF type.form = SyntaxTree.Static THEN
  6047. imm := IntermediateCode.Immediate(int32,val);
  6048. next := NewLabel();
  6049. BrneL(next,imm,res);
  6050. IF increment THEN
  6051. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6052. ELSE
  6053. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6054. END;
  6055. Emit(MovReplace(position,res,imm));
  6056. BrL(end);
  6057. ELSE hasDynamicPart := TRUE;
  6058. END;
  6059. t := type.arrayBase.resolved;
  6060. val := val + 1;
  6061. IF next # NIL THEN SetLabel(next) END;
  6062. END;
  6063. IF hasDynamicPart THEN
  6064. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6065. Emit(Mov(position,res2,dim));
  6066. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6067. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6068. Emit(Add(position,res2,res2,imm));
  6069. Emit(Add(position,res2,res2,operand.tag));
  6070. IntermediateCode.MakeMemory(res2,int32);
  6071. Emit(MovReplace(position,res,res2));
  6072. ReleaseIntermediateOperand(res2);
  6073. END;
  6074. SetLabel(end);
  6075. Convert(res,int32);
  6076. InitOperand(result,ModeValue);
  6077. result.op := res;
  6078. END;
  6079. END MathArrayLenOrIncr;
  6080. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6081. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6082. offset: LONGINT;
  6083. BEGIN
  6084. offset := operand.dimOffset+DynamicDim(type)-1;
  6085. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6086. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6087. val := SHORT(dim.intValue);
  6088. t := SemanticChecker.ArrayBase(type,val);
  6089. type := t.resolved(SyntaxTree.ArrayType);
  6090. IF type.form = SyntaxTree.Static THEN
  6091. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6092. ELSE
  6093. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6094. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6095. END;
  6096. UseIntermediateOperand(res);
  6097. InitOperand(result,ModeValue);
  6098. result.op := res;
  6099. ELSE
  6100. Convert(dim,addressType);
  6101. IF ~isUnchecked THEN
  6102. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6103. END;
  6104. end := NewLabel(); next := NIL;
  6105. (* ReuseCopy(dim,res); *)
  6106. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6107. Emit(Mov(position,res,dim));
  6108. Convert(res,int32);
  6109. Convert(res,int32);
  6110. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6111. WHILE t IS SyntaxTree.ArrayType DO
  6112. type := t(SyntaxTree.ArrayType);
  6113. IF type.form = SyntaxTree.Static THEN
  6114. imm := IntermediateCode.Immediate(int32,val);
  6115. next := NewLabel();
  6116. BrneL(next,imm,res);
  6117. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6118. Emit(MovReplace(position,res,imm));
  6119. BrL(end);
  6120. ELSE hasDynamicPart := TRUE;
  6121. END;
  6122. t := type.arrayBase.resolved;
  6123. val := val + 1;
  6124. IF next # NIL THEN SetLabel(next) END;
  6125. END;
  6126. IF hasDynamicPart THEN
  6127. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6128. Convert(res2,addressType);
  6129. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6130. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6131. Emit(Sub(position,res2,imm,res2));
  6132. Emit(Add(position,res2,res2,operand.tag));
  6133. IntermediateCode.MakeMemory(res2,int32);
  6134. Emit(MovReplace(position,res,res2));
  6135. ReleaseIntermediateOperand(res2);
  6136. END;
  6137. SetLabel(end);
  6138. Convert(res,int32);
  6139. InitOperand(result,ModeValue);
  6140. result.op := res;
  6141. END;
  6142. END ArrayLen;
  6143. (**
  6144. create a temporary variable in current scope
  6145. **)
  6146. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6147. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6148. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6149. BEGIN
  6150. IF ~register THEN
  6151. variable := temporaries.GetFreeVariable(type, index);
  6152. ELSE
  6153. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6154. END;
  6155. scope := currentScope;
  6156. IF variable = NIL THEN
  6157. COPY("@hiddenIRVar",string);
  6158. Basic.AppendNumber(string,index);
  6159. name := SyntaxTree.NewIdentifier(string);
  6160. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6161. variable.SetType(type);
  6162. variable.SetAccess(SyntaxTree.Hidden);
  6163. IF ~register THEN
  6164. temporaries.AddVariable(variable);
  6165. IF scope.lastVariable # NIL THEN
  6166. offset := scope.lastVariable.offsetInBits;
  6167. ELSE
  6168. offset := 0;
  6169. END;
  6170. DEC(offset,system.SizeOf(variable.type));
  6171. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6172. variable(SyntaxTree.Variable).SetOffset(offset);
  6173. scope.AddVariable(variable(SyntaxTree.Variable));
  6174. scope.EnterSymbol(variable, duplicate);
  6175. ASSERT(~duplicate);
  6176. InitVariable(variable(SyntaxTree.Variable));
  6177. ELSE
  6178. variable.SetUseRegister(TRUE);
  6179. variable(SyntaxTree.Variable).SetOffset(0);
  6180. END;
  6181. ELSE
  6182. InitVariable(variable(SyntaxTree.Variable));
  6183. (*
  6184. ASSERT(variable.type.resolved = type.resolved)
  6185. *)
  6186. END;
  6187. RETURN variable(SyntaxTree.Variable)
  6188. END GetTemporaryVariable;
  6189. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6190. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6191. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6192. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6193. i: LONGINT; duplicate: BOOLEAN;
  6194. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6195. VAR variable: SyntaxTree.Variable;
  6196. BEGIN
  6197. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6198. variable.SetType(type);
  6199. recordScope.AddVariable(variable);
  6200. END AddVariable;
  6201. BEGIN
  6202. name := "@ArrayDescriptor";
  6203. Basic.AppendNumber(name,dimensions);
  6204. identifier := SyntaxTree.NewIdentifier(name);
  6205. parentScope := module.module.moduleScope;
  6206. symbol := parentScope.FindSymbol(identifier);
  6207. IF symbol # NIL THEN
  6208. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6209. type := typeDeclaration.declaredType;
  6210. ELSE
  6211. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6212. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6213. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6214. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6215. recordType.SetTypeDeclaration(typeDeclaration);
  6216. recordType.SetState(SyntaxTree.Resolved);
  6217. typeDeclaration.SetDeclaredType(recordType);
  6218. AddVariable("@ptr",system.anyType);
  6219. AddVariable("@adr",system.addressType);
  6220. AddVariable("@flags",system.addressType);
  6221. AddVariable("@dim",system.addressType);
  6222. AddVariable("@elementSize",system.addressType);
  6223. FOR i := 0 TO dimensions-1 DO
  6224. name := "@len";
  6225. Basic.AppendNumber(name,i);
  6226. AddVariable(name,system.addressType);
  6227. name := "@incr";
  6228. Basic.AppendNumber(name,i);
  6229. AddVariable(name,system.addressType);
  6230. END;
  6231. parentScope.AddTypeDeclaration(typeDeclaration);
  6232. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6233. ASSERT(~duplicate);
  6234. type := recordType;
  6235. END;
  6236. RETURN type
  6237. END GetMathArrayDescriptorType;
  6238. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6239. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6240. BEGIN
  6241. type := GetMathArrayDescriptorType(dimensions);
  6242. Emit(Push(position,op.op));
  6243. (* push type descriptor *)
  6244. reg := TypeDescriptorAdr(type);
  6245. IF ~newObjectFile THEN
  6246. IntermediateCode.MakeMemory(reg,addressType);
  6247. END;
  6248. Emit(Push(position,reg));
  6249. ReleaseIntermediateOperand(reg);
  6250. (* push realtime flag: false by default *)
  6251. Emit(Push(position,false));
  6252. CallThis(position,"Heaps","NewRec",3);
  6253. END NewMathArrayDescriptor;
  6254. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6255. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6256. BEGIN
  6257. NEW(string, LEN(s)); COPY(s, string^);
  6258. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6259. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6260. sv.SetType(type);
  6261. Designate(sv,res);
  6262. Emit(Push(position,res.tag));
  6263. Emit(Push(position,res.op));
  6264. ReleaseOperand(res);
  6265. END PushConstString;
  6266. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6267. BEGIN
  6268. IF b THEN
  6269. Emit(Push(Basic.invalidPosition, true));
  6270. ELSE
  6271. Emit(Push(Basic.invalidPosition, false));
  6272. END;
  6273. END PushConstBoolean;
  6274. PROCEDURE PushConstSet(v: SET);
  6275. VAR value: SyntaxTree.Value; op: Operand;
  6276. BEGIN
  6277. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6278. value.SetType(system.setType);
  6279. Evaluate(value, op);
  6280. Emit(Push(Basic.invalidPosition, op.op));
  6281. ReleaseOperand(op);
  6282. END PushConstSet;
  6283. PROCEDURE PushConstInteger(v: LONGINT);
  6284. VAR value: SyntaxTree.Value; op: Operand;
  6285. BEGIN
  6286. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6287. value.SetType(system.longintType);
  6288. Evaluate(value, op);
  6289. Emit(Push(Basic.invalidPosition, op.op));
  6290. ReleaseOperand(op);
  6291. END PushConstInteger;
  6292. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6293. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6294. section: IntermediateCode.Section;
  6295. BEGIN
  6296. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6297. Global.GetSymbolSegmentedName(symbol, name);
  6298. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6299. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6300. procedure.SetScope(moduleScope);
  6301. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6302. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6303. procedure.SetAccess(SyntaxTree.Hidden);
  6304. currentScope := procedureScope;
  6305. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6306. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6307. RETURN section;
  6308. END OpenInitializer;
  6309. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6310. BEGIN
  6311. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6312. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6313. section := prev;
  6314. END CloseInitializer;
  6315. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6316. VAR name: SyntaxTree.IdentifierString;
  6317. variable: SyntaxTree.Variable;
  6318. parameter: SyntaxTree.Parameter;
  6319. type: SyntaxTree.Type;
  6320. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6321. BEGIN
  6322. InitOperand(op,ModeReference);
  6323. op.op := fp;
  6324. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6325. Dereference(op, x, FALSE);
  6326. result := op;
  6327. Symbol(symbol, op);
  6328. END Field;
  6329. PROCEDURE Direction(direction: LONGINT): SET;
  6330. BEGIN
  6331. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6332. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6333. ELSE HALT(100);
  6334. END;
  6335. END Direction;
  6336. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6337. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6338. BEGIN
  6339. Field(port, op);
  6340. ToMemory(op.op,addressType,0);
  6341. Emit(Push(Basic.invalidPosition, op.op));
  6342. ReleaseOperand(op);
  6343. Basic.GetString(modifier.identifier, name);
  6344. PushConstString(name);
  6345. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6346. ASSERT(SemanticChecker.IsStringType(value.type));
  6347. Designate(value, op);
  6348. Emit(Push(modifier.position, op.tag));
  6349. Emit(Push(modifier.position, op.op));
  6350. ReleaseOperand(op);
  6351. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6352. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6353. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6354. Evaluate(value, op);
  6355. Emit(Push(modifier.position, op.op));
  6356. ReleaseOperand(op);
  6357. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6358. ELSE
  6359. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6360. END;
  6361. END AddPortProperty;
  6362. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6363. VAR modifier: SyntaxTree.Modifier;
  6364. BEGIN
  6365. modifier := parameter.modifiers;
  6366. WHILE modifier # NIL DO
  6367. AddPortProperty(parameter,modifier, modifier.expression);
  6368. modifier := modifier.nextModifier;
  6369. END;
  6370. END AddPortProperties;
  6371. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6372. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6373. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6374. PROCEDURE PushLens(type: SyntaxTree.Type);
  6375. BEGIN
  6376. IF IsSemiDynamicArray(type) THEN
  6377. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6378. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6379. Emit(Push(Basic.invalidPosition, op.op));
  6380. ReleaseOperand(op);
  6381. INC(dim);
  6382. ELSIF IsStaticArray(type) THEN
  6383. len := len * type(SyntaxTree.ArrayType).staticLength;
  6384. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6385. INC(dim);
  6386. ELSE
  6387. baseType := type;
  6388. END;
  6389. END PushLens;
  6390. BEGIN
  6391. (* cell *)
  6392. IF parameter.type IS SyntaxTree.ArrayType THEN
  6393. type := parameter.type;
  6394. dim := 0;
  6395. len := 1;
  6396. PushLens(type);
  6397. portType := baseType.resolved(SyntaxTree.PortType);
  6398. ELSE
  6399. portType := parameter.type(SyntaxTree.PortType);
  6400. END;
  6401. PushSelfPointer();
  6402. (* port / array of ports *)
  6403. IF IsStaticArray(type) THEN
  6404. PushConstInteger(len);
  6405. END;
  6406. Field(parameter, op);
  6407. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6408. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6409. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6410. Designate(d, op);*)
  6411. Emit(Push(Basic.invalidPosition, op.op));
  6412. ReleaseOperand(op);
  6413. (* name *)
  6414. PushConstString(name);
  6415. (* inout *)
  6416. PushConstSet(Direction(portType.direction));
  6417. (* width *)
  6418. PushConstInteger(portType.sizeInBits);
  6419. IF parameter.type IS SyntaxTree.PortType THEN
  6420. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6421. AddPortProperties(parameter);
  6422. ELSIF IsStaticArray(type)THEN
  6423. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6424. ELSIF IsSemiDynamicArray(type) THEN
  6425. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6426. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6427. Emit(Add(position,reg, sp, size));
  6428. (* dim *)
  6429. PushConstInteger(dim);
  6430. (* len array *)
  6431. Emit(Push(position, reg));
  6432. ReleaseIntermediateOperand(reg);
  6433. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6434. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6435. Emit(Add(position, sp,sp, size));
  6436. ELSE
  6437. HALT(100);
  6438. END;
  6439. END Parameter;
  6440. BEGIN
  6441. IF backend.cellsAreObjects THEN
  6442. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6443. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6444. END;
  6445. parameter := x.firstParameter;
  6446. WHILE (parameter # NIL) DO
  6447. type := parameter.type.resolved;
  6448. WHILE (type IS SyntaxTree.ArrayType) DO
  6449. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6450. END;
  6451. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6452. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6453. Parameter(name,parameter);
  6454. END;
  6455. parameter := parameter.nextParameter;
  6456. END;
  6457. ELSE HALT(200)
  6458. END;
  6459. END AddPorts;
  6460. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6461. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6462. BEGIN
  6463. Symbol(cell,op);
  6464. ToMemory(op.op,addressType,0);
  6465. Emit(Push(position,op.op));
  6466. ReleaseOperand(op);
  6467. property.GetName(name);
  6468. (* does not work when inheritance is used:
  6469. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6470. *)
  6471. PushConstString(name);
  6472. IF (value # NIL) THEN
  6473. ASSERT(
  6474. SemanticChecker.IsStringType(property.type)
  6475. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6476. OR (property.type.resolved IS SyntaxTree.FloatType)
  6477. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6478. OR (property.type.resolved IS SyntaxTree.SetType)
  6479. );
  6480. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6481. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6482. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6483. Designate(d, op);
  6484. IF SemanticChecker.IsStringType(property.type) THEN
  6485. Emit(Push(Basic.invalidPosition, op.tag))
  6486. END;
  6487. Emit(Push(Basic.invalidPosition, op.op));
  6488. ReleaseOperand(op);
  6489. END;
  6490. IF value = NIL THEN
  6491. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6492. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6493. ASSERT(SemanticChecker.IsStringType(value.type));
  6494. Designate(value, op);
  6495. PushString(op, value.type);
  6496. ReleaseOperand(op);
  6497. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6498. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6499. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6500. Evaluate(value, op);
  6501. Emit(Push(property.position, op.op));
  6502. ReleaseOperand(op);
  6503. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6504. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6505. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6506. Evaluate(value, op);
  6507. Emit(Push(property.position, op.op));
  6508. ReleaseOperand(op);
  6509. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6510. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6511. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6512. Evaluate(value, op);
  6513. Emit(Push(property.position, op.op));
  6514. ReleaseOperand(op);
  6515. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6516. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6517. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6518. Evaluate(value, op);
  6519. Emit(Push(property.position, op.op));
  6520. ReleaseOperand(op);
  6521. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6522. ELSE
  6523. HALT(200);
  6524. END;
  6525. END AddProperty;
  6526. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6527. VAR symbol: SyntaxTree.Symbol;
  6528. BEGIN
  6529. WHILE modifier # NIL DO
  6530. symbol := cellType.FindProperty(modifier.identifier);
  6531. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6532. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6533. modifier := modifier.nextModifier;
  6534. END;
  6535. END AddModifiers;
  6536. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6537. VAR last: SyntaxTree.Modifier;
  6538. BEGIN
  6539. IF to = NIL THEN
  6540. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6541. ELSE
  6542. last := to;
  6543. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6544. last := last.nextModifier;
  6545. END;
  6546. IF last.identifier # this.identifier THEN
  6547. ASSERT(last.nextModifier = NIL);
  6548. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6549. END;
  6550. END;
  6551. END AppendModifier;
  6552. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6553. BEGIN
  6554. WHILE this # NIL DO
  6555. AppendModifier(to, this);
  6556. this := this.nextModifier;
  6557. END;
  6558. END AppendModifiers;
  6559. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6560. VAR base: SyntaxTree.Type;
  6561. BEGIN
  6562. AppendModifiers(to, c.modifiers);
  6563. base := c.GetBaseValueType();
  6564. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6565. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6566. END;
  6567. END AppendCellTypeModifiers;
  6568. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6569. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6570. BEGIN
  6571. Basic.GetString(modifier.identifier, name);
  6572. PushConstString(name);
  6573. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6574. ASSERT(SemanticChecker.IsStringType(value.type));
  6575. Designate(value, op);
  6576. PushString(op, value.type);
  6577. ReleaseOperand(op);
  6578. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6579. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6580. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6581. Evaluate(value, op);
  6582. Emit(Push(modifier.position, op.op));
  6583. ReleaseOperand(op);
  6584. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6585. ELSE
  6586. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6587. END;
  6588. END AddPortProperty;
  6589. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6590. BEGIN
  6591. WHILE modifier # NIL DO
  6592. AddPortProperty(modifier, modifier.expression);
  6593. modifier := modifier.nextModifier;
  6594. END;
  6595. END AddPortProperties;
  6596. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6597. VAR op: Operand;
  6598. BEGIN
  6599. Evaluate(p, op);
  6600. Emit(Push(p.position, op.op));
  6601. ReleaseOperand(op);
  6602. IF p IS SyntaxTree.Designator THEN
  6603. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6604. END;
  6605. END PushPort;
  6606. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6607. VAR tmp: IntermediateCode.Operand;
  6608. BEGIN
  6609. actualType := actualType.resolved;
  6610. IF actualType IS SyntaxTree.StringType THEN
  6611. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6612. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6613. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6614. ELSE
  6615. tmp := op.tag;
  6616. IntermediateCode.MakeMemory(tmp,addressType);
  6617. Emit(Push(position,tmp));
  6618. END;
  6619. Emit(Push(position,op.op))
  6620. END PushString;
  6621. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6622. VAR
  6623. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6624. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6625. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6626. tmp:IntermediateCode.Operand;
  6627. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6628. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6629. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6630. dest: IntermediateCode.Operand;
  6631. staticLength: LONGINT; itype: IntermediateCode.Type;
  6632. convert,isTensor: BOOLEAN;
  6633. recordType: SyntaxTree.RecordType;
  6634. baseType: SyntaxTree.Type;
  6635. flags: SET;
  6636. left: SyntaxTree.Expression;
  6637. call: SyntaxTree.Designator;
  6638. procedure: SyntaxTree.Procedure;
  6639. temporaryVariable: SyntaxTree.Variable;
  6640. dummy: IntermediateCode.Operand;
  6641. customBuiltin: SyntaxTree.CustomBuiltin;
  6642. isVarPar: ARRAY 3 OF BOOLEAN;
  6643. callsection: Sections.Section;
  6644. segmentedName: Basic.SegmentedName;
  6645. needsTrace: BOOLEAN;
  6646. n: ARRAY 256 OF CHAR;
  6647. modifier: SyntaxTree.Modifier;
  6648. previous, init: IntermediateCode.Section;
  6649. prevScope: SyntaxTree.Scope;
  6650. firstPar: LONGINT;
  6651. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6652. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6653. priority: IntermediateCode.Operand;
  6654. op,callop: Operand;
  6655. BEGIN
  6656. IF type = NIL THEN RETURN END;
  6657. type := type.resolved;
  6658. IF type IS SyntaxTree.PointerType THEN
  6659. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6660. END;
  6661. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6662. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6663. recordScope := type(SyntaxTree.RecordType).recordScope;
  6664. IF recordScope.bodyProcedure # NIL THEN
  6665. procedure := recordScope.bodyProcedure;
  6666. body := procedure.procedureScope.body;
  6667. Emit(Push(position,self));
  6668. IF body.isActive THEN
  6669. StaticCallOperand(callop,procedure);
  6670. Emit(Push(position,callop.op));
  6671. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6672. Convert(priority,sizeType);
  6673. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6674. END;
  6675. Emit(Push(position,priority));
  6676. ReleaseIntermediateOperand(priority);
  6677. IF backend.cooperative THEN
  6678. Emit(Push(position,self));
  6679. CallThis(position,"Activities","Create",3)
  6680. ELSE
  6681. flags := 0;
  6682. IF body.isSafe THEN
  6683. flags := 1;
  6684. END;
  6685. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6686. Emit(Push(position,self));
  6687. CallThis(position,"Objects","CreateProcess",4)
  6688. END;
  6689. ELSE
  6690. Emit(Push(position,self));
  6691. StaticCallOperand(callop,procedure);
  6692. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6693. END;
  6694. Emit(Pop(position,self));
  6695. END;
  6696. END CallBodies;
  6697. PROCEDURE PushTD(type: SyntaxTree.Type);
  6698. VAR op: IntermediateCode.Operand;
  6699. BEGIN
  6700. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6701. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6702. ELSE
  6703. IF type.resolved IS SyntaxTree.PointerType THEN
  6704. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6705. END;
  6706. op := TypeDescriptorAdr(type.resolved);
  6707. IF ~newObjectFile THEN
  6708. IntermediateCode.MakeMemory(op,addressType);
  6709. END;
  6710. Emit(Push(position,op));
  6711. END
  6712. END PushTD;
  6713. BEGIN
  6714. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6715. dest := destination; destination := emptyOperand;
  6716. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6717. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6718. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6719. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6720. CASE x.id OF
  6721. (* ---- COPY ----- *)
  6722. |Global.Copy:
  6723. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6724. (* ---- EXCL, INCL----- *)
  6725. |Global.Excl,Global.Incl:
  6726. Evaluate(p0,s0);
  6727. Evaluate(p1,s1);
  6728. Convert(s1.op,setType);
  6729. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6730. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6731. END;
  6732. ReuseCopy(res,s0.op);
  6733. ReleaseOperand(s0);
  6734. Reuse1(tmp,s1.op);
  6735. ReleaseOperand(s1);
  6736. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6737. IF x.id = Global.Excl THEN
  6738. Emit(Not(position,tmp,tmp));
  6739. Emit(And(position,res,res,tmp));
  6740. ELSE
  6741. Emit(Or(position,res,res,tmp));
  6742. END;
  6743. ReleaseIntermediateOperand(tmp);
  6744. Designate(p0,s0);
  6745. ToMemory(s0.op,setType,0);
  6746. Emit(Mov(position,s0.op,res));
  6747. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6748. (* ---- DISPOSE ----- *)
  6749. |Global.Dispose:
  6750. Designate(p0,s0);
  6751. Emit(Push(position,s0.op));
  6752. ReleaseOperand(s0);
  6753. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6754. (* ---- GETPROCEDURE ----- *)
  6755. |Global.GetProcedure:
  6756. Designate(p0,s0);
  6757. PushString(s0,p0.type);
  6758. Designate(p1,s1);
  6759. PushString(s1,p1.type);
  6760. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6761. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6762. ELSE PushTD(procedureType.firstParameter.type)
  6763. END;
  6764. PushTD(procedureType.returnType);
  6765. Designate(p2,s2);
  6766. Emit(Push(position,s2.op));
  6767. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6768. CallThis(position,"Modules","GetProcedure", 7);
  6769. (* ---- ASH, LSH, ROT ----- *)
  6770. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6771. Evaluate(p0,s0);
  6772. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6773. (* make unsigned arguments in order to produced a logical shift *)
  6774. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6775. convert:= TRUE;
  6776. itype := s0.op.type;
  6777. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6778. Convert(s0.op,itype);
  6779. ELSE
  6780. convert := FALSE;
  6781. END;
  6782. END;
  6783. Evaluate(p1,s1);
  6784. IF IsIntegerConstant(p1,hint) THEN
  6785. ReuseCopy(reg,s0.op);
  6786. IF hint > 0 THEN
  6787. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6788. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6789. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6790. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6791. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6792. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6793. END;
  6794. ELSIF hint < 0 THEN
  6795. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6796. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6797. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6798. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6799. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6800. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6801. END;
  6802. END;
  6803. ReleaseOperand(s0); ReleaseOperand(s1);
  6804. ELSE
  6805. exit := NewLabel();
  6806. end := NewLabel();
  6807. ReuseCopy(reg,s0.op);
  6808. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6809. Reuse1(tmp,s1.op);
  6810. Emit(Neg(position,tmp,s1.op));
  6811. Convert(tmp,s1.op.type);
  6812. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6813. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6814. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6815. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6816. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6817. END;
  6818. ReleaseIntermediateOperand(tmp);
  6819. BrL(end);
  6820. SetLabel(exit);
  6821. ReuseCopy(tmp,s1.op);
  6822. Convert(tmp,s1.op.type);
  6823. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6824. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6825. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6826. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6827. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6828. END;
  6829. ReleaseIntermediateOperand(tmp);
  6830. SetLabel(end);
  6831. ReleaseOperand(s0); ReleaseOperand(s1);
  6832. END;
  6833. InitOperand(result,ModeValue);
  6834. IF convert THEN
  6835. itype := reg.type;
  6836. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6837. Convert(reg,itype);
  6838. END;
  6839. result.op := reg;
  6840. (* ---- CAP ----- *)
  6841. |Global.Cap:
  6842. Evaluate(p0,result);
  6843. ReuseCopy(reg,result.op);
  6844. ReleaseIntermediateOperand(result.op);
  6845. ignore := NewLabel();
  6846. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6847. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6848. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6849. SetLabel(ignore);
  6850. result.op := reg;
  6851. (* ---- CHR ----- *)
  6852. |Global.Chr, Global.Chr32:
  6853. Evaluate(p0,result);
  6854. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6855. |Global.Entier, Global.EntierH:
  6856. Evaluate(p0,result);
  6857. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6858. (* ---- MIN and MAX ----- *)
  6859. |Global.Max,Global.Min:
  6860. Evaluate(p0,s0);
  6861. Evaluate(p1,s1);
  6862. Reuse2(res,s0.op,s1.op);
  6863. else := NewLabel();
  6864. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6865. ELSE BrltL(else,s1.op,s0.op) END;
  6866. Emit(Mov(position,res,s0.op));
  6867. ReleaseOperand(s0);
  6868. end := NewLabel();
  6869. BrL(end);
  6870. SetLabel(else);
  6871. Emit(MovReplace(position,res,s1.op));
  6872. SetLabel(end);
  6873. ReleaseOperand(s1);
  6874. InitOperand(result,ModeValue);
  6875. result.op := res;
  6876. (* ---- ODD ----- *)
  6877. |Global.Odd:
  6878. IF ~conditional THEN
  6879. ConditionToValue(x)
  6880. ELSE
  6881. Evaluate(p0,result);
  6882. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6883. Reuse1(res,result.op);
  6884. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6885. ReleaseIntermediateOperand(result.op);
  6886. result.op := res;
  6887. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6888. ReleaseOperand(result);
  6889. BrL(falseLabel);
  6890. END;
  6891. (* ---- ORD ----- *)
  6892. |Global.Ord, Global.Ord32:
  6893. Evaluate(p0,result);
  6894. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6895. (* ---- SHORT, LONG ----- *)
  6896. |Global.Short, Global.Long:
  6897. Evaluate(p0,result);
  6898. IF x.type IS SyntaxTree.ComplexType THEN
  6899. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6900. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6901. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6902. ELSE
  6903. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6904. END
  6905. (* ---- HALT, SYSTEM.HALT----- *)
  6906. |Global.Halt, Global.systemHalt:
  6907. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6908. EmitTrap (position, val);
  6909. (* ---- ASSERT ----- *)
  6910. |Global.Assert:
  6911. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6912. trueL := NewLabel();
  6913. falseL := NewLabel();
  6914. Condition(p0,trueL,falseL);
  6915. IF p1 = NIL THEN val := AssertTrap
  6916. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6917. END;
  6918. SetLabel(falseL);
  6919. EmitTrap(position,val);
  6920. SetLabel(trueL);
  6921. END;
  6922. (*
  6923. Emit(TrapC(result.op,val);
  6924. *)
  6925. (* ---- INC, DEC----- *)
  6926. |Global.Inc,Global.Dec:
  6927. Expression(p0); adr := result.op;
  6928. LoadValue(result,p0.type); l := result;
  6929. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6930. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6931. END;
  6932. IF x.id = Global.Inc THEN
  6933. Emit(Add(position,l.op,l.op,r.op));
  6934. ELSE
  6935. Emit(Sub(position,l.op,l.op,r.op));
  6936. END;
  6937. ReleaseOperand(l); ReleaseOperand(r);
  6938. (* ---- LEN ----- *)
  6939. |Global.Len: (* dynamic length, static length done by checker *)
  6940. Designate(p0,operand);
  6941. IF p1 = NIL THEN
  6942. InitOperand(l,ModeValue);
  6943. l.op := IntermediateCode.Immediate(int32,0);
  6944. ELSE
  6945. Evaluate(p1,l);
  6946. END;
  6947. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6948. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6949. Dereference(operand, p0.type.resolved, FALSE);
  6950. END;
  6951. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6952. ReleaseOperand(operand); ReleaseOperand(l);
  6953. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6954. ASSERT(p1 # NIL);
  6955. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6956. Dereference(operand,p0.type.resolved,FALSE);
  6957. END;
  6958. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6959. ReleaseOperand(operand); ReleaseOperand(l);
  6960. ELSE HALT(100);
  6961. END;
  6962. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6963. (* ---- FIRST ---- *)
  6964. |Global.First:
  6965. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6966. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6967. ELSE
  6968. Designate(p0, result)
  6969. END
  6970. (* ---- LAST ---- *)
  6971. |Global.Last:
  6972. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6973. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6974. ELSE
  6975. Designate(p0, result);
  6976. (* make sure result.op is a register *)
  6977. tmp := result.op;
  6978. ReuseCopy(result.op, result.op);
  6979. ReleaseIntermediateOperand(tmp);
  6980. (* add offset to result.op *)
  6981. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6982. END
  6983. (* ---- STEP ---- *)
  6984. |Global.Step:
  6985. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6986. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6987. ELSE
  6988. Designate(p0, result);
  6989. (* make sure result.op is a register *)
  6990. tmp := result.op;
  6991. ReuseCopy(result.op, result.op);
  6992. ReleaseIntermediateOperand(tmp);
  6993. (* add offset to result.op *)
  6994. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6995. END
  6996. (* ---- RE ---- *)
  6997. |Global.Re:
  6998. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6999. Designate(p0, result)
  7000. ELSE
  7001. Evaluate(p0, result)
  7002. END
  7003. (* ---- IM ---- *)
  7004. |Global.Im:
  7005. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  7006. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  7007. Designate(p0, result);
  7008. (* make sure result.op is a register *)
  7009. tmp := result.op;
  7010. ReuseCopy(result.op, result.op);
  7011. ReleaseIntermediateOperand(tmp);
  7012. (* add offset to result.op *)
  7013. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7014. (* ---- ABS ----- *)
  7015. |Global.Abs:
  7016. Evaluate(p0,operand);
  7017. type := p0.type.resolved;
  7018. InitOperand(result,ModeValue);
  7019. Reuse1a(result.op,operand.op,dest);
  7020. Emit(Abs(position,result.op,operand.op));
  7021. ReleaseOperand(operand);
  7022. (* ---- WAIT ----- *)
  7023. |Global.Wait:
  7024. Evaluate(p0,operand);
  7025. Emit(Push(position,operand.op));
  7026. ReleaseOperand(operand);
  7027. CallThis(position,"Activities","Wait", 1);
  7028. (* ---- NEW ----- *)
  7029. |Global.New:
  7030. (*! the following code is only correct for "standard" Oberon calling convention *)
  7031. IF x.type # NIL THEN
  7032. type := x.type.resolved;
  7033. firstPar := 0;
  7034. ELSE
  7035. type := p0.type.resolved;
  7036. firstPar := 1;
  7037. END;
  7038. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7039. THEN
  7040. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7041. IF backend.cooperative THEN
  7042. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7043. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7044. IF recordType.isObject THEN
  7045. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7046. IF recordType.IsActive() THEN
  7047. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7048. END;
  7049. IF recordType.IsProtected() THEN
  7050. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7051. END;
  7052. ELSE
  7053. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7054. END;
  7055. END;
  7056. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7057. CallThis(position,"Runtime","New", 1);
  7058. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7059. Emit(Result(position, pointer));
  7060. exit := NewLabel();
  7061. BreqL(exit,pointer,nil);
  7062. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7063. GetRecordTypeName (recordType,name);
  7064. IF ~recordType.isObject THEN
  7065. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7066. END;
  7067. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7068. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7069. IF recordType.isObject THEN
  7070. IF recordType.IsProtected() THEN
  7071. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7072. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7073. END;
  7074. IF recordType.IsActive() THEN
  7075. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7076. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7077. END;
  7078. END;
  7079. END;
  7080. (* initialize fields *)
  7081. IF type(SyntaxTree.PointerType).isPlain THEN
  7082. size := 0;
  7083. ELSIF recordType.isObject THEN
  7084. size := BaseObjectTypeSize;
  7085. ELSE
  7086. size := BaseRecordTypeSize;
  7087. END;
  7088. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7089. (* call initializer *)
  7090. constructor := GetConstructor(recordType);
  7091. IF constructor # NIL THEN
  7092. (*! should be unified with ProcedureCallDesignator *)
  7093. Emit(Push(position,pointer));
  7094. ReleaseIntermediateOperand(pointer);
  7095. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7096. FOR i := firstPar TO x.parameters.Length()-1 DO
  7097. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7098. formalParameter := formalParameter.nextParameter;
  7099. END;
  7100. (* static call of the constructor *)
  7101. GetCodeSectionNameForSymbol(constructor,name);
  7102. ASSERT(~constructor.isInline);
  7103. IF constructor.scope.ownerModule # module.module THEN
  7104. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7105. ELSE
  7106. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7107. END;
  7108. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7109. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7110. Emit(Pop(position,pointer));
  7111. END;
  7112. (* call bodies *)
  7113. CallBodies(pointer,type);
  7114. SetLabel(exit);
  7115. needsTrace := p0.NeedsTrace();
  7116. IF needsTrace THEN ModifyAssignments(true) END;
  7117. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7118. Emit(Push(position, pointer));
  7119. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7120. Emit(Pop(position, pointer));
  7121. END;
  7122. Designate(p0,l);
  7123. IF needsTrace THEN
  7124. CallAssignPointer(l.op, pointer);
  7125. ELSE
  7126. ToMemory(l.op,addressType,0);
  7127. Emit(Mov(position,l.op,pointer));
  7128. END;
  7129. ReleaseIntermediateOperand(pointer);
  7130. ReleaseOperand(l);
  7131. IF needsTrace THEN ModifyAssignments(false) END;
  7132. ELSE (* not cooperative backend *)
  7133. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7134. IF temporaryVariable # NIL THEN
  7135. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7136. ELSE
  7137. Designate(p0,l);
  7138. END;
  7139. (* l.op contains address of pointer to record *)
  7140. Emit(Push(position,l.op)); (* address for use after syscall *)
  7141. Emit(Push(position,l.op));
  7142. ReleaseOperand(l);
  7143. (* push type descriptor *)
  7144. reg := TypeDescriptorAdr(recordType);
  7145. IF ~newObjectFile THEN
  7146. IntermediateCode.MakeMemory(reg,addressType);
  7147. END;
  7148. Emit(Push(position,reg));
  7149. ReleaseIntermediateOperand(reg);
  7150. (* push realtime flag *)
  7151. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7152. ELSE Emit(Push(position,false));
  7153. END;
  7154. CallThis(position,"Heaps","NewRec", 3);
  7155. (* check allocation success, if not successful then do not call initializers and bodies *)
  7156. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7157. Emit(Pop(position,pointer));
  7158. MakeMemory(reg,pointer,addressType,0);
  7159. ReleaseIntermediateOperand(pointer);
  7160. pointer := reg;
  7161. exit := NewLabel();
  7162. BreqL(exit,pointer,nil);
  7163. Emit(Push(position,pointer));
  7164. (* initialize fields *)
  7165. InitFields(recordType, pointer,0);
  7166. (* call initializer *)
  7167. constructor := GetConstructor(recordType);
  7168. IF constructor # NIL THEN
  7169. (*! should be unified with ProcedureCallDesignator *)
  7170. Emit(Push(position,pointer));
  7171. ReleaseIntermediateOperand(pointer);
  7172. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7173. FOR i := firstPar TO x.parameters.Length()-1 DO
  7174. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7175. formalParameter := formalParameter.nextParameter;
  7176. END;
  7177. (* static call of the constructor *)
  7178. GetCodeSectionNameForSymbol(constructor,name);
  7179. ASSERT(~constructor.isInline);
  7180. IF constructor.scope.ownerModule # module.module THEN
  7181. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7182. ELSE
  7183. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7184. END;
  7185. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7186. ELSE
  7187. ReleaseIntermediateOperand(pointer);
  7188. END;
  7189. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7190. Emit(Pop(position,pointer));
  7191. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7192. Designate(p0,l);
  7193. ToMemory(l.op,addressType,0);
  7194. Emit(Mov(position,l.op,pointer));
  7195. ReleaseOperand(l);
  7196. result.tag := emptyOperand;
  7197. ELSIF (x.type # NIL) THEN
  7198. result := l; (* temporary variable is the result of NEW Type() *)
  7199. END;
  7200. (* call bodies *)
  7201. CallBodies(pointer,type);
  7202. ReleaseIntermediateOperand(pointer);
  7203. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7204. end := NewLabel();
  7205. BrL(end);
  7206. SetLabel(exit);
  7207. Designate(p0,l);
  7208. ToMemory(l.op,addressType,0);
  7209. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7210. ReleaseOperand(l);
  7211. SetLabel(end);
  7212. ELSE
  7213. SetLabel(exit);
  7214. END;
  7215. END;
  7216. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7217. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7218. dim := 0;
  7219. IntermediateCode.InitOperand(reg);
  7220. IF p1 # NIL THEN
  7221. FOR i := firstPar TO x.parameters.Length()-1 DO
  7222. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7223. parameter := x.parameters.GetExpression(i);
  7224. Evaluate(parameter,r);
  7225. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7226. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7227. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7228. END;
  7229. Emit(Push(position,r.op));
  7230. IF i=1 THEN
  7231. CopyInt(reg,r.op);
  7232. ELSE
  7233. MulInt(reg, reg, r.op);
  7234. END;
  7235. ReleaseOperand(r);
  7236. INC(dim);
  7237. END;
  7238. Convert(reg,addressType);
  7239. ELSE
  7240. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7241. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7242. END;
  7243. openDim := dim;
  7244. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7245. IF backend.cooperative THEN
  7246. size := ToMemoryUnits(system,system.SizeOf(type));
  7247. WHILE type IS SyntaxTree.ArrayType DO
  7248. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7249. END;
  7250. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7251. IF (size # 1) THEN
  7252. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7253. END;
  7254. Emit(Push(position,reg));
  7255. size := ToMemoryUnits(system,system.SizeOf(type));
  7256. IF (size # 1) THEN
  7257. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7258. END;
  7259. AddInt(reg, reg, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize)));
  7260. (*Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));*)
  7261. Emit(Push(position,reg));
  7262. ReleaseIntermediateOperand(reg);
  7263. CallThis(position,"Runtime","New", 1);
  7264. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7265. Emit(Result(position, pointer));
  7266. exit := NewLabel();
  7267. else := NewLabel();
  7268. BreqL(else,pointer,nil);
  7269. IF ~type.hasPointers THEN
  7270. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7271. ELSIF type IS SyntaxTree.RecordType THEN
  7272. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7273. ELSIF type IS SyntaxTree.ProcedureType THEN
  7274. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7275. ELSE
  7276. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7277. END;
  7278. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7279. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7280. 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))));
  7281. IF type IS SyntaxTree.RecordType THEN
  7282. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7283. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7284. ELSE
  7285. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7286. END;
  7287. i := openDim;
  7288. WHILE i > 0 DO
  7289. DEC (i);
  7290. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7291. END;
  7292. needsTrace := p0.NeedsTrace();
  7293. IF needsTrace THEN ModifyAssignments(true) END;
  7294. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7295. Emit(Push(position, pointer));
  7296. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7297. Emit(Pop(position, pointer));
  7298. END;
  7299. Designate(p0,l);
  7300. IF needsTrace THEN
  7301. CallAssignPointer(l.op, pointer);
  7302. ModifyAssignments(false);
  7303. ELSE
  7304. ToMemory(l.op,addressType,0);
  7305. Emit(Mov(position,l.op,pointer));
  7306. END;
  7307. ReleaseIntermediateOperand(pointer);
  7308. ReleaseOperand(l);
  7309. BrL(exit);
  7310. SetLabel(else);
  7311. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7312. Designate(p0,l);
  7313. IF needsTrace THEN
  7314. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7315. ELSE
  7316. ToMemory(l.op,addressType,0);
  7317. Emit(Mov(position,l.op,pointer));
  7318. END;
  7319. ReleaseOperand(l);
  7320. SetLabel(exit);
  7321. ELSE
  7322. (*! the following code is only correct for "standard" Oberon calling convention *)
  7323. IF SemanticChecker.ContainsPointer(type) THEN
  7324. IF type IS SyntaxTree.ArrayType THEN
  7325. staticLength := 1;
  7326. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7327. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7328. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7329. END;
  7330. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7331. MulInt(reg,reg,tmp);
  7332. END;
  7333. Designate(p0,l);
  7334. IF openDim > 0 THEN
  7335. Emit(Push(position,l.op)); (* address for use after syscall *)
  7336. END;
  7337. Emit(Push(position,l.op)); (* address *)
  7338. ReleaseOperand(l);
  7339. tmp := TypeDescriptorAdr(type);
  7340. IF ~newObjectFile THEN
  7341. IntermediateCode.MakeMemory(tmp,addressType);
  7342. END;
  7343. Emit(Push(position,tmp)); (* type descriptor *)
  7344. ReleaseIntermediateOperand(tmp);
  7345. Emit(Push(position,reg)); (* number Elements *)
  7346. ReleaseIntermediateOperand(reg);
  7347. tmp := IntermediateCode.Immediate(addressType,dim);
  7348. Emit(Push(position,tmp)); (* dimensions *)
  7349. (* push realtime flag *)
  7350. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7351. ELSE Emit(Push(position,false));
  7352. END;
  7353. CallThis(position,"Heaps","NewArr",5)
  7354. ELSE
  7355. size := ToMemoryUnits(system,system.SizeOf(type));
  7356. IF (size # 1) THEN
  7357. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7358. (*
  7359. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7360. *)
  7361. END;
  7362. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7363. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7364. AddInt(reg, reg, tmp);
  7365. (*
  7366. Emit(Add(position,reg,reg,tmp));
  7367. *)
  7368. Designate(p0,l);
  7369. IF openDim >0 THEN
  7370. Emit(Push(position,l.op)); (* address for use after syscall *)
  7371. END;
  7372. Emit(Push(position,l.op)); (* address for syscall *)
  7373. ReleaseOperand(l); (* pointer address *)
  7374. Emit(Push(position,reg)); (* size *)
  7375. ReleaseIntermediateOperand(reg);
  7376. (* push realtime flag *)
  7377. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7378. ELSE Emit(Push(position,false));
  7379. END;
  7380. CallThis(position,"Heaps","NewSys", 3)
  7381. END;
  7382. IF openDim > 0 THEN
  7383. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7384. Emit(Pop(position,adr));
  7385. ToMemory(adr,addressType,0);
  7386. ReuseCopy(tmp,adr);
  7387. ReleaseIntermediateOperand(adr);
  7388. adr := tmp;
  7389. else := NewLabel();
  7390. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7391. i := openDim-1;
  7392. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7393. WHILE (i >= 0) DO
  7394. Emit(Pop(position,reg));
  7395. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7396. Emit(Mov(position,res,reg));
  7397. DEC(i);
  7398. END;
  7399. ReleaseIntermediateOperand(adr);
  7400. ReleaseIntermediateOperand(reg);
  7401. exit := NewLabel();
  7402. BrL(exit);
  7403. SetLabel(else);
  7404. (* else part: array could not be allocated *)
  7405. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7406. Emit(Add(position,sp,sp,tmp));
  7407. SetLabel(exit);
  7408. END;
  7409. END;
  7410. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7411. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7412. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7413. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7414. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7415. left.SetType(procedure.type);
  7416. formalParameter := procedureType.firstParameter;
  7417. (* push array to allocate *)
  7418. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7419. formalParameter :=formalParameter.nextParameter;
  7420. (* push length array *)
  7421. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7422. (* push size *)
  7423. type := t0;
  7424. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7425. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7426. END;
  7427. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7428. Emit(Push(position,tmp));
  7429. (* *)
  7430. IF SemanticChecker.ContainsPointer(type) THEN
  7431. tmp := TypeDescriptorAdr(type);
  7432. IF ~newObjectFile THEN
  7433. IntermediateCode.MakeMemory(tmp,addressType);
  7434. END;
  7435. ELSE
  7436. tmp := IntermediateCode.Immediate(addressType, 0);
  7437. END;
  7438. Emit(Push(position,tmp)); (* type descriptor *)
  7439. StaticCallOperand(result,procedure);
  7440. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7441. ReleaseOperand(result);
  7442. END;
  7443. (*
  7444. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7445. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7446. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7447. *)
  7448. ELSE
  7449. dim := 0;
  7450. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7451. (* generate geometry descriptor *)
  7452. Designate(p0,l);
  7453. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7454. ReleaseOperand(l);
  7455. isTensor := TRUE;
  7456. ELSE
  7457. isTensor := FALSE;
  7458. END;
  7459. FOR i := firstPar TO x.parameters.Length()-1 DO
  7460. IF ~isTensor THEN
  7461. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7462. END;
  7463. parameter := x.parameters.GetExpression(i);
  7464. Evaluate(parameter,r);
  7465. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7466. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7467. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7468. END;
  7469. Emit(Push(position,r.op));
  7470. IF i=1 THEN
  7471. CopyInt(reg, r.op);
  7472. ELSE
  7473. MulInt(reg, reg, r.op);
  7474. END;
  7475. ReleaseOperand(r);
  7476. INC(dim);
  7477. END;
  7478. Convert(reg,addressType);
  7479. openDim := dim;
  7480. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7481. (*! the following code is only correct for "standard" Oberon calling convention *)
  7482. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7483. t := type;
  7484. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7485. staticLength := 1;
  7486. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7487. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7488. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7489. END;
  7490. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7491. MulInt(reg,reg,tmp);
  7492. END;
  7493. Designate(p0,l);
  7494. IF isTensor THEN
  7495. Dereference(l,type,FALSE);
  7496. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7497. END;
  7498. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7499. Emit(Push(position,l.tag)); (* address *)
  7500. ReleaseOperand(l);
  7501. tmp := TypeDescriptorAdr(t);
  7502. IF ~newObjectFile THEN
  7503. IntermediateCode.MakeMemory(tmp,addressType);
  7504. END;
  7505. Emit(Push(position,tmp)); (* type descriptor *)
  7506. ReleaseIntermediateOperand(tmp);
  7507. Emit(Push(position,reg)); (* number Elements *)
  7508. ReleaseIntermediateOperand(reg);
  7509. tmp := IntermediateCode.Immediate(addressType,0);
  7510. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7511. (* push realtime flag: false by default *)
  7512. Emit(Push(position,false));
  7513. CallThis(position,"Heaps","NewArr",5);
  7514. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7515. Emit(Pop(position,adr));
  7516. GetMathArrayField(tmp,adr,MathPtrOffset);
  7517. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7518. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7519. PutMathArrayField(adr,reg,MathAdrOffset);
  7520. ReleaseIntermediateOperand(tmp);
  7521. ReleaseIntermediateOperand(reg);
  7522. ELSE
  7523. IF isTensor THEN
  7524. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7525. ELSE
  7526. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7527. END;
  7528. IF (size # 1) THEN
  7529. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7530. END;
  7531. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7532. AddInt(reg,reg,tmp);
  7533. Designate(p0,l);
  7534. IF isTensor THEN
  7535. Dereference(l,type,FALSE);
  7536. END;
  7537. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7538. Emit(Push(position,l.tag)); (* address for syscall *)
  7539. ReleaseOperand(l); (* pointer address *)
  7540. Emit(Push(position,reg)); (* size *)
  7541. ReleaseIntermediateOperand(reg);
  7542. (* push realtime flag: false by default *)
  7543. Emit(Push(position,false));
  7544. CallThis(position,"Heaps","NewSys",3);
  7545. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7546. Emit(Pop(position,adr));
  7547. GetMathArrayField(tmp,adr,MathPtrOffset);
  7548. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7549. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7550. PutMathArrayField(adr,reg,MathAdrOffset);
  7551. ReleaseIntermediateOperand(tmp);
  7552. ReleaseIntermediateOperand(reg);
  7553. END;
  7554. flags := {};
  7555. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7556. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7557. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7558. PutMathArrayField(adr,tmp,MathDimOffset);
  7559. else := NewLabel();
  7560. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7561. i := openDim-1;
  7562. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7563. IF isTensor THEN
  7564. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7565. ELSE
  7566. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7567. END;
  7568. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7569. WHILE (i >= 0) DO
  7570. Emit(Pop(position,reg));
  7571. PutMathArrayLength(adr,reg,i);
  7572. PutMathArrayIncrement(adr,tmp,i);
  7573. IF i > 0 THEN
  7574. IF i=openDim-1 THEN
  7575. CopyInt(tmp,tmp);
  7576. END;
  7577. MulInt(tmp,tmp,reg);
  7578. END;
  7579. DEC(i);
  7580. END;
  7581. ReleaseIntermediateOperand(adr);
  7582. ReleaseIntermediateOperand(reg);
  7583. ReleaseIntermediateOperand(tmp);
  7584. exit := NewLabel();
  7585. BrL(exit);
  7586. SetLabel(else);
  7587. (* else part: array could not be allocated *)
  7588. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7589. Emit(Add(position,sp,sp,tmp));
  7590. SetLabel(exit);
  7591. END;
  7592. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7593. THEN
  7594. IF ~backend.cellsAreObjects THEN RETURN END;
  7595. IF InCellScope(currentScope) THEN
  7596. PushSelfPointer()
  7597. ELSE
  7598. Emit(Push(position, nil));
  7599. END;
  7600. (* push temp address *)
  7601. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7602. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7603. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7604. (* l.op contains address of pointer to record *)
  7605. Emit(Push(position,l.op)); (* address for use after syscall *)
  7606. ReleaseOperand(l);
  7607. (* push type descriptor *)
  7608. reg := TypeDescriptorAdr(baseType);
  7609. IF ~newObjectFile THEN
  7610. IntermediateCode.MakeMemory(reg,addressType);
  7611. END;
  7612. Emit(Push(position,reg));
  7613. ReleaseIntermediateOperand(reg);
  7614. (* push name *)
  7615. (*Global.GetSymbolName(p0, n);*)
  7616. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7617. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7618. ELSE
  7619. Global.GetModuleName(module.module, n);
  7620. END;
  7621. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7622. (*type.typeDeclaration.GetName(n);*)
  7623. PushConstString(n);
  7624. (* push cellnet boolean *)
  7625. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7626. (* push engine boolean *)
  7627. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7628. (* allocate *)
  7629. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7630. (* add capabilities *)
  7631. modifier := p0(SyntaxTree.Designator).modifiers;
  7632. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7633. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7634. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7635. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7636. END;
  7637. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7638. (*
  7639. modifier := baseType(SyntaxTree.CellType).modifiers;
  7640. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7641. modifier := p0(SyntaxTree.Designator).modifiers;
  7642. *)
  7643. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7644. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7645. (* l.op contains address of pointer to record *)
  7646. ToMemory(l.op,addressType,0);
  7647. (* l.op contains value of pointer to record *)
  7648. Emit(Push(position,l.op)); (* address for use after syscall *)
  7649. ReleaseOperand(l);
  7650. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7651. prevScope := currentScope;
  7652. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7653. previous := section;
  7654. section := init;
  7655. (* add ports *)
  7656. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7657. CloseInitializer(previous);
  7658. currentScope := prevScope;
  7659. Symbol(temporaryVariable,l);
  7660. ToMemory(l.op,addressType,0);
  7661. Emit(Push(position,l.op));
  7662. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7663. (*
  7664. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7665. IF constructor # NIL THEN
  7666. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7667. FOR i := 1 TO x.parameters.Length()-1 DO
  7668. p := x.parameters.GetExpression(i);
  7669. Global.GetSymbolName(parameter,name);
  7670. Evaluate(p, value);
  7671. ASSERT(value.type # NIL);
  7672. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7673. par := instance.AddParameter(name);
  7674. par.SetInteger(value.integer);
  7675. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7676. par := instance.AddParameter(name);
  7677. par.SetBoolean(value.boolean);
  7678. ELSE Error(x.position,NotYetImplemented)
  7679. END;
  7680. parameter := parameter.nextParameter
  7681. END;
  7682. END;
  7683. *)
  7684. (* call initializer *)
  7685. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7686. IF constructor # NIL THEN
  7687. (*! should be unified with ProcedureCallDesignator *)
  7688. IF backend.cellsAreObjects THEN
  7689. Symbol(temporaryVariable,l);
  7690. ToMemory(l.op,addressType,0);
  7691. Emit(Push(position,l.op));
  7692. ReleaseOperand(l);
  7693. END;
  7694. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7695. FOR i := firstPar TO x.parameters.Length()-1 DO
  7696. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7697. formalParameter := formalParameter.nextParameter;
  7698. END;
  7699. (* static call of the constructor *)
  7700. Global.GetSymbolSegmentedName(constructor,name);
  7701. ASSERT(~constructor.isInline);
  7702. IF constructor.scope.ownerModule # module.module THEN
  7703. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7704. ELSE
  7705. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7706. END;
  7707. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7708. (*ELSE
  7709. ReleaseIntermediateOperand(pointer);*)
  7710. END;
  7711. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7712. ToMemory(l.op, addressType, 0);
  7713. Designate(p0,s0);
  7714. ToMemory(s0.op,addressType,0);
  7715. Emit(Mov(position,s0.op,l.op));
  7716. ReleaseOperand(l);
  7717. ReleaseOperand(s0);
  7718. result.tag := emptyOperand;
  7719. (* start *)
  7720. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7721. (* push cell *)
  7722. Symbol(temporaryVariable, l);
  7723. ToMemory(l.op,addressType,0);
  7724. Emit(Push(Basic.invalidPosition,l.op));
  7725. (* push delegate *)
  7726. Emit(Push(Basic.invalidPosition,l.op));
  7727. ReleaseOperand(l);
  7728. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7729. Emit(Push(position, s1.op));
  7730. ReleaseOperand(s1);
  7731. CallThis(position,"ActiveCellsRuntime","Start",3);
  7732. END;
  7733. (*IF temporaryVariable # NIL THEN
  7734. end := NewLabel();
  7735. BrL(end);
  7736. SetLabel(exit);
  7737. Designate(p0,l);
  7738. ToMemory(l.op,addressType,0);
  7739. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7740. ReleaseOperand(l);
  7741. SetLabel(end);
  7742. ELSE
  7743. SetLabel(exit);
  7744. END;
  7745. *)
  7746. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7747. ELSE (* no pointer to record, no pointer to array *)
  7748. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7749. (* ignore new statement *)
  7750. Warning(p0.position, "cannot run on final hardware");
  7751. ELSE
  7752. HALT(200);
  7753. END;
  7754. END;
  7755. (* ---- ADDRESSOF----- *)
  7756. |Global.systemAdr:
  7757. Designate(p0,s0);
  7758. s0.mode := ModeValue;
  7759. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7760. ReleaseIntermediateOperand(s0.op);
  7761. s0.op := s0.tag;
  7762. IntermediateCode.InitOperand(s0.tag);
  7763. END;
  7764. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7765. result := s0;
  7766. (* ---- BIT ----- *)
  7767. |Global.systemBit:
  7768. Evaluate(p0,s0);
  7769. ToMemory(s0.op,addressType,0);
  7770. ReuseCopy(res,s0.op);
  7771. ReleaseOperand(s0);
  7772. Evaluate(p1,s1);
  7773. Emit(Ror(position,res,res,s1.op));
  7774. ReleaseOperand(s1);
  7775. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7776. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7777. IF ~conditional THEN
  7778. InitOperand(result,ModeValue); result.op := res;
  7779. ELSE
  7780. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7781. BrL(falseLabel);
  7782. ReleaseIntermediateOperand(res);
  7783. END;
  7784. (* --- MSK ----*)
  7785. |Global.systemMsk:
  7786. Evaluate(p0,s0);
  7787. Evaluate(p1,s1);
  7788. ReuseCopy(res,s0.op);
  7789. ReleaseOperand(s0);
  7790. Emit(And(position,res,res,s1.op));
  7791. ReleaseOperand(s1);
  7792. InitOperand(result,ModeValue);
  7793. result.op := res;
  7794. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7795. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7796. Evaluate(p0,s0);
  7797. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7798. ReleaseOperand(s0);
  7799. InitOperand(result,ModeValue);
  7800. result.op := res;
  7801. (* ---- SYSTEM.GetStackPointer ----- *)
  7802. |Global.systemGetStackPointer:
  7803. InitOperand(result,ModeValue);
  7804. result.op := sp;
  7805. (* ---- SYSTEM.GetFramePointer ----- *)
  7806. |Global.systemGetFramePointer:
  7807. InitOperand(result,ModeValue);
  7808. result.op := fp;
  7809. (* ---- SYSTEM.GetActivity ----- *)
  7810. |Global.systemGetActivity:
  7811. ASSERT(backend.cooperative);
  7812. InitOperand(result,ModeValue);
  7813. result.op := ap;
  7814. (* ---- SYSTEM.SetStackPointer ----- *)
  7815. |Global.systemSetStackPointer:
  7816. Evaluate(p0,s0); (* *)
  7817. Emit(Mov(position,sp,s0.op));
  7818. ReleaseOperand(s0);
  7819. (* ---- SYSTEM.SetFramePointer ----- *)
  7820. |Global.systemSetFramePointer:
  7821. Evaluate(p0,s0); (* *)
  7822. Emit(Mov(position,fp,s0.op));
  7823. ReleaseOperand(s0);
  7824. (* ---- SYSTEM.Activity ----- *)
  7825. |Global.systemSetActivity:
  7826. ASSERT(backend.cooperative);
  7827. Evaluate(p0,s0); (* *)
  7828. Emit(Mov(position,ap,s0.op));
  7829. ReleaseOperand(s0);
  7830. (* ---- SYSTEM.VAL ----- *)
  7831. |Global.systemVal:
  7832. Expression(p1);
  7833. s1 := result;
  7834. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7835. IF s1.mode = ModeReference THEN
  7836. (* nothing to be done if not record type, just take over new type *)
  7837. IF (type IS SyntaxTree.RecordType) THEN
  7838. ReleaseIntermediateOperand(s1.tag);
  7839. s1.tag := TypeDescriptorAdr(type);
  7840. IF ~newObjectFile THEN
  7841. IntermediateCode.MakeMemory(s1.tag,addressType);
  7842. END;
  7843. UseIntermediateOperand(s1.tag);
  7844. END;
  7845. result := s1;
  7846. ELSE (* copy over result to different type, may not use convert *)
  7847. itype := IntermediateCode.GetType(system,type);
  7848. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7849. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7850. Emit(Mov(position,s0.op,s1.op));
  7851. ReleaseOperand(s1);
  7852. InitOperand(result,ModeValue);
  7853. result.op := s0.op;
  7854. ELSE (* different size, must convert *)
  7855. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7856. Convert(s1.op, IntermediateCode.GetType(system,type));
  7857. result := s1;
  7858. END;
  7859. END;
  7860. (* ---- SYSTEM.GET ----- *)
  7861. |Global.systemGet:
  7862. Evaluate(p0,s0); (* adr *)
  7863. Designate(p1,s1); (* variable *)
  7864. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7865. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7866. Emit(Mov(position,s1.op,s0.op));
  7867. ReleaseOperand(s1);
  7868. ReleaseOperand(s0);
  7869. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7870. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7871. Evaluate(p0,s0); (* *)
  7872. Evaluate(p1,s1); (* variable *)
  7873. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7874. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7875. (* real part *)
  7876. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7877. Emit(Mov(position,res, s1.op));
  7878. ReleaseIntermediateOperand(res);
  7879. (* imaginary part *)
  7880. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7881. Emit(Mov(position,res, s1.tag));
  7882. ReleaseIntermediateOperand(res);
  7883. ReleaseOperand(s1);
  7884. ReleaseOperand(s0);
  7885. ELSE
  7886. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7887. ReleaseOperand(s0);
  7888. Emit(Mov(position,res,s1.op));
  7889. ReleaseIntermediateOperand(res);
  7890. ReleaseOperand(s1);
  7891. END;
  7892. (* ---- SYSTEM.MOVE ----- *)
  7893. |Global.systemMove:
  7894. Evaluate(p0,s0);
  7895. Evaluate(p1,s1);
  7896. Evaluate(p2,s2);
  7897. Emit(Copy(position,s1.op,s0.op,s2.op));
  7898. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7899. (* ---- SYSTEM.NEW ----- *)
  7900. |Global.systemNew:
  7901. Designate(p0,s0);
  7902. Emit(Push(position,s0.op));
  7903. ReleaseOperand(s0);
  7904. Evaluate(p1,s1);
  7905. Emit(Push(position,s1.op));
  7906. ReleaseOperand(s1);
  7907. (* push realtime flag: false by default *)
  7908. Emit(Push(position,false));
  7909. CallThis(position,"Heaps","NewSys",3);
  7910. (* ---- SYSTEM.CALL ----- *)
  7911. |Global.systemRef:
  7912. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7913. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7914. s0.mode := ModeValue;
  7915. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7916. result := s0
  7917. (* ---- INCR ----- *)
  7918. |Global.Incr:
  7919. Designate(p0,operand);
  7920. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7921. Dereference(operand,p0.type.resolved,FALSE);
  7922. END;
  7923. ASSERT(p1 # NIL);
  7924. Evaluate(p1,l);
  7925. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7926. ReleaseOperand(operand); ReleaseOperand(l);
  7927. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7928. (* ---- SUM ----- *)
  7929. |Global.Sum: HALT(200);
  7930. (* ---- ALL ----- *)
  7931. |Global.All: HALT(200);
  7932. (* ---- CAS ----- *)
  7933. |Global.Cas:
  7934. needsTrace := p0.NeedsTrace();
  7935. IF needsTrace THEN ModifyAssignments(true) END;
  7936. Designate(p0,s0);
  7937. Evaluate(p1,s1);
  7938. Evaluate(p2,s2);
  7939. IF needsTrace THEN
  7940. Emit(Push(position, s0.op));
  7941. Emit(Push(position, s1.op));
  7942. Emit(Push(position, s2.op));
  7943. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7944. ELSE
  7945. Emit(Cas(position,s0.op,s1.op,s2.op));
  7946. END;
  7947. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7948. IF needsTrace THEN ModifyAssignments(false) END;
  7949. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7950. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7951. IF conditional THEN
  7952. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7953. BrL(falseLabel);
  7954. ReleaseIntermediateOperand(res);
  7955. END;
  7956. (* ---- DIM ----- *)
  7957. |Global.Dim:
  7958. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7959. Designate(p0,s0);
  7960. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7961. Dereference(s0,p0.type.resolved,FALSE);
  7962. END;
  7963. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7964. ReleaseOperand(s0);
  7965. (* ---- RESHAPE ----- *)
  7966. |Global.Reshape:
  7967. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7968. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7969. left.SetType(procedure.type);
  7970. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7971. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7972. END;
  7973. (* ---- SYSTEM.TYPECODE ----- *)
  7974. |Global.systemTypeCode:
  7975. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7976. IF type.resolved IS SyntaxTree.PointerType THEN
  7977. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7978. END;
  7979. result.op := TypeDescriptorAdr(type);
  7980. IF ~newObjectFile THEN
  7981. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7982. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7983. END;
  7984. result.mode := ModeValue;
  7985. (* ---- SYSTEM.TRACE ----- *)
  7986. |Global.systemTrace:
  7987. SystemTrace(x.parameters, x.position);
  7988. (* ----- CONNECT ------*)
  7989. |Global.Connect:
  7990. IF backend.cellsAreObjects THEN
  7991. PushPort(p0);
  7992. PushPort(p1);
  7993. IF p2 # NIL THEN
  7994. Evaluate(p2, s2);
  7995. Emit(Push(p2.position, s2.op));
  7996. ReleaseOperand(s2);
  7997. ELSE
  7998. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7999. END;
  8000. CallThis(position,"ActiveCellsRuntime","Connect",3);
  8001. ELSE
  8002. Warning(x.position, "cannot run on final hardware");
  8003. END;
  8004. (* ----- DELEGATE ------*)
  8005. |Global.Delegate:
  8006. IF backend.cellsAreObjects THEN
  8007. PushPort(p0);
  8008. PushPort(p1);
  8009. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  8010. ELSE
  8011. Warning(x.position, "cannot run on final hardware");
  8012. END;
  8013. (* ----- SEND ------*)
  8014. |Global.Send:
  8015. Evaluate(p0,s0);
  8016. Evaluate(p1,s1);
  8017. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8018. Emit(Push(position,s0.op));
  8019. Emit(Push(position,s1.op));
  8020. (*
  8021. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8022. *)
  8023. IF ~backend.cellsAreObjects THEN
  8024. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8025. END;
  8026. ReleaseOperand(s0);
  8027. ReleaseOperand(s1);
  8028. IF backend.cellsAreObjects THEN
  8029. CallThis(position,"ActiveCellsRuntime","Send",2);
  8030. ELSE
  8031. CallThis(position,ChannelModuleName,"Send",2);
  8032. END;
  8033. (* ----- RECEIVE ------*)
  8034. |Global.Receive:
  8035. Evaluate(p0,s0);
  8036. Emit(Push(position,s0.op));
  8037. Designate(p1,s1);
  8038. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8039. Emit(Push(position,s1.op));
  8040. IF p2 # NIL THEN
  8041. Designate(p2,s2);
  8042. Emit(Push(position,s2.op));
  8043. END;
  8044. (*
  8045. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8046. *)
  8047. IF ~backend.cellsAreObjects THEN
  8048. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8049. END;
  8050. ReleaseOperand(s0);
  8051. ReleaseOperand(s1);
  8052. ReleaseOperand(s2);
  8053. IF backend.cellsAreObjects THEN
  8054. IF p2 = NIL THEN
  8055. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8056. ELSE
  8057. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8058. END;
  8059. ELSE
  8060. IF p2 = NIL THEN
  8061. CallThis(position,ChannelModuleName,"Receive",2)
  8062. ELSE
  8063. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8064. END;
  8065. END;
  8066. | Global.systemSpecial:
  8067. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8068. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8069. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8070. (* determine if parameters are of the VAR kind *)
  8071. ASSERT(x.parameters.Length() <= 3);
  8072. formalParameter := procedureType.firstParameter;
  8073. FOR i := 0 TO x.parameters.Length() - 1 DO
  8074. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8075. formalParameter := formalParameter.nextParameter
  8076. END;
  8077. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8078. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8079. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8080. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8081. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8082. IF procedureType.returnType # NIL THEN
  8083. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8084. Emit(Result(position, res));
  8085. (*InitOperand(result,ModeValue);
  8086. result.op := res;
  8087. *)
  8088. IF ~conditional THEN
  8089. InitOperand(result,ModeValue); result.op := res;
  8090. ELSE
  8091. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8092. BrL(falseLabel);
  8093. ReleaseIntermediateOperand(res);
  8094. END;
  8095. END
  8096. ELSE (* function not yet implemented *)
  8097. Error(position,"not yet implemented");
  8098. END;
  8099. destination := dest;
  8100. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8101. END VisitBuiltinCallDesignator;
  8102. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8103. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8104. BEGIN
  8105. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8106. dest := destination; destination := emptyOperand;
  8107. Expression(x.left);
  8108. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8109. ELSIF isUnchecked THEN (* no check *)
  8110. ELSE
  8111. trueL := NewLabel();
  8112. falseL := NewLabel();
  8113. IF IsPointerToRecord(x.left.type,recordType) THEN
  8114. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8115. Emit(Mov(position,tag, result.op));
  8116. IF result.mode # ModeValue THEN
  8117. ptr := tag;
  8118. IntermediateCode.MakeMemory(ptr,addressType);
  8119. Emit(Mov(position,tag, ptr));
  8120. END;
  8121. IF ~backend.cooperative THEN
  8122. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8123. END;
  8124. IntermediateCode.MakeMemory(tag,addressType);
  8125. ELSE
  8126. tag := result.tag;
  8127. UseIntermediateOperand(tag);
  8128. END;
  8129. TypeTest(tag,x.type,trueL,falseL);
  8130. ReleaseIntermediateOperand(tag);
  8131. SetLabel(falseL);
  8132. EmitTrap(position,TypeCheckTrap);
  8133. SetLabel(trueL);
  8134. END;
  8135. destination := dest;
  8136. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8137. END VisitTypeGuardDesignator;
  8138. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8139. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8140. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8141. VAR label: Label; pc: LONGINT;
  8142. BEGIN
  8143. IF backend.cooperative & ~isUnchecked THEN
  8144. pc := section.pc;
  8145. label := NewLabel();
  8146. BrneL(label, operand.op, nil);
  8147. EmitTrap(position, NilPointerTrap);
  8148. SetLabel(label);
  8149. INC(statCoopNilCheck, section.pc - pc);
  8150. END;
  8151. END NilCheck;
  8152. BEGIN
  8153. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8154. ReuseCopy(dereferenced,operand.op);
  8155. ReleaseOperand(operand);
  8156. operand.mode := ModeReference;
  8157. operand.op := dereferenced;
  8158. operand.tag := dereferenced;
  8159. UseIntermediateOperand(operand.tag);
  8160. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8161. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8162. ReleaseIntermediateOperand(operand.tag);
  8163. operand.tag := TypeDescriptorAdr(type);
  8164. IF ~newObjectFile THEN
  8165. IntermediateCode.MakeMemory(operand.tag,addressType);
  8166. END;
  8167. ELSE
  8168. IF ~backend.cooperative THEN
  8169. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8170. END;
  8171. IntermediateCode.MakeMemory(operand.tag,addressType);
  8172. END;
  8173. NilCheck(operand.op);
  8174. ELSIF type IS SyntaxTree.ArrayType THEN
  8175. IF isUnsafe THEN
  8176. NilCheck(operand.op);
  8177. ReleaseIntermediateOperand(operand.tag);
  8178. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8179. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8180. ELSE
  8181. operand.tag := emptyOperand;
  8182. END;
  8183. ELSE
  8184. NilCheck(operand.op);
  8185. IF backend.cooperative THEN
  8186. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8187. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8188. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8189. ELSE
  8190. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8191. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8192. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8193. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8194. END;
  8195. END;
  8196. ELSIF type IS SyntaxTree.MathArrayType THEN
  8197. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8198. IntermediateCode.MakeMemory(operand.op,addressType);
  8199. ELSE HALT(100);
  8200. END;
  8201. END Dereference;
  8202. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8203. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8204. BEGIN
  8205. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8206. dest := destination; destination := emptyOperand;
  8207. Evaluate(x.left,d);
  8208. type := x.type.resolved;
  8209. prevIsUnchecked := isUnchecked;
  8210. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8211. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8212. END;
  8213. Dereference(d,type,IsUnsafePointer(x.left.type));
  8214. isUnchecked := prevIsUnchecked;
  8215. result := d;
  8216. 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
  8217. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8218. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8219. END;
  8220. END;
  8221. destination := dest;
  8222. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8223. END VisitDereferenceDesignator;
  8224. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8225. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8226. BEGIN
  8227. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8228. dest := destination; destination := emptyOperand;
  8229. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8230. tag := result.op;
  8231. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8232. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8233. StaticCallOperand(result,procedure.super);
  8234. ReleaseIntermediateOperand(result.tag);
  8235. UseIntermediateOperand(tag); (* necessary ? *)
  8236. result.tag := tag;
  8237. destination := dest;
  8238. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8239. END VisitSupercallDesignator;
  8240. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8241. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8242. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8243. name: Basic.SegmentedName;
  8244. procedure: SyntaxTree.Procedure;
  8245. procedureType: SyntaxTree.ProcedureType;
  8246. BEGIN
  8247. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8248. dest := destination; destination := emptyOperand;
  8249. scope := currentScope;
  8250. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8251. scope := scope.outerScope;
  8252. END;
  8253. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8254. IF newObjectFile THEN
  8255. moduleSection := meta.ModuleSection();
  8256. IF backend.cooperative THEN
  8257. moduleOffset := 0;
  8258. ELSE
  8259. moduleOffset := moduleSection.pc;
  8260. END;
  8261. result.mode := ModeValue;
  8262. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8263. ELSE
  8264. Symbol(moduleSelf,result);
  8265. IntermediateCode.MakeMemory(result.op,addressType);
  8266. END
  8267. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8268. result.mode := ModeValue;
  8269. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8270. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8271. ELSE
  8272. GetBaseRegister(basereg,currentScope,scope);
  8273. InitOperand(result,ModeReference);
  8274. result.op := basereg;
  8275. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8276. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8277. parametersSize := ProcedureParametersSize(system,procedure);
  8278. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8279. IF backend.cooperative THEN
  8280. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8281. END;
  8282. IF ~x.type.resolved.IsPointer() THEN (* var par ! *)
  8283. MakeMemory(result.op, result.op, addressType, 0);
  8284. END;
  8285. (* tag must be loaded when dereferencing SELF pointer *)
  8286. END;
  8287. destination := dest;
  8288. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8289. END VisitSelfDesignator;
  8290. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8291. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; map: SymbolMap;
  8292. BEGIN
  8293. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8294. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8295. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8296. parameter := procedureType.returnParameter;
  8297. IF currentIsInline THEN
  8298. map := currentMapper.Get(NIL);
  8299. IF map # NIL THEN
  8300. Designate(map.to, result);
  8301. ELSE
  8302. HALT(200);
  8303. END;
  8304. RETURN;
  8305. END;
  8306. VisitParameter(parameter);
  8307. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8308. END VisitResultDesignator;
  8309. (** values *)
  8310. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8311. BEGIN
  8312. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8313. IF conditional THEN
  8314. IF x.value THEN BrL(trueLabel)
  8315. ELSE BrL(falseLabel)
  8316. END;
  8317. ELSE
  8318. InitOperand(result,ModeValue);
  8319. IF x.value THEN result.op := true ELSE result.op := false END;
  8320. END;
  8321. END VisitBooleanValue;
  8322. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8323. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8324. BEGIN
  8325. Global.GetModuleSegmentedName(module.module, name);
  8326. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8327. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8328. RETURN section
  8329. END GetDataSection;
  8330. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8331. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8332. BEGIN
  8333. type := vop.type;
  8334. data := GetDataSection();
  8335. pc := EnterImmediate(data,vop);
  8336. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8337. IntermediateCode.MakeMemory(vop, type);
  8338. END GetImmediateMem;
  8339. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8340. BEGIN
  8341. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8342. InitOperand(result,ModeValue);
  8343. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8344. IF ~supportedImmediate(result.op) &~inData THEN
  8345. GetImmediateMem(result.op)
  8346. END;
  8347. END VisitIntegerValue;
  8348. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8349. BEGIN
  8350. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8351. InitOperand(result,ModeValue);
  8352. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8353. END VisitCharacterValue;
  8354. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8355. BEGIN
  8356. IF Trace THEN TraceEnter("VisitSetValue") END;
  8357. InitOperand(result,ModeValue);
  8358. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8359. END VisitSetValue;
  8360. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8361. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8362. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8363. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8364. BEGIN
  8365. numberElements := x.elements.Length();
  8366. FOR i := 0 TO numberElements-1 DO
  8367. expression := x.elements.GetExpression(i);
  8368. IF expression IS SyntaxTree.MathArrayExpression THEN
  8369. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8370. ELSE
  8371. inData := TRUE;
  8372. Evaluate(expression,op);
  8373. irv.Emit(Data(position,op.op));
  8374. inData := FALSE;
  8375. ReleaseOperand(op);
  8376. END;
  8377. END;
  8378. END RecursiveData;
  8379. BEGIN
  8380. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8381. IF ~TryConstantDeclaration() THEN
  8382. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8383. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8384. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8385. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8386. ELSE
  8387. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8388. END;
  8389. RecursiveData(x.array);
  8390. InitOperand(result,ModeReference);
  8391. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8392. END
  8393. END VisitMathArrayValue;
  8394. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8395. VAR constant: Sections.Section;
  8396. BEGIN
  8397. IF constantDeclaration = NIL THEN
  8398. RETURN FALSE
  8399. ELSE
  8400. (* Is a constant in this module: did we generate it already? *)
  8401. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8402. IF constant # NIL THEN
  8403. InitOperand(result,ModeReference);
  8404. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8405. RETURN TRUE;
  8406. END;
  8407. END;
  8408. RETURN FALSE
  8409. END TryConstantDeclaration;
  8410. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8411. BEGIN
  8412. constantDeclaration := x;
  8413. x.value.resolved.Accept(SELF);
  8414. END VisitConstant;
  8415. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8416. BEGIN
  8417. IF Trace THEN TraceEnter("VisitRealValue") END;
  8418. InitOperand(result,ModeValue);
  8419. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8420. END VisitRealValue;
  8421. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8422. VAR
  8423. componentType: SyntaxTree.Type;
  8424. BEGIN
  8425. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8426. ASSERT(x.type IS SyntaxTree.ComplexType);
  8427. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8428. InitOperand(result,ModeValue);
  8429. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8430. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8431. END VisitComplexValue;
  8432. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8433. VAR i: LONGINT; name: Basic.SegmentedName;
  8434. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8435. BEGIN
  8436. IF Trace THEN TraceEnter("VisitStringValue") END;
  8437. IF ~TryConstantDeclaration() THEN
  8438. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8439. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8440. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8441. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8442. ELSE
  8443. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8444. END;
  8445. FOR i := 0 TO x.length-1 DO
  8446. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8447. irv.Emit(Data(position,op));
  8448. END;
  8449. InitOperand(result,ModeReference);
  8450. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8451. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8452. END
  8453. END VisitStringValue;
  8454. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8455. BEGIN
  8456. IF Trace THEN TraceEnter("VisitNilValue") END;
  8457. InitOperand(result,ModeValue);
  8458. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8459. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8460. END VisitNilValue;
  8461. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8462. BEGIN
  8463. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8464. InitOperand(result,ModeValue);
  8465. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8466. END VisitEnumerationValue;
  8467. (** symbols *)
  8468. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8469. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8470. END VisitImport;
  8471. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8472. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8473. BEGIN
  8474. IF scope # baseScope THEN
  8475. (* left := [fp+8] *)
  8476. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8477. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8478. ReuseCopy(left,right);
  8479. ReleaseIntermediateOperand(right);
  8480. scope := scope.outerScope; DEC(level);
  8481. (* { left := [left+8] } *)
  8482. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8483. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8484. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8485. Emit(Mov(position,left,right));
  8486. scope := scope.outerScope; DEC(level);
  8487. END;
  8488. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8489. result := left;
  8490. ELSE
  8491. result := fp;
  8492. END;
  8493. END GetBaseRegister;
  8494. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8495. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8496. BEGIN
  8497. IF Trace THEN TraceEnter("VisitVariable"); END;
  8498. type := x.type.resolved;
  8499. IF (x.useRegister) THEN
  8500. InitOperand(result, ModeValue);
  8501. IF x.registerNumber < 0 THEN
  8502. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8503. reg := x.registerNumber;
  8504. ELSE
  8505. reg := registerUsageCount.Map(x.registerNumber);
  8506. UseRegister(reg);
  8507. END;
  8508. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8509. ELSIF x.externalName # NIL THEN
  8510. InitOperand(result,ModeReference);
  8511. Basic.ToSegmentedName(x.externalName^, name);
  8512. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8513. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8514. InitOperand(result,ModeReference);
  8515. GetBaseRegister(result.op,currentScope,x.scope);
  8516. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8517. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8518. InitOperand(result,ModeReference);
  8519. GetCodeSectionNameForSymbol(x,name);
  8520. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8521. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8522. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8523. InitOperand(result,ModeReference);
  8524. GetCodeSectionNameForSymbol(x,name);
  8525. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8526. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8527. ELSE (* field, left designator must have been emitted *)
  8528. ASSERT(result.mode = ModeReference);
  8529. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8530. ReuseCopy(temp,result.op);
  8531. ReleaseIntermediateOperand(result.op);
  8532. result.op := temp;
  8533. END;
  8534. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8535. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8536. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8537. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8538. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8539. END;
  8540. END;
  8541. END;
  8542. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8543. ValueToCondition(result);
  8544. ELSIF type IS SyntaxTree.ProcedureType THEN
  8545. ReleaseIntermediateOperand(result.tag);
  8546. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8547. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8548. UseIntermediateOperand(result.tag);
  8549. ELSE
  8550. result.tag := nil; (* nil *)
  8551. END;
  8552. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8553. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8554. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8555. ReleaseIntermediateOperand(result.tag);
  8556. Global.GetSymbolSegmentedName(x,name);
  8557. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8558. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8559. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8560. ELSE
  8561. END;
  8562. ELSE
  8563. ReleaseIntermediateOperand(result.tag);
  8564. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8565. END;
  8566. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8567. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8568. ReleaseIntermediateOperand(result.tag);
  8569. result.tag := result.op;
  8570. UseIntermediateOperand(result.tag);
  8571. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8572. IntermediateCode.MakeMemory(result.op,addressType);
  8573. END;
  8574. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8575. ReleaseIntermediateOperand(result.tag);
  8576. result.tag := TypeDescriptorAdr(type);
  8577. IF ~newObjectFile THEN
  8578. IntermediateCode.MakeMemory(result.tag,addressType);
  8579. END;
  8580. UseIntermediateOperand(result.tag);
  8581. (* tag for pointer type computed not here but during dereferencing *)
  8582. END;
  8583. IF Trace THEN TraceExit("VisitVariable") END;
  8584. END VisitVariable;
  8585. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8586. BEGIN
  8587. VisitVariable(property);
  8588. END VisitProperty;
  8589. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8590. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8591. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8592. BEGIN
  8593. type := x.type.resolved;
  8594. IF Trace THEN TraceEnter("VisitParameter") END;
  8595. IF x.ownerType IS SyntaxTree.CellType THEN
  8596. ptype := x.type.resolved;
  8597. IF backend.cellsAreObjects THEN
  8598. ASSERT(result.mode = ModeReference);
  8599. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8600. ReuseCopy(temp,result.op);
  8601. ReleaseIntermediateOperand(result.op);
  8602. result.op := temp;
  8603. END;
  8604. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8605. RETURN;
  8606. ELSE
  8607. InitOperand(result,ModeReference);
  8608. GetCodeSectionNameForSymbol(x,name);
  8609. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8610. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8611. RETURN;
  8612. END;
  8613. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8614. InitOperand(result,ModeReference);
  8615. GetCodeSectionNameForSymbol(x,name);
  8616. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8617. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8618. RETURN
  8619. ELSE
  8620. GetBaseRegister(basereg,currentScope,x.scope);
  8621. InitOperand(result,ModeReference);
  8622. result.op := basereg;
  8623. END;
  8624. IF IsOpenArray(type) THEN
  8625. result.tag := basereg;
  8626. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8627. IntermediateCode.MakeMemory(result.op,addressType);
  8628. IF Global.IsOberonProcedure(x.ownerType) THEN
  8629. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8630. UseIntermediateOperand(result.tag);
  8631. ELSE
  8632. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8633. END;
  8634. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8635. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8636. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8637. ELSIF IsStaticArray(type) THEN
  8638. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8639. IntermediateCode.MakeMemory(result.op,addressType);
  8640. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8641. ELSIF type IS SyntaxTree.MathArrayType THEN
  8642. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8643. WITH type: SyntaxTree.MathArrayType DO
  8644. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8645. IF type.form = SyntaxTree.Tensor THEN
  8646. ELSIF type.form = SyntaxTree.Open THEN
  8647. result.tag := result.op;
  8648. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8649. IntermediateCode.MakeMemory(result.op,addressType);
  8650. UseIntermediateOperand(result.tag);
  8651. ELSIF type.form = SyntaxTree.Static THEN
  8652. IF x.kind = SyntaxTree.ConstParameter THEN
  8653. IntermediateCode.MakeMemory(result.op,addressType);
  8654. END;
  8655. ELSE HALT(100)
  8656. END;
  8657. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8658. IF type.form = SyntaxTree.Tensor THEN
  8659. ToMemory(result.op,addressType,0);
  8660. ELSIF type.form = SyntaxTree.Open THEN
  8661. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8662. ReuseCopy(result.tag, mem);
  8663. ReleaseIntermediateOperand(mem);
  8664. ReleaseIntermediateOperand(result.op);
  8665. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8666. ELSIF type.form = SyntaxTree.Static THEN
  8667. IntermediateCode.MakeMemory(result.op,addressType);
  8668. ELSE HALT(100)
  8669. END;
  8670. ELSE HALT(100)
  8671. END;
  8672. END;
  8673. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8674. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8675. IntermediateCode.MakeMemory(result.op,addressType);
  8676. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8677. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8678. END;
  8679. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8680. ValueToCondition(result);
  8681. ELSIF type IS SyntaxTree.ProcedureType THEN
  8682. ReleaseIntermediateOperand(result.tag);
  8683. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8684. IF x.kind = SyntaxTree.VarParameter THEN
  8685. ReuseCopy(result.tag,result.op);
  8686. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8687. IntermediateCode.MakeMemory(result.tag,addressType);
  8688. ELSE
  8689. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8690. UseIntermediateOperand(result.tag);
  8691. END;
  8692. ELSE
  8693. result.tag := nil;
  8694. END;
  8695. (* tag for pointer type computed not here but during dereferencing *)
  8696. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) & ~(x.selfParameter) THEN
  8697. ReleaseIntermediateOperand(result.tag);
  8698. result.tag := basereg;
  8699. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8700. IntermediateCode.MakeMemory(result.tag,addressType);
  8701. UseIntermediateOperand(result.tag);
  8702. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & ((x.kind = SyntaxTree.ValueParameter) OR x.selfParameter) THEN
  8703. ReleaseIntermediateOperand(result.tag);
  8704. result.tag := TypeDescriptorAdr(type);
  8705. IF ~newObjectFile THEN
  8706. IntermediateCode.MakeMemory(result.tag,addressType);
  8707. END;
  8708. UseIntermediateOperand(result.tag);
  8709. END;
  8710. IF Trace THEN TraceExit("VisitParameter") END;
  8711. END VisitParameter;
  8712. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8713. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8714. BEGIN
  8715. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8716. (* left.p: left already emitted *)
  8717. tag := result.op; (* value of pointer to left *)
  8718. (* get type desc *)
  8719. tmp := result.tag;
  8720. IntermediateCode.MakeMemory(tmp,addressType);
  8721. (* get method adr *)
  8722. Reuse1(reg,tmp);
  8723. ReleaseIntermediateOperand(tmp);
  8724. IF backend.cooperative THEN
  8725. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8726. WHILE recordType.baseType # NIL DO
  8727. recordType := recordType.GetBaseRecord ();
  8728. END;
  8729. GetRecordTypeName (recordType,name);
  8730. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8731. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8732. offset := 0;
  8733. ELSE
  8734. offset := 2;
  8735. END;
  8736. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8737. ELSE
  8738. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8739. END;
  8740. InitOperand(operand,ModeReference);
  8741. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8742. operand.op := reg;
  8743. operand.tag := tag;
  8744. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8745. END DynamicCallOperand;
  8746. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8747. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8748. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8749. BEGIN
  8750. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8751. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8752. tag := operand.op;
  8753. ReleaseIntermediateOperand(operand.tag);
  8754. ELSE tag := nil
  8755. END;
  8756. IF x.isInline THEN
  8757. sectionType := Sections.InlineCodeSection;
  8758. ELSE
  8759. sectionType := Sections.CodeSection;
  8760. END;
  8761. IF x.externalName # NIL THEN
  8762. Basic.ToSegmentedName(x.externalName^, name);
  8763. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8764. ELSE
  8765. GetCodeSectionNameForSymbol(x, name);
  8766. IF (x.scope.ownerModule = module.module) THEN
  8767. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8768. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8769. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8770. IF source.pc = 0 THEN (* no code yet *)
  8771. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8772. END;
  8773. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8774. bits := x.procedureScope.body.code.inlineCode;
  8775. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8776. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8777. binary.CopyBits(bits, 0, bits.GetSize());
  8778. source.SetResolved(binary);
  8779. ELSE
  8780. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8781. END;
  8782. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8783. END;
  8784. InitOperand(operand,ModeValue);
  8785. operand.op := reg;
  8786. operand.tag := tag;
  8787. IF Trace THEN TraceExit("StaticCallOperand") END;
  8788. END StaticCallOperand;
  8789. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8790. (* handle expressions of the form designator.procedure or procedure *)
  8791. BEGIN
  8792. IF Trace THEN TraceEnter("VisitProcedure") END;
  8793. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) & ~(result.tag.mode = IntermediateCode.ModeImmediate) THEN
  8794. DynamicCallOperand(result,x);
  8795. ELSIF x.isInline THEN
  8796. StaticCallOperand(result,x);
  8797. ELSE
  8798. StaticCallOperand(result,x);
  8799. END;
  8800. IF Trace THEN TraceExit("VisitProcedure") END;
  8801. END VisitProcedure;
  8802. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8803. BEGIN
  8804. VisitProcedure(x);
  8805. END VisitOperator;
  8806. (** statements *)
  8807. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8808. BEGIN
  8809. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8810. Expression(x.call);
  8811. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8812. ReleaseOperand(result)
  8813. END;
  8814. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8815. END VisitProcedureCallStatement;
  8816. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8817. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8818. leftBase, rightBase: SyntaxTree.Type;
  8819. procedureName,s: SyntaxTree.IdentifierString;
  8820. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8821. size: LONGINT; rightKind: LONGINT;
  8822. dummy: IntermediateCode.Operand;
  8823. CONST moduleName = "FoxArrayBase";
  8824. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8825. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8826. BEGIN
  8827. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8828. IF base IS SyntaxTree.MathArrayType THEN
  8829. base := OpenArray(base(SyntaxTree.MathArrayType));
  8830. END;
  8831. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8832. result.SetArrayBase(base);
  8833. RETURN result
  8834. END OpenArray;
  8835. BEGIN
  8836. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8837. SaveRegisters();ReleaseUsedRegisters(saved);
  8838. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8839. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8840. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8841. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8842. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8843. IF leftType.form = SyntaxTree.Tensor THEN
  8844. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8845. ELSIF leftType.form = SyntaxTree.Open THEN
  8846. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8847. ELSIF leftType.form = SyntaxTree.Static THEN
  8848. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8849. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8850. END;
  8851. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8852. IF procedure = NIL THEN
  8853. s := "Instruction not supported on target, emulation procedure ";
  8854. Strings.Append(s,moduleName);
  8855. Strings.Append(s,".");
  8856. Strings.Append(s,procedureName);
  8857. Strings.Append(s," not present");
  8858. Error(position,s);
  8859. ELSE
  8860. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8861. parameter.SetType(leftType);
  8862. parameter.SetAccess(SyntaxTree.Internal);
  8863. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8864. parameter.SetKind(rightKind);
  8865. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8866. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8867. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8868. StaticCallOperand(result,procedure);
  8869. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8870. ReleaseOperand(result);
  8871. END;
  8872. RestoreRegisters(saved);
  8873. END;
  8874. END AssignMathArray;
  8875. VAR modifyAssignmentCounter := 0: LONGINT;
  8876. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8877. VAR processor,mem,dst: IntermediateCode.Operand;
  8878. BEGIN
  8879. IF value.intValue = true.intValue THEN
  8880. INC(modifyAssignmentCounter);
  8881. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8882. modifyAssignmentsPC := section.pc;
  8883. ELSE
  8884. DEC(modifyAssignmentCounter);
  8885. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8886. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8887. END;
  8888. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8889. dst := NewRegisterOperand (addressType);
  8890. Emit(Mov(position,dst, processor));
  8891. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8892. Emit(Mov(position,mem, value));
  8893. ReleaseIntermediateOperand(dst);
  8894. END ModifyAssignments;
  8895. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8896. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8897. BEGIN
  8898. type := left.type.resolved;
  8899. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8900. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8901. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8902. IF procedureType.returnParameter = parameter THEN
  8903. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8904. END;
  8905. END;
  8906. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8907. END CopySize;
  8908. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8909. VAR
  8910. leftO, rightO: Operand;
  8911. mem, sizeOp: IntermediateCode.Operand;
  8912. leftType, rightType, componentType: SyntaxTree.Type;
  8913. size: LONGINT;
  8914. parameters: SyntaxTree.ExpressionList;
  8915. procedure: SyntaxTree.Procedure;
  8916. call: SyntaxTree.ProcedureCallDesignator;
  8917. designator: SyntaxTree.Designator;
  8918. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8919. VAR procedureType: SyntaxTree.ProcedureType;
  8920. BEGIN
  8921. IF SemanticChecker.ReturnedAsParameter(right.type) THEN
  8922. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8923. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8924. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8925. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8926. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8927. IF right.id = Global.Reshape THEN RETURN TRUE
  8928. END;
  8929. END;
  8930. END;
  8931. END;
  8932. RETURN FALSE
  8933. END CanPassAsResultParameter;
  8934. BEGIN
  8935. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8936. leftType := left.type.resolved; rightType:= right.type.resolved;
  8937. IF backend.cooperative & left.NeedsTrace() THEN
  8938. ModifyAssignments(true);
  8939. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8940. Designate(right, rightO);
  8941. Designate(left, leftO);
  8942. ASSERT(leftO.mode = ModeReference);
  8943. TransferToRegister(leftO.op, leftO.op);
  8944. TransferToRegister(rightO.op, rightO.op);
  8945. Emit(Push(position, leftO.op));
  8946. Emit(Push(position, rightO.op));
  8947. CallAssignMethod(leftO.op, rightO.op, left.type);
  8948. Emit(Pop(position, rightO.op));
  8949. Emit(Pop(position, leftO.op));
  8950. sizeOp := CopySize(left);
  8951. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8952. ReleaseOperand(leftO);
  8953. ReleaseOperand(rightO);
  8954. ELSE
  8955. Evaluate(right,rightO);
  8956. Designate(left,leftO);
  8957. ASSERT(leftO.mode = ModeReference);
  8958. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8959. (* copy procedure address first *)
  8960. MakeMemory(mem,leftO.op,addressType,0);
  8961. Emit(Mov(position,mem,rightO.op));
  8962. ReleaseIntermediateOperand(mem);
  8963. (* copy pointer address *)
  8964. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8965. CallAssignPointer(leftO.tag, rightO.tag);
  8966. ELSE
  8967. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8968. CallAssignPointer(leftO.op, rightO.op);
  8969. END;
  8970. ReleaseOperand(leftO);
  8971. ReleaseOperand(rightO);
  8972. END;
  8973. ModifyAssignments(false);
  8974. RETURN;
  8975. END;
  8976. IF CanPassAsResultParameter(right) THEN
  8977. procedureResultDesignator := left(SyntaxTree.Designator);
  8978. Expression(right);
  8979. procedureResultDesignator := NIL;
  8980. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8981. (* left <-- ALIAS OF right: zerocopy *)
  8982. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8983. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8984. designator.SetType(procedure.type);
  8985. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8986. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8987. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8988. END;
  8989. ELSIF leftType IS SyntaxTree.RangeType THEN
  8990. (* LHS is of array range type *)
  8991. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8992. Evaluate(right, rightO);
  8993. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8994. (* first *)
  8995. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8996. Emit(Mov(position,mem, rightO.op));
  8997. ReleaseIntermediateOperand(mem);
  8998. (* last *)
  8999. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9000. Emit(Mov(position,mem, rightO.tag));
  9001. ReleaseIntermediateOperand(mem);
  9002. (* step *)
  9003. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9004. Emit(Mov(position,mem, rightO.extra));
  9005. ReleaseIntermediateOperand(mem);
  9006. ReleaseOperand(rightO);
  9007. ReleaseOperand(leftO)
  9008. ELSIF leftType IS SyntaxTree.ComplexType THEN
  9009. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  9010. Evaluate(right, rightO);
  9011. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  9012. componentType := leftType(SyntaxTree.ComplexType).componentType;
  9013. (* real part *)
  9014. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  9015. Emit(Mov(position,mem, rightO.op));
  9016. ReleaseIntermediateOperand(mem);
  9017. (* imaginary part *)
  9018. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9019. Emit(Mov(position,mem, rightO.tag));
  9020. ReleaseIntermediateOperand(mem);
  9021. ReleaseOperand(rightO);
  9022. ReleaseOperand(leftO)
  9023. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9024. OR (leftType IS SyntaxTree.PortType) THEN
  9025. (* rightO := leftO;*)
  9026. Evaluate(right,rightO);
  9027. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9028. Designate(left,leftO);
  9029. IF leftO.mode = ModeReference THEN
  9030. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9031. destination := mem;
  9032. ELSE
  9033. destination := leftO.op;
  9034. END;
  9035. ReleaseOperand(leftO);
  9036. IF destination.mode # IntermediateCode.Undefined THEN
  9037. Emit(Mov(position,destination,rightO.op));
  9038. END;
  9039. ReleaseOperand(rightO);
  9040. ReleaseIntermediateOperand(mem);
  9041. IntermediateCode.InitOperand(destination);
  9042. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9043. Evaluate(right,rightO);
  9044. Designate(left,leftO);
  9045. MakeMemory(mem,leftO.op,addressType,0);
  9046. Emit(Mov(position,mem,rightO.op));
  9047. ReleaseIntermediateOperand(mem);
  9048. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9049. (* delegate *)
  9050. (*
  9051. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9052. *)
  9053. Emit(Mov(position,leftO.tag,rightO.tag));
  9054. END;
  9055. ReleaseOperand(leftO);
  9056. ReleaseOperand(rightO);
  9057. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9058. Designate(right,rightO);
  9059. Designate(left,leftO);
  9060. sizeOp := CopySize(left);
  9061. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9062. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9063. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9064. IF (rightType IS SyntaxTree.StringType) THEN
  9065. CopyString(left,right);
  9066. 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
  9067. Designate(right,rightO);
  9068. Designate(left,leftO);
  9069. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9070. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9071. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9072. ELSE
  9073. HALT(201)
  9074. END;
  9075. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9076. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9077. Designate(right,rightO);
  9078. Designate(left,leftO);
  9079. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9080. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9081. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9082. END;
  9083. AssignMathArray(left,right);
  9084. ELSE
  9085. HALT(200);
  9086. END;
  9087. END Assign;
  9088. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9089. BEGIN
  9090. IF Trace THEN TraceEnter("VisitAssignment") END;
  9091. Assign(x.left,x.right);
  9092. IF Trace THEN TraceExit("VisitAssignment") END;
  9093. END VisitAssignment;
  9094. PROCEDURE EmitCooperativeSwitch;
  9095. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9096. BEGIN
  9097. ASSERT (cooperativeSwitches);
  9098. pc := section.pc;
  9099. IF lastSwitchPC = section.pc THEN RETURN END;
  9100. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9101. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9102. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9103. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9104. INC(statCoopSwitch, section.pc - pc);
  9105. END EmitCooperativeSwitch;
  9106. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9107. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9108. BEGIN
  9109. p0 := communication.left; p1 := communication.right;
  9110. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9111. Evaluate(p0,s0);
  9112. Evaluate(p1,s1);
  9113. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9114. Emit(Push(position,s0.op));
  9115. Emit(Push(position,s1.op));
  9116. (*
  9117. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9118. *)
  9119. IF ~backend.cellsAreObjects THEN
  9120. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9121. END;
  9122. ReleaseOperand(s0);
  9123. ReleaseOperand(s1);
  9124. IF backend.cellsAreObjects THEN
  9125. CallThis(position,"ActiveCellsRuntime","Send",2);
  9126. ELSE
  9127. CallThis(position,ChannelModuleName,"Send",2);
  9128. END;
  9129. (* ----- RECEIVE ------*)
  9130. ELSE
  9131. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9132. tmp := p0; p0 := p1; p1 := tmp;
  9133. END;
  9134. Evaluate(p0,s0);
  9135. Emit(Push(position,s0.op));
  9136. Designate(p1,s1);
  9137. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9138. Emit(Push(position,s1.op));
  9139. (*
  9140. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9141. *)
  9142. IF ~backend.cellsAreObjects THEN
  9143. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9144. END;
  9145. ReleaseOperand(s0);
  9146. ReleaseOperand(s1);
  9147. IF backend.cellsAreObjects THEN
  9148. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9149. ELSE
  9150. CallThis(position,ChannelModuleName,"Receive",2)
  9151. END;
  9152. END;
  9153. END VisitCommunicationStatement;
  9154. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9155. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9156. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9157. VAR true, false: Label; condition, value: BOOLEAN;
  9158. BEGIN
  9159. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9160. IF condition THEN
  9161. true := NewLabel();
  9162. false := NewLabel();
  9163. Condition(if.condition,true,false);
  9164. SetLabel(true);
  9165. StatementSequence(if.statements);
  9166. BrL(end);
  9167. SetLabel(false);
  9168. ELSE
  9169. IF value THEN (* always true *)
  9170. escape := TRUE;
  9171. StatementSequence(if.statements);
  9172. (* no branch necessary -- rest skipped *)
  9173. END;
  9174. END;
  9175. END IfPart;
  9176. BEGIN
  9177. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9178. end := NewLabel();
  9179. elsifs := x.ElsifParts();
  9180. IfPart(x.ifPart);
  9181. FOR i := 0 TO elsifs-1 DO
  9182. IF ~escape THEN
  9183. elsif := x.GetElsifPart(i);
  9184. IfPart(elsif);
  9185. END;
  9186. END;
  9187. IF (x.elsePart # NIL) & ~escape THEN
  9188. StatementSequence(x.elsePart);
  9189. END;
  9190. SetLabel(end);
  9191. IF Trace THEN TraceExit("VisitIfStatement") END;
  9192. END VisitIfStatement;
  9193. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9194. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9195. BEGIN
  9196. (*IF x.variable.type.resolved = x.type.resolved THEN
  9197. (* always true, do nothing *)
  9198. ELSE*)
  9199. Designate(x.variable,res);
  9200. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9201. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9202. END;
  9203. trueL := NewLabel();
  9204. TypeTest(res.tag,x.type,trueL,falseL);
  9205. ReleaseOperand(res);
  9206. SetLabel(trueL);
  9207. StatementSequence(x.statements);
  9208. BrL(endL);
  9209. END WithPart;
  9210. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9211. VAR endL,falseL: Label;i: LONGINT;
  9212. BEGIN
  9213. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9214. endL := NewLabel();
  9215. FOR i := 0 TO x.WithParts()-1 DO
  9216. falseL := NewLabel();
  9217. WithPart(x.GetWithPart(i),falseL,endL);
  9218. SetLabel(falseL);
  9219. END;
  9220. IF x.elsePart = NIL THEN
  9221. IF ~isUnchecked THEN
  9222. EmitTrap(position,WithTrap);
  9223. END;
  9224. ELSE
  9225. StatementSequence(x.elsePart)
  9226. END;
  9227. SetLabel(endL);
  9228. IF Trace THEN TraceExit("VisitWithStatement") END;
  9229. END VisitWithStatement;
  9230. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9231. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9232. out,else: Label; label: Label;
  9233. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9234. symbol: SyntaxTree.Symbol;
  9235. BEGIN
  9236. (*! split case statement into if-elsif statements for large case label lists *)
  9237. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9238. size := x.max-x.min+1;
  9239. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9240. END;
  9241. Evaluate(x.variable,var);
  9242. ReuseCopy(tmp,var.op);
  9243. ReleaseIntermediateOperand(var.op);
  9244. var.op := tmp;
  9245. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9246. Convert(var.op,addressType);
  9247. size := x.max-x.min+1;
  9248. else := NewLabel();
  9249. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9250. (*
  9251. UniqueId(name,module.module,"case",caseId);
  9252. *)
  9253. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9254. Global.GetModuleSegmentedName(module.module, name);
  9255. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9256. symbol := SyntaxTree.NewSymbol(name[1]);
  9257. symbol.SetScope(moduleScope);
  9258. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9259. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9260. section.isCaseTable := TRUE;
  9261. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9262. ReuseCopy(res,var.op);
  9263. ReleaseOperand(var);
  9264. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9265. Emit(Add(position,res,res,jmp));
  9266. IntermediateCode.MakeMemory(res,addressType);
  9267. Emit(Br(position,res));
  9268. ReleaseIntermediateOperand(res);
  9269. out := NewLabel();
  9270. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9271. part := x.GetCasePart(i);
  9272. constant := part.firstConstant;
  9273. label := NewLabel();
  9274. SetLabel(label);
  9275. WHILE(constant # NIL) DO (* case labels for this case part *)
  9276. FOR j := constant.min TO constant.max DO
  9277. fixups[j-x.min] := label;
  9278. END;
  9279. constant := constant.next;
  9280. END;
  9281. StatementSequence(part.statements);
  9282. BrL(out);
  9283. END;
  9284. SetLabel(else);
  9285. FOR i := 0 TO size-1 DO
  9286. IF fixups[i] = NIL THEN
  9287. fixups[i] := else;
  9288. END;
  9289. END;
  9290. IF x.elsePart # NIL THEN
  9291. StatementSequence(x.elsePart);
  9292. ELSIF ~isUnchecked THEN
  9293. EmitTrap(position,CaseTrap);
  9294. END;
  9295. SetLabel(out);
  9296. FOR i := 0 TO size-1 DO
  9297. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9298. section.Emit(Data(position,op));
  9299. END;
  9300. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9301. END VisitCaseStatement;
  9302. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9303. VAR start: Label; true,false: Label;
  9304. BEGIN
  9305. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9306. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9307. start := NewLabel();
  9308. true := NewLabel();
  9309. false := NewLabel();
  9310. SetLabel(start);
  9311. Condition(x.condition,true,false);
  9312. SetLabel(true);
  9313. StatementSequence(x.statements);
  9314. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9315. BrL(start);
  9316. SetLabel(false);
  9317. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9318. END VisitWhileStatement;
  9319. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9320. VAR false,true: Label;
  9321. BEGIN
  9322. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9323. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9324. true := NewLabel();
  9325. false := NewLabel();
  9326. SetLabel(false);
  9327. StatementSequence(x.statements);
  9328. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9329. Condition(x.condition,true,false);
  9330. SetLabel(true);
  9331. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9332. END VisitRepeatStatement;
  9333. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9334. VAR
  9335. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9336. temporaryVariable: SyntaxTree.Variable;
  9337. temporaryVariableDesignator : SyntaxTree.Designator;
  9338. BEGIN
  9339. IF Trace THEN TraceEnter("VisitForStatement") END;
  9340. true := NewLabel();
  9341. false := NewLabel();
  9342. start := NewLabel();
  9343. Assign(x.variable,x.from);
  9344. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9345. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9346. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9347. Assign(temporaryVariableDesignator,x.to);
  9348. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9349. IF by > 0 THEN
  9350. cmp := Scanner.LessEqual
  9351. ELSE
  9352. cmp := Scanner.GreaterEqual
  9353. END;
  9354. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9355. binary.SetType(system.booleanType);
  9356. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9357. SetLabel(start);
  9358. Condition(binary,true,false);
  9359. SetLabel(true);
  9360. StatementSequence(x.statements);
  9361. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9362. binary.SetType(x.variable.type);
  9363. Assign(x.variable,binary);
  9364. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9365. BrL(start);
  9366. SetLabel(false);
  9367. IF Trace THEN TraceExit("VisitForStatement") END;
  9368. END VisitForStatement;
  9369. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9370. VAR prevLoop: Label;
  9371. BEGIN
  9372. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9373. prevLoop := currentLoop;
  9374. currentLoop := NewLabel();
  9375. StatementSequence(x.statements);
  9376. SetLabel(currentLoop);
  9377. currentLoop := prevLoop;
  9378. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9379. END VisitExitableBlock;
  9380. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9381. VAR prevLoop,start: Label;
  9382. BEGIN
  9383. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9384. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9385. start := NewLabel();
  9386. prevLoop := currentLoop;
  9387. SetLabel(start);
  9388. currentLoop := NewLabel();
  9389. StatementSequence(x.statements);
  9390. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9391. BrL(start);
  9392. SetLabel(currentLoop);
  9393. currentLoop := prevLoop;
  9394. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9395. END VisitLoopStatement;
  9396. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9397. VAR outer: SyntaxTree.Statement;
  9398. BEGIN
  9399. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9400. IF locked THEN (* r if we jump out of an exclusive block *)
  9401. outer := x.outer;
  9402. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9403. outer := outer.outer;
  9404. END;
  9405. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9406. Lock(FALSE);
  9407. END;
  9408. END;
  9409. BrL(currentLoop);
  9410. IF Trace THEN TraceExit("VisitExitStatement") END;
  9411. END VisitExitStatement;
  9412. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9413. VAR
  9414. expression, parameterDesignator: SyntaxTree.Expression;
  9415. type, componentType: SyntaxTree.Type;
  9416. res, right: Operand;
  9417. left, mem, reg: IntermediateCode.Operand;
  9418. parameter: SyntaxTree.Parameter;
  9419. procedure: SyntaxTree.Procedure;
  9420. procedureType: SyntaxTree.ProcedureType;
  9421. returnTypeOffset: LONGINT;
  9422. delegate: BOOLEAN;
  9423. map: SymbolMap;
  9424. cc, parametersSize: LONGINT;
  9425. BEGIN
  9426. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9427. expression := x.returnValue;
  9428. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9429. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9430. IF currentIsInline THEN
  9431. IF expression # NIL THEN
  9432. map := currentMapper.Get(NIL);
  9433. IF map # NIL THEN
  9434. Assign(map.to, expression);
  9435. ELSE
  9436. Evaluate(expression,res);
  9437. Emit(Return(position,res.op));
  9438. ReleaseOperand(res);
  9439. END;
  9440. END;
  9441. BrL(currentInlineExit);
  9442. RETURN;
  9443. END;
  9444. IF expression # NIL THEN
  9445. type := expression.type.resolved;
  9446. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9447. IF locked THEN Lock(FALSE) END;
  9448. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9449. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9450. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9451. (* return without structured return parameter *)
  9452. Evaluate(expression,res);
  9453. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9454. IF locked OR profile THEN
  9455. Emit(Push(position,res.op));
  9456. IF delegate THEN HALT(200) END;
  9457. ReleaseOperand(res);
  9458. IF locked THEN Lock(FALSE) END;
  9459. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9460. reg := NewRegisterOperand(res.op.type);
  9461. Emit(Pop(position,reg));
  9462. Emit(Return(position,reg));
  9463. ReleaseIntermediateOperand(reg);
  9464. ELSE
  9465. Emit(Return(position,res.op));
  9466. ReleaseOperand(res);
  9467. END;
  9468. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9469. THEN
  9470. (* return using structured return parameter *)
  9471. 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)
  9472. OR SemanticChecker.IsPointerType(type));
  9473. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9474. parameter :=procedureType.returnParameter;
  9475. ASSERT(parameter # NIL);
  9476. returnTypeOffset := parameter.offsetInBits;
  9477. (*
  9478. IF parameter# NIL THEN
  9479. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9480. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9481. ELSE
  9482. returnTypeOffset := system.offsetFirstParameter
  9483. END;
  9484. *)
  9485. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9486. IF type IS SyntaxTree.RangeType THEN
  9487. (* array range type *)
  9488. Evaluate(expression, right);
  9489. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9490. Emit(Mov(position,mem, right.op)); (* first *)
  9491. ReleaseIntermediateOperand(mem);
  9492. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9493. Emit(Mov(position,mem, right.tag)); (* last *)
  9494. ReleaseIntermediateOperand(mem);
  9495. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9496. Emit(Mov(position,mem, right.extra)); (* step *)
  9497. ReleaseIntermediateOperand(mem);
  9498. ReleaseOperand(right);
  9499. ELSIF type IS SyntaxTree.ComplexType THEN
  9500. Evaluate(expression, right);
  9501. componentType := type(SyntaxTree.ComplexType).componentType;
  9502. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9503. Emit(Mov(position,mem, right.op)); (* real part *)
  9504. ReleaseIntermediateOperand(mem);
  9505. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9506. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9507. ReleaseIntermediateOperand(mem);
  9508. ReleaseOperand(right);
  9509. ELSE (* covers cases: pointer / record / array *)
  9510. parameter := procedureType.returnParameter;
  9511. checker.SetCurrentScope(currentScope);
  9512. ASSERT(parameter # NIL);
  9513. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9514. Assign(parameterDesignator,expression);
  9515. END;
  9516. ReleaseIntermediateOperand(left);
  9517. IF locked THEN Lock(FALSE) END;
  9518. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9519. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9520. parameter := procedureType.returnParameter;
  9521. checker.SetCurrentScope(currentScope);
  9522. IF parameter = NIL THEN
  9523. Error(procedure.position, "structured return of parameter of procedure not found");
  9524. ELSE
  9525. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9526. Assign(parameterDesignator,expression);
  9527. END;
  9528. IF locked THEN Lock(FALSE) END;
  9529. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9530. ELSE
  9531. HALT(200);
  9532. END;
  9533. ELSE
  9534. IF locked THEN Lock(FALSE) END;
  9535. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9536. END;
  9537. IF backend.cooperative THEN
  9538. BrL(exitLabel);
  9539. ELSE
  9540. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9541. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9542. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9543. ELSE
  9544. parametersSize := 0;
  9545. END;
  9546. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9547. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9548. END;
  9549. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9550. END VisitReturnStatement;
  9551. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9552. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9553. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9554. statements: SyntaxTree.StatementSequence;
  9555. name, suffix: SyntaxTree.IdentifierString;
  9556. BEGIN
  9557. Strings.IntToStr(awaitProcCounter,suffix);
  9558. Strings.Concat("@AwaitProcedure",suffix,name);
  9559. identifier := SyntaxTree.NewIdentifier(name);
  9560. INC(awaitProcCounter);
  9561. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9562. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9563. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9564. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9565. procedure.SetAccess(SyntaxTree.Hidden);
  9566. procedure.SetScope(currentScope);
  9567. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9568. procedureType.SetReturnType(system.booleanType);
  9569. procedure.SetType(procedureType);
  9570. body := SyntaxTree.NewBody(x.position,procedureScope);
  9571. procedureScope.SetBody(body);
  9572. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9573. returnStatement.SetReturnValue(x.condition);
  9574. statements := SyntaxTree.NewStatementSequence();
  9575. statements.AddStatement(returnStatement);
  9576. body.SetStatementSequence(statements);
  9577. currentScope.AddProcedure(procedure);
  9578. RETURN procedure
  9579. END MakeAwaitProcedure;
  9580. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9581. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9582. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9583. BEGIN
  9584. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9585. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9586. IF backend.cooperative THEN
  9587. start := NewLabel();
  9588. true := NewLabel();
  9589. false := NewLabel();
  9590. SetLabel(start);
  9591. Condition(x.condition,true,false);
  9592. SetLabel(false);
  9593. PushSelfPointer();
  9594. CallThis(position,"ExclusiveBlocks","Await",1);
  9595. BrL(start);
  9596. SetLabel(true);
  9597. PushSelfPointer();
  9598. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9599. ELSE
  9600. proc := MakeAwaitProcedure(x);
  9601. Emit(Push(position,fp));
  9602. GetCodeSectionNameForSymbol(proc,name);
  9603. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9604. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9605. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9606. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9607. Emit(Result(position,res));
  9608. (*
  9609. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9610. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9611. *)
  9612. InitOperand(result,ModeValue);
  9613. result.op := res;
  9614. label := NewLabel();
  9615. BreqL(label, result.op, SELF.true);
  9616. ReleaseOperand(result);
  9617. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9618. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9619. Emit(Push(position,res));
  9620. Emit(Push(position,fp));
  9621. PushSelfPointer();
  9622. Emit(Push(position,nil));
  9623. CallThis(position,"Objects","Await",4);
  9624. SetLabel(label);
  9625. END;
  9626. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9627. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9628. END VisitAwaitStatement;
  9629. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9630. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9631. BEGIN
  9632. FOR i := 0 TO x.Length() - 1 DO
  9633. statement := x.GetStatement( i );
  9634. Statement(statement);
  9635. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9636. END;
  9637. END StatementSequence;
  9638. PROCEDURE PushSelfPointer;
  9639. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9640. procedureType: SyntaxTree.ProcedureType;
  9641. BEGIN
  9642. scope := currentScope;
  9643. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9644. scope := scope.outerScope;
  9645. END;
  9646. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9647. IF ~newObjectFile THEN
  9648. Symbol(moduleSelf,op);
  9649. IntermediateCode.MakeMemory(op.op,addressType);
  9650. ELSE
  9651. moduleSection := meta.ModuleSection();
  9652. IF backend.cooperative THEN
  9653. moduleOffset := 0;
  9654. ELSE
  9655. moduleOffset := moduleSection.pc;
  9656. END;
  9657. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9658. END;
  9659. ELSE
  9660. GetBaseRegister(op.op,currentScope,scope);
  9661. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9662. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9663. parametersSize := ProcedureParametersSize(system,procedure);
  9664. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9665. IF backend.cooperative THEN
  9666. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9667. END;
  9668. IntermediateCode.MakeMemory(op.op,addressType);
  9669. END;
  9670. Emit(Push(position,op.op));
  9671. ReleaseOperand(op);
  9672. END PushSelfPointer;
  9673. PROCEDURE Lock(lock: BOOLEAN);
  9674. BEGIN
  9675. IF Trace THEN TraceEnter("Lock") END;
  9676. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9677. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9678. ASSERT(modifyAssignmentCounter = 0);
  9679. IF dump # NIL THEN
  9680. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9681. dump.Ln;dump.Update;
  9682. END;
  9683. PushSelfPointer;
  9684. IF backend.cooperative THEN
  9685. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9686. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9687. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9688. END;
  9689. ELSE
  9690. Emit(Push(position,true));
  9691. IF lock THEN CallThis(position,"Objects","Lock",2)
  9692. ELSE CallThis(position,"Objects","Unlock",2);
  9693. END;
  9694. END;
  9695. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9696. IF Trace THEN TraceExit("Lock") END;
  9697. END Lock;
  9698. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9699. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9700. BEGIN
  9701. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9702. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9703. previouslyUnchecked := isUnchecked;
  9704. isUnchecked := isUnchecked OR x.isUnchecked;
  9705. previouslyCooperativeSwitches := cooperativeSwitches;
  9706. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9707. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9708. IF x.statements # NIL THEN
  9709. StatementSequence(x.statements);
  9710. END;
  9711. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9712. isUnchecked := previouslyUnchecked;
  9713. cooperativeSwitches := previouslyCooperativeSwitches;
  9714. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9715. END VisitStatementBlock;
  9716. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9717. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9718. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9719. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9720. procedure: SyntaxTree.Procedure;
  9721. map: SymbolMap;
  9722. cc, parametersSize: LONGINT;
  9723. BEGIN
  9724. scope := currentScope;
  9725. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9726. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9727. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9728. return := emptyOperand;
  9729. IF Trace THEN TraceEnter("VisitCode") END;
  9730. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9731. NEW(in, x.inRules.Length());
  9732. FOR i := 0 TO LEN(in)-1 DO
  9733. statement := x.inRules.GetStatement(i);
  9734. WITH statement: SyntaxTree.Assignment DO
  9735. Evaluate(statement.right, operand);
  9736. result := operand.op;
  9737. NEW(str, 64);
  9738. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9739. in[i] := result; IntermediateCode.SetString(in[i], str);
  9740. ReleaseIntermediateOperand(operand.tag);
  9741. END;
  9742. END;
  9743. ELSE in := NIL
  9744. END;
  9745. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9746. NEW(out, x.outRules.Length());
  9747. FOR i := 0 TO LEN(out)-1 DO
  9748. statement := x.outRules.GetStatement(i);
  9749. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9750. WITH statement: SyntaxTree.Assignment DO
  9751. Designate(statement.left, operand);
  9752. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9753. NEW(str, 64);
  9754. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9755. out[i] := result; IntermediateCode.SetString(out[i], str);
  9756. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9757. |statement: SyntaxTree.ReturnStatement DO
  9758. NEW(str, 64);
  9759. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9760. IF currentIsInline THEN
  9761. map := currentMapper.Get(NIL);
  9762. Designate(map.to, operand);
  9763. IF map.isAddress THEN
  9764. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9765. ELSE
  9766. result := operand.op;
  9767. END;
  9768. (*! only if it does not fit into register
  9769. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9770. *)
  9771. (*Evaluate(map.to, operand);*)
  9772. out[i] := result;
  9773. ELSE
  9774. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9775. END;
  9776. IntermediateCode.SetString(out[i], str);
  9777. ReleaseIntermediateOperand(operand.tag);
  9778. return := out[i];
  9779. ELSE
  9780. END;
  9781. END;
  9782. ELSE out := NIL
  9783. END;
  9784. Emit(Asm(x.position,x.sourceCode, in, out));
  9785. IF in # NIL THEN
  9786. FOR i := 0 TO LEN(in)-1 DO
  9787. ReleaseIntermediateOperand(in[i]);
  9788. END;
  9789. END;
  9790. IF out # NIL THEN
  9791. FOR i := 0 TO LEN(out)-1 DO
  9792. WITH statement: SyntaxTree.Assignment DO
  9793. ReleaseIntermediateOperand(out[i]);
  9794. |statement: SyntaxTree.ReturnStatement DO
  9795. (* release happens below *)
  9796. ELSE
  9797. END;
  9798. statement := x.outRules.GetStatement(i);
  9799. END;
  9800. END;
  9801. IF return.mode # IntermediateCode.Undefined THEN
  9802. IF currentIsInline THEN
  9803. ELSIF SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  9804. Symbol(procedureType.returnParameter, par);
  9805. MakeMemory(mem, par.op, return.type, 0);
  9806. ReleaseOperand(par);
  9807. Emit(Mov(position, mem, return));
  9808. ReleaseIntermediateOperand(mem);
  9809. ELSE
  9810. Emit(Return(position,return));
  9811. END;
  9812. ReleaseIntermediateOperand(return);
  9813. IF currentIsInline THEN RETURN END;
  9814. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9815. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9816. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9817. ELSE
  9818. parametersSize := 0;
  9819. END;
  9820. EmitLeave(section, position,NIL, cc);
  9821. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9822. END;
  9823. IF Trace THEN TraceExit("VisitCode") END;
  9824. END VisitCode;
  9825. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9826. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9827. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9828. const, call: IntermediateCode.Operand;
  9829. parameterDesignator: SyntaxTree.Expression;
  9830. saved: RegisterEntry;
  9831. name: Basic.SegmentedName;
  9832. BEGIN
  9833. IF Trace THEN TraceEnter("ParameterCopies") END;
  9834. parameter := x.firstParameter;
  9835. WHILE parameter # NIL DO
  9836. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9837. type := parameter.type.resolved;
  9838. IF IsOpenArray(type) THEN
  9839. VisitParameter(parameter);
  9840. op := result;
  9841. IF backend.cooperative & parameter.NeedsTrace() THEN
  9842. length := GetArrayLength(type, op.tag);
  9843. size := NewRegisterOperand(addressType);
  9844. base := ArrayBaseType(type);
  9845. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9846. Emit(Mul(position, size, length, const));
  9847. dst := NewRegisterOperand (addressType);
  9848. Emit(Mov(position, dst, size));
  9849. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9850. Emit(Sub(position,dst,sp,dst));
  9851. Emit(And(position,dst,dst,const));
  9852. Emit(Mov(position,sp,dst));
  9853. par := fp;
  9854. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9855. IntermediateCode.InitImmediate(null, byteType, 0);
  9856. Emit(Fill(position, dst, size, null));
  9857. ReleaseIntermediateOperand(dst);
  9858. ReleaseIntermediateOperand(length);
  9859. SaveRegisters();ReleaseUsedRegisters(saved);
  9860. (* register dst has been freed before SaveRegisters already *)
  9861. base := ArrayBaseType(type);
  9862. (* assign method of open array *)
  9863. IF base.IsRecordType() THEN
  9864. Emit (Push(position, length));
  9865. Emit (Push(position, dst));
  9866. Emit (Push(position, op.op));
  9867. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9868. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9869. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9870. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9871. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9872. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9873. Emit (Push(position,length));
  9874. Emit (Push(position, dst));
  9875. Emit (Push(position, length));
  9876. Emit (Push(position, op.op));
  9877. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9878. ELSE
  9879. Emit (Push(position, length));
  9880. Emit (Push(position, dst));
  9881. Emit (Push(position, length));
  9882. Emit (Push(position, op.op));
  9883. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9884. ASSERT(ArrayBaseType(type).IsPointer());
  9885. END;
  9886. RestoreRegisters(saved);
  9887. ELSE
  9888. temp := GetDynamicSize(type,op.tag);
  9889. ReuseCopy(size,temp);
  9890. ReleaseIntermediateOperand(temp);
  9891. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9892. Emit(Sub(position,size,sp,size));
  9893. Emit(And(position,size,size,const));
  9894. Emit(Mov(position,sp,size));
  9895. par := fp;
  9896. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9897. ReleaseIntermediateOperand(size);
  9898. size := GetDynamicSize(type,op.tag);
  9899. END;
  9900. Emit(Copy(position,sp,op.op,size));
  9901. ReleaseIntermediateOperand(size);
  9902. ReleaseOperand(op);
  9903. IntermediateCode.MakeMemory(par,addressType);
  9904. Emit(Mov(position,par,sp));
  9905. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9906. checker.SetCurrentScope(currentScope);
  9907. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9908. Assign(parameterDesignator,parameterDesignator);
  9909. END;
  9910. END;
  9911. parameter := parameter.nextParameter;
  9912. END;
  9913. IF Trace THEN TraceExit("ParameterCopies") END;
  9914. END ParameterCopies;
  9915. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9916. VAR x: SyntaxTree.Variable;
  9917. BEGIN
  9918. x := scope.firstVariable;
  9919. WHILE x # NIL DO
  9920. InitVariable(x);
  9921. x := x.nextVariable;
  9922. END;
  9923. END InitVariables;
  9924. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9925. BEGIN
  9926. IF (symbol # NIL) THEN
  9927. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9928. ELSE
  9929. RETURN 0
  9930. END;
  9931. END GetFingerprint;
  9932. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9933. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9934. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9935. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9936. name: Basic.SegmentedName;
  9937. offset: LONGINT;
  9938. BEGIN
  9939. IF Trace THEN TraceEnter("Body") END;
  9940. ReleaseUsedRegisters(saved); (* just in case ... *)
  9941. section := ir;
  9942. exitLabel := NewLabel ();
  9943. IF moduleBody THEN moduleBodySection := section END;
  9944. IF ir.comments # NIL THEN
  9945. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9946. commentPrintout.SingleStatement(TRUE);
  9947. dump := ir.comments;
  9948. ELSE
  9949. commentPrintout := NIL;
  9950. dump := NIL;
  9951. END;
  9952. prevScope := currentScope;
  9953. currentScope := scope;
  9954. lastSwitchPC := 0;
  9955. cooperativeSwitches := backend.cooperative;
  9956. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9957. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9958. IF x # NIL THEN
  9959. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9960. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9961. IF moduleBody THEN
  9962. ProfilerInit();
  9963. ELSE
  9964. Basic.SegmentedNameToString(ir.name, string);
  9965. ProfilerAddProcedure(numberProcedures,string);
  9966. ProfilerEnterExit(numberProcedures,TRUE);
  9967. END;
  9968. END;
  9969. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9970. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9971. END;
  9972. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9973. IF moduleBody & ~newObjectFile THEN
  9974. InitVariables(moduleScope)
  9975. END;
  9976. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9977. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9978. IF procedure = cellScope.bodyProcedure THEN
  9979. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9980. StaticCallOperand(op, cellScope.constructor);
  9981. Emit(Call(position,op.op,0));
  9982. END;
  9983. END;
  9984. END;
  9985. ParameterCopies(procedureType);
  9986. InitVariables(scope);
  9987. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9988. GetCodeSectionNameForSymbol(procedure, name);
  9989. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9990. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9991. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9992. IntermediateCode.SetOffset(right,offset); (* tag *)
  9993. IntermediateCode.InitMemory(left,addressType,fp,0);
  9994. Emit(Mov(position, left, right));
  9995. END;
  9996. IF x.code = NIL THEN
  9997. VisitStatementBlock(x);
  9998. ELSE
  9999. VisitCode(x.code)
  10000. END;
  10001. IF x.finally # NIL THEN (*! mark finally block for object file *)
  10002. ir.SetFinally(ir.pc);
  10003. StatementSequence(x.finally)
  10004. END;
  10005. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  10006. IF ~backend.cooperative THEN
  10007. ProfilerEnterExit(numberProcedures,FALSE);
  10008. END;
  10009. INC(numberProcedures);
  10010. END;
  10011. END;
  10012. IF backend.cooperative THEN
  10013. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  10014. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  10015. END;
  10016. IF x # NIL THEN
  10017. SELF.position := x.position;
  10018. END;
  10019. CheckRegistersFree();
  10020. ASSERT(modifyAssignmentCounter = 0);
  10021. currentScope := prevScope;
  10022. IF Trace THEN TraceExit("Body") END;
  10023. END Body;
  10024. END ImplementationVisitor;
  10025. MetaDataGenerator=OBJECT
  10026. VAR
  10027. implementationVisitor: ImplementationVisitor;
  10028. declarationVisitor: DeclarationVisitor;
  10029. module: Sections.Module;
  10030. moduleName: ARRAY 128 OF CHAR;
  10031. moduleNamePool: Basic.HashTableInt;
  10032. moduleNamePoolSection: IntermediateCode.Section;
  10033. modulePointerSection: IntermediateCode.Section;
  10034. modulePointerSizePC: LONGINT;
  10035. modulePointerSectionOffset: LONGINT;
  10036. modulePointers: LONGINT;
  10037. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10038. RecordBaseOffset: LONGINT;
  10039. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10040. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10041. TypeTags: LONGINT; (* type extension level support *)
  10042. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10043. patchInfoPC: LONGINT;
  10044. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10045. BEGIN
  10046. IF implementationVisitor.backend.cooperative THEN
  10047. TypeTags := MAX(LONGINT);
  10048. BaseTypesTableOffset := 0;
  10049. MethodTableOffset := 2;
  10050. TypeRecordBaseOffset := 0;
  10051. RecordBaseOffset := 0;
  10052. ELSIF simple THEN
  10053. TypeTags := 3; (* only 3 extensions allowed *)
  10054. BaseTypesTableOffset := 1;
  10055. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10056. TypeRecordBaseOffset := 0;
  10057. RecordBaseOffset := 1;
  10058. ELSE
  10059. TypeTags := 16;
  10060. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10061. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10062. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10063. RecordBaseOffset := 8;
  10064. END;
  10065. SELF.simple := simple;
  10066. SELF.implementationVisitor := implementationVisitor;
  10067. SELF.declarationVisitor := declarationVisitor;
  10068. implementationVisitor.meta := SELF;
  10069. declarationVisitor.meta := SELF;
  10070. END InitMetaDataGenerator;
  10071. PROCEDURE SetModule(module: Sections.Module);
  10072. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10073. BEGIN
  10074. SELF.module := module;
  10075. Global.GetModuleName(module.module,moduleName);
  10076. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10077. NEW(moduleNamePool, 32);
  10078. (*! require GC protection *)
  10079. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10080. name := "Heaps.AnyPtr";
  10081. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10082. (* set base pointer *)
  10083. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10084. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10085. modulePointers := 0;
  10086. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10087. AddPointer(moduleNamePoolSection, namePoolOffset);
  10088. END;
  10089. END SetModule;
  10090. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10091. BEGIN
  10092. IF ~implementationVisitor.backend.cooperative THEN
  10093. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10094. INC(modulePointers);
  10095. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10096. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10097. END;
  10098. END AddPointer;
  10099. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10100. BEGIN
  10101. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10102. END GetTypeRecordBaseOffset;
  10103. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10104. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10105. BEGIN
  10106. INC(dataAdrOffset,6);
  10107. Info(section,"headerAdr");
  10108. Address(section,0);
  10109. Info(section,"typeDesc");
  10110. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10111. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10112. NamedSymbol(section, name, symbol, 0, offset);
  10113. Info(section,"mark: LONGINT;");
  10114. Longint(section,-1);
  10115. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10116. Info(section,"dataAdr-: ADDRESS");
  10117. Symbol(section,section, dataAdrOffset,0);
  10118. Info(section,"size-: SIZE");
  10119. Address(section,0);
  10120. Info(section,"nextRealtime: HeapBlock;");
  10121. Address(section,0);
  10122. END HeapBlock;
  10123. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10124. VAR i: LONGINT;
  10125. BEGIN
  10126. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10127. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10128. Info(section,"count*: LONGINT");
  10129. Longint(section,0);
  10130. Info(section,"locked*: BOOLEAN");
  10131. Longint(section,0);
  10132. Info(section,"awaitingLock*: ProcessQueue");
  10133. Address(section,0);
  10134. Address(section,0);
  10135. Info(section,"awaitingCond*: ProcessQueue");
  10136. Address(section,0);
  10137. Address(section,0);
  10138. Info(section,"lockedBy*: ANY");
  10139. Address(section,0);
  10140. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10141. Longint(section,1);
  10142. FOR i := 2 TO 6 DO
  10143. Longint(section,0);
  10144. END;
  10145. Info(section,"lock*: ANY");
  10146. Address(section,0);
  10147. END ProtectedHeapBlock;
  10148. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10149. BEGIN
  10150. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10151. END Info;
  10152. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10153. VAR op: IntermediateCode.Operand;
  10154. BEGIN
  10155. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10156. section.Emit(Data(Basic.invalidPosition,op));
  10157. END Address;
  10158. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10159. VAR op: IntermediateCode.Operand;
  10160. BEGIN
  10161. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10162. section.Emit(Data(Basic.invalidPosition,op));
  10163. END Size;
  10164. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10165. VAR op: IntermediateCode.Operand;
  10166. BEGIN
  10167. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10168. section.Emit(Data(Basic.invalidPosition,op));
  10169. END Set;
  10170. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10171. VAR op: IntermediateCode.Operand;
  10172. BEGIN
  10173. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10174. section.Emit(Data(Basic.invalidPosition,op));
  10175. END Longint;
  10176. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10177. VAR op,noOperand: IntermediateCode.Operand;
  10178. BEGIN
  10179. IntermediateCode.InitOperand(noOperand);
  10180. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10181. section.PatchOperands(pc,op,noOperand,noOperand);
  10182. END PatchAddress;
  10183. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10184. VAR op,noOperand: IntermediateCode.Operand;
  10185. BEGIN
  10186. IntermediateCode.InitOperand(noOperand);
  10187. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10188. section.PatchOperands(pc,op,noOperand,noOperand);
  10189. END PatchSize;
  10190. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10191. VAR op,noOperand: IntermediateCode.Operand;
  10192. BEGIN
  10193. IntermediateCode.InitOperand(noOperand);
  10194. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10195. section.PatchOperands(pc,op,noOperand,noOperand);
  10196. END PatchLongint;
  10197. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10198. VAR op, noOperand: IntermediateCode.Operand;
  10199. BEGIN
  10200. IntermediateCode.InitOperand(noOperand);
  10201. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10202. section.PatchOperands(pc,op,noOperand,noOperand);
  10203. END PatchSymbol;
  10204. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10205. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10206. BEGIN
  10207. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10208. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10209. section.Emit(Data(Basic.invalidPosition,op));
  10210. END Boolean;
  10211. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10212. VAR op: IntermediateCode.Operand;
  10213. BEGIN
  10214. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10215. section.Emit(Data(Basic.invalidPosition,op));
  10216. END Char;
  10217. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10218. VAR op: IntermediateCode.Operand;
  10219. BEGIN
  10220. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10221. section.Emit(Data(Basic.invalidPosition,op));
  10222. END Integer;
  10223. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10224. VAR i: LONGINT;
  10225. BEGIN
  10226. Info(section,str);
  10227. i := 0;
  10228. WHILE(str[i] # 0X) DO
  10229. Char(section,str[i]);
  10230. INC(i);
  10231. END;
  10232. Char(section,0X);
  10233. END String;
  10234. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10235. VAR s: Basic.SectionName;
  10236. BEGIN
  10237. StringPool.GetString(str, s);
  10238. String(section, s);
  10239. END String0;
  10240. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10241. VAR op: IntermediateCode.Operand;
  10242. BEGIN
  10243. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10244. IntermediateCode.SetOffset(op,realOffset);
  10245. section.Emit(Data(Basic.invalidPosition,op));
  10246. END NamedSymbol;
  10247. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10248. VAR op: IntermediateCode.Operand;
  10249. BEGIN
  10250. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10251. IntermediateCode.SetOffset(op,realOffset);
  10252. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10253. END NamedSymbolAt;
  10254. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10255. BEGIN
  10256. IF symbol= NIL THEN
  10257. Address( section, realOffset);
  10258. ASSERT(virtualOffset = 0);
  10259. ELSE
  10260. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10261. END;
  10262. END Symbol;
  10263. (* OutPointers delivers
  10264. {pointerOffset}
  10265. *)
  10266. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10267. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10268. BEGIN
  10269. type := type.resolved;
  10270. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10271. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10272. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10273. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10274. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10275. ELSIF type IS SyntaxTree.PointerType THEN
  10276. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10277. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10278. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10279. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10280. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10281. ELSIF (type IS SyntaxTree.RecordType) THEN
  10282. (* never treat a record like a pointer, even if the pointer field is set! *)
  10283. WITH type: SyntaxTree.RecordType DO
  10284. base := type.GetBaseRecord();
  10285. IF base # NIL THEN
  10286. Pointers(offset,symbol,section, base,numberPointers);
  10287. END;
  10288. variable := type.recordScope.firstVariable;
  10289. WHILE(variable # NIL) DO
  10290. IF ~(variable.untraced) THEN
  10291. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10292. END;
  10293. variable := variable.nextVariable;
  10294. END;
  10295. END;
  10296. ELSIF (type IS SyntaxTree.CellType) THEN
  10297. WITH type: SyntaxTree.CellType DO
  10298. base := type.GetBaseRecord();
  10299. IF base # NIL THEN
  10300. Pointers(offset,symbol,section, base,numberPointers);
  10301. END;
  10302. variable := type.cellScope.firstVariable;
  10303. WHILE(variable # NIL) DO
  10304. IF ~(variable.untraced) THEN
  10305. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10306. END;
  10307. variable := variable.nextVariable;
  10308. END;
  10309. property := type.firstProperty;
  10310. WHILE(property # NIL) DO
  10311. IF ~(property.untraced) THEN
  10312. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10313. END;
  10314. property := property.nextProperty;
  10315. END;
  10316. parameter := type.firstParameter;
  10317. WHILE(parameter # NIL) DO
  10318. IF ~(parameter.untraced) THEN
  10319. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10320. END;
  10321. parameter := parameter.nextParameter;
  10322. END;
  10323. END;
  10324. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10325. WITH type: SyntaxTree.ArrayType DO
  10326. IF type.form= SyntaxTree.Static THEN
  10327. n := type.staticLength;
  10328. base := type.arrayBase.resolved;
  10329. WHILE(base IS SyntaxTree.ArrayType) DO
  10330. type := base(SyntaxTree.ArrayType);
  10331. n := n* type.staticLength;
  10332. base := type.arrayBase.resolved;
  10333. END;
  10334. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10335. IF SemanticChecker.ContainsPointer(base) THEN
  10336. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10337. FOR i := 0 TO n-1 DO
  10338. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10339. END;
  10340. END;
  10341. ELSE
  10342. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10343. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10344. END;
  10345. END;
  10346. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10347. WITH type: SyntaxTree.MathArrayType DO
  10348. IF type.form = SyntaxTree.Static THEN
  10349. n := type.staticLength;
  10350. base := type.arrayBase.resolved;
  10351. WHILE(base IS SyntaxTree.MathArrayType) DO
  10352. type := base(SyntaxTree.MathArrayType);
  10353. n := n* type.staticLength;
  10354. base := type.arrayBase.resolved;
  10355. END;
  10356. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10357. IF SemanticChecker.ContainsPointer(base) THEN
  10358. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10359. FOR i := 0 TO n-1 DO
  10360. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10361. END;
  10362. END;
  10363. ELSE
  10364. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10365. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10366. END
  10367. END;
  10368. (* ELSE no pointers in type *)
  10369. END;
  10370. END Pointers;
  10371. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10372. VAR position,i: LONGINT; ch: CHAR;
  10373. BEGIN
  10374. IF pool.Has(index) THEN
  10375. RETURN pool.GetInt(index)
  10376. ELSE
  10377. position := source.pc;
  10378. pool.PutInt(index, position);
  10379. Info(source, name);
  10380. i := 0;
  10381. REPEAT
  10382. ch := name[i]; INC(i);
  10383. Char( source, ch);
  10384. UNTIL ch = 0X;
  10385. END;
  10386. RETURN position;
  10387. END EnterDynamicName;
  10388. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10389. VAR name: Basic.SectionName; position: LONGINT;
  10390. BEGIN
  10391. IF pool.Has(index) THEN
  10392. RETURN pool.GetInt(index)
  10393. ELSE
  10394. StringPool.GetString(index, name);
  10395. position := EnterDynamicName(source,name,index, pool);
  10396. END;
  10397. RETURN position;
  10398. END DynamicName;
  10399. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10400. VAR section: IntermediateCode.Section;
  10401. BEGIN
  10402. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10403. IF implementationVisitor.backend.cooperative THEN
  10404. Info(section, "TypeDescriptor");
  10405. Basic.ToSegmentedName("BaseTypes.Array", name);
  10406. NamedSymbol(section, name,NIL, 0, 0);
  10407. BasePointer(section);
  10408. offset := 0;
  10409. ELSE
  10410. HeapBlock(mName,typeName,section,2);
  10411. Info(section, "HeapBlock");
  10412. (*
  10413. Symbol(section,section,2,0);
  10414. *)
  10415. Address(section,0); (* empty such that GC does not go on traversing *)
  10416. Info(section, "TypeDescriptor");
  10417. Address(section,0);
  10418. offset := section.pc;
  10419. END;
  10420. RETURN section
  10421. END NamedBlock;
  10422. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10423. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10424. BEGIN
  10425. COPY(moduleName,name);
  10426. Strings.Append(name,suffix);
  10427. Basic.ToSegmentedName(name, pooledName);
  10428. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10429. END Block;
  10430. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10431. VAR name: Basic.SegmentedName;
  10432. BEGIN
  10433. Info(source,"ArrayHeader");
  10434. IF implementationVisitor.backend.cooperative THEN
  10435. sizePC := source.pc;
  10436. Address(source,0);
  10437. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10438. IF baseType # "" THEN
  10439. Basic.ToSegmentedName(baseType, name);
  10440. NamedSymbol(source, name,NIL, 0, 0);
  10441. ELSE
  10442. Address(source,0);
  10443. END;
  10444. Address(source,0);
  10445. ELSE
  10446. Address(source,0);
  10447. Address(source,0);
  10448. (* first pointer for GC *)
  10449. IF hasPointer THEN
  10450. (* points to first element in the array, this is NOT the base type descriptor *)
  10451. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10452. ELSE
  10453. Address(source,0);
  10454. END;
  10455. sizePC := source.pc;
  10456. Address(source,0);
  10457. Info(source,"array data");
  10458. END;
  10459. END ArrayBlock;
  10460. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10461. BEGIN
  10462. IF implementationVisitor.backend.cooperative THEN
  10463. PatchSize(section, pc, size);
  10464. PatchSize(section, pc + 3, size);
  10465. ELSE
  10466. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10467. PatchSize(section, pc, size);
  10468. END;
  10469. END PatchArray;
  10470. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10471. VAR
  10472. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10473. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10474. poolMap: Basic.HashTableInt;
  10475. namePool: IntermediateCode.Section;
  10476. namePoolOffset: LONGINT;
  10477. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10478. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10479. BEGIN
  10480. Basic.SegmentedNameToString(s1.name,n1);
  10481. Basic.SegmentedNameToString(s2.name,n2);
  10482. index := 0;
  10483. ch1 := n1[index];
  10484. ch2 := n2[index];
  10485. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10486. INC(index);
  10487. ch1 := n1[index];
  10488. ch2 := n2[index];
  10489. END;
  10490. RETURN ch1 < ch2;
  10491. END Compare;
  10492. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10493. VAR
  10494. i, j: LONGINT;
  10495. x, t: Sections.Section;
  10496. BEGIN
  10497. IF lo < hi THEN
  10498. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10499. WHILE i <= j DO
  10500. WHILE Compare(list[i], x) DO INC(i) END;
  10501. WHILE Compare(x, list[j]) DO DEC(j) END;
  10502. IF i <= j THEN
  10503. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10504. INC(i); DEC(j)
  10505. END
  10506. END;
  10507. IF lo < j THEN QuickSort(list, lo, j) END;
  10508. IF i < hi THEN QuickSort(list, i, hi) END
  10509. END;
  10510. END QuickSort;
  10511. (*
  10512. ExportDesc* = RECORD
  10513. fp*: ADDRESS;
  10514. name* {UNTRACED}: DynamicName;
  10515. adr*: ADDRESS;
  10516. exports*: LONGINT;
  10517. dsc* {UNTRACED}: ExportArray
  10518. END;
  10519. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10520. *)
  10521. PROCEDURE ExportDesc2(
  10522. source: IntermediateCode.Section;
  10523. namePool: IntermediateCode.Section;
  10524. fingerPrinter: FingerPrinter.FingerPrinter;
  10525. symbol: Sections.Section;
  10526. name: StringPool.Index;
  10527. VAR patchAdr: LONGINT
  10528. ): BOOLEAN;
  10529. VAR fingerPrint: SyntaxTree.FingerPrint;
  10530. BEGIN
  10531. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10532. & (symbol.type # Sections.InlineCodeSection)
  10533. THEN
  10534. *)
  10535. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10536. & (symbol.type # Sections.InlineCodeSection))
  10537. THEN
  10538. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10539. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10540. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10541. Longint(source,fingerPrint.shallow);
  10542. ELSE
  10543. Longint(source, 0);
  10544. END;
  10545. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10546. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10547. Symbol(source, symbol,0,0);
  10548. patchAdr := source.pc;
  10549. Longint(source, 0);
  10550. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10551. Address(source,0);
  10552. END;
  10553. RETURN TRUE
  10554. ELSE
  10555. RETURN FALSE
  10556. END;
  10557. END ExportDesc2;
  10558. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10559. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10560. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10561. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10562. nextPatch: LONGINT;
  10563. TYPE
  10564. Scope = RECORD
  10565. elements: LONGINT;
  10566. gelements: LONGINT;
  10567. section: IntermediateCode.Section;
  10568. patchAdr: LONGINT;
  10569. arraySizePC: LONGINT;
  10570. beginPC: LONGINT; (* current scope start pc *)
  10571. END;
  10572. BEGIN
  10573. Basic.InitSegmentedName(prev);
  10574. olevel := -1;
  10575. scopes[0].section := source;
  10576. scopes[0].arraySizePC := MIN(LONGINT);
  10577. FOR s := 0 TO LEN(sections)-1 DO
  10578. symbol := sections[s];
  10579. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10580. this := sections[s].name;
  10581. level := 0;
  10582. WHILE (this[level] > 0) DO
  10583. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10584. INC(level);
  10585. END;
  10586. WHILE level < olevel DO
  10587. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10588. IF olevel > 0 THEN
  10589. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10590. nextPatch := scopes[olevel-1].patchAdr+1;
  10591. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10592. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10593. END;
  10594. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10595. DEC(olevel);
  10596. END;
  10597. IF (this[level] > 0) THEN
  10598. IF level > olevel THEN
  10599. (*TRACE("opening",level); *)
  10600. IF scopes[level].section = NIL THEN
  10601. arrayName := ".@ExportArray";
  10602. Strings.AppendInt(arrayName, level);
  10603. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10604. AddPointer(scopes[level].section,offset);
  10605. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10606. END;
  10607. scopes[level].beginPC := scopes[level].section.pc;
  10608. olevel := level;
  10609. scopes[olevel].elements := 0;
  10610. END;
  10611. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10612. sym := sections[s];
  10613. ELSE
  10614. sym := NIL;
  10615. END;
  10616. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10617. THEN
  10618. INC(scopes[olevel].elements);
  10619. END;
  10620. (* enter string in scope *)
  10621. INC(level);
  10622. END;
  10623. END;
  10624. Basic.SegmentedNameToString(this, name);
  10625. prev := this;
  10626. END;
  10627. END;
  10628. WHILE 0 <= olevel DO
  10629. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10630. IF olevel > 0 THEN
  10631. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10632. nextPatch := scopes[olevel-1].patchAdr+1;
  10633. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10634. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10635. END;
  10636. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10637. DEC(olevel);
  10638. END;
  10639. level := 0;
  10640. WHILE (level < LEN(scopes)) DO
  10641. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10642. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10643. END;
  10644. INC(level);
  10645. END;
  10646. END Export;
  10647. BEGIN
  10648. NEW(fingerPrinter);
  10649. NEW(poolMap, 64);
  10650. (* this is the name pool private to the export table -- it is sorted and should not be mixed / used for other names in a module *)
  10651. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10652. NEW(sectionArray, module.allSections.Length());
  10653. FOR i := 0 TO module.allSections.Length() - 1 DO
  10654. section := module.allSections.GetSection(i);
  10655. sectionArray[i] := section;
  10656. END;
  10657. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10658. Export(sectionArray^);
  10659. END ExportDesc;
  10660. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10661. VAR
  10662. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10663. BEGIN
  10664. Info(source, "exception table offsets array descriptor");
  10665. size := 0;
  10666. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10667. Info(source, "exception table content");
  10668. FOR i := 0 TO module.allSections.Length() - 1 DO
  10669. p := module.allSections.GetSection(i);
  10670. IF p.type = Sections.CodeSection THEN
  10671. finallyPC := p(IntermediateCode.Section).finally;
  10672. IF finallyPC>=0 THEN
  10673. Symbol( source, p, 0,0);
  10674. Symbol( source, p, finallyPC, 0);
  10675. Symbol( source, p, finallyPC,0);
  10676. INC(size);
  10677. END;
  10678. END
  10679. END;
  10680. PatchArray(source,sizePC,size);
  10681. END ExceptionArray;
  10682. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10683. VAR i: LONGINT; ch: CHAR;
  10684. BEGIN
  10685. i := 0;
  10686. REPEAT
  10687. ch := name[i]; INC(i);
  10688. Char( section, ch);
  10689. UNTIL ch = 0X;
  10690. WHILE i < 32 DO
  10691. Char( section, 0X); INC(i);
  10692. END;
  10693. END Name;
  10694. PROCEDURE References(section: IntermediateCode.Section);
  10695. CONST
  10696. sfTypeNone = 0X;
  10697. sfTypeCHAR = 01X;
  10698. sfTypeCHAR8 = 02X;
  10699. sfTypeCHAR16 = 03X;
  10700. sfTypeCHAR32 = 04X;
  10701. sfTypeRANGE = 05X;
  10702. sfTypeSHORTINT = 06X;
  10703. sfTypeINTEGER = 07X;
  10704. sfTypeLONGINT = 08X;
  10705. sfTypeHUGEINT = 09X;
  10706. sfTypeWORD = 0AX;
  10707. sfTypeLONGWORD = 0BX;
  10708. sfTypeSIGNED8 = 0CX;
  10709. sfTypeSIGNED16 = 0DX;
  10710. sfTypeSIGNED32 = 0EX;
  10711. sfTypeSIGNED64 = 0FX;
  10712. sfTypeUNSIGNED8 = 10X;
  10713. sfTypeUNSIGNED16 = 11X;
  10714. sfTypeUNSIGNED32 = 12X;
  10715. sfTypeUNSIGNED64 = 13X;
  10716. sfTypeREAL = 14X;
  10717. sfTypeLONGREAL = 15X;
  10718. sfTypeCOMPLEX = 16X;
  10719. sfTypeLONGCOMPLEX = 17X;
  10720. sfTypeBOOLEAN = 18X;
  10721. sfTypeSET = 19X;
  10722. sfTypeANY = 1AX;
  10723. sfTypeOBJECT = 1BX;
  10724. sfTypeBYTE = 1CX;
  10725. sfTypeADDRESS = 1DX;
  10726. sfTypeSIZE = 1EX;
  10727. sfTypeIndirect = 1FX;
  10728. sfTypeRecord = 20X;
  10729. sfTypePointerToRecord = 21X;
  10730. sfTypePointerToArray = 22X;
  10731. sfTypeOpenArray = 23X;
  10732. sfTypeStaticArray = 24X;
  10733. sfTypeDynamicArray = 25X;
  10734. sfTypeMathStaticArray = 26X;
  10735. sfTypeMathOpenArray = 27X;
  10736. sfTypeMathTensor = 28X;
  10737. sfTypeDelegate = 29X;
  10738. sfTypeENUM = 2AX;
  10739. sfTypeCELL = 2BX;
  10740. sfTypePORT = 2CX;
  10741. sfIN = 0X;
  10742. sfOUT = 1X;
  10743. flagDelegate = 0;
  10744. flagConstructor = 1;
  10745. (* variable / parameter addressing modes *)
  10746. sfAbsolute = 0X; (* global vars *)
  10747. sfRelative = 1X; (* variables, value parameters *)
  10748. sfIndirect = 2X; (* var parameters *)
  10749. sfScopeBegin = 0F0X;
  10750. sfScopeEnd = 0F1X;
  10751. sfProcedure = 0F2X;
  10752. sfVariable = 0F3X;
  10753. sfTypeDeclaration = 0F4X;
  10754. sfModule = 0FFX;
  10755. RefInfo = TRUE;
  10756. VAR
  10757. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10758. indirectTypes: Basic.HashTable;
  10759. PROCEDURE CurrentIndex(): SIZE;
  10760. VAR i: LONGINT;
  10761. BEGIN
  10762. FOR i := startPC TO section.pc -1 DO
  10763. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10764. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10765. END;
  10766. startPC := section.pc;
  10767. RETURN lastOffset;
  10768. END CurrentIndex;
  10769. (*
  10770. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10771. Module = sfModule prevSymbol:SIZE name:String Scope.
  10772. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10773. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10774. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10775. Type =
  10776. sfTypePointerToRecord
  10777. | sfTypePointerToArray Type
  10778. | sfTypeOpenArray Type
  10779. | sfTypeDynamicArray Type
  10780. | sfTypeStaticArray length:SIZE Type
  10781. | sfTypeMathOpenArray Type
  10782. | sfTypeMathStaticArray length:SIZE Type
  10783. | sfTypeMathTensor Type
  10784. | sfTypeRecord tdAdr:ADDRESS
  10785. | sfTypeDelegate {Parameter} return:Type
  10786. | sfTypePort (sfIN | sfOUT)
  10787. | sfTypeBOOLEAN
  10788. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10789. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10790. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10791. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10792. | sfTypeWORD | sfTypeLONGWORD
  10793. | sfTypeREAL | sfTypeLONGREAL
  10794. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10795. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10796. | sfTypeIndirect offset:SIZE.
  10797. *)
  10798. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10799. VAR offset: SIZE;
  10800. BEGIN
  10801. IF indirectTypes.Has(type) THEN
  10802. offset := indirectTypes.GetInt(type);
  10803. Char(section, sfTypeIndirect);
  10804. Size(section, offset);
  10805. RETURN TRUE;
  10806. ELSE
  10807. indirectTypes.PutInt(type, CurrentIndex());
  10808. RETURN FALSE;
  10809. END;
  10810. END Indirect;
  10811. PROCEDURE NType(type: SyntaxTree.Type);
  10812. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10813. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10814. BEGIN
  10815. IF type = NIL THEN
  10816. Char(section, sfTypeNone)
  10817. ELSE
  10818. type := type.resolved;
  10819. size := type.sizeInBits;
  10820. WITH type:SyntaxTree.PointerType DO
  10821. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10822. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10823. Char(section, sfTypePointerToRecord);
  10824. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10825. ELSE
  10826. IF RefInfo THEN Info(section,"PointerToArray") END;
  10827. Char(section, sfTypePointerToArray);
  10828. NType(type.pointerBase);
  10829. END;
  10830. | type: SyntaxTree.ArrayType DO
  10831. IF ~Indirect(type) THEN
  10832. IF type.form = SyntaxTree.Open THEN
  10833. IF RefInfo THEN Info(section,"OpenArray") END;
  10834. Char(section, sfTypeOpenArray);
  10835. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10836. IF RefInfo THEN Info(section,"DynamicArray") END;
  10837. Char(section, sfTypeDynamicArray);
  10838. ELSIF type.form = SyntaxTree.Static THEN
  10839. IF RefInfo THEN Info(section,"StaticArray") END;
  10840. Char(section, sfTypeStaticArray);
  10841. Size(section, type.staticLength);
  10842. ELSE
  10843. HALT(100);
  10844. END;
  10845. NType(type.arrayBase);
  10846. END;
  10847. | type: SyntaxTree.MathArrayType DO
  10848. IF ~Indirect(type) THEN
  10849. IF type.form = SyntaxTree.Open THEN
  10850. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10851. Char(section, sfTypeMathOpenArray);
  10852. ELSIF type.form = SyntaxTree.Static THEN
  10853. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10854. Char(section, sfTypeMathStaticArray);
  10855. Size(section, type.staticLength);
  10856. ELSIF type.form = SyntaxTree.Tensor THEN
  10857. IF RefInfo THEN Info(section,"MathTensor") END;
  10858. Char(section, sfTypeMathTensor);
  10859. ELSE
  10860. HALT(100);
  10861. END;
  10862. NType(type.arrayBase);
  10863. END;
  10864. | type: SyntaxTree.RecordType DO
  10865. IF ~Indirect(type) THEN
  10866. IF type.pointerType # NIL (* OBJECT *) THEN
  10867. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10868. Char(section, sfTypePointerToRecord)
  10869. ELSE
  10870. IF RefInfo THEN Info(section,"Record") END;
  10871. Char(section, sfTypeRecord);
  10872. td := type.typeDeclaration;
  10873. IF RefInfo THEN Info(section,"TD") END;
  10874. IF (td # NIL) THEN
  10875. Global.GetSymbolSegmentedName(td,segmentedName);
  10876. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10877. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10878. ELSE
  10879. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10880. END;
  10881. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10882. Symbol(section, tir, 0, offset);
  10883. ELSE
  10884. Address(section, 0);
  10885. END;
  10886. END;
  10887. END;
  10888. | type: SyntaxTree.CellType DO
  10889. IF ~Indirect(type) THEN
  10890. IF RefInfo THEN Info(section,"Record") END;
  10891. Char(section, sfTypeRecord);
  10892. td := type.typeDeclaration;
  10893. IF RefInfo THEN Info(section,"TD") END;
  10894. IF (td # NIL) THEN
  10895. Global.GetSymbolSegmentedName(td,segmentedName);
  10896. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10897. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10898. ELSE
  10899. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10900. END;
  10901. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10902. Symbol(section, tir, 0, offset);
  10903. ELSE
  10904. Address(section, 0);
  10905. END;
  10906. END;
  10907. | type: SyntaxTree.PortType DO
  10908. Char(section, sfTypePORT);
  10909. IF type.direction = SyntaxTree.OutPort THEN
  10910. Char(section, sfOUT)
  10911. ELSE
  10912. Char(section, sfIN)
  10913. END;
  10914. | type: SyntaxTree.ProcedureType DO
  10915. IF ~Indirect(type) THEN
  10916. Char(section, sfTypeDelegate);
  10917. parameter := type.firstParameter;
  10918. WHILE(parameter # NIL) DO
  10919. NParameter(parameter, -1);
  10920. parameter := parameter.nextParameter;
  10921. END;
  10922. NType(type.returnType);
  10923. END;
  10924. | type:SyntaxTree.EnumerationType DO
  10925. Char(section, sfTypeENUM);
  10926. | type: SyntaxTree.BasicType DO
  10927. WITH type: SyntaxTree.BooleanType DO
  10928. IF RefInfo THEN Info(section,"Boolean") END;
  10929. Char(section, sfTypeBOOLEAN);
  10930. | type: SyntaxTree.CharacterType DO
  10931. IF type = module.system.characterType THEN
  10932. IF RefInfo THEN Info(section,"CHAR") END;
  10933. Char(section, sfTypeCHAR);
  10934. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10935. IF RefInfo THEN Info(section,"CHAR8") END;
  10936. Char(section, sfTypeCHAR8)
  10937. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10938. IF RefInfo THEN Info(section,"CHAR16") END;
  10939. Char(section, sfTypeCHAR16);
  10940. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10941. IF RefInfo THEN Info(section,"CHAR32") END;
  10942. Char(section, sfTypeCHAR32);
  10943. ELSE
  10944. HALT(100);
  10945. END;
  10946. |type: SyntaxTree.IntegerType DO
  10947. IF type(SyntaxTree.IntegerType).signed THEN
  10948. IF (type = module.system.shortintType) THEN
  10949. IF RefInfo THEN Info(section,"SHORTINT") END;
  10950. Char(section, sfTypeSHORTINT)
  10951. ELSIF (type = module.system.integerType) THEN
  10952. IF RefInfo THEN Info(section,"INTEGER") END;
  10953. Char(section, sfTypeINTEGER)
  10954. ELSIF (type = module.system.longintType) THEN
  10955. IF RefInfo THEN Info(section,"LONGINT") END;
  10956. Char(section, sfTypeLONGINT)
  10957. ELSIF (type = module.system.hugeintType) THEN
  10958. IF RefInfo THEN Info(section,"HUGEINT") END;
  10959. Char(section, sfTypeHUGEINT)
  10960. ELSIF (type = module.system.wordType) THEN
  10961. IF RefInfo THEN Info(section,"WORD") END;
  10962. Char(section, sfTypeWORD)
  10963. ELSIF (type = module.system.longWordType) THEN
  10964. IF RefInfo THEN Info(section,"LONGWORD") END;
  10965. Char(section, sfTypeLONGWORD);
  10966. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10967. IF RefInfo THEN Info(section,"SIGNED8") END;
  10968. Char(section, sfTypeSIGNED8)
  10969. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10970. IF RefInfo THEN Info(section,"SIGNED16") END;
  10971. Char(section, sfTypeSIGNED16)
  10972. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10973. IF RefInfo THEN Info(section,"SIGNED32") END;
  10974. Char(section, sfTypeSIGNED32)
  10975. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10976. IF RefInfo THEN Info(section,"SIGNED64") END;
  10977. Char(section, sfTypeSIGNED64)
  10978. ELSE
  10979. HALT(100);
  10980. END
  10981. ELSE (* unsigned *)
  10982. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10983. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10984. Char(section, sfTypeUNSIGNED8)
  10985. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10986. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10987. Char(section, sfTypeUNSIGNED16)
  10988. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10989. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10990. Char(section, sfTypeUNSIGNED32)
  10991. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10992. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10993. Char(section, sfTypeUNSIGNED64)
  10994. ELSE
  10995. HALT(100)
  10996. END
  10997. END;
  10998. | type: SyntaxTree.FloatType DO
  10999. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  11000. IF RefInfo THEN Info(section,"REAL") END;
  11001. Char(section, sfTypeREAL);
  11002. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  11003. IF RefInfo THEN Info(section,"LONGREAL") END;
  11004. Char(section, sfTypeLONGREAL);
  11005. ELSE
  11006. HALT(100);
  11007. END;
  11008. |type: SyntaxTree.ComplexType DO
  11009. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  11010. IF RefInfo THEN Info(section,"COMPLEX") END;
  11011. Char(section, sfTypeCOMPLEX);
  11012. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  11013. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  11014. Char(section, sfTypeLONGCOMPLEX);
  11015. ELSE
  11016. HALT(100);
  11017. END;
  11018. |type:SyntaxTree.SetType DO
  11019. IF RefInfo THEN Info(section,"SET") END;
  11020. Char(section, sfTypeSET);
  11021. |type:SyntaxTree.AnyType DO
  11022. IF RefInfo THEN Info(section,"ANY") END;
  11023. Char(section, sfTypeANY);
  11024. |type:SyntaxTree.ObjectType DO
  11025. IF RefInfo THEN Info(section,"OBJECT") END;
  11026. Char(section, sfTypeOBJECT);
  11027. |type:SyntaxTree.ByteType DO
  11028. IF RefInfo THEN Info(section,"BYTE") END;
  11029. Char(section, sfTypeBYTE);
  11030. |type:SyntaxTree.RangeType DO
  11031. IF RefInfo THEN Info(section,"RANGE") END;
  11032. Char(section, sfTypeRANGE)
  11033. |type:SyntaxTree.AddressType DO
  11034. IF RefInfo THEN Info(section,"ADDRESS") END;
  11035. Char(section, sfTypeADDRESS)
  11036. |type:SyntaxTree.SizeType DO
  11037. IF RefInfo THEN Info(section,"SIZE") END;
  11038. Char(section, sfTypeSIZE)
  11039. ELSE
  11040. HALT(100)
  11041. END;
  11042. ELSE HALT(101);
  11043. END;
  11044. END;
  11045. END NType;
  11046. (*
  11047. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11048. *)
  11049. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11050. VAR pos: LONGINT; type: SyntaxTree.Type;
  11051. BEGIN
  11052. IF RefInfo THEN Info(section, "Parameter") END;
  11053. Char(section, sfVariable);
  11054. Size(section, procOffset);
  11055. String0(section, parameter.name);
  11056. type := parameter.type.resolved;
  11057. IF parameter.kind = SyntaxTree.VarParameter THEN
  11058. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11059. ELSE Char(section, sfIndirect)
  11060. END;
  11061. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11062. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11063. Char(section, sfIndirect);
  11064. ELSE
  11065. Char(section, sfRelative);
  11066. END;
  11067. ELSE
  11068. Char(section, sfRelative);
  11069. END;
  11070. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11071. NType(parameter.type);
  11072. END NParameter;
  11073. (*
  11074. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11075. *)
  11076. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11077. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11078. name: Basic.SegmentedName; flags: SET;
  11079. BEGIN
  11080. IF RefInfo THEN Info(section, "Procedure") END;
  11081. pos := CurrentIndex();
  11082. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11083. Char(section, sfProcedure);
  11084. Size(section, scopeOffset);
  11085. String0(section,procedure.name);
  11086. s := module.allSections.FindBySymbol(procedure);
  11087. Symbol(section,s,0,0); (* start *)
  11088. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11089. flags := {};
  11090. IF procedureType.isDelegate THEN
  11091. INCL(flags, flagDelegate);
  11092. END;
  11093. IF procedure.isConstructor THEN
  11094. INCL(flags, flagConstructor);
  11095. END;
  11096. Set(section, flags);
  11097. Global.GetSymbolSegmentedName(procedure,name);
  11098. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11099. IF RefInfo THEN Info(section, "Parameters") END;
  11100. parameter := procedureType.firstParameter;
  11101. WHILE(parameter # NIL) DO
  11102. NParameter(parameter, pos);
  11103. parameter := parameter.nextParameter;
  11104. END;
  11105. IF RefInfo THEN Info(section, "ReturnType") END;
  11106. NType(procedureType.returnType);
  11107. NScope(procedure.procedureScope, pos);
  11108. END NProcedure;
  11109. (*
  11110. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11111. *)
  11112. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11113. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11114. BEGIN
  11115. IF RefInfo THEN Info(section, "Variable") END;
  11116. pos := CurrentIndex();
  11117. Char(section, sfVariable);
  11118. Size(section, scopeOffset);
  11119. String0(section, variable.name);
  11120. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11121. Char(section, sfAbsolute);
  11122. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11123. NamedSymbol(section, sn,variable, 0,0);
  11124. ELSE
  11125. Char(section, sfRelative);
  11126. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11127. END;
  11128. NType(variable.type);
  11129. s := module.allSections.FindBySymbol(variable);
  11130. END NVariable;
  11131. (*
  11132. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11133. *)
  11134. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11135. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11136. BEGIN
  11137. IF typeDeclaration = NIL THEN RETURN END;
  11138. pos := CurrentIndex();
  11139. s := module.allSections.FindBySymbol(typeDeclaration);
  11140. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11141. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11142. Char(section, sfTypeDeclaration);
  11143. Size(section, scopeOffset);
  11144. String0(section, typeDeclaration.name);
  11145. declared := typeDeclaration.declaredType.resolved;
  11146. IF (declared IS SyntaxTree.PointerType) THEN
  11147. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11148. END;
  11149. WITH declared: SyntaxTree.RecordType DO
  11150. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11151. Symbol(section, s, 0, offset);
  11152. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11153. Basic.AppendToSegmentedName(name,".@Info");
  11154. s := module.allSections.FindByName(name);
  11155. IF s # NIL THEN (* does not work for coop *)
  11156. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11157. NScope(declared.recordScope, pos);
  11158. END;
  11159. |declared: SyntaxTree.CellType DO
  11160. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11161. Symbol(section, s, 0, offset);
  11162. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11163. Basic.AppendToSegmentedName(name,".@Info");
  11164. s := module.allSections.FindByName(name);
  11165. IF s # NIL THEN
  11166. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11167. NScope(declared.cellScope, pos);
  11168. END;
  11169. ELSE
  11170. Address(section, 0);
  11171. END;
  11172. END NTypeDeclaration;
  11173. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11174. VAR pos: LONGINT;
  11175. BEGIN
  11176. pos := CurrentIndex();
  11177. Char(section,sfModule);
  11178. Size(section, prevSymbol);
  11179. String0(section, module.name);
  11180. NScope(module.moduleScope, pos);
  11181. END NModule;
  11182. (*
  11183. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11184. *)
  11185. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11186. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11187. BEGIN
  11188. IF scope = NIL THEN RETURN END;
  11189. IF RefInfo THEN Info(section, "Scope") END;
  11190. Char(section, sfScopeBegin);
  11191. variable := scope.firstVariable;
  11192. WHILE (variable # NIL) DO
  11193. NVariable(variable, prevSymbol);
  11194. variable := variable.nextVariable;
  11195. END;
  11196. WITH scope: SyntaxTree.ModuleScope DO
  11197. bodyProcedure := scope.bodyProcedure;
  11198. |scope: SyntaxTree.RecordScope DO
  11199. bodyProcedure := scope.bodyProcedure;
  11200. ELSE
  11201. bodyProcedure := NIL;
  11202. END;
  11203. IF bodyProcedure # NIL THEN
  11204. NProcedure(bodyProcedure, prevSymbol)
  11205. END;
  11206. procedure := scope.firstProcedure;
  11207. WHILE procedure # NIL DO
  11208. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11209. procedure := procedure.nextProcedure;
  11210. END;
  11211. typeDeclaration := scope.firstTypeDeclaration;
  11212. WHILE typeDeclaration # NIL DO
  11213. NTypeDeclaration(typeDeclaration, prevSymbol);
  11214. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11215. END;
  11216. Char(section, sfScopeEnd); (* scope ends *)
  11217. END NScope;
  11218. BEGIN
  11219. NEW(indirectTypes, 32);
  11220. ArrayBlock(section,sizePC,"", FALSE);
  11221. IF ~implementationVisitor.backend.cooperative THEN
  11222. startPC := section.pc;
  11223. NModule(module.module, -1);
  11224. PatchArray(section,sizePC,CurrentIndex());
  11225. END;
  11226. END References;
  11227. (*
  11228. Command* = RECORD
  11229. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11230. name*: Name; (* name of the procedure *)
  11231. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11232. entryAdr* : ADDRESS; (* entry address of procedure *)
  11233. END;
  11234. *)
  11235. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11236. VAR
  11237. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11238. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11239. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11240. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11241. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11242. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11243. BEGIN
  11244. RETURN
  11245. (type = NIL) OR
  11246. (type.resolved IS SyntaxTree.RecordType) OR
  11247. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11248. (type.resolved IS SyntaxTree.AnyType);
  11249. END TypeAllowed;
  11250. BEGIN
  11251. numberParameters := procedureType.numberParameters;
  11252. RETURN
  11253. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11254. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11255. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11256. END GetProcedureAllowed;
  11257. PROCEDURE WriteType(type : SyntaxTree.Type);
  11258. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11259. name: Basic.SegmentedName; offset: LONGINT;
  11260. BEGIN
  11261. IF type = NIL THEN
  11262. Address(source,0);
  11263. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11264. Address(source,1);
  11265. ELSE
  11266. type := type.resolved;
  11267. IF type IS SyntaxTree.PointerType THEN
  11268. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11269. END;
  11270. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11271. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11272. name[0] := typeDeclaration.name; name[1] := -1;
  11273. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11274. ASSERT(section # NIL);
  11275. ELSE
  11276. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11277. (* TODO *)
  11278. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11279. END;
  11280. IF implementationVisitor.backend.cooperative THEN
  11281. offset := 0;
  11282. ELSE
  11283. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11284. END;
  11285. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11286. END;
  11287. END WriteType;
  11288. BEGIN
  11289. Info(source, "command array descriptor");
  11290. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11291. numberCommands := 0;
  11292. Info(source, "command array content");
  11293. FOR i := 0 TO module.allSections.Length() - 1 DO
  11294. p := module.allSections.GetSection(i);
  11295. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11296. procedure := p.symbol(SyntaxTree.Procedure);
  11297. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11298. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11299. procedure.GetName(name);
  11300. Name(source,name);
  11301. numberParameters := procedureType.numberParameters;
  11302. (* offset of type of first parameter *)
  11303. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11304. ELSE WriteType(procedureType.firstParameter.type)
  11305. END;
  11306. (* offset of type of return parameter *)
  11307. WriteType(procedureType.returnType);
  11308. (* command name *)
  11309. (* command code offset *)
  11310. Symbol(source,p,0,0);
  11311. INC(numberCommands);
  11312. IF Trace THEN
  11313. D.Ln;
  11314. END;
  11315. END;
  11316. END
  11317. END;
  11318. PatchArray(source,sizePC,numberCommands);
  11319. END CommandArray;
  11320. (* to prevent from double import of different module aliases *)
  11321. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11322. VAR i: SyntaxTree.Import;
  11323. BEGIN
  11324. i := module.module.moduleScope.firstImport;
  11325. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11326. i := i.nextImport;
  11327. END;
  11328. RETURN i = import
  11329. END IsFirstDirectOccurence;
  11330. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11331. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11332. BEGIN
  11333. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11334. ArrayBlock(source,pc,"", FALSE);
  11335. Info(source, "import module array data");
  11336. IF implementationVisitor.backend.cooperative THEN
  11337. offset := 0;
  11338. ELSE
  11339. IF module.system.addressType.sizeInBits = 64 THEN
  11340. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11341. ELSE
  11342. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11343. END;
  11344. END;
  11345. import := module.module.moduleScope.firstImport;
  11346. numberImports := 0;
  11347. WHILE import # NIL DO
  11348. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11349. Global.GetModuleSegmentedName(import.module,name);
  11350. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11351. NamedSymbol(source, name, NIL, 0, offset);
  11352. INC(numberImports);
  11353. END;
  11354. import := import.nextImport
  11355. END;
  11356. PatchArray(source,pc,numberImports);
  11357. END ImportsArray;
  11358. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11359. VAR
  11360. p: Sections.Section; sizePC, size, i: LONGINT;
  11361. BEGIN
  11362. Info(source, "Type info section");
  11363. size := 0;
  11364. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11365. FOR i := 0 TO module.allSections.Length() - 1 DO
  11366. p := module.allSections.GetSection(i);
  11367. WITH p: IntermediateCode.Section DO
  11368. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11369. Symbol(source,p,0,0);
  11370. INC(size);
  11371. END;
  11372. END
  11373. END;
  11374. PatchArray(source,sizePC,size);
  11375. END TypeInfoSection;
  11376. (*
  11377. ProcTableEntry* = RECORD
  11378. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11379. noPtr*: LONGINT;
  11380. END;
  11381. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11382. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11383. *)
  11384. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11385. VAR
  11386. numberPointers: SIZE;
  11387. procedure: SyntaxTree.Procedure;
  11388. BEGIN
  11389. Info(section,"pcFrom");
  11390. Symbol(section,procedureSection,0,0);
  11391. Info(section,"pcTo");
  11392. Symbol(section, procedureSection, procedureSection.pc, 0);
  11393. Info(section,"pointer to offsets array");
  11394. Symbol(section, section,section.pc+1,0);
  11395. Info(section,"offsets array");
  11396. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11397. PointerArray(section, procedure.procedureScope, numberPointers);
  11398. END ProcedureDescriptor;
  11399. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11400. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11401. BEGIN
  11402. name := procedureSection.name;
  11403. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11404. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11405. ProcedureDescriptor(dest, procedureSection);
  11406. Symbol(section, dest, offset, 0);
  11407. END ProcedureDescriptorPointer;
  11408. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11409. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11410. BEGIN
  11411. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11412. numberProcs := 0;
  11413. FOR i := 0 TO module.allSections.Length() - 1 DO
  11414. destination := module.allSections.GetSection(i);
  11415. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11416. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11417. INC(numberProcs);
  11418. END
  11419. END;
  11420. PatchArray(section, sizePC, numberProcs);
  11421. END ProcedureDescriptorArray;
  11422. (*
  11423. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11424. VAR
  11425. next*: Module; (** once a module is published, all fields are read-only *)
  11426. name*: Name;
  11427. init, published: BOOLEAN;
  11428. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11429. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11430. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11431. command*: POINTER TO ARRAY OF Command;
  11432. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11433. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11434. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11435. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11436. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11437. data*, code*: Bytes;
  11438. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11439. export*: ExportDesc;
  11440. term*: TerminationHandler;
  11441. exTable*: ExceptionTable;
  11442. noProcs*: LONGINT;
  11443. firstProc*: ADDRESS; <- done by loader
  11444. maxPtrs*: LONGINT;
  11445. crc*: LONGINT;
  11446. *)
  11447. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11448. BEGIN
  11449. Info(section, "cycle");
  11450. Size(section,0);
  11451. Info(section, "references");
  11452. Size(section,0);
  11453. Info(section, "nextMarked");
  11454. Address(section,0);
  11455. Info(section, "nextWatched");
  11456. Address(section,0);
  11457. END BasePointer;
  11458. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11459. BEGIN
  11460. BasePointer(section);
  11461. Info(section, "action");
  11462. Address(section,0);
  11463. Info(section, "monitor");
  11464. Address(section,0);
  11465. END BaseObject;
  11466. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11467. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11468. pooledName: Basic.SegmentedName;
  11469. symbol: SyntaxTree.Symbol;
  11470. BEGIN
  11471. Global.GetModuleName(module.module,name);
  11472. Strings.Append(name,".@Module.@Descriptor");
  11473. Basic.ToSegmentedName(name, pooledName);
  11474. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11475. Symbol(section,descriptorSection,0,0);
  11476. Info(descriptorSection, "descriptor");
  11477. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11478. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11479. Address(descriptorSection,0);
  11480. Global.GetModuleName(module.module,name);
  11481. Strings.Append(name,".@Trace");
  11482. Basic.ToSegmentedName(name, pooledName);
  11483. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11484. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11485. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11486. END ModuleDescriptor;
  11487. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11488. VAR name: ARRAY 128 OF CHAR;
  11489. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11490. symbol: SyntaxTree.Symbol;
  11491. BEGIN
  11492. Global.GetModuleName(module.module,name);
  11493. Strings.Append(name,".@Module");
  11494. Basic.ToSegmentedName(name, pooledName);
  11495. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11496. moduleSection.SetExported(TRUE);
  11497. IF moduleSection.pc = 0 THEN
  11498. IF implementationVisitor.backend.cooperative THEN
  11499. Info(moduleSection, "descriptor");
  11500. ModuleDescriptor(moduleSection);
  11501. BaseObject(moduleSection);
  11502. implementationVisitor.CreateTraceModuleMethod(module.module);
  11503. ELSE
  11504. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11505. Info(moduleSection, "HeapBlock");
  11506. Symbol(moduleSection,moduleSection,2,0);
  11507. Info(moduleSection, "TypeDescriptor");
  11508. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11509. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11510. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11511. END;
  11512. END;
  11513. RETURN moduleSection;
  11514. END ModuleSection;
  11515. PROCEDURE NewModuleInfo();
  11516. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11517. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11518. sectionName: Basic.SectionName;
  11519. CONST MPO=-40000000H;
  11520. BEGIN
  11521. (*
  11522. TypeDesc* = POINTER TO RECORD
  11523. descSize: LONGINT;
  11524. sentinel: LONGINT; (* = MPO-4 *)
  11525. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11526. flags*: SET;
  11527. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11528. name*: Name;
  11529. END;
  11530. *)
  11531. Global.GetSymbolSegmentedName(module.module,name);
  11532. Basic.AppendToSegmentedName(name,".@Info");
  11533. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11534. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11535. Address(source,MPO-4);
  11536. Info(source, "type tag pointer");
  11537. Address( source,0);
  11538. Info(source, "type flags");
  11539. flags := {};
  11540. Set( source, flags);
  11541. Info(source, "pointer to module");
  11542. moduleSection := ModuleSection();
  11543. Symbol( source, moduleSection, moduleSection.pc,0);
  11544. Info(source, "type name");
  11545. i := 0;
  11546. sectionName := "@Self";
  11547. (*
  11548. Global.GetSymbolSegmentedName(td,name);
  11549. Basic.SegmentedNameToString(name, sectionName);
  11550. *)
  11551. Name(source,sectionName);
  11552. source.SetReferenced(FALSE);
  11553. patchInfoPC := source.pc;
  11554. Size(source, 0);
  11555. END NewModuleInfo;
  11556. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11557. VAR
  11558. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11559. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11560. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11561. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11562. referenceSectionOffset : LONGINT;
  11563. name: Basic.SegmentedName; offset: LONGINT;
  11564. BEGIN
  11565. NewModuleInfo();
  11566. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11567. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11568. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11569. ImportsArray(importSection);
  11570. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11571. CommandArray(commandsSection);
  11572. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11573. ExceptionArray(exceptionSection);
  11574. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11575. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11576. TypeInfoSection(typeInfoSection);
  11577. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11578. References(referenceSection);
  11579. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11580. IF ~implementationVisitor.backend.cooperative THEN
  11581. ProcedureDescriptorArray(procTableSection, numberProcs);
  11582. name := "Heaps.AnyPtr";
  11583. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11584. (* set base pointer *)
  11585. NamedSymbolAt(procTableSection, procTableSectionOffset -1 , name, NIL, 0, offset);
  11586. END;
  11587. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11588. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11589. moduleSection := ModuleSection();
  11590. Info(moduleSection, "nextRoot*: RootObject");
  11591. Address(moduleSection,0);
  11592. Info(moduleSection, "next*: Module");
  11593. Address(moduleSection,0);
  11594. Info(moduleSection, "name*: Name");
  11595. Name(moduleSection,moduleName);
  11596. Info(moduleSection, "init, published: BOOLEAN");
  11597. Boolean(moduleSection,FALSE);
  11598. Boolean(moduleSection,FALSE);
  11599. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11600. Boolean(moduleSection,FALSE);
  11601. Boolean(moduleSection,FALSE);
  11602. Info(moduleSection, "refcnt*: LONGINT");
  11603. Longint(moduleSection,0);
  11604. Info(moduleSection, "sb*: ADDRESS");
  11605. Address(moduleSection,0);
  11606. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11607. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11608. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11609. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11610. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11611. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11612. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11613. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11614. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11615. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11616. Info(moduleSection, "procTable*: ProcTable");
  11617. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11618. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11619. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11620. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11621. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11622. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11623. Info(moduleSection, "export*: ExportDesc");
  11624. ExportDesc(moduleSection);
  11625. Info(moduleSection, "term*: TerminationHandler");
  11626. Address(moduleSection,0);
  11627. Info(moduleSection, "exTable*: ExceptionTable");
  11628. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11629. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11630. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11631. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11632. Info(moduleSection, "crc*: LONGINT");
  11633. Longint(moduleSection, 0); (*! must be implemented *)
  11634. Info(moduleSection, "body*: ADDRESS");
  11635. Symbol(moduleSection, bodyProc, 0,0);
  11636. IF implementationVisitor.backend.cooperative THEN
  11637. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11638. Info(moduleSection, "monitor.owner");
  11639. Address(moduleSection,0);
  11640. Info(moduleSection, "monitor.nestingLevel");
  11641. Address(moduleSection,0);
  11642. Info(moduleSection, "monitor.blockedQueue");
  11643. Address(moduleSection,0); Address(moduleSection,0);
  11644. Info(moduleSection, "monitor.waitingQueue");
  11645. Address(moduleSection,0); Address(moduleSection,0);
  11646. Info(moduleSection, "monitor.waitingSentinel");
  11647. Address(moduleSection,0);
  11648. END;
  11649. END Module;
  11650. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11651. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11652. BEGIN
  11653. ArrayBlock(source,pc,"",FALSE);
  11654. Info(source, "pointer offsets array data");
  11655. IF scope IS SyntaxTree.RecordScope THEN
  11656. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11657. ELSIF scope IS SyntaxTree.CellScope THEN
  11658. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11659. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11660. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11661. WHILE variable # NIL DO
  11662. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11663. symbol := module.allSections.FindBySymbol(variable);
  11664. ASSERT(symbol # NIL);
  11665. Pointers(0,symbol, source,variable.type,numberPointers);
  11666. END;
  11667. variable := variable.nextVariable;
  11668. END;
  11669. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11670. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11671. WHILE parameter # NIL DO
  11672. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11673. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11674. END;
  11675. parameter := parameter.nextParameter;
  11676. END;
  11677. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11678. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11679. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11680. IF implementationVisitor.backend.preciseGC THEN
  11681. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11682. END;
  11683. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11684. END;
  11685. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11686. WHILE(variable # NIL) DO
  11687. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11688. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11689. END;
  11690. variable := variable.nextVariable
  11691. END;
  11692. END;
  11693. PatchArray(source,pc,numberPointers);
  11694. END PointerArray;
  11695. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11696. VAR
  11697. name: Basic.SegmentedName;
  11698. section: IntermediateCode.Section;
  11699. BEGIN
  11700. ASSERT(implementationVisitor.newObjectFile);
  11701. Global.GetSymbolSegmentedName(symbol,name);
  11702. Basic.AppendToSegmentedName(name,suffix);
  11703. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11704. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11705. Info(section, "HeapBlock");
  11706. Address(section,0); (* empty such that GC does not go on traversing *)
  11707. Info(section, suffix);
  11708. Address(section,0);
  11709. pc := section.pc;
  11710. RETURN section;
  11711. END SymbolSection;
  11712. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11713. VAR recordType: SyntaxTree.RecordType;
  11714. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11715. section: Sections.Section; cellType: SyntaxTree.CellType;
  11716. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11717. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11718. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11719. sectionName: Basic.SectionName;
  11720. CONST MPO=-40000000H;
  11721. BEGIN
  11722. (*
  11723. TypeDesc* = POINTER TO RECORD
  11724. descSize: LONGINT;
  11725. sentinel: LONGINT; (* = MPO-4 *)
  11726. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11727. flags*: SET;
  11728. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11729. name*: Name;
  11730. END;
  11731. *)
  11732. (* source := module.sections.FindByName(...) *)
  11733. Global.GetSymbolSegmentedName(td,name);
  11734. Basic.AppendToSegmentedName(name,".@Info");
  11735. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11736. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11737. Address(source,MPO-4);
  11738. Info(source, "type tag pointer");
  11739. Symbol( source, tag, offset, 0);
  11740. Info(source, "type flags");
  11741. flags := {};
  11742. IF isProtected THEN INCL(flags,31) END;
  11743. Set( source, flags);
  11744. Info(source, "pointer to module");
  11745. moduleSection := ModuleSection();
  11746. Symbol( source, moduleSection, moduleSection.pc,0);
  11747. Info(source, "type name");
  11748. i := 0;
  11749. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11750. (*
  11751. Global.GetSymbolSegmentedName(td,name);
  11752. Basic.SegmentedNameToString(name, sectionName);
  11753. *)
  11754. Name(source,sectionName);
  11755. source.SetReferenced(FALSE);
  11756. Size(source, 0);
  11757. RETURN source;
  11758. END NewTypeDescriptorInfo;
  11759. PROCEDURE NewTypeDescriptor;
  11760. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11761. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11762. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11763. numberPointers: LONGINT; padding, i: LONGINT;
  11764. CONST MPO=-40000000H;
  11765. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11766. VAR i: LONGINT;
  11767. PROCEDURE Td(record: SyntaxTree.RecordType);
  11768. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11769. BEGIN
  11770. IF record # NIL THEN
  11771. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11772. baseTD := record.typeDeclaration;
  11773. Global.GetSymbolSegmentedName(baseTD,name);
  11774. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11775. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11776. ELSE
  11777. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11778. END;
  11779. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11780. Symbol(source, tir, 0, offset);
  11781. IF reverse THEN Td(record.GetBaseRecord()) END;
  11782. END;
  11783. END Td;
  11784. BEGIN
  11785. Info(source, "tag table");
  11786. baseRecord := recordType;
  11787. i := 0;
  11788. WHILE baseRecord # NIL DO
  11789. INC(i);
  11790. baseRecord := baseRecord.GetBaseRecord();
  11791. END;
  11792. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11793. IF ~reverse THEN Td(recordType) END;
  11794. WHILE i < size DO
  11795. Address(source,0);
  11796. INC(i);
  11797. END;
  11798. IF reverse THEN Td(recordType) END;
  11799. END TdTable;
  11800. PROCEDURE MethodTable(reverse: BOOLEAN);
  11801. VAR i,methods: LONGINT;
  11802. BEGIN
  11803. Info(source, "method table");
  11804. IF recordType # NIL THEN
  11805. methods := recordType.recordScope.numberMethods;
  11806. IF reverse THEN
  11807. FOR i := methods-1 TO 0 BY -1 DO
  11808. procedure := recordType.recordScope.FindMethod(i);
  11809. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11810. NamedSymbol(source, name,procedure, 0,0);
  11811. END;
  11812. ELSE
  11813. FOR i := 0 TO methods-1 DO
  11814. procedure := recordType.recordScope.FindMethod(i);
  11815. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11816. NamedSymbol(source, name,procedure, 0,0);
  11817. END;
  11818. END;
  11819. END;
  11820. END MethodTable;
  11821. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11822. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11823. BEGIN
  11824. Info(source, "method table");
  11825. baseRecord := recordType;
  11826. WHILE baseRecord.baseType # NIL DO
  11827. baseRecord := baseRecord.GetBaseRecord ();
  11828. END;
  11829. GetRecordTypeName (baseRecord, name);
  11830. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11831. IF name = stackFrame THEN
  11832. start := 0;
  11833. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11834. baseRecord := recordType;
  11835. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11836. baseRecord := baseRecord.GetBaseRecord ();
  11837. END;
  11838. IF baseRecord # NIL THEN
  11839. GetRecordTypeName (baseRecord, name);
  11840. IF pointer & ~baseRecord.isObject THEN
  11841. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11842. END;
  11843. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11844. ELSIF recordType.isObject THEN
  11845. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11846. ELSIF pointer THEN
  11847. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11848. ELSE
  11849. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11850. END;
  11851. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11852. Symbol(source, tir, 0, 0);
  11853. start := 0;
  11854. baseRecord := recordType;
  11855. WHILE (baseRecord # NIL) DO
  11856. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11857. baseRecord := baseRecord.GetBaseRecord ();
  11858. END;
  11859. ELSE
  11860. (* explicit trace method: *)
  11861. procedure := recordType.recordScope.FindMethod(0);
  11862. IF ~procedure.isFinalizer THEN
  11863. Global.GetSymbolSegmentedName(procedure, name);
  11864. NamedSymbol(source, name,procedure, 0,0);
  11865. END;
  11866. start := 1;
  11867. END;
  11868. IF (name # stackFrame) & recordType.isObject THEN
  11869. baseRecord := recordType;
  11870. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11871. baseRecord := baseRecord.GetBaseRecord ();
  11872. END;
  11873. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11874. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11875. ELSE
  11876. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11877. END;
  11878. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11879. Symbol(source, tir, 0, 0);
  11880. END;
  11881. methods := recordType.recordScope.numberMethods;
  11882. FOR i := start TO methods-1 DO
  11883. procedure := recordType.recordScope.FindMethod(i);
  11884. IF ~procedure.isFinalizer THEN
  11885. Global.GetSymbolSegmentedName(procedure, name);
  11886. NamedSymbol(source, name,procedure, 0,0);
  11887. END;
  11888. END;
  11889. END CooperativeMethodTable;
  11890. BEGIN
  11891. Global.GetSymbolSegmentedName(td,name);
  11892. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11893. source.SetExported(IsExported(td));
  11894. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11895. IF implementationVisitor.backend.cooperative THEN
  11896. base := NIL;
  11897. baseRecord := recordType.GetBaseRecord();
  11898. IF baseRecord # NIL THEN
  11899. baseTD := baseRecord.typeDeclaration;
  11900. END;
  11901. IF ~recordType.isObject THEN
  11902. Info(source, "parent");
  11903. IF baseRecord # NIL THEN
  11904. Global.GetSymbolSegmentedName(baseTD,name);
  11905. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11906. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11907. ELSE
  11908. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11909. END;
  11910. Symbol(source, tir, 0, 0);
  11911. ELSE
  11912. Address(source,0);
  11913. END;
  11914. Info(source, "record size");
  11915. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11916. source.SetReferenced(FALSE);
  11917. CooperativeMethodTable(FALSE);
  11918. base := source;
  11919. Global.GetSymbolSegmentedName(td,name);
  11920. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11921. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11922. source.SetExported(IsExported(td));
  11923. source.SetReferenced(FALSE);
  11924. END;
  11925. Info(source, "parent");
  11926. IF baseRecord # NIL THEN
  11927. Global.GetSymbolSegmentedName(baseTD,name);
  11928. sym := baseTD;
  11929. IF ~recordType.isObject THEN
  11930. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11931. sym := NIL;
  11932. END;
  11933. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11934. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11935. ELSE
  11936. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11937. END;
  11938. Symbol(source, tir, 0, 0);
  11939. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11940. Address(source,0);
  11941. ELSE
  11942. IF recordType.isObject THEN
  11943. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11944. ELSE
  11945. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11946. END;
  11947. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11948. Symbol(source, tir, 0, 0);
  11949. END;
  11950. Info(source, "base record descriptor");
  11951. Symbol(source, base, 0, 0);
  11952. CooperativeMethodTable(TRUE);
  11953. source.SetReferenced(FALSE);
  11954. IF recordType.hasPointers THEN
  11955. IF ~HasExplicitTraceMethod (recordType) THEN
  11956. implementationVisitor.CreateTraceMethod(recordType);
  11957. END;
  11958. implementationVisitor.CreateResetProcedure(recordType);
  11959. implementationVisitor.CreateAssignProcedure(recordType);
  11960. END;
  11961. ELSIF ~simple THEN
  11962. (*
  11963. MethodEnd = MPO
  11964. ---
  11965. methods (# methods)
  11966. ---
  11967. tags (16)
  11968. ---
  11969. TypeDesc = TypeInfoAdr
  11970. ---
  11971. td adr ---> rec size
  11972. ----
  11973. pointer offsets
  11974. ----
  11975. (padding)
  11976. -----
  11977. empty [2 addresses aligned]
  11978. empty
  11979. empty
  11980. numPtrs
  11981. ---
  11982. pointer offsets
  11983. ---
  11984. *)
  11985. Info(source, "MethodEnd = MPO");
  11986. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11987. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11988. MethodTable(TRUE);
  11989. TdTable(TypeTags, TRUE);
  11990. Info(source, "type descriptor info pointer");
  11991. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11992. IF (cellType # NIL) THEN
  11993. IF cellType.sizeInBits < 0 THEN
  11994. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11995. END;
  11996. Info(source, "cell size");
  11997. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11998. ELSE
  11999. Info(source, "record size");
  12000. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12001. END;
  12002. Info(source, "pointer offsets pointer");
  12003. padding := 1- source.pc MOD 2;
  12004. Symbol(source, source, source.pc+1+padding,0);
  12005. IF padding >0 THEN
  12006. Info(source, "padding");
  12007. FOR i := 1 TO padding DO Address(source,0) END;
  12008. END;
  12009. IF cellType # NIL THEN
  12010. PointerArray(source, cellType.cellScope, numberPointers);
  12011. ELSE
  12012. PointerArray(source, recordType.recordScope, numberPointers);
  12013. END;
  12014. ELSE
  12015. (*
  12016. simple:
  12017. td adr --> size
  12018. tag(1)
  12019. tag(2)
  12020. tag(3)
  12021. methods ->
  12022. *)
  12023. Info(source, "record size");
  12024. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12025. TdTable(TypeTags, FALSE);
  12026. MethodTable(FALSE);
  12027. source.SetReferenced(FALSE);
  12028. END;
  12029. END NewTypeDescriptor;
  12030. BEGIN
  12031. x := x.resolved;
  12032. IF (x IS SyntaxTree.PointerType) THEN
  12033. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12034. END;
  12035. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12036. recordType := x(SyntaxTree.RecordType);
  12037. td := x.typeDeclaration;
  12038. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12039. ASSERT(td # NIL);
  12040. section := module.allSections.FindBySymbol(td); (* TODO *)
  12041. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12042. IF implementationVisitor.newObjectFile THEN
  12043. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12044. NewTypeDescriptor
  12045. END;
  12046. ELSE
  12047. (* data section in intermediate code *)
  12048. Global.GetSymbolSegmentedName(td,name);
  12049. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12050. Basic.SuffixSegmentedName (name, module.module.name);
  12051. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12052. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12053. tir.Emit(Data(Basic.invalidPosition,op));
  12054. END;
  12055. END;
  12056. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12057. cellType := x(SyntaxTree.CellType);
  12058. td := x.typeDeclaration;
  12059. section := module.allSections.FindBySymbol(td); (* TODO *)
  12060. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12061. IF implementationVisitor.newObjectFile THEN
  12062. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12063. NewTypeDescriptor
  12064. END;
  12065. END;
  12066. END;
  12067. END
  12068. END CheckTypeDeclaration
  12069. END MetaDataGenerator;
  12070. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12071. VAR
  12072. trace-: BOOLEAN;
  12073. traceString-: SyntaxTree.IdentifierString;
  12074. traceModuleName-: SyntaxTree.IdentifierString;
  12075. newObjectFile-: BOOLEAN;
  12076. profile-: BOOLEAN;
  12077. noRuntimeChecks: BOOLEAN;
  12078. simpleMetaData-: BOOLEAN;
  12079. noAsserts: BOOLEAN;
  12080. optimize-: BOOLEAN;
  12081. cooperative-: BOOLEAN;
  12082. preregisterStatic-: BOOLEAN;
  12083. dump-: Basic.Writer;
  12084. cellsAreObjects: BOOLEAN;
  12085. preciseGC: BOOLEAN;
  12086. PROCEDURE &InitIntermediateBackend*;
  12087. BEGIN
  12088. simpleMetaData := FALSE;
  12089. newObjectFile := FALSE;
  12090. InitBackend;
  12091. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12092. SetTraceModuleName(DefaultTraceModuleName);
  12093. END InitIntermediateBackend;
  12094. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12095. VAR
  12096. declarationVisitor: DeclarationVisitor;
  12097. implementationVisitor: ImplementationVisitor;
  12098. module: Sections.Module;
  12099. name, platformName: SyntaxTree.IdentifierString;
  12100. meta: MetaDataGenerator;
  12101. BEGIN
  12102. ResetError;
  12103. Global.GetSymbolName(x,name);
  12104. NEW(module,x,system); (* backend structures *)
  12105. Global.GetModuleName(x, name);
  12106. module.SetModuleName(name);
  12107. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12108. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12109. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12110. declarationVisitor.Module(x,module);
  12111. IF newObjectFile & ~meta.simple THEN
  12112. meta.Module(implementationVisitor.moduleBodySection);
  12113. END;
  12114. GetDescription(platformName);
  12115. module.SetPlatformName(platformName);
  12116. RETURN module
  12117. END GenerateIntermediate;
  12118. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12119. BEGIN RETURN TRUE
  12120. END SupportedImmediate;
  12121. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12122. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12123. END ProcessSyntaxTreeModule;
  12124. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12125. VAR
  12126. result: Sections.Module;
  12127. traceName: Basic.MessageString;
  12128. BEGIN
  12129. ASSERT(intermediateCodeModule IS Sections.Module);
  12130. result := intermediateCodeModule(Sections.Module);
  12131. IF trace THEN
  12132. traceName := "intermediate code trace: ";
  12133. Strings.Append(traceName,traceString);
  12134. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12135. IF (traceString="") OR (traceString="*") THEN
  12136. result.Dump(dump);
  12137. dump.Update
  12138. ELSE
  12139. Sections.DumpFiltered(dump, result, traceString);
  12140. END
  12141. END;
  12142. RETURN result
  12143. END ProcessIntermediateCodeModule;
  12144. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12145. BEGIN instructionSet := "Intermediate";
  12146. END GetDescription;
  12147. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12148. BEGIN
  12149. SELF.newObjectFile := newObjectFile;
  12150. SELF.simpleMetaData := simpleMetaData;
  12151. END SetNewObjectFile;
  12152. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12153. BEGIN COPY(name, traceModuleName)
  12154. END SetTraceModuleName;
  12155. PROCEDURE DefineOptions(options: Options.Options);
  12156. BEGIN
  12157. DefineOptions^(options);
  12158. options.Add(0X,"trace",Options.String);
  12159. options.Add(0X,"runtime",Options.String);
  12160. options.Add(0X,"newObjectFile",Options.Flag);
  12161. options.Add(0X,"traceModule",Options.String);
  12162. options.Add(0X,"profile",Options.Flag);
  12163. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12164. options.Add(0X,"noAsserts",Options.Flag);
  12165. options.Add(0X,"metaData",Options.String);
  12166. options.Add('o',"optimize", Options.Flag);
  12167. options.Add(0X,"preregisterStatic", Options.Flag);
  12168. options.Add(0X,"cellsAreObjects", Options.Flag);
  12169. options.Add(0X,"preciseGC", Options.Flag);
  12170. END DefineOptions;
  12171. PROCEDURE GetOptions(options: Options.Options);
  12172. VAR name,string: SyntaxTree.IdentifierString;
  12173. BEGIN
  12174. GetOptions^(options);
  12175. trace := options.GetString("trace",traceString);
  12176. profile := options.GetFlag("profile");
  12177. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12178. noAsserts := options.GetFlag("noAsserts");
  12179. cooperative := options.GetFlag("cooperative");
  12180. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12181. IF cooperative THEN
  12182. SetRuntimeModuleName("CPU") END
  12183. END;
  12184. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12185. simpleMetaData := TRUE
  12186. END;
  12187. IF options.GetString("metaData",string) THEN
  12188. IF string = "simple" THEN simpleMetaData := TRUE
  12189. ELSIF string ="full" THEN simpleMetaData := FALSE
  12190. END;
  12191. END;
  12192. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12193. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12194. optimize := options.GetFlag("optimize");
  12195. preregisterStatic := options.GetFlag("preregisterStatic");
  12196. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12197. preciseGC := options.GetFlag("preciseGC");
  12198. END GetOptions;
  12199. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12200. BEGIN RETURN SymbolFileFormat.Get()
  12201. END DefaultSymbolFileFormat;
  12202. END IntermediateBackend;
  12203. (* ----------------------------------- register allocation ------------------------------------- *)
  12204. (* register mapping scheme
  12205. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12206. spill offset
  12207. part(n) --> ticket <--> physical register
  12208. spill offset
  12209. *)
  12210. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12211. emptyOperand: IntermediateCode.Operand;
  12212. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12213. statCoopResetVariables: LONGINT;
  12214. statCoopModifyAssignments: LONGINT;
  12215. modifyAssignmentsPC : LONGINT;
  12216. statCoopNilCheck: LONGINT;
  12217. statCoopSwitch: LONGINT;
  12218. statCoopAssignProcedure: LONGINT;
  12219. statCoopTraceMethod: LONGINT;
  12220. statCoopResetProcedure: LONGINT;
  12221. statCoopTraceModule: LONGINT;
  12222. PROCEDURE ResetStatistics*;
  12223. BEGIN
  12224. statCoopResetVariables := 0;
  12225. statCoopModifyAssignments := 0;
  12226. statCoopNilCheck:= 0;
  12227. statCoopSwitch:= 0;
  12228. statCoopAssignProcedure:= 0;
  12229. statCoopTraceMethod:= 0;
  12230. statCoopResetProcedure:= 0;
  12231. statCoopTraceModule:= 0;
  12232. END ResetStatistics;
  12233. PROCEDURE Statistics*;
  12234. BEGIN
  12235. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12236. TRACE(statCoopNilCheck, statCoopSwitch);
  12237. TRACE(statCoopAssignProcedure,
  12238. statCoopTraceMethod,
  12239. statCoopResetProcedure,
  12240. statCoopTraceModule)
  12241. END Statistics;
  12242. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12243. VAR h: LONGINT;
  12244. BEGIN
  12245. WHILE b # 0 DO
  12246. h := a MOD b;
  12247. a := b;
  12248. b := h;
  12249. END;
  12250. RETURN a
  12251. END GCD;
  12252. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12253. BEGIN
  12254. RETURN a*b DIV GCD(a,b)
  12255. END SCM;
  12256. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12257. BEGIN
  12258. (*TRACE(a,b);*)
  12259. IF a = 0 THEN RETURN b
  12260. ELSIF b = 0 THEN RETURN a
  12261. ELSE RETURN SCM(a,b)
  12262. END;
  12263. END CommonAlignment;
  12264. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12265. BEGIN
  12266. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12267. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12268. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12269. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12270. RETURN ~(parameter.type.resolved IS SyntaxTree.ArrayType) & ~(parameter.type.resolved IS SyntaxTree.MathArrayType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12271. END
  12272. END PassBySingleReference;
  12273. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12274. BEGIN
  12275. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12276. END PassInRegister;
  12277. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12278. VAR new: RegisterEntry;
  12279. BEGIN
  12280. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12281. IF queue = NIL THEN
  12282. queue := new
  12283. ELSE
  12284. new.next := queue;
  12285. IF queue#NIL THEN queue.prev := new END;
  12286. queue := new
  12287. END;
  12288. END AddRegisterEntry;
  12289. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12290. VAR this: RegisterEntry;
  12291. BEGIN
  12292. this := queue;
  12293. WHILE (this # NIL) & (this.register # register) DO
  12294. this := this.next;
  12295. END;
  12296. IF this = NIL THEN
  12297. RETURN FALSE
  12298. END;
  12299. ASSERT(this # NIL);
  12300. IF this = queue THEN queue := queue.next END;
  12301. IF this.prev # NIL THEN this.prev.next := this.next END;
  12302. IF this.next # NIL THEN this.next.prev := this.prev END;
  12303. RETURN TRUE
  12304. END RemoveRegisterEntry;
  12305. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12306. BEGIN ASSERT(cond);
  12307. END Assert;
  12308. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12309. BEGIN
  12310. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12311. END ReusableRegister;
  12312. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12313. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12314. BEGIN
  12315. procedure := moduleScope.bodyProcedure;
  12316. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12317. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12318. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12319. procedure.SetScope(moduleScope);
  12320. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12321. procedure.SetAccess(SyntaxTree.Hidden);
  12322. moduleScope.SetBodyProcedure(procedure);
  12323. moduleScope.AddProcedure(procedure);
  12324. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12325. END;
  12326. END EnsureBodyProcedure;
  12327. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12328. VAR import: SyntaxTree.Import;
  12329. selfName: SyntaxTree.IdentifierString;
  12330. module: SyntaxTree.Module;
  12331. BEGIN
  12332. scope.ownerModule.GetName(selfName);
  12333. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12334. module := scope.ownerModule
  12335. ELSE
  12336. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12337. IF import = NIL THEN
  12338. RETURN NIL
  12339. ELSIF import.module = NIL THEN
  12340. RETURN NIL
  12341. ELSE module := import.module
  12342. END;
  12343. END;
  12344. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12345. END GetSymbol;
  12346. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12347. BEGIN
  12348. op.mode := mode;
  12349. IntermediateCode.InitOperand(op.op);
  12350. IntermediateCode.InitOperand(op.tag);
  12351. IntermediateCode.InitOperand(op.extra);
  12352. op.dimOffset := 0;
  12353. END InitOperand;
  12354. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12355. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12356. BEGIN RETURN IntermediateCode.GetType(system, type)
  12357. END GetType;
  12358. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12359. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12360. BEGIN
  12361. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12362. (*
  12363. UniqueId(name,module,name,adr);
  12364. *)
  12365. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12366. constant.SetValue(value);
  12367. constant.SetAccess(SyntaxTree.Hidden);
  12368. module.moduleScope.AddConstant(constant);
  12369. constant.SetScope(module.moduleScope);
  12370. RETURN constant
  12371. END BuildConstant;
  12372. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12373. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12374. BEGIN
  12375. variable := scope.firstVariable;
  12376. WHILE variable # NIL DO
  12377. IF variable.NeedsTrace() THEN
  12378. RETURN TRUE;
  12379. END;
  12380. variable := variable.nextVariable;
  12381. END;
  12382. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12383. WHILE parameter # NIL DO
  12384. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12385. RETURN TRUE;
  12386. END;
  12387. parameter := parameter.nextParameter;
  12388. END;
  12389. RETURN FALSE;
  12390. END HasPointers;
  12391. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12392. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12393. END IsVariableParameter;
  12394. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12395. VAR parameter: SyntaxTree.Parameter;
  12396. BEGIN
  12397. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12398. WHILE parameter # NIL DO
  12399. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12400. IF parameter.movable THEN RETURN TRUE END;
  12401. parameter := parameter.nextParameter;
  12402. END;
  12403. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12404. END HasVariableParameters;
  12405. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12406. BEGIN
  12407. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12408. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12409. END HasExplicitTraceMethod;
  12410. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12411. BEGIN
  12412. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12413. IF (expression IS SyntaxTree.IntegerValue) THEN
  12414. val := expression(SyntaxTree.IntegerValue).value;
  12415. RETURN TRUE
  12416. ELSE
  12417. RETURN FALSE
  12418. END;
  12419. END IsIntegerConstant;
  12420. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12421. BEGIN
  12422. IF val <= 0 THEN RETURN FALSE END;
  12423. exp := 0;
  12424. WHILE ~ODD(val) DO
  12425. val := val DIV 2;
  12426. INC(exp)
  12427. END;
  12428. RETURN val = 1
  12429. END PowerOf2;
  12430. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12431. VAR procedure: SyntaxTree.Procedure;
  12432. BEGIN
  12433. procedure := record.recordScope.constructor;
  12434. IF procedure = NIL THEN
  12435. record := record.GetBaseRecord();
  12436. IF record # NIL THEN
  12437. procedure := GetConstructor(record)
  12438. END;
  12439. END;
  12440. RETURN procedure;
  12441. END GetConstructor;
  12442. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12443. BEGIN
  12444. value := SHORT(op.intValue);
  12445. RETURN op.mode = IntermediateCode.ModeImmediate;
  12446. END IsIntegerImmediate;
  12447. (** whether a type strictily is a pointer to record or object type
  12448. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12449. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12450. BEGIN
  12451. IF type = NIL THEN
  12452. RETURN FALSE
  12453. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12454. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12455. ELSE
  12456. RETURN FALSE
  12457. END
  12458. END IsStrictlyPointerToRecord;
  12459. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12460. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12461. END IsUnsafePointer;
  12462. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12463. BEGIN type := type.resolved;
  12464. IF type IS SyntaxTree.PointerType THEN
  12465. type := type(SyntaxTree.PointerType).pointerBase;
  12466. type := type.resolved;
  12467. IF type IS SyntaxTree.RecordType THEN
  12468. recordType := type(SyntaxTree.RecordType);
  12469. RETURN TRUE
  12470. ELSE
  12471. RETURN FALSE
  12472. END
  12473. ELSIF type IS SyntaxTree.RecordType THEN
  12474. recordType := type(SyntaxTree.RecordType);
  12475. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12476. ELSIF type IS SyntaxTree.ObjectType THEN
  12477. RETURN TRUE
  12478. ELSIF type IS SyntaxTree.AnyType THEN
  12479. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12480. ELSE
  12481. RETURN FALSE
  12482. END;
  12483. END IsPointerToRecord;
  12484. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12485. BEGIN
  12486. type := type.resolved;
  12487. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12488. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12489. END IsArrayOfSystemByte;
  12490. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12491. BEGIN
  12492. IF type = NIL THEN RETURN FALSE END;
  12493. type := type.resolved;
  12494. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12495. END IsOpenArray;
  12496. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12497. BEGIN
  12498. IF type = NIL THEN RETURN FALSE END;
  12499. type := type.resolved;
  12500. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12501. END IsSemiDynamicArray;
  12502. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12503. BEGIN
  12504. IF type = NIL THEN RETURN FALSE END;
  12505. type := type.resolved;
  12506. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12507. END IsStaticArray;
  12508. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12509. VAR size: LONGINT;
  12510. BEGIN
  12511. size := 1;
  12512. WHILE (IsStaticArray(type)) DO
  12513. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12514. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12515. END;
  12516. RETURN size;
  12517. END StaticArrayNumElements;
  12518. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12519. BEGIN
  12520. WHILE (IsStaticArray(type)) DO
  12521. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12522. END;
  12523. RETURN type;
  12524. END StaticArrayBaseType;
  12525. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12526. BEGIN
  12527. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12528. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12529. END;
  12530. RETURN type;
  12531. END ArrayBaseType;
  12532. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12533. BEGIN
  12534. IF type = NIL THEN RETURN FALSE END;
  12535. type := type.resolved;
  12536. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12537. END IsDelegate;
  12538. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12539. VAR i: LONGINT;
  12540. BEGIN
  12541. i := 0; type := type.resolved;
  12542. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12543. INC(i);
  12544. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12545. END;
  12546. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12547. INC(i);
  12548. type := type(SyntaxTree.MathArrayType).arrayBase;
  12549. IF type # NIL THEN type := type.resolved END;
  12550. END;
  12551. RETURN i
  12552. END DynamicDim;
  12553. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12554. BEGIN
  12555. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12556. type := type(SyntaxTree.ArrayType).arrayBase;
  12557. END;
  12558. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12559. type := type(SyntaxTree.MathArrayType).arrayBase;
  12560. END;
  12561. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12562. END StaticSize;
  12563. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12564. BEGIN
  12565. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12566. END IsImmediate;
  12567. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12568. BEGIN
  12569. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12570. END IsAddress;
  12571. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12572. BEGIN
  12573. RETURN (x.mode = IntermediateCode.ModeRegister);
  12574. END IsRegister;
  12575. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12576. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12577. BEGIN
  12578. typeDeclaration := recordType.typeDeclaration;
  12579. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12580. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12581. END GetRecordTypeName;
  12582. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12583. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12584. BEGIN
  12585. parSize := 0;
  12586. IF SemanticChecker.StructuredReturnType(procedureType) THEN
  12587. parameter := procedureType.returnParameter;
  12588. INC(parSize,system.SizeOfParameter(parameter));
  12589. parSize := parSize + (-parSize) MOD system.addressSize;
  12590. END;
  12591. parameter :=procedureType.lastParameter;
  12592. WHILE (parameter # NIL) DO
  12593. INC(parSize,system.SizeOfParameter(parameter));
  12594. parSize := parSize + (-parSize) MOD system.addressSize;
  12595. parameter := parameter.prevParameter;
  12596. END;
  12597. IF procedureType.selfParameter # NIL THEN
  12598. parameter := procedureType.selfParameter;
  12599. INC(parSize,system.SizeOfParameter(parameter));
  12600. parSize := parSize + (-parSize) MOD system.addressSize;
  12601. ELSIF procedureType.isDelegate THEN INC(parSize,system.addressSize)
  12602. END; (* method => self pointer *)
  12603. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12604. RETURN ToMemoryUnits(system,parSize)
  12605. END ParametersSize;
  12606. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12607. BEGIN
  12608. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12609. END IsNested;
  12610. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12611. BEGIN
  12612. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12613. scope := scope.outerScope;
  12614. END;
  12615. RETURN scope # NIL;
  12616. END InCellScope;
  12617. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12618. BEGIN
  12619. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12620. RETURN 0
  12621. ELSE
  12622. *)
  12623. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12624. (*END;*)
  12625. END ProcedureParametersSize;
  12626. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12627. VAR dataUnit: LONGINT;
  12628. BEGIN dataUnit := system.dataUnit;
  12629. ASSERT(size MOD system.dataUnit = 0);
  12630. RETURN size DIV system.dataUnit
  12631. END ToMemoryUnits;
  12632. PROCEDURE Get*(): Backend.Backend;
  12633. VAR backend: IntermediateBackend;
  12634. BEGIN NEW(backend); RETURN backend
  12635. END Get;
  12636. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12637. VAR instruction: IntermediateCode.Instruction;
  12638. BEGIN
  12639. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12640. RETURN instruction
  12641. END Nop;
  12642. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12643. VAR instruction: IntermediateCode.Instruction;
  12644. BEGIN
  12645. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12646. RETURN instruction
  12647. END Mov;
  12648. (* like Mov but ensures that no new register will be allocated for dest *)
  12649. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12650. VAR instruction: IntermediateCode.Instruction;
  12651. BEGIN
  12652. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12653. RETURN instruction
  12654. END MovReplace;
  12655. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12656. VAR instruction: IntermediateCode.Instruction;
  12657. BEGIN
  12658. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12659. RETURN instruction
  12660. END Conv;
  12661. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12662. BEGIN
  12663. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12664. END SysvABI;
  12665. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12666. BEGIN
  12667. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12668. END SysvABIorWINAPI;
  12669. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12670. VAR instruction: IntermediateCode.Instruction;
  12671. BEGIN
  12672. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12673. RETURN instruction
  12674. END Call;
  12675. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12676. VAR op1, op2, op3: IntermediateCode.Operand;
  12677. VAR instruction: IntermediateCode.Instruction;
  12678. BEGIN
  12679. IntermediateCode.InitNumber(op1,pcOffset);
  12680. IntermediateCode.InitNumber(op2,callingConvention);
  12681. IntermediateCode.InitNumber(op3,unwind);
  12682. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12683. RETURN instruction
  12684. END Exit;
  12685. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12686. VAR instruction: IntermediateCode.Instruction;
  12687. BEGIN
  12688. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12689. RETURN instruction
  12690. END Return;
  12691. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12692. VAR instruction: IntermediateCode.Instruction;
  12693. BEGIN
  12694. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12695. RETURN instruction
  12696. END Result;
  12697. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12698. VAR op1: IntermediateCode.Operand;
  12699. VAR instruction: IntermediateCode.Instruction;
  12700. BEGIN
  12701. IntermediateCode.InitNumber(op1,nr);
  12702. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12703. RETURN instruction
  12704. END Trap;
  12705. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12706. VAR instruction: IntermediateCode.Instruction;
  12707. BEGIN
  12708. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12709. RETURN instruction
  12710. END Br;
  12711. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12712. VAR instruction: IntermediateCode.Instruction;
  12713. BEGIN
  12714. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12715. RETURN instruction
  12716. END Breq;
  12717. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12718. VAR instruction: IntermediateCode.Instruction;
  12719. BEGIN
  12720. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12721. RETURN instruction
  12722. END Brne;
  12723. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12724. VAR instruction: IntermediateCode.Instruction;
  12725. BEGIN
  12726. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12727. RETURN instruction
  12728. END Brge;
  12729. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12730. VAR instruction: IntermediateCode.Instruction;
  12731. BEGIN
  12732. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12733. RETURN instruction
  12734. END Brlt;
  12735. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12736. VAR instruction: IntermediateCode.Instruction;
  12737. BEGIN
  12738. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12739. RETURN instruction
  12740. END Pop;
  12741. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12742. VAR instruction: IntermediateCode.Instruction;
  12743. BEGIN
  12744. ASSERT(op.mode # IntermediateCode.Undefined);
  12745. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12746. RETURN instruction
  12747. END Push;
  12748. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12749. VAR instruction: IntermediateCode.Instruction;
  12750. BEGIN
  12751. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12752. RETURN instruction
  12753. END Neg;
  12754. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12755. VAR instruction: IntermediateCode.Instruction;
  12756. BEGIN
  12757. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12758. RETURN instruction
  12759. END Not;
  12760. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12761. VAR instruction: IntermediateCode.Instruction;
  12762. BEGIN
  12763. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12764. RETURN instruction
  12765. END Abs;
  12766. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12767. VAR instruction: IntermediateCode.Instruction;
  12768. BEGIN
  12769. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12770. ASSERT(~IsImmediate(instruction.op1));
  12771. RETURN instruction
  12772. END Mul;
  12773. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12774. VAR instruction: IntermediateCode.Instruction;
  12775. BEGIN
  12776. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12777. RETURN instruction
  12778. END Div;
  12779. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12780. VAR instruction: IntermediateCode.Instruction;
  12781. BEGIN
  12782. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12783. RETURN instruction
  12784. END Mod;
  12785. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12786. VAR instruction: IntermediateCode.Instruction;
  12787. BEGIN
  12788. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12789. RETURN instruction
  12790. END Sub;
  12791. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12792. VAR instruction: IntermediateCode.Instruction;
  12793. BEGIN
  12794. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12795. RETURN instruction
  12796. END Add;
  12797. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12798. VAR instruction: IntermediateCode.Instruction;
  12799. BEGIN
  12800. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12801. RETURN instruction
  12802. END And;
  12803. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12804. VAR instruction: IntermediateCode.Instruction;
  12805. BEGIN
  12806. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12807. RETURN instruction
  12808. END Or;
  12809. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12810. VAR instruction: IntermediateCode.Instruction;
  12811. BEGIN
  12812. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12813. RETURN instruction
  12814. END Xor;
  12815. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12816. VAR instruction: IntermediateCode.Instruction;
  12817. BEGIN
  12818. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12819. RETURN instruction
  12820. END Shl;
  12821. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12822. VAR instruction: IntermediateCode.Instruction;
  12823. BEGIN
  12824. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12825. RETURN instruction
  12826. END Shr;
  12827. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12828. VAR instruction: IntermediateCode.Instruction;
  12829. BEGIN
  12830. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12831. RETURN instruction
  12832. END Rol;
  12833. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12834. VAR instruction: IntermediateCode.Instruction;
  12835. BEGIN
  12836. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12837. RETURN instruction
  12838. END Ror;
  12839. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12840. VAR instruction: IntermediateCode.Instruction;
  12841. BEGIN
  12842. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12843. RETURN instruction
  12844. END Cas;
  12845. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12846. VAR instruction: IntermediateCode.Instruction;
  12847. BEGIN
  12848. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12849. RETURN instruction
  12850. END Copy;
  12851. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12852. VAR instruction: IntermediateCode.Instruction;
  12853. BEGIN
  12854. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12855. RETURN instruction
  12856. END Fill;
  12857. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12858. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12859. BEGIN
  12860. string := IntermediateCode.String(s);
  12861. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12862. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12863. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12864. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12865. RETURN instruction
  12866. END Asm;
  12867. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12868. VAR instruction: IntermediateCode.Instruction;
  12869. BEGIN
  12870. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12871. RETURN instruction
  12872. END Data;
  12873. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12874. VAR instruction: IntermediateCode.Instruction;
  12875. BEGIN
  12876. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12877. IntermediateCode.SetSubType(instruction, subtype);
  12878. RETURN instruction
  12879. END SpecialInstruction;
  12880. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12881. VAR op1: IntermediateCode.Operand;
  12882. VAR instruction: IntermediateCode.Instruction;
  12883. BEGIN
  12884. (*! generate a warning if size exceeds a certain limit *)
  12885. (*
  12886. ASSERT(bytes < 1000000); (* sanity check *)
  12887. *)
  12888. ASSERT(0 <= units); (* sanity check *)
  12889. IntermediateCode.InitNumber(op1,units);
  12890. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12891. RETURN instruction
  12892. END Reserve;
  12893. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12894. VAR op1: IntermediateCode.Operand;
  12895. VAR instruction: IntermediateCode.Instruction;
  12896. BEGIN
  12897. IntermediateCode.InitNumber(op1,position.start);
  12898. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12899. RETURN instruction
  12900. END LabelInstruction;
  12901. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12902. VAR pc: LONGINT;
  12903. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12904. BEGIN
  12905. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12906. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12907. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12908. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12909. IF instr.op1.type.form = IntermediateCode.Float THEN
  12910. RETURN instr.op1.floatValue = op.floatValue
  12911. ELSE
  12912. RETURN instr.op1.intValue = op.intValue
  12913. END;
  12914. END ProvidesValue;
  12915. BEGIN
  12916. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12917. pc := 0;
  12918. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12919. INC(pc);
  12920. END;
  12921. IF pc = data.pc THEN
  12922. data.Emit(Data(Basic.invalidPosition,vop));
  12923. END;
  12924. RETURN pc
  12925. END EnterImmediate;
  12926. PROCEDURE Init;
  12927. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12928. BEGIN
  12929. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12930. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12931. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12932. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12933. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12934. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12935. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12936. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12937. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12938. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12939. IntermediateCode.InitOperand(emptyOperand);
  12940. FOR i := 0 TO NumberSystemCalls-1 DO
  12941. name := "@SystemCall";
  12942. Basic.AppendNumber(name,i);
  12943. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12944. END;
  12945. END Init;
  12946. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12947. BEGIN
  12948. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12949. END IsExported;
  12950. BEGIN
  12951. Init;
  12952. END FoxIntermediateBackend.
  12953. Compiler.Compile FoxIntermediateBackend.Mod ~
  12954. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12955. # StaticLinker.Link --fileFormat=PE32 --fileName=A2Z.exe --extension=GofW --displacement=401000H --path="/temp/obg/" Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands Files WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~
  12956. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12957. # StaticLinker.Link --fileFormat=PE32 --fileName=A2H.exe --extension=GofW --displacement=401000H Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands Files WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~
  12958. FSTools.CloseFiles A2Z.exe ~
  12959. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12960. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12961. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12962. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12963. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12964. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12965. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12966. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12967. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12968. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12969. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod