FoxIntermediateBackend.Mod 558 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225
  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. Reuse2a(result.op,left.op,right.op,dest);
  3007. Emit(Add(position,result.op,left.op,right.op));
  3008. END;
  3009. END;
  3010. ReleaseOperand(left); ReleaseOperand(right);
  3011. |Scanner.Minus:
  3012. Evaluate(x.left,left);
  3013. Evaluate(x.right,right);
  3014. IF leftType IS SyntaxTree.SetType THEN
  3015. InitOperand(result,ModeValue);
  3016. Reuse1(result.op,right.op);
  3017. Emit(Not(position,result.op,right.op));
  3018. ReleaseOperand(right);
  3019. Emit(And(position,result.op,result.op,left.op));
  3020. ReleaseOperand(left);
  3021. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3022. InitOperand(result,ModeValue);
  3023. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3024. Reuse2(result.tag,left.tag,right.tag);
  3025. Emit(Sub(position,result.op,left.op,right.op));
  3026. Emit(Sub(position,result.tag,left.tag,right.tag));
  3027. ReleaseOperand(left); ReleaseOperand(right)
  3028. ELSE
  3029. InitOperand(result,ModeValue);
  3030. Reuse2a(result.op,left.op,right.op,dest);
  3031. Emit(Sub(position,result.op,left.op,right.op));
  3032. ReleaseOperand(left); ReleaseOperand(right);
  3033. END;
  3034. |Scanner.Times:
  3035. Evaluate(x.left,left);
  3036. Evaluate(x.right,right);
  3037. IF leftType IS SyntaxTree.SetType THEN
  3038. InitOperand(result,ModeValue);
  3039. Reuse2a(result.op,left.op,right.op,dest);
  3040. Emit(And(position,result.op,left.op,right.op));
  3041. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3042. InitOperand(result, ModeValue);
  3043. componentType := left.op.type;
  3044. (* TODO: review this *)
  3045. (*
  3046. result.op = left.op * right.op - left.tag * right.tag
  3047. result.tag = left.tag * right.op + left.op * right.tag
  3048. *)
  3049. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3050. Emit(Mul(position,result.op, left.op, right.op));
  3051. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3052. Emit(Mul(position,tempReg, left.tag, right.tag));
  3053. Emit(Sub(position,result.op, result.op, tempReg));
  3054. Reuse2(result.tag, left.tag, right.op);
  3055. Emit(Mul(position,result.tag, left.tag, right.op));
  3056. Emit(Mul(position,tempReg, left.op, right.tag));
  3057. Emit(Add(position,result.tag, result.tag, tempReg));
  3058. ReleaseIntermediateOperand(tempReg)
  3059. ELSE
  3060. InitOperand(result,ModeValue);
  3061. Reuse2a(result.op,left.op,right.op,dest);
  3062. Emit(Mul(position,result.op,left.op,right.op));
  3063. END;
  3064. ReleaseOperand(left); ReleaseOperand(right);
  3065. |Scanner.Div:
  3066. Evaluate(x.left,left);
  3067. Evaluate(x.right,right);
  3068. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3069. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3070. IF ~isUnchecked THEN
  3071. exit := NewLabel();
  3072. BrltL(exit,zero,right.op);
  3073. EmitTrap(position,NegativeDivisorTrap);
  3074. SetLabel(exit);
  3075. END;
  3076. END;
  3077. InitOperand(result,ModeValue);
  3078. Reuse2a(result.op,left.op,right.op,dest);
  3079. Emit(Div(position,result.op,left.op,right.op));
  3080. ReleaseOperand(left); ReleaseOperand(right);
  3081. |Scanner.Mod:
  3082. Evaluate(x.left,left);
  3083. Evaluate(x.right,right);
  3084. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3085. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3086. IF ~isUnchecked THEN
  3087. exit := NewLabel();
  3088. BrltL(exit,zero,right.op);
  3089. EmitTrap(position,NegativeDivisorTrap);
  3090. SetLabel(exit);
  3091. END;
  3092. END;
  3093. InitOperand(result,ModeValue);
  3094. Reuse2a(result.op,left.op,right.op,dest);
  3095. Emit(Mod(position,result.op,left.op,right.op));
  3096. ReleaseOperand(left); ReleaseOperand(right);
  3097. |Scanner.Slash:
  3098. Evaluate(x.left,left);
  3099. Evaluate(x.right,right);
  3100. IF leftType IS SyntaxTree.SetType THEN
  3101. InitOperand(result,ModeValue);
  3102. Reuse2a(result.op,left.op,right.op,dest);
  3103. Emit(Xor(position,result.op,left.op,right.op));
  3104. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3105. InitOperand(result,ModeValue);
  3106. componentType := left.op.type;
  3107. (* review this *)
  3108. (*
  3109. divisor = right.op * right.op + right.tag * right.tag
  3110. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3111. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3112. *)
  3113. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3114. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3115. Emit(Mul(position,tempReg, right.op, right.op));
  3116. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3117. Emit(Add(position,tempReg, tempReg, tempReg2));
  3118. result.op := tempReg2;
  3119. Emit(Mul(position,result.op, left.op, right.op));
  3120. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3121. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3122. Emit(Add(position,result.op, result.op, tempReg2));
  3123. Emit(Div(position,result.op, result.op, tempReg));
  3124. Reuse2(result.tag, left.tag, right.op);
  3125. Emit(Mul(position,result.tag, left.tag, right.op));
  3126. Emit(Mul(position,tempReg2, left.op, right.tag));
  3127. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3128. Emit(Div(position,result.tag, result.tag, tempReg));
  3129. ReleaseIntermediateOperand(tempReg);
  3130. ReleaseIntermediateOperand(tempReg2)
  3131. ELSE
  3132. InitOperand(result,ModeValue);
  3133. Reuse2a(result.op,left.op,right.op,dest);
  3134. Emit(Div(position,result.op,left.op,right.op));
  3135. END;
  3136. ReleaseOperand(left); ReleaseOperand(right);
  3137. |Scanner.Equal:
  3138. IF ~conditional THEN ConditionToValue(x);
  3139. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3140. CompareString(BreqL,x.left,x.right);
  3141. ELSE
  3142. Evaluate(x.left,left);
  3143. Evaluate(x.right,right);
  3144. IF leftType IS SyntaxTree.RangeType THEN
  3145. ASSERT(rightType IS SyntaxTree.RangeType);
  3146. BrneL(falseLabel, left.op, right.op); (* first *)
  3147. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3148. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3149. ReleaseOperand(left); ReleaseOperand(right);
  3150. BrL(trueLabel)
  3151. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3152. BrneL(falseLabel, left.op, right.op); (* first *)
  3153. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3154. ReleaseOperand(left); ReleaseOperand(right);
  3155. BrL(trueLabel)
  3156. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3157. (* TODO: review this *)
  3158. BrneL(falseLabel, left.op, right.op); (* real part *)
  3159. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3160. ReleaseOperand(left); ReleaseOperand(right);
  3161. BrL(trueLabel)
  3162. ELSE
  3163. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3164. ReleaseOperand(left); ReleaseOperand(right);
  3165. BrL(trueLabel);
  3166. END;
  3167. END;
  3168. |Scanner.LessEqual:
  3169. IF ~conditional THEN ConditionToValue(x);
  3170. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3171. CompareString(BrgeL,x.right,x.left);
  3172. ELSE
  3173. Evaluate(x.left,left);
  3174. Evaluate(x.right,right);
  3175. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3176. Reuse1(temp.op,right.op);
  3177. Emit(And(position,temp.op,left.op,right.op));
  3178. ReleaseOperand(right);
  3179. BreqL(trueLabel,temp.op,left.op);
  3180. BrL(falseLabel);
  3181. ReleaseOperand(temp);ReleaseOperand(left);
  3182. ELSE
  3183. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3184. ReleaseOperand(left); ReleaseOperand(right);
  3185. BrL(trueLabel);
  3186. END;
  3187. END;
  3188. |Scanner.Less:
  3189. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3190. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3191. leftExpression.SetType(system.booleanType);
  3192. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3193. rightExpression.SetType(system.booleanType);
  3194. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3195. leftExpression.SetType(system.booleanType);
  3196. Expression(leftExpression);
  3197. ELSIF ~conditional THEN ConditionToValue(x);
  3198. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3199. CompareString(BrltL,x.left,x.right);
  3200. ELSE
  3201. Evaluate(x.left,left);
  3202. Evaluate(x.right,right);
  3203. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3204. ReleaseOperand(left); ReleaseOperand(right);
  3205. BrL(trueLabel);
  3206. END;
  3207. |Scanner.Greater:
  3208. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3209. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3210. leftExpression.SetType(system.booleanType);
  3211. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3212. rightExpression.SetType(system.booleanType);
  3213. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3214. leftExpression.SetType(system.booleanType);
  3215. Expression(leftExpression);
  3216. ELSIF ~conditional THEN ConditionToValue(x);
  3217. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3218. CompareString(BrltL,x.right,x.left);
  3219. ELSE
  3220. Evaluate(x.left,left);
  3221. Evaluate(x.right,right);
  3222. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3223. ReleaseOperand(left); ReleaseOperand(right);
  3224. BrL(trueLabel);
  3225. END;
  3226. |Scanner.GreaterEqual:
  3227. IF ~conditional THEN ConditionToValue(x);
  3228. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3229. CompareString(BrgeL,x.left,x.right);
  3230. ELSE
  3231. Evaluate(x.left,left);
  3232. Evaluate(x.right,right);
  3233. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3234. Reuse1(temp.op,left.op);
  3235. Emit(And(position,temp.op,left.op,right.op));
  3236. ReleaseOperand(left);
  3237. BreqL(trueLabel, temp.op,right.op);
  3238. ReleaseOperand(temp); ReleaseOperand(right);
  3239. BrL(falseLabel);
  3240. ELSE
  3241. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3242. ReleaseOperand(left); ReleaseOperand(right);
  3243. BrL(trueLabel);
  3244. END;
  3245. END;
  3246. |Scanner.Unequal:
  3247. IF ~conditional THEN ConditionToValue(x);
  3248. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3249. CompareString(BrneL,x.left,x.right);
  3250. ELSE
  3251. Evaluate(x.left,left);
  3252. Evaluate(x.right,right);
  3253. IF leftType IS SyntaxTree.RangeType THEN
  3254. ASSERT(rightType IS SyntaxTree.RangeType);
  3255. BrneL(trueLabel, left.op, right.op); (* first *)
  3256. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3257. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3258. ReleaseOperand(left); ReleaseOperand(right);
  3259. BrL(falseLabel)
  3260. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3261. BrneL(trueLabel, left.op, right.op); (* first *)
  3262. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3263. ReleaseOperand(left); ReleaseOperand(right);
  3264. BrL(falseLabel)
  3265. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3266. (* TODO: review this *)
  3267. BrneL(trueLabel, left.op, right.op); (* real part *)
  3268. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3269. ReleaseOperand(left); ReleaseOperand(right);
  3270. BrL(falseLabel)
  3271. ELSE
  3272. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3273. ReleaseOperand(left); ReleaseOperand(right);
  3274. BrL(trueLabel);
  3275. END;
  3276. END;
  3277. |Scanner.In:
  3278. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3279. Evaluate(x.left,left);
  3280. Evaluate(x.right,right);
  3281. Convert(left.op,setType);
  3282. ReuseCopy(temp.op,right.op);
  3283. Emit(Shr(position,temp.op,temp.op,left.op));
  3284. ReleaseOperand(right); ReleaseOperand(left);
  3285. IntermediateCode.InitImmediate(one,setType,1);
  3286. Emit(And(position,temp.op,temp.op,one));
  3287. IF conditional THEN
  3288. IntermediateCode.InitImmediate(zero,setType,0);
  3289. BrneL(trueLabel,temp.op,zero);
  3290. ReleaseOperand(temp);
  3291. BrL(falseLabel);
  3292. ELSE
  3293. Convert(temp.op,bool);
  3294. result.mode := ModeValue;
  3295. result.op := temp.op;
  3296. result.tag := nil; (* may be left over from calls to evaluate *)
  3297. END;
  3298. ELSE
  3299. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3300. IF x.operator = Scanner.Questionmarks THEN
  3301. leftExpression := x.left;
  3302. rightExpression := x.right;
  3303. ELSE
  3304. leftExpression := x.right;
  3305. rightExpression := x.left;
  3306. END;
  3307. Evaluate(leftExpression, left);
  3308. Emit(Push(position,left.op));
  3309. ReleaseOperand(left);
  3310. Designate(rightExpression, right);
  3311. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3312. IF ~backend.cellsAreObjects THEN
  3313. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3314. END;
  3315. Emit(Push(position,right.op));
  3316. ReleaseOperand(right);
  3317. IF backend.cellsAreObjects THEN
  3318. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3319. ELSE
  3320. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3321. END;
  3322. InitOperand(result, ModeValue);
  3323. result.op := NewRegisterOperand(bool);
  3324. Emit(Result(position,result.op));
  3325. IF conditional THEN
  3326. IntermediateCode.InitImmediate(zero,setType,0);
  3327. BrneL(trueLabel,result.op,zero);
  3328. ReleaseOperand(result);
  3329. BrL(falseLabel);
  3330. END;
  3331. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3332. leftExpression := x.left;
  3333. rightExpression := x.right;
  3334. Evaluate(leftExpression, left);
  3335. Emit(Push(position,left.op));
  3336. ReleaseOperand(left);
  3337. Evaluate(rightExpression, right);
  3338. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3339. IF ~backend.cellsAreObjects THEN
  3340. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3341. END;
  3342. Emit(Push(position,right.op));
  3343. ReleaseOperand(right);
  3344. IF backend.cellsAreObjects THEN
  3345. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3346. ELSE
  3347. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3348. END;
  3349. InitOperand(result, ModeValue);
  3350. result.op := NewRegisterOperand(bool);
  3351. Emit(Result(position,result.op));
  3352. IF conditional THEN
  3353. IntermediateCode.InitImmediate(zero,setType,0);
  3354. BrneL(trueLabel,result.op,zero);
  3355. ReleaseOperand(result);
  3356. BrL(falseLabel);
  3357. END;
  3358. ELSE
  3359. HALT(100);
  3360. END;
  3361. END;
  3362. destination := dest;
  3363. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3364. END VisitBinaryExpression;
  3365. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3366. VAR localResult, operand: Operand;
  3367. BEGIN
  3368. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3369. InitOperand(localResult, ModeValue);
  3370. ASSERT(x.first # NIL);
  3371. Evaluate(x.first, operand);
  3372. localResult.op := operand.op;
  3373. ReleaseOperand(operand);
  3374. UseIntermediateOperand(localResult.op);
  3375. ASSERT(x.last # NIL);
  3376. Evaluate(x.last, operand);
  3377. localResult.tag := operand.op;
  3378. ReleaseOperand(operand);
  3379. UseIntermediateOperand(localResult.tag);
  3380. IF x.step # NIL THEN
  3381. Evaluate(x.step, operand);
  3382. localResult.extra := operand.op;
  3383. ReleaseOperand(operand);
  3384. UseIntermediateOperand(localResult.extra);
  3385. END;
  3386. result := localResult;
  3387. IF Trace THEN TraceExit("VisitRangeExpression") END
  3388. END VisitRangeExpression;
  3389. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3390. BEGIN
  3391. HALT(100); (* should never be evaluated *)
  3392. END VisitTensorRangeExpression;
  3393. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3394. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3395. BEGIN
  3396. IF Trace THEN TraceEnter("VisitConversion") END;
  3397. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3398. dest := destination; destination := emptyOperand;
  3399. Evaluate(x.expression,old);
  3400. InitOperand(result,ModeValue);
  3401. result.op := old.op;
  3402. ASSERT(result.op.mode # 0);
  3403. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3404. (* convert TO a complex number *)
  3405. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3406. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3407. ASSERT(result.op.mode # 0);
  3408. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3409. (* convert FROM a complex number TO a complex number*)
  3410. result.tag := old.tag;
  3411. ASSERT(result.tag.mode # 0);
  3412. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3413. ASSERT(result.tag.mode # 0)
  3414. ELSE
  3415. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3416. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3417. END
  3418. ELSE
  3419. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3420. ASSERT(result.op.mode # 0);
  3421. result.tag := old.tag; (*! probably never used *)
  3422. END;
  3423. destination := dest;
  3424. IF Trace THEN TraceExit("VisitConversion") END;
  3425. END VisitConversion;
  3426. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3427. BEGIN
  3428. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3429. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3430. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3431. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3432. END VisitTypeDeclaration;
  3433. (** designators (expressions) *)
  3434. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3435. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3436. BEGIN
  3437. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3438. IF x.left # NIL THEN Expression(x.left) END;
  3439. Symbol(x.symbol,result);
  3440. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3441. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3442. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3443. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3444. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3445. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3446. END;
  3447. END;
  3448. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3449. END VisitSymbolDesignator;
  3450. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3451. BEGIN
  3452. IF isUnchecked THEN RETURN END;
  3453. IF tagsAvailable THEN
  3454. TrapC(BrltL,index,length,IndexCheckTrap);
  3455. END;
  3456. END BoundCheck;
  3457. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3458. VAR d: IntermediateCode.Operand;
  3459. BEGIN
  3460. IF isUnchecked THEN RETURN END;
  3461. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3462. TrapC(op,dim,d,ArraySizeTrap);
  3463. ReleaseIntermediateOperand(d);
  3464. END DimensionCheck;
  3465. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3466. VAR
  3467. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3468. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3469. expression: SyntaxTree.Expression;
  3470. resultingType, leftType, baseType: SyntaxTree.Type;
  3471. skipLabel1: Label;
  3472. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3473. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3474. variableOp: Operand;
  3475. variable: SyntaxTree.Variable;
  3476. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3477. VAR expression: SyntaxTree.Expression;
  3478. BEGIN
  3479. (* count the number of indices, ranges and tensorRanges in the index list *)
  3480. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3481. FOR i := 0 TO parameters.Length() - 1 DO
  3482. expression := parameters.GetExpression(i);
  3483. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3484. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3485. ELSE INC(indexCount)
  3486. END
  3487. END;
  3488. END CountIndices;
  3489. BEGIN
  3490. ASSERT(tagsAvailable);
  3491. resultingType := x.type.resolved; (* resulting type *)
  3492. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3493. InitOperand(localResult, ModeReference);
  3494. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3495. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3496. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3497. (* a globally defined array destination tag is available -> use and invalidate it*)
  3498. localResult.tag := arrayDestinationTag;
  3499. IntermediateCode.InitOperand(arrayDestinationTag)
  3500. ELSE
  3501. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3502. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3503. Symbol(variable, variableOp);
  3504. ReuseCopy(localResult.tag, variableOp.op);
  3505. ReleaseOperand(variableOp);
  3506. END
  3507. END;
  3508. indexListSize := x.parameters.Length();
  3509. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3510. ASSERT(tensorRangeCount <= 1);
  3511. (* designate the array to be indexed over, perform tensor range check if known *)
  3512. Designate(x.left, array);
  3513. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3514. Dereference(array, leftType,FALSE);
  3515. IF tensorRangeCount=0 THEN
  3516. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3517. END
  3518. END;
  3519. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3520. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3521. srcDimOffset := -indexListSize;
  3522. destDimOffset := -rangeCount
  3523. ELSE
  3524. srcDimOffset := 0;
  3525. destDimOffset := 0
  3526. END;
  3527. indexDim := 0;
  3528. (* use address of source array as basis *)
  3529. (*
  3530. ReuseCopy(localResult.op, array.op);
  3531. *)
  3532. localResult.op := array.op;
  3533. UseIntermediateOperand(localResult.op);
  3534. (* go through the index list *)
  3535. FOR i := 0 TO indexListSize - 1 DO
  3536. expression := x.parameters.GetExpression(i);
  3537. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3538. (* nothing to do *)
  3539. ELSE
  3540. (* determine which dimension of source array is currently looked at *)
  3541. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3542. (* get the memory operand pointing to array descriptor dimension *)
  3543. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3544. (* make a reusable register from it *)
  3545. ReuseCopy(srcDim, tmp);
  3546. ReleaseIntermediateOperand(tmp);
  3547. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3548. ELSE
  3549. srcDim := IntermediateCode.Immediate(int32, i)
  3550. END;
  3551. (* get length and increment of source array for current dimension *)
  3552. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3553. Convert(sourceLength.op, sizeType);
  3554. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3555. Convert(sourceIncrement.op, sizeType);
  3556. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3557. ReleaseIntermediateOperand(srcDim);
  3558. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3559. (* SINGLE INDEX *)
  3560. Evaluate(expression, index);
  3561. ReleaseIntermediateOperand(index.tag);
  3562. index.tag := emptyOperand;
  3563. Convert(index.op, sizeType);
  3564. (* lower bound check *)
  3565. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3566. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3567. ELSIF isUnchecked THEN (* do nothing *)
  3568. ELSE
  3569. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3570. END;
  3571. (* upper bound check *)
  3572. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3573. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3574. ELSIF isUnchecked THEN (* do nothing *)
  3575. ELSE
  3576. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3577. END;
  3578. ReleaseOperand(sourceLength);
  3579. Convert(index.op, addressType);
  3580. summand := index.op;
  3581. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3582. (* RANGE OF INDICES *)
  3583. Evaluate(expression, range);
  3584. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3585. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3586. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3587. ReleaseOperand(range);
  3588. Convert(firstIndex, sizeType);
  3589. Convert(lastIndex, sizeType);
  3590. Convert(stepSize, sizeType);
  3591. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3592. if it is 'MAX(LONGINT)' *)
  3593. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3594. TransferToRegister(lastIndex, lastIndex);
  3595. skipLabel1 := NewLabel();
  3596. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3597. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3598. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3599. SetLabel(skipLabel1)
  3600. END;
  3601. (* check if step size is valid *)
  3602. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3603. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3604. ELSIF isUnchecked THEN (* do nothing *)
  3605. ELSE
  3606. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3607. END;
  3608. (* check lower bound check *)
  3609. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3610. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3611. ELSIF isUnchecked THEN (* do nothing *)
  3612. ELSE
  3613. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3614. END;
  3615. (* check upper bound check *)
  3616. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3617. (* statically open range: nothing to do *)
  3618. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3619. ASSERT(staticLastIndex < staticSourceLength)
  3620. ELSIF isUnchecked THEN (* do nothing *)
  3621. ELSE
  3622. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3623. END;
  3624. (* determine length of target array for current dimension *)
  3625. (* 1. incorporate last index: *)
  3626. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3627. (* last index is static *)
  3628. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3629. targetLength := sourceLength.op
  3630. ELSE
  3631. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3632. END;
  3633. UseIntermediateOperand(targetLength);
  3634. ELSE
  3635. (* targetLength := lastIndex + 1
  3636. Reuse1(targetLength, lastIndex);
  3637. *)
  3638. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3639. END;
  3640. ReleaseOperand(sourceLength);
  3641. ReleaseIntermediateOperand(lastIndex);
  3642. (* 2. incorporate first index: *)
  3643. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3644. (* first index and current target length are static *)
  3645. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3646. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3647. (* first index = 0: nothing to do *)
  3648. ELSE
  3649. (* targetLength := targetLength - firstIndex *)
  3650. TransferToRegister(targetLength, targetLength);
  3651. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3652. END;
  3653. (* clip negative lengths to 0 *)
  3654. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3655. IF staticTargetLength < 0 THEN
  3656. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3657. END
  3658. ELSE
  3659. skipLabel1 := NewLabel();
  3660. TransferToRegister(targetLength, targetLength);
  3661. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3662. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3663. SetLabel(skipLabel1)
  3664. END;
  3665. (* 3. incorporate index step size: *)
  3666. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3667. (*step size and current target length are static *)
  3668. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3669. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3670. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3671. (* step size = 1: nothing to do *)
  3672. ELSE
  3673. (* emit code for this:
  3674. targetLength := (targetLength-1) DIV stepSize +1;
  3675. *)
  3676. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3677. DivInt(targetLength, targetLength, stepSize);
  3678. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3679. END;
  3680. (* determine increment of target array for current dimension *)
  3681. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3682. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3683. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3684. (* step size = 1 *)
  3685. targetIncrement := sourceIncrement.op;
  3686. UseIntermediateOperand(targetIncrement)
  3687. ELSE
  3688. (* targetIncrement := sourceIncrement * stepSize *)
  3689. Reuse1(targetIncrement, stepSize);
  3690. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3691. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3692. END;
  3693. ReleaseIntermediateOperand(stepSize);
  3694. (* write length and increment of target array to descriptor *)
  3695. IF destDimOffset < 0 THEN
  3696. (* determine which dimension of target array is currently looked at *)
  3697. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3698. TransferToRegister(destDim, tmp);
  3699. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3700. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3701. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3702. ReleaseIntermediateOperand(destDim)
  3703. ELSE
  3704. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3705. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3706. END;
  3707. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3708. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3709. INC(indexDim);
  3710. Convert(firstIndex, addressType);
  3711. TransferToRegister(summand, firstIndex);
  3712. ELSE HALT(100);
  3713. END;
  3714. (*
  3715. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3716. *)
  3717. Convert(sourceIncrement.op, addressType);
  3718. Convert(summand, addressType);
  3719. MulInt(summand, summand, sourceIncrement.op);
  3720. ReleaseIntermediateOperand(sourceIncrement.op);
  3721. AddInt(localResult.op, localResult.op, summand);
  3722. ReleaseIntermediateOperand(summand);
  3723. END
  3724. END;
  3725. result := localResult;
  3726. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3727. ReleaseIntermediateOperand(result.tag);
  3728. result.tag := TypeDescriptorAdr(resultingType);
  3729. IF ~newObjectFile THEN
  3730. IntermediateCode.MakeMemory(result.tag,addressType);
  3731. END;
  3732. ELSIF IsDelegate(resultingType) THEN
  3733. ReleaseIntermediateOperand(result.tag);
  3734. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3735. UseIntermediateOperand(result.tag);
  3736. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3737. ReleaseIntermediateOperand(result.tag);
  3738. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3739. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3740. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3741. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3742. (* finalize target array descriptor *)
  3743. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3744. (* write lengths and increments of target array for remaining dimensions *)
  3745. i := indexListSize;
  3746. WHILE indexDim < targetArrayDimensionality DO
  3747. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3748. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3749. ReleaseOperand(sourceLength);
  3750. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3751. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3752. ReleaseOperand(sourceIncrement);
  3753. INC(i); INC(indexDim);
  3754. END;
  3755. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3756. tmp := nil;
  3757. ELSE
  3758. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3759. END;
  3760. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3761. ReleaseIntermediateOperand(tmp);
  3762. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3763. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3764. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3765. ELSE
  3766. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3767. END;
  3768. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3769. ReleaseIntermediateOperand(tmp);
  3770. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3771. (* write dimensionality *)
  3772. IF targetArrayDimensionality # 0 THEN
  3773. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3774. END;
  3775. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3776. END;
  3777. ReleaseOperand(array);
  3778. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3779. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3780. END;
  3781. END MathIndexDesignator;
  3782. (* get the length of an array , trying to make use of static information *)
  3783. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3784. VAR res: IntermediateCode.Operand; size: LONGINT;
  3785. BEGIN
  3786. type := type.resolved;
  3787. IF type IS SyntaxTree.ArrayType THEN
  3788. WITH type: SyntaxTree.ArrayType DO
  3789. IF type.form = SyntaxTree.Static THEN
  3790. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3791. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3792. Evaluate(type.length, op);
  3793. ReleaseIntermediateOperand(op.tag);
  3794. RETURN op.op;*)
  3795. ELSE
  3796. res := tag;
  3797. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3798. IntermediateCode.MakeMemory(res,addressType);
  3799. UseIntermediateOperand(res);
  3800. RETURN res
  3801. END
  3802. END;
  3803. ELSE
  3804. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3805. RETURN IntermediateCode.Immediate(addressType,size);
  3806. END;
  3807. END ArrayLength;
  3808. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3809. BEGIN
  3810. IF IsImmediate(x) THEN
  3811. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3812. ELSE
  3813. IF ~ReusableRegister(res) THEN
  3814. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3815. ELSE
  3816. UseIntermediateOperand(res);
  3817. END;
  3818. Emit(Mov(position,res,x))
  3819. END;
  3820. END CopyInt;
  3821. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3822. BEGIN
  3823. ReleaseIntermediateOperand(res);
  3824. IF IsImmediate(x) & IsImmediate(y) THEN
  3825. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3826. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3827. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3828. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3829. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3830. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3831. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3832. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3833. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3834. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3835. UseIntermediateOperand(res);
  3836. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3837. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3838. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3839. UseIntermediateOperand(res);
  3840. ELSE
  3841. IF ~ReusableRegister(res) THEN
  3842. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3843. ELSE
  3844. UseIntermediateOperand(res);
  3845. END;
  3846. IF IsImmediate(x) & (x.intValue = 0) THEN
  3847. Emit(Mov(position,res,y))
  3848. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3849. Emit(Mov(position,res,x))
  3850. ELSE
  3851. Emit(Add(position,res, x, y));
  3852. END;
  3853. END;
  3854. END AddInt;
  3855. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3856. BEGIN
  3857. ReleaseIntermediateOperand(res);
  3858. IF IsImmediate(x) & IsImmediate(y) THEN
  3859. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3860. ELSE
  3861. IF ~ReusableRegister(res) THEN
  3862. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3863. ELSE UseIntermediateOperand(res);
  3864. END;
  3865. IF IsImmediate(x) & (x.intValue = 1) THEN
  3866. Emit(Mov(position,res,y))
  3867. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3868. Emit(Mov(position,res,x))
  3869. ELSE
  3870. Emit(Mul(position,res, x, y));
  3871. END;
  3872. END;
  3873. END MulInt;
  3874. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3875. BEGIN
  3876. ReleaseIntermediateOperand(res);
  3877. IF IsImmediate(x) & IsImmediate(y) THEN
  3878. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3879. ELSE
  3880. IF ~ReusableRegister(res) THEN
  3881. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3882. ELSE UseIntermediateOperand(res);
  3883. END;
  3884. IF IsImmediate(x) & (x.intValue = 1) THEN
  3885. Emit(Mov(position,res,y))
  3886. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3887. Emit(Mov(position,res,x))
  3888. ELSE
  3889. Emit(Div(position,res, x, y));
  3890. END;
  3891. END;
  3892. END DivInt;
  3893. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3894. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3895. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3896. BEGIN
  3897. type := x.left.type.resolved;
  3898. IF type IS SyntaxTree.StringType THEN
  3899. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3900. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3901. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3902. type := atype;
  3903. x.left.SetType(type);
  3904. END;
  3905. IntermediateCode.InitImmediate(res,addressType,0);
  3906. maxDim := x.parameters.Length()-1;
  3907. (*
  3908. computation rule:
  3909. a: ARRAY X,Y,Z OF Element with size S
  3910. a[i,j,k] -->
  3911. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3912. *)
  3913. FOR i := 0 TO maxDim DO
  3914. e := x.parameters.GetExpression(i);
  3915. Evaluate(e,index);
  3916. Convert(index.op,addressType);
  3917. AddInt(res, res, index.op);
  3918. IF i = 0 THEN
  3919. (*
  3920. ReuseCopy(res, index.op);
  3921. *)
  3922. Designate(x.left,array);
  3923. type := x.left.type.resolved;
  3924. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3925. Dereference(array, type, FALSE);
  3926. END;
  3927. (*
  3928. ELSE AddInt(res, res, index.op);
  3929. *)
  3930. END;
  3931. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3932. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3933. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3934. BoundCheck(index.op, length);
  3935. END;
  3936. ReleaseIntermediateOperand(length);
  3937. END;
  3938. ReleaseOperand(index);
  3939. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3940. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3941. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3942. MulInt(res,res,length);
  3943. END;
  3944. ReleaseIntermediateOperand(length);
  3945. END;
  3946. (* remaining open dimensions -- compute address *)
  3947. i := maxDim+1;
  3948. IF type IS SyntaxTree.ArrayType THEN
  3949. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3950. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3951. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3952. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3953. MulInt(res,res,length);
  3954. END;
  3955. ReleaseIntermediateOperand(length);
  3956. INC(i);
  3957. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3958. END;
  3959. END;
  3960. IF (type IS SyntaxTree.ArrayType) THEN
  3961. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3962. size := StaticSize(system, type);
  3963. IF size # 1 THEN
  3964. length := IntermediateCode.Immediate(addressType,size);
  3965. MulInt(res,res,length);
  3966. END;
  3967. ELSE
  3968. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3969. IF size # 1 THEN
  3970. length := IntermediateCode.Immediate(addressType,size);
  3971. MulInt(res,res,length);
  3972. END;
  3973. END;
  3974. END;
  3975. AddInt(res,res,array.op);
  3976. InitOperand(result,ModeReference);
  3977. result.op := res;
  3978. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3979. ReleaseIntermediateOperand(result.tag);
  3980. result.tag := TypeDescriptorAdr(type);
  3981. IF ~newObjectFile THEN
  3982. IntermediateCode.MakeMemory(result.tag,addressType);
  3983. END
  3984. ELSIF IsDelegate(type) THEN
  3985. ReleaseIntermediateOperand(result.tag);
  3986. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3987. UseIntermediateOperand(result.tag);
  3988. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3989. ReleaseIntermediateOperand(result.tag);
  3990. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3991. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3992. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3993. END;
  3994. ReleaseOperand(array);
  3995. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3996. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3997. END;
  3998. END IndexDesignator;
  3999. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  4000. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  4001. BEGIN
  4002. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  4003. dest := destination; destination := emptyOperand;
  4004. type := x.left.type.resolved;
  4005. IF type IS SyntaxTree.MathArrayType THEN
  4006. MathIndexDesignator(x);
  4007. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4008. IndexDesignator(x);
  4009. END;
  4010. destination := dest;
  4011. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4012. END VisitIndexDesignator;
  4013. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4014. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4015. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4016. procedure: SyntaxTree.Procedure;
  4017. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4018. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4019. BEGIN
  4020. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4021. parameters := expression.parameters;
  4022. moduleName := "FoxArrayBase";
  4023. procedureName := "CopyDescriptor";
  4024. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4025. SaveRegisters();ReleaseUsedRegisters(saved);
  4026. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4027. IF procedure = NIL THEN
  4028. s := "procedure ";
  4029. Strings.Append(s,moduleName);
  4030. Strings.Append(s,".");
  4031. Strings.Append(s,procedureName);
  4032. Strings.Append(s," not present");
  4033. Error(position,s);
  4034. ELSE
  4035. (* push address of temporary variable *)
  4036. Symbol(variable,destOperand);
  4037. Emit(Push(position,destOperand.op));
  4038. ReleaseOperand(destOperand);
  4039. (* push src *)
  4040. Evaluate(expression.left,srcOperand);
  4041. (*
  4042. Dereference(srcOperand,expression.type.resolved);
  4043. Emit(Push(position,srcOperand.tag));
  4044. *)
  4045. Emit(Push(position,srcOperand.op));
  4046. ReleaseOperand(srcOperand);
  4047. tensorFound := FALSE;
  4048. FOR i := 0 TO parameters.Length()-1 DO
  4049. e := parameters.GetExpression(i);
  4050. IF e IS SyntaxTree.TensorRangeExpression THEN
  4051. tensorFound := TRUE;
  4052. ELSIF e IS SyntaxTree.RangeExpression THEN
  4053. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4054. ELSE
  4055. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4056. END;
  4057. END;
  4058. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4059. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4060. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4061. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4062. StaticCallOperand(procOp,procedure);
  4063. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4064. ReleaseOperand(procOp);
  4065. END;
  4066. RestoreRegisters(saved);
  4067. END;
  4068. RETURN variable
  4069. END PrepareTensorDescriptor;
  4070. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4071. VAR
  4072. type, descriptorType, baseType: SyntaxTree.Type;
  4073. operand, tmpOperand, variableOp, variable2Op: Operand;
  4074. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4075. variable, variable2: SyntaxTree.Variable;
  4076. dim, i, size: LONGINT;
  4077. (* TODO: needed? *)
  4078. oldArrayDestinationTag: IntermediateCode.Operand;
  4079. oldArrayDestinationDimension: LONGINT;
  4080. position: Position;
  4081. saved: RegisterEntry;
  4082. arrayFlags: SET;
  4083. m, n: LONGINT;
  4084. PROCEDURE Pass(op: IntermediateCode.Operand);
  4085. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4086. BEGIN
  4087. IF numberRegister >= 0 THEN
  4088. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4089. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4090. Emit(Mov(position,parameterRegister, op));
  4091. ELSE
  4092. Emit(Push(position,op))
  4093. END
  4094. END Pass;
  4095. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4096. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4097. BEGIN
  4098. formalType := formalType.resolved; actualType := actualType.resolved;
  4099. IF IsOpenArray(formalType)THEN
  4100. IF actualType IS SyntaxTree.StringType THEN
  4101. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4102. RETURN;
  4103. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4104. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4105. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4106. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4107. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4108. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4109. ELSE
  4110. tmp := baseReg;
  4111. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4112. IntermediateCode.MakeMemory(tmp,addressType);
  4113. Pass((tmp));
  4114. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4115. END;
  4116. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4117. END;
  4118. END PushArrayLens;
  4119. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4120. BEGIN
  4121. CASE size OF
  4122. |2: INCL(flags,Size2Flag);
  4123. |3: INCL(flags,Size3Flag);
  4124. |4: INCL(flags,Size4Flag);
  4125. |5: INCL(flags,Size5Flag);
  4126. |6: INCL(flags,Size6Flag);
  4127. |7: INCL(flags,Size7Flag);
  4128. |8: INCL(flags,Size8Flag);
  4129. END;
  4130. END SetSmallArraySizeFlag;
  4131. BEGIN
  4132. IF Trace THEN TraceEnter("PushParameter") END;
  4133. position := expression.position;
  4134. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4135. type := expression.type.resolved;
  4136. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4137. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4138. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4139. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4140. );
  4141. (* TODO: needed? *)
  4142. oldArrayDestinationTag := arrayDestinationTag;
  4143. oldArrayDestinationDimension := arrayDestinationDimension;
  4144. IF IsArrayOfSystemByte(parameter.type) THEN
  4145. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4146. Designate(expression,operand);
  4147. ELSE
  4148. Evaluate(expression, tmpOperand);
  4149. variable := GetTemporaryVariable(expression.type, FALSE);
  4150. Symbol(variable, operand);
  4151. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4152. Emit(Mov(position,tmp, tmpOperand.op));
  4153. ReleaseOperand(tmpOperand);
  4154. END;
  4155. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4156. ReleaseIntermediateOperand(operand.tag);
  4157. operand.tag := tmp;
  4158. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4159. Pass((operand.tag));
  4160. END;
  4161. Pass((operand.op));
  4162. ELSIF IsOpenArray(parameter.type) THEN
  4163. Designate(expression,operand);
  4164. baseReg := operand.tag;
  4165. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4166. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4167. END;
  4168. Pass((operand.op)); (* address of the array *)
  4169. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4170. (* case 1 *)
  4171. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4172. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4173. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4174. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4175. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4176. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4177. arrayDestinationTag := sp;
  4178. (* case 1b *)
  4179. IF expression IS SyntaxTree.IndexDesignator THEN
  4180. (*
  4181. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4182. descriptorType := GetMathArrayDescriptorType(dim);
  4183. variable := GetTemporaryVariable(descriptorType,expression.position);
  4184. Symbol(variable,variableOp);
  4185. arrayDestinationTag := variableOp.op;
  4186. *)
  4187. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4188. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4189. arrayDestinationDimension := dim;
  4190. Designate(expression,operand);
  4191. (* case 1a *)
  4192. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4193. Designate(expression,operand);
  4194. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4195. i := 0;
  4196. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4197. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4198. INC(i);
  4199. END;
  4200. type := expression.type.resolved;
  4201. WHILE (i<dim) DO (* remaining static dimensions *)
  4202. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4203. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4204. ReleaseOperand(tmpOperand);
  4205. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4206. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4207. ReleaseOperand(tmpOperand);
  4208. INC(i);
  4209. END;
  4210. dimOp := IntermediateCode.Immediate(addressType,dim);
  4211. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4212. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4213. (* case 1d *)
  4214. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4215. Designate(expression,operand);
  4216. Dereference(operand,type.resolved,FALSE);
  4217. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4218. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4219. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4220. (* case 1f *)
  4221. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4222. Designate(expression,operand);
  4223. FOR i := 0 TO dim-1 DO
  4224. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4225. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4226. ReleaseOperand(tmpOperand);
  4227. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4228. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4229. ReleaseOperand(tmpOperand);
  4230. END;
  4231. (*******
  4232. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4233. *)
  4234. arrayFlags := {StaticFlag};
  4235. IF dim = 1 THEN
  4236. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4237. ReleaseOperand(tmpOperand);
  4238. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4239. m := LONGINT(tmpOperand.op.intValue);
  4240. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4241. ELSIF dim = 2 THEN
  4242. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4243. ReleaseOperand(tmpOperand);
  4244. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4245. m := LONGINT(tmpOperand.op.intValue);
  4246. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4247. ReleaseOperand(tmpOperand);
  4248. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4249. n := LONGINT(tmpOperand.op.intValue);
  4250. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4251. INCL(arrayFlags,SmallMatrixFlag);
  4252. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4253. END;
  4254. END;
  4255. (*******)
  4256. dimOp := IntermediateCode.Immediate(addressType,dim);
  4257. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4258. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4259. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4260. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4261. baseType := SemanticChecker.ArrayBase(type,dim);
  4262. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4263. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4264. ELSE HALT(100);
  4265. END;
  4266. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4267. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4268. (* case 2b *)
  4269. IF expression IS SyntaxTree.IndexDesignator THEN
  4270. descriptorType := GetMathArrayDescriptorType(dim);
  4271. variable := GetTemporaryVariable(descriptorType, FALSE);
  4272. Symbol(variable,variableOp);
  4273. arrayDestinationTag := variableOp.op;
  4274. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4275. arrayDestinationDimension := dim;
  4276. NeedDescriptor := TRUE;
  4277. Designate(expression,operand);
  4278. Pass((operand.tag));
  4279. NeedDescriptor := FALSE;
  4280. (* case 2a *)
  4281. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4282. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4283. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4284. INC(i);
  4285. END;
  4286. IF i = dim THEN
  4287. Designate(expression,operand);
  4288. Pass((operand.tag));
  4289. ELSE (* open-static *)
  4290. type := expression.type.resolved;
  4291. descriptorType := GetMathArrayDescriptorType(dim);
  4292. variable := GetTemporaryVariable(descriptorType, FALSE);
  4293. Symbol(variable,variableOp);
  4294. arrayDestinationTag := variableOp.op;
  4295. Designate(expression,operand);
  4296. FOR i := 0 TO dim-1 DO
  4297. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4298. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4299. ReleaseOperand(tmpOperand);
  4300. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4301. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4302. ReleaseOperand(tmpOperand);
  4303. END;
  4304. dimOp := IntermediateCode.Immediate(addressType,dim);
  4305. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4306. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4307. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4308. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4309. baseType := SemanticChecker.ArrayBase(type,dim);
  4310. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4311. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4312. Pass((arrayDestinationTag));
  4313. END;
  4314. (* case 2d *)
  4315. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4316. Designate(expression,operand);
  4317. Dereference(operand,type.resolved,FALSE);
  4318. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4319. Pass((operand.tag));
  4320. (* case 2f *)
  4321. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4322. descriptorType := GetMathArrayDescriptorType(dim);
  4323. variable := GetTemporaryVariable(descriptorType, FALSE);
  4324. Symbol(variable,variableOp);
  4325. arrayDestinationTag := variableOp.op;
  4326. Designate(expression,operand);
  4327. FOR i := 0 TO dim-1 DO
  4328. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4329. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4330. ReleaseOperand(tmpOperand);
  4331. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4332. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4333. ReleaseOperand(tmpOperand);
  4334. END;
  4335. (*
  4336. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4337. *)
  4338. arrayFlags := {StaticFlag};
  4339. IF dim = 1 THEN
  4340. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4341. ReleaseOperand(tmpOperand);
  4342. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4343. m := LONGINT(tmpOperand.op.intValue);
  4344. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4345. ELSIF dim = 2 THEN
  4346. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4347. ReleaseOperand(tmpOperand);
  4348. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4349. m := LONGINT(tmpOperand.op.intValue);
  4350. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4351. ReleaseOperand(tmpOperand);
  4352. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4353. n := LONGINT(tmpOperand.op.intValue);
  4354. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4355. INCL(arrayFlags,SmallMatrixFlag);
  4356. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4357. END;
  4358. END;
  4359. (*******)
  4360. dimOp := IntermediateCode.Immediate(addressType,dim);
  4361. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4362. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4363. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4364. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4365. baseType := SemanticChecker.ArrayBase(type,dim);
  4366. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4367. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4368. Pass((arrayDestinationTag));
  4369. ELSE HALT(100);
  4370. END;
  4371. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4372. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4373. (* case 3b *)
  4374. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4375. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4376. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4377. Symbol(variable,variableOp);
  4378. LoadValue(variableOp,system.addressType);
  4379. ELSE
  4380. descriptorType := GetMathArrayDescriptorType(dim);
  4381. variable := GetTemporaryVariable(descriptorType, FALSE);
  4382. Symbol(variable,variableOp);
  4383. END;
  4384. arrayDestinationTag := variableOp.op;
  4385. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4386. arrayDestinationDimension := 0;
  4387. Designate(expression,operand);
  4388. Pass((operand.tag));
  4389. (* case 3a *)
  4390. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4391. i := 0;
  4392. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4393. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4394. INC(i);
  4395. END;
  4396. IF i = dim THEN
  4397. Designate(expression,operand);
  4398. Pass((operand.tag));
  4399. ELSE (* open-static *)
  4400. type := expression.type.resolved;
  4401. descriptorType := GetMathArrayDescriptorType(dim);
  4402. variable := GetTemporaryVariable(descriptorType, FALSE);
  4403. Symbol(variable,variableOp);
  4404. arrayDestinationTag := variableOp.op;
  4405. Designate(expression,operand);
  4406. FOR i := 0 TO dim-1 DO
  4407. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4408. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4409. ReleaseOperand(tmpOperand);
  4410. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4411. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4412. ReleaseOperand(tmpOperand);
  4413. END;
  4414. dimOp := IntermediateCode.Immediate(addressType,dim);
  4415. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4416. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4417. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4418. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4419. baseType := SemanticChecker.ArrayBase(type,dim);
  4420. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4421. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4422. Pass((arrayDestinationTag));
  4423. END;
  4424. (* case 3d *)
  4425. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4426. Designate(expression,operand);
  4427. Dereference(operand,type.resolved,FALSE);
  4428. (*
  4429. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4430. *)
  4431. Pass((operand.tag));
  4432. (* case 3f *)
  4433. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4434. descriptorType := GetMathArrayDescriptorType(dim);
  4435. variable := GetTemporaryVariable(descriptorType, FALSE);
  4436. Symbol(variable,variableOp);
  4437. arrayDestinationTag := variableOp.op;
  4438. Designate(expression,operand);
  4439. IF operand.op.type.length >1 THEN (* vector register *)
  4440. variable2 := GetTemporaryVariable(type, FALSE);
  4441. Symbol(variable2, variable2Op);
  4442. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4443. Emit(Mov(position,tmp, operand.op));
  4444. ReleaseOperand(operand);
  4445. Symbol(variable2, operand);
  4446. END;
  4447. FOR i := 0 TO dim-1 DO
  4448. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4449. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4450. ReleaseOperand(tmpOperand);
  4451. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4452. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4453. ReleaseOperand(tmpOperand);
  4454. END;
  4455. dimOp := IntermediateCode.Immediate(addressType,dim);
  4456. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4457. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4458. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4459. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4460. baseType := SemanticChecker.ArrayBase(type,dim);
  4461. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4462. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4463. Pass((arrayDestinationTag));
  4464. ELSE HALT(100);
  4465. END;
  4466. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4467. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4468. (* case 4b *)
  4469. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4470. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4471. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4472. Symbol(variable,variableOp);
  4473. LoadValue(variableOp,system.addressType);
  4474. ELSE
  4475. descriptorType := GetMathArrayDescriptorType(dim);
  4476. variable := GetTemporaryVariable(descriptorType, FALSE);
  4477. Symbol(variable,variableOp);
  4478. END;
  4479. arrayDestinationTag := variableOp.op;
  4480. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4481. arrayDestinationDimension := 0;
  4482. Designate(expression,operand);
  4483. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4484. Symbol(variable,variableOp);
  4485. ELSE
  4486. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4487. Symbol(variable,variableOp);
  4488. MakeMemory(tmp,variableOp.op,addressType,0);
  4489. Emit(Mov(position,tmp,operand.tag));
  4490. ReleaseIntermediateOperand(tmp);
  4491. END;
  4492. Pass((variableOp.op));
  4493. ReleaseOperand(variableOp);
  4494. (* case 4a *)
  4495. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4496. i := 0;
  4497. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4498. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4499. INC(i);
  4500. END;
  4501. IF i = dim THEN
  4502. Designate(expression,operand);
  4503. arrayDestinationTag := operand.tag;
  4504. ELSE (* open-static *)
  4505. type := expression.type.resolved;
  4506. descriptorType := GetMathArrayDescriptorType(dim);
  4507. variable := GetTemporaryVariable(descriptorType, FALSE);
  4508. Symbol(variable,variableOp);
  4509. arrayDestinationTag := variableOp.op;
  4510. Designate(expression,operand);
  4511. FOR i := 0 TO dim-1 DO
  4512. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4513. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4514. ReleaseOperand(tmpOperand);
  4515. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4516. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4517. ReleaseOperand(tmpOperand);
  4518. END;
  4519. dimOp := IntermediateCode.Immediate(addressType,dim);
  4520. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4521. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4522. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4523. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4524. baseType := SemanticChecker.ArrayBase(type,dim);
  4525. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4526. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4527. END;
  4528. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4529. Symbol(variable,variableOp);
  4530. MakeMemory(tmp,variableOp.op,addressType,0);
  4531. Emit(Mov(position,tmp,arrayDestinationTag));
  4532. ReleaseIntermediateOperand(tmp);
  4533. Pass((variableOp.op));
  4534. ReleaseOperand(variableOp);
  4535. (* case 4d *)
  4536. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4537. Designate(expression,operand);
  4538. (*
  4539. Dereference(operand,type.resolved,FALSE);
  4540. *)
  4541. (*
  4542. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4543. *)
  4544. Pass((operand.op));
  4545. (* case 4f *)
  4546. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4547. descriptorType := GetMathArrayDescriptorType(dim);
  4548. variable := GetTemporaryVariable(descriptorType, FALSE);
  4549. Symbol(variable,variableOp);
  4550. arrayDestinationTag := variableOp.op;
  4551. Designate(expression,operand);
  4552. FOR i := 0 TO dim-1 DO
  4553. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4554. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4555. ReleaseOperand(tmpOperand);
  4556. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4557. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4558. ReleaseOperand(tmpOperand);
  4559. END;
  4560. dimOp := IntermediateCode.Immediate(addressType,dim);
  4561. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4562. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4563. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4564. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4565. baseType := SemanticChecker.ArrayBase(type,dim);
  4566. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4567. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4568. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4569. Symbol(variable,variableOp);
  4570. MakeMemory(tmp,variableOp.op,addressType,0);
  4571. Emit(Mov(position,tmp,arrayDestinationTag));
  4572. ReleaseIntermediateOperand(tmp);
  4573. Pass((variableOp.op));
  4574. ReleaseOperand(variableOp);
  4575. ELSE HALT(100);
  4576. END;
  4577. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4578. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4579. Designate(expression,operand);
  4580. IF operand.op.type.length > 1 THEN
  4581. Emit(Push(position, operand.op));
  4582. ELSE
  4583. size := system.SizeOf(type);
  4584. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4585. size := ToMemoryUnits(system,size);
  4586. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4587. arrayDestinationTag := sp;
  4588. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4589. END;
  4590. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4591. Designate(expression,operand);
  4592. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4593. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4594. Symbol(variable,variableOp);
  4595. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4596. Emit(Mov(position,tmp,operand.op));
  4597. Emit(Push(position,variableOp.op));
  4598. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4599. Pass((operand.op));
  4600. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4601. END;
  4602. ELSE HALT(200)
  4603. END;
  4604. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4605. IF parameter.kind = SyntaxTree.VarParameter THEN
  4606. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4607. Designate(expression, operand);
  4608. Pass((operand.op));
  4609. ELSE
  4610. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4611. Evaluate(expression, operand);
  4612. Pass((operand.extra)); (* step *)
  4613. Pass((operand.tag)); (* last *)
  4614. Pass((operand.op)) (* first *)
  4615. END
  4616. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4617. IF parameter.kind = SyntaxTree.VarParameter THEN
  4618. Designate(expression, operand);
  4619. Pass((operand.op));
  4620. ELSE
  4621. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4622. Evaluate(expression, operand);
  4623. Pass((operand.tag)); (* real part *)
  4624. Pass((operand.op)) (* imaginary part *)
  4625. END
  4626. ELSE
  4627. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4628. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4629. Designate(expression,operand);
  4630. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4631. (* stack allocation *)
  4632. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4633. (*! parameter alignment to be discussed ... *)
  4634. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4635. size := type(SyntaxTree.StringType).length;
  4636. END;
  4637. IF backend.cooperative & parameter.NeedsTrace() THEN
  4638. dst := NewRegisterOperand (addressType);
  4639. Emit(Mov(position,dst, sp));
  4640. IntermediateCode.InitImmediate(null, byteType, 0);
  4641. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4642. ReleaseIntermediateOperand(dst);
  4643. SaveRegisters();ReleaseUsedRegisters(saved);
  4644. (* register dst has been freed before SaveRegisters already *)
  4645. CallAssignMethod(dst, operand.op, parameter.type);
  4646. RestoreRegisters(saved);
  4647. END;
  4648. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4649. ELSIF IsOpenArray(parameter.type) THEN
  4650. Designate(expression,operand);
  4651. baseReg := operand.tag;
  4652. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4653. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4654. END;
  4655. Pass((operand.op)); (* address of the array *)
  4656. ELSIF IsDelegate(parameter.type) THEN
  4657. Evaluate(expression,operand);
  4658. IF backend.cooperative & parameter.NeedsTrace() THEN
  4659. Emit(Push(position, nil));
  4660. dst := NewRegisterOperand (addressType);
  4661. Emit(Mov(position,dst, sp));
  4662. ReleaseIntermediateOperand(dst);
  4663. SaveRegisters();ReleaseUsedRegisters(saved);
  4664. Emit(Push(position, dst));
  4665. (* register dst has been freed before SaveRegisters already *)
  4666. Emit(Push(position, operand.tag));
  4667. CallThis(position,"GarbageCollector","Assign",2);
  4668. RestoreRegisters(saved);
  4669. ELSE
  4670. Pass((operand.tag));
  4671. END;
  4672. Pass((operand.op));
  4673. ELSE
  4674. Evaluate(expression,operand);
  4675. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4676. Emit(Push(position, nil));
  4677. dst := NewRegisterOperand (addressType);
  4678. Emit(Mov(position,dst, sp));
  4679. ReleaseIntermediateOperand(dst);
  4680. SaveRegisters();ReleaseUsedRegisters(saved);
  4681. Emit(Push(position, dst));
  4682. (* register dst has been freed before SaveRegisters already *)
  4683. Emit(Push(position, operand.op));
  4684. CallThis(position,"GarbageCollector","Assign",2);
  4685. RestoreRegisters(saved);
  4686. ELSE
  4687. Pass((operand.op));
  4688. END;
  4689. END;
  4690. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4691. Evaluate(expression,operand);
  4692. Pass((operand.op));
  4693. ELSE (* var parameter *)
  4694. Designate(expression,operand);
  4695. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4696. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4697. Pass((operand.tag));
  4698. END;
  4699. END;
  4700. Pass((operand.op));
  4701. END;
  4702. END;
  4703. (* TODO: needed? *)
  4704. arrayDestinationTag := oldArrayDestinationTag;
  4705. arrayDestinationDimension := oldArrayDestinationDimension;
  4706. IF needsParameterBackup THEN
  4707. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4708. ReuseCopy(parameterBackup, operand.op)
  4709. END;
  4710. ReleaseOperand(operand);
  4711. IF Trace THEN TraceExit("PushParameter") END;
  4712. END PushParameter;
  4713. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4714. VAR prevConditional: BOOLEAN;
  4715. BEGIN
  4716. prevConditional := conditional;
  4717. conditional := FALSE;
  4718. 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
  4719. Expression(x.result); (* use register *)
  4720. END;
  4721. Statement(x.statement);
  4722. conditional := prevConditional;
  4723. IF x.result # NIL THEN Expression(x.result) END;
  4724. IF (x.result # NIL) & (x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4725. ReleaseIntermediateOperand(result.op);
  4726. END;
  4727. END VisitStatementDesignator;
  4728. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4729. VAR
  4730. procedure: SyntaxTree.Procedure;
  4731. procedureType: SyntaxTree.ProcedureType;
  4732. wasInline: BOOLEAN;
  4733. actualParameters: SyntaxTree.ExpressionList;
  4734. formalParameter: SyntaxTree.Parameter;
  4735. actualParameter: SyntaxTree.Expression;
  4736. i: LONGINT;
  4737. localVariable: SyntaxTree.Variable;
  4738. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4739. src, dest: Operand;
  4740. prevInlineExit : Label;
  4741. prevMapper: SymbolMapper;
  4742. tooComplex: BOOLEAN;
  4743. resultDesignator: SyntaxTree.Expression;
  4744. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4745. BEGIN
  4746. IF e = NIL THEN RETURN TRUE
  4747. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4748. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4749. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4750. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4751. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4752. ELSE RETURN FALSE
  4753. END;
  4754. END SimpleExpression;
  4755. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4756. BEGIN
  4757. RETURN checker.CanPassInRegister(type)
  4758. END FitsInRegister;
  4759. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4760. VAR
  4761. variable: SyntaxTree.Variable;
  4762. variableDesignator: SyntaxTree.Designator;
  4763. BEGIN
  4764. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4765. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4766. variableDesignator.SetType(type);
  4767. RETURN variableDesignator
  4768. END GetTemp;
  4769. BEGIN
  4770. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4771. wasInline := currentIsInline;
  4772. prevInlineExit := currentInlineExit;
  4773. prevMapper := currentMapper;
  4774. currentInlineExit := NewLabel();
  4775. tooComplex := FALSE;
  4776. NEW(currentMapper);
  4777. currentIsInline := TRUE;
  4778. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4779. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4780. formalParameter := procedureType.firstParameter;
  4781. actualParameters := x.parameters;
  4782. i := 0;
  4783. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4784. actualParameter := actualParameters.GetExpression(i);
  4785. IF actualParameter.resolved # NIL THEN
  4786. actualParameter := actualParameter.resolved
  4787. END;
  4788. (*
  4789. if expression is simple and can be passed immediately
  4790. or if type fits in register then we can proceed
  4791. otherwise we escape to ordinary procedure call.
  4792. *)
  4793. (* cases where the expression can be mapped identically *)
  4794. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4795. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4796. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4797. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4798. (*
  4799. Assign(variableDesignator, actualParameter);
  4800. *)
  4801. Evaluate(actualParameter, src);
  4802. Designate(variableDesignator, dest);
  4803. Emit(Mov(x.position, dest.op, src.op));
  4804. ReleaseOperand(dest);
  4805. ReleaseOperand(src);
  4806. (* the src operand should now have been completely released ! *)
  4807. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4808. ELSE tooComplex := TRUE
  4809. END;
  4810. (*
  4811. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4812. currentMapper.Add(formalParameter, actualParameter, NIL);
  4813. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4814. variableDesignator := GetTemp(system.addressType, FALSE);
  4815. Designate(actualParameter, src);
  4816. Designate(variableDesignator, dest);
  4817. IntermediateCode.MakeMemory(dest.op,addressType);
  4818. Emit(Mov(x.position, dest.op, src.op));
  4819. ReleaseOperand(dest);
  4820. IF src.tag.mode # IntermediateCode.Undefined THEN
  4821. tagDesignator := GetTemp(system.addressType, FALSE);
  4822. Designate(tagDesignator, dest);
  4823. IntermediateCode.MakeMemory(dest.op,addressType);
  4824. Emit(Mov(x.position, dest.op, src.op));
  4825. END;
  4826. ReleaseOperand(dest); ReleaseOperand(src);
  4827. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4828. END;
  4829. *)
  4830. formalParameter := formalParameter.nextParameter;
  4831. INC(i);
  4832. END;
  4833. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4834. IF resultDesignator # NIL THEN
  4835. returnDesignator := resultDesignator
  4836. ELSE
  4837. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4838. END;
  4839. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4840. END;
  4841. localVariable := procedure.procedureScope.firstVariable;
  4842. WHILE ~tooComplex & (localVariable # NIL) DO
  4843. variableDesignator := GetTemp(localVariable.type, FALSE);
  4844. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4845. localVariable := localVariable.nextVariable;
  4846. END;
  4847. IF ~tooComplex THEN
  4848. VisitStatementBlock(procedure.procedureScope.body);
  4849. SetLabel(currentInlineExit);
  4850. IF procedureType.returnType # NIL THEN
  4851. Designate(returnDesignator, result);
  4852. IF conditional THEN
  4853. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4854. ValueToCondition(result)
  4855. END;
  4856. END;
  4857. END;
  4858. currentMapper := prevMapper;
  4859. currentInlineExit := prevInlineExit;
  4860. currentIsInline := wasInline;
  4861. RETURN ~tooComplex
  4862. END InlineProcedureCall;
  4863. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4864. VAR
  4865. parameters: SyntaxTree.ExpressionList;
  4866. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4867. designator: SyntaxTree.Designator;
  4868. procedureType: SyntaxTree.ProcedureType;
  4869. formalParameter: SyntaxTree.Parameter;
  4870. operand, returnValue: Operand;
  4871. reg, size, mask, dest: IntermediateCode.Operand;
  4872. saved: RegisterEntry;
  4873. symbol: SyntaxTree.Symbol;
  4874. variable: SyntaxTree.Variable;
  4875. i, parametersSize, returnTypeSize : LONGINT;
  4876. structuredReturnType: BOOLEAN;
  4877. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4878. tempVariableDesignator: SyntaxTree.Designator;
  4879. gap, alignment: LONGINT; (*fld*)
  4880. (* TODO: remove unnecessary backup variables *)
  4881. oldResult: Operand;
  4882. oldCurrentScope: SyntaxTree.Scope;
  4883. oldArrayDestinationTag: IntermediateCode.Operand;
  4884. oldArrayDestinationDimension: LONGINT;
  4885. oldConstantDeclaration: SyntaxTree.Symbol;
  4886. oldDestination: IntermediateCode.Operand;
  4887. oldCurrentLoop: Label;
  4888. oldConditional: BOOLEAN;
  4889. oldTrueLabel, oldFalseLabel: Label;
  4890. oldLocked: BOOLEAN;
  4891. usedRegisters,oldUsedRegisters: RegisterEntry;
  4892. return: IntermediateCode.Operand;
  4893. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4894. arg: IntermediateCode.Operand;
  4895. dummy: IntermediateCode.Operand;
  4896. recordType: SyntaxTree.RecordType;
  4897. operatorSelectionProcedureOperand: Operand;
  4898. operatorSelectionProcedure: SyntaxTree.Procedure;
  4899. fingerPrint: SyntaxTree.FingerPrint;
  4900. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4901. identifierNumber: LONGINT;
  4902. parameterRegister: Backend.Registers;
  4903. parameterRegisters: LONGINT;
  4904. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4905. procedure: SyntaxTree.Procedure;
  4906. PROCEDURE BackupGlobalState;
  4907. BEGIN
  4908. oldResult := result;
  4909. oldCurrentScope := currentScope;
  4910. oldArrayDestinationTag := arrayDestinationTag;
  4911. oldArrayDestinationDimension := arrayDestinationDimension;
  4912. oldConstantDeclaration := constantDeclaration;
  4913. oldDestination := destination;
  4914. oldCurrentLoop := currentLoop;
  4915. oldConditional := conditional;
  4916. oldTrueLabel := trueLabel;
  4917. oldFalseLabel := falseLabel;
  4918. oldLocked := locked;
  4919. oldUsedRegisters := usedRegisters
  4920. END BackupGlobalState;
  4921. PROCEDURE RestoreGlobalState;
  4922. BEGIN
  4923. result := oldResult;
  4924. currentScope := oldCurrentScope;
  4925. arrayDestinationTag := oldArrayDestinationTag;
  4926. arrayDestinationDimension := oldArrayDestinationDimension;
  4927. constantDeclaration := oldConstantDeclaration;
  4928. destination := oldDestination;
  4929. currentLoop := oldCurrentLoop;
  4930. conditional := oldConditional;
  4931. trueLabel := oldTrueLabel;
  4932. falseLabel := oldFalseLabel;
  4933. locked := oldLocked;
  4934. usedRegisters := oldUsedRegisters
  4935. END RestoreGlobalState;
  4936. (** do preparations before parameter push for array-structured object types (ASOTs):
  4937. if ASOT is passed as VAR parameter:
  4938. - allocate temporary variable of math array type
  4939. - copy contents of ASOT to be passed to temporary variable
  4940. - use temporary variable as the actual parameter instead
  4941. - 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)
  4942. **)
  4943. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4944. VAR
  4945. expression: SyntaxTree.Expression;
  4946. BEGIN
  4947. IF actualParameter IS SyntaxTree.Designator THEN
  4948. designator := actualParameter(SyntaxTree.Designator);
  4949. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4950. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4951. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4952. ASSERT(checker # NIL);
  4953. checker.SetCurrentScope(currentScope);
  4954. (* allocate temporary variable *)
  4955. ASSERT(actualParameter.type # NIL);
  4956. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4957. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4958. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4959. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4960. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4961. ASSERT(tempVariableDesignator.type # NIL);
  4962. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4963. (* copy math array stored in actual parameter to temporary variable *)
  4964. BackupGlobalState;
  4965. AssignMathArray(tempVariableDesignator, actualParameter);
  4966. RestoreGlobalState;
  4967. (* use temporary variable as actual parameter instead of the original one *)
  4968. actualParameter := tempVariableDesignator;
  4969. (* create write-back call and store it in linked list *)
  4970. (* create new list entry *)
  4971. IF firstWriteBackCall = NIL THEN
  4972. NEW(firstWriteBackCall);
  4973. currentWriteBackCall := firstWriteBackCall
  4974. ELSE
  4975. ASSERT(currentWriteBackCall # NIL);
  4976. NEW(currentWriteBackCall.next);
  4977. currentWriteBackCall := currentWriteBackCall.next
  4978. END;
  4979. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4980. ASSERT(expression.type = NIL);
  4981. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4982. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4983. (* prepare writeback for any other "normal" indexer *)
  4984. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4985. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4986. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4987. Assign(tempVariableDesignator, actualParameter);
  4988. actualParameter := tempVariableDesignator;
  4989. IF firstWriteBackCall = NIL THEN
  4990. NEW(firstWriteBackCall);
  4991. currentWriteBackCall := firstWriteBackCall
  4992. ELSE
  4993. ASSERT(currentWriteBackCall # NIL);
  4994. NEW(currentWriteBackCall.next);
  4995. currentWriteBackCall := currentWriteBackCall.next
  4996. END;
  4997. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  4998. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4999. END
  5000. END
  5001. END PrepareParameter;
  5002. BEGIN
  5003. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5004. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5005. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5006. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5007. IF InlineProcedureCall(x) THEN
  5008. RETURN
  5009. ELSE
  5010. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5011. END
  5012. END;
  5013. END;
  5014. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5015. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5016. dest := destination; destination := emptyOperand;
  5017. SaveRegisters();ReleaseUsedRegisters(saved);
  5018. parameters := x.parameters;
  5019. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5020. (* an operator is called *)
  5021. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5022. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5023. (* check if a dynamic operator call should be performed *)
  5024. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5025. ELSE
  5026. isCallOfDynamicOperator := FALSE
  5027. END;
  5028. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5029. Emit(Push(position, ap));
  5030. END;
  5031. alignment := procedureType.stackAlignment;
  5032. IF alignment > 1 THEN
  5033. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5034. Emit(Mov(position,reg, sp));
  5035. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5036. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5037. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5038. Emit(And(position,sp, sp, mask));
  5039. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5040. Emit(Push(position,reg));
  5041. (*
  5042. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5043. Emit(Mov(position,mem,reg));
  5044. *)
  5045. ReleaseIntermediateOperand(reg);
  5046. END;
  5047. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5048. (* align stack to 16-byte boundary *)
  5049. IntermediateCode.InitImmediate(mask,addressType,-16);
  5050. Emit(And(position,sp, sp, mask));
  5051. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5052. IF gap # 0 THEN
  5053. IntermediateCode.InitImmediate(size,addressType,gap);
  5054. Emit(Sub(position,sp,sp,size))
  5055. END;
  5056. END;
  5057. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5058. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5059. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5060. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5061. ELSE
  5062. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5063. END;
  5064. Evaluate(x.left, operand);
  5065. IF symbol IS SyntaxTree.Procedure THEN
  5066. IF (procedureType.selfParameter # NIL) THEN
  5067. Emit(Push(position,operand.tag));
  5068. ELSIF x.left IS SyntaxTree.SupercallDesignator THEN
  5069. Emit(Push(position,operand.tag));
  5070. ELSIF (procedureType.isDelegate) THEN
  5071. Emit(Push(position,operand.tag));
  5072. END;
  5073. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5074. IF (procedureType.selfParameter # NIL) THEN
  5075. Emit(Push(position,operand.tag));
  5076. ELSIF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5077. Emit(Push(position,operand.tag));
  5078. END;
  5079. ELSE HALT(200);
  5080. END;
  5081. ReleaseIntermediateOperand(operand.tag);
  5082. operand.tag := emptyOperand;
  5083. (* determine if a structured return type is needed *)
  5084. structuredReturnType := SemanticChecker.StructuredReturnType(procedureType);
  5085. IF structuredReturnType THEN
  5086. IF resultDesignator # NIL THEN
  5087. d := resultDesignator;
  5088. ELSE
  5089. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5090. variable.SetUntraced(procedureType.hasUntracedReturn);
  5091. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5092. d.SetType(variable.type);
  5093. END;
  5094. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5095. Designate(d,returnValue);
  5096. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5097. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5098. Emit(Push(position,size));
  5099. Emit(Push(position,returnValue.op));
  5100. ReleaseOperand(returnValue);
  5101. ELSE
  5102. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5103. END;
  5104. END;
  5105. firstWriteBackCall := NIL; (* reset write-back call list *)
  5106. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5107. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5108. IF parameterRegister = NIL THEN parameterRegisters := 0
  5109. ELSE parameterRegisters := LEN(parameterRegister)
  5110. END;
  5111. passByRegister := parameterRegisters > 0;
  5112. registerNumber := 0;
  5113. formalParameter := procedureType.lastParameter;
  5114. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5115. actualParameter := parameters.GetExpression(i);
  5116. PrepareParameter(actualParameter, formalParameter);
  5117. IF passByRegister & (i < parameterRegisters) THEN
  5118. IF ~PassInRegister(formalParameter) THEN
  5119. Error(actualParameter.position,"cannot be passed by register")
  5120. ELSE
  5121. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5122. END;
  5123. INC(registerNumber);
  5124. ELSE
  5125. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5126. END;
  5127. formalParameter := formalParameter.prevParameter;
  5128. END;
  5129. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5130. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5131. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5132. END;
  5133. ELSE
  5134. hasDynamicOperands := FALSE;
  5135. formalParameter := procedureType.firstParameter;
  5136. FOR i := 0 TO parameters.Length() - 1 DO
  5137. actualParameter := parameters.GetExpression(i);
  5138. IF formalParameter # NIL THEN (* TENTATIVE *)
  5139. PrepareParameter(actualParameter, formalParameter);
  5140. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5141. ASSERT(i < 2);
  5142. hasDynamicOperands := TRUE;
  5143. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5144. ELSE
  5145. IF passByRegister & (registerNumber > 0) THEN
  5146. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5147. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5148. END;
  5149. passByRegister := FALSE;
  5150. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5151. END;
  5152. formalParameter := formalParameter.nextParameter;
  5153. END;
  5154. END;
  5155. END;
  5156. IF symbol IS SyntaxTree.Procedure THEN
  5157. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5158. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5159. Emit(Push(position,reg));
  5160. ReleaseIntermediateOperand(reg);
  5161. END;
  5162. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5163. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5164. parametersSize := ParametersSize(system,procedureType,FALSE);
  5165. END;
  5166. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5167. (*
  5168. dynamic operator overloading:
  5169. first push parameters, regularly:
  5170. [self]
  5171. par1
  5172. par2
  5173. then push parameters for GetOperator
  5174. identifier
  5175. ptr1
  5176. tag
  5177. ptr2
  5178. tag
  5179. call GetOperatorRuntimeProc
  5180. call Operator
  5181. *)
  5182. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5183. (* push ID *)
  5184. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5185. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5186. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5187. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5188. formalParameter := procedureType.firstParameter;
  5189. FOR i := 0 TO parameters.Length() - 1 DO
  5190. IF formalParameter.access # SyntaxTree.Hidden THEN
  5191. ASSERT(i < 2);
  5192. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5193. (* push pointer *)
  5194. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5195. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5196. (* add dereference *)
  5197. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5198. (*! better: do refer to stack above than using parameter backups !!*)
  5199. ReleaseIntermediateOperand(parameterBackups[i]);
  5200. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5201. END;
  5202. Emit(Push(position,parameterBackups[i]));
  5203. ReleaseIntermediateOperand(parameterBackups[i]);
  5204. (* push typetag *)
  5205. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5206. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5207. arg := TypeDescriptorAdr(recordType);
  5208. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5209. Emit(Push(position,arg));
  5210. ELSE
  5211. (* push 'NonPointer' *)
  5212. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5213. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5214. (* push fingerprint *)
  5215. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5216. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5217. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5218. END
  5219. END;
  5220. formalParameter := formalParameter.nextParameter
  5221. END;
  5222. (* for unary operators: complete the information for the second parameter *)
  5223. IF procedureType.numberParameters < 2 THEN
  5224. (* push 'NonPointer' *)
  5225. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5226. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5227. (* push 'NoType' *)
  5228. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5229. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5230. END;
  5231. (* call operator selection procedure *)
  5232. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5233. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5234. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5235. ReleaseOperand(operatorSelectionProcedureOperand);
  5236. (* use the address that the operator selection procedure returned as the target address of the call *)
  5237. InitOperand(operand, ModeValue);
  5238. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5239. Emit(Result(position,operand.op))
  5240. END
  5241. END;
  5242. ReleaseParameterRegisters();
  5243. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5244. Emit(Call(position,operand.op,0));
  5245. ELSE
  5246. Emit(Call(position,operand.op,parametersSize));
  5247. END;
  5248. ReleaseOperand(operand);
  5249. IF procedureType.noReturn THEN
  5250. EmitTrap(position,NoReturnTrap);
  5251. END;
  5252. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5253. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5254. Emit(Result(position,return));
  5255. END;
  5256. (* === return parameter space === *)
  5257. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5258. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5259. IF parametersSize < 32 THEN
  5260. (* allocated space for all parameter registers *)
  5261. parametersSize := 32
  5262. END;
  5263. size := IntermediateCode.Immediate(addressType,parametersSize);
  5264. Emit(Add(position,sp,sp,size))
  5265. END;
  5266. IF SysvABI(procedureType.callingConvention) THEN
  5267. IF passByRegister THEN
  5268. IF parameters.Length() > parameterRegisters THEN
  5269. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5270. ELSE
  5271. parametersSize := 0
  5272. END;
  5273. ELSE
  5274. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5275. INC( parametersSize, (-parametersSize) MOD 16 )
  5276. END;
  5277. IF parametersSize > 0 THEN
  5278. size := IntermediateCode.Immediate(addressType,parametersSize);
  5279. Emit(Add(position,sp,sp,size))
  5280. END;
  5281. END;
  5282. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5283. IF structuredReturnType THEN
  5284. (* stack pointer rewinding done by callee
  5285. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5286. Emit(Add(position,sp,sp,size));
  5287. *)
  5288. RestoreRegisters(saved);
  5289. InitOperand(result,ModeReference);
  5290. Symbol(variable,result);
  5291. ELSE
  5292. RestoreRegisters(saved);
  5293. InitOperand(result,ModeValue);
  5294. result.op := return;
  5295. END;
  5296. END;
  5297. IF alignment > 1 THEN
  5298. Emit(Pop(position,sp));
  5299. END;
  5300. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5301. Emit(Pop(position, ap));
  5302. END;
  5303. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5304. ValueToCondition(result);
  5305. END;
  5306. destination := dest;
  5307. (* perform all write-back calls in the list *)
  5308. BackupGlobalState;
  5309. currentWriteBackCall := firstWriteBackCall;
  5310. WHILE currentWriteBackCall # NIL DO
  5311. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5312. currentWriteBackCall := currentWriteBackCall.next
  5313. END;
  5314. RestoreGlobalState;
  5315. (* TENATIVE *)
  5316. (*
  5317. IF isOperatorCall THEN
  5318. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5319. END;
  5320. *)
  5321. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5322. END VisitProcedureCallDesignator;
  5323. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5324. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5325. td: SyntaxTree.Symbol;
  5326. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5327. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5328. BEGIN
  5329. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5330. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5331. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5332. variable.SetType(system.anyType);
  5333. scope.AddVariable(variable);
  5334. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5335. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5336. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5337. typeDeclaration.SetScope(module.module.moduleScope);
  5338. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5339. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5340. END;
  5341. RETURN hiddenPointerType;
  5342. END GetHiddenPointerType;
  5343. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5344. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5345. BEGIN
  5346. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5347. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5348. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5349. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5350. scope.AddVariable(variable);
  5351. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5352. variable.SetType(system.anyType);
  5353. scope.AddVariable(variable);
  5354. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5355. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5356. typeDeclaration.SetDeclaredType(delegatePointerType);
  5357. typeDeclaration.SetScope(module.module.moduleScope);
  5358. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5359. delegatePointerType.SetState(SyntaxTree.Resolved);
  5360. END;
  5361. RETURN delegatePointerType
  5362. END GetDelegateType;
  5363. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5364. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5365. such as in VAR a: RECORD ... END;
  5366. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5367. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5368. *)
  5369. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5370. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5371. BEGIN (* no code emission *)
  5372. source := NIL;
  5373. x := x.resolved;
  5374. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5375. x := GetHiddenPointerType();
  5376. ELSIF IsDelegate(x) THEN
  5377. x := GetDelegateType();
  5378. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5379. ELSE HALT(200);
  5380. END;
  5381. td := x.typeDeclaration;
  5382. IF td = NIL THEN
  5383. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5384. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5385. ASSERT(td # NIL);
  5386. END;
  5387. IF newObjectFile THEN
  5388. GetCodeSectionNameForSymbol(td,name);
  5389. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5390. meta.CheckTypeDeclaration(x);
  5391. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5392. ELSE
  5393. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5394. END;
  5395. IF backend.cooperative OR meta.simple THEN
  5396. offset := 0;
  5397. ELSE
  5398. IF x IS SyntaxTree.CellType THEN
  5399. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5400. IF baseRecord = NIL THEN
  5401. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5402. ELSE
  5403. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5404. END;
  5405. ELSE
  5406. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5407. END;
  5408. END;
  5409. ELSE
  5410. offset := 0;
  5411. source := module.allSections.FindBySymbol(td); (*TODO*)
  5412. IF source = NIL THEN
  5413. null := 0;
  5414. GetCodeSectionNameForSymbol(td,name);
  5415. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5416. Basic.SuffixSegmentedName (name, module.module.name);
  5417. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5418. IntermediateCode.InitImmediate(op,addressType,0);
  5419. source(IntermediateCode.Section).Emit(Data(position,op));
  5420. source.SetReferenced(FALSE)
  5421. ELSE
  5422. name := source.name;
  5423. END;
  5424. END;
  5425. RETURN td
  5426. END GetBackendType;
  5427. BEGIN
  5428. (*td := t.typeDeclaration;*)
  5429. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5430. (*
  5431. IF t IS SyntaxTree.PointerType THEN
  5432. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5433. ELSE
  5434. source := GetBackendType(t);
  5435. END;
  5436. *)
  5437. IF newObjectFile THEN
  5438. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5439. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5440. IntermediateCode.SetOffset(res,offset);
  5441. ELSE
  5442. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5443. END;
  5444. (*
  5445. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5446. make memory should be used when tag is used, not earlier
  5447. *)
  5448. RETURN res
  5449. END TypeDescriptorAdr;
  5450. (*
  5451. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5452. VAR result: IntermediateCode.Operand;
  5453. BEGIN
  5454. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5455. operand.tag := TypeDescriptorAdr(operand.type);
  5456. IntermediateCode.MakeMemory(operand.tag,addressType);
  5457. UseIntermediateOperand(operand.tag);
  5458. END;
  5459. END MakeTypeTag;
  5460. *)
  5461. PROCEDURE ProfilerInit;
  5462. VAR reg: IntermediateCode.Operand;
  5463. BEGIN
  5464. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5465. Emit(Call(position,reg,0));
  5466. END ProfilerInit;
  5467. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5468. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5469. BEGIN
  5470. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5471. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5472. THEN
  5473. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5474. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5475. Emit(Push(position,reg));
  5476. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5477. Emit(Push(position,reg));
  5478. StaticCallOperand(result,procedure);
  5479. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5480. ReleaseOperand(result);
  5481. END;
  5482. END ProfilerEnterExit;
  5483. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5484. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5485. BEGIN
  5486. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5487. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5488. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5489. profileInit.Emit(Push(position,reg));
  5490. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5491. profileInit.Emit(Push(position,reg));
  5492. NEW(string, LEN(name)); COPY(name, string^);
  5493. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5494. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5495. sv.SetType(type);
  5496. Designate(sv,result);
  5497. profileInit.Emit(Push(position,result.tag));
  5498. profileInit.Emit(Push(position,result.op));
  5499. StaticCallOperand(result,procedure);
  5500. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5501. ReleaseOperand(result);
  5502. END;
  5503. END ProfilerAddProcedure;
  5504. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5505. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5506. BEGIN
  5507. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5508. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5509. profileInit.Emit(Push(position,reg));
  5510. profileInitPatchPosition := profileInit.pc;
  5511. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5512. NEW(string, LEN(name)); COPY(name, string^);
  5513. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5514. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5515. sv.SetType(type);
  5516. Designate(sv,result);
  5517. profileInit.Emit(Push(position,result.tag));
  5518. profileInit.Emit(Push(position,result.op));
  5519. StaticCallOperand(result,procedure);
  5520. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5521. ReleaseOperand(result);
  5522. END;
  5523. END ProfilerAddModule;
  5524. PROCEDURE ProfilerPatchInit;
  5525. VAR reg: IntermediateCode.Operand;
  5526. BEGIN
  5527. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5528. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5529. EmitLeave(profileInit,position,NIL,0);
  5530. profileInit.Emit(Exit(position,0,0,0));
  5531. END ProfilerPatchInit;
  5532. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5533. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5534. VAR
  5535. id: LONGINT;
  5536. leftType, rightType: SyntaxTree.Type;
  5537. procedureType: SyntaxTree.ProcedureType;
  5538. runtimeProcedure: SyntaxTree.Procedure;
  5539. runtimeProcedureOperand, operatorOperand: Operand;
  5540. kind: SET;
  5541. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5542. VAR
  5543. arg: IntermediateCode.Operand;
  5544. recordType: SyntaxTree.RecordType;
  5545. fingerPrint: SyntaxTree.FingerPrint;
  5546. BEGIN
  5547. IF type = NIL THEN
  5548. (* no type: push 'NoType' *)
  5549. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5550. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5551. ELSIF IsStrictlyPointerToRecord(type) THEN
  5552. (* pointer to record type: push typetag *)
  5553. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5554. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5555. arg := TypeDescriptorAdr(recordType);
  5556. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5557. ELSE
  5558. (* non-pointer to record type: push fingerprint *)
  5559. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5560. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5561. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5562. END;
  5563. operatorInitializationCodeSection.Emit(Push(position,arg))
  5564. END PushTypeInfo;
  5565. BEGIN
  5566. ASSERT(operatorInitializationCodeSection # NIL);
  5567. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5568. procedureType := operator.type(SyntaxTree.ProcedureType);
  5569. (* determine types *)
  5570. leftType := procedureType.firstParameter.type;
  5571. IF procedureType.numberParameters = 2 THEN
  5572. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5573. rightType := procedureType.firstParameter.nextParameter.type;
  5574. ELSE
  5575. rightType := NIL
  5576. END;
  5577. (* determine operator kind *)
  5578. IF IsStrictlyPointerToRecord(leftType) THEN
  5579. kind := {LhsIsPointer}
  5580. ELSE
  5581. kind := {}
  5582. END;
  5583. IF IsStrictlyPointerToRecord(rightType) THEN
  5584. kind := kind + {RhsIsPointer}
  5585. END;
  5586. IF kind # {} THEN (* TODO: to be removed later on *)
  5587. (* at least one of the types is a pointer to record *)
  5588. (* emit a code that registers this specific operator in the runtime *)
  5589. dump := operatorInitializationCodeSection.comments;
  5590. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5591. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5592. (* push ID *)
  5593. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5594. id := Global.GetSymbol(module.module.case, operator.name);
  5595. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5596. (* push kind *)
  5597. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5598. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5599. (* push type infos *)
  5600. PushTypeInfo(leftType);
  5601. PushTypeInfo(rightType);
  5602. (* push operator address *)
  5603. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5604. StaticCallOperand(operatorOperand, operator);
  5605. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5606. ReleaseOperand(operatorOperand);
  5607. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5608. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5609. ReleaseOperand(runtimeProcedureOperand)
  5610. END
  5611. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5612. END
  5613. END RegisterDynamicOperator;
  5614. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5615. VAR
  5616. traceModule: SyntaxTree.Module;
  5617. procedure: SyntaxTree.Procedure;
  5618. procedureVariable: SyntaxTree.Variable;
  5619. s,msg: Basic.MessageString;
  5620. res: Operand;
  5621. i: LONGINT;
  5622. sv: SyntaxTree.StringValue;
  5623. type: SyntaxTree.Type;
  5624. recordType: SyntaxTree.RecordType;
  5625. printout: Printout.Printer;
  5626. stringWriter: Streams.StringWriter;
  5627. expression: SyntaxTree.Expression;
  5628. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5629. BEGIN
  5630. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5631. IF procedure = NIL THEN
  5632. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5633. END;
  5634. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5635. s := "procedure ";
  5636. Strings.Append(s,backend.traceModuleName);
  5637. Strings.Append(s,".");
  5638. Strings.Append(s,procedureName);
  5639. Strings.Append(s," not present");
  5640. Error(position,s);
  5641. RETURN FALSE
  5642. ELSE
  5643. RETURN TRUE
  5644. END;
  5645. END GetProcedure;
  5646. PROCEDURE CallProcedure;
  5647. VAR size: LONGINT;
  5648. BEGIN
  5649. IF procedure # NIL THEN
  5650. StaticCallOperand(result,procedure);
  5651. size := ProcedureParametersSize(system,procedure);
  5652. ELSE
  5653. Symbol(procedureVariable, result);
  5654. LoadValue(result, procedureVariable.type.resolved);
  5655. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5656. END;
  5657. Emit(Call(position,result.op,size));
  5658. END CallProcedure;
  5659. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5660. VAR res: Operand; string: SyntaxTree.String;
  5661. BEGIN
  5662. IF GetProcedure("String") THEN
  5663. NEW(string, LEN(s)); COPY(s, string^);
  5664. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5665. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5666. sv.SetType(type);
  5667. Designate(sv,res);
  5668. Emit(Push(position,res.tag));
  5669. Emit(Push(position,res.op));
  5670. ReleaseOperand(res);
  5671. CallProcedure;
  5672. END;
  5673. END String;
  5674. PROCEDURE Integer(op: IntermediateCode.Operand);
  5675. BEGIN
  5676. IF GetProcedure("Int") THEN
  5677. Emit(Push(position,op));
  5678. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5679. CallProcedure;
  5680. END;
  5681. END Integer;
  5682. PROCEDURE Float(op: IntermediateCode.Operand);
  5683. BEGIN
  5684. IF GetProcedure("HIntHex") THEN
  5685. Emit(Push(position,op));
  5686. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5687. CallProcedure;
  5688. END;
  5689. END Float;
  5690. PROCEDURE Set(op: IntermediateCode.Operand);
  5691. BEGIN
  5692. IF GetProcedure("Set") THEN
  5693. Emit(Push(position,op));
  5694. (*
  5695. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5696. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5697. *)
  5698. CallProcedure;
  5699. END;
  5700. END Set;
  5701. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5702. BEGIN
  5703. IF GetProcedure("Boolean") THEN
  5704. Emit(Push(position,op));
  5705. CallProcedure;
  5706. END;
  5707. END Boolean;
  5708. PROCEDURE Char(op: IntermediateCode.Operand);
  5709. BEGIN
  5710. IF GetProcedure("Char") THEN
  5711. Emit(Push(position,op));
  5712. CallProcedure;
  5713. END;
  5714. END Char;
  5715. PROCEDURE Address(op: IntermediateCode.Operand);
  5716. BEGIN
  5717. IF GetProcedure("Address") THEN
  5718. Emit(Push(position,op));
  5719. CallProcedure;
  5720. END;
  5721. END Address;
  5722. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5723. VAR len: IntermediateCode.Operand;
  5724. BEGIN
  5725. IF GetProcedure("String") THEN
  5726. len := GetArrayLength(type, op.tag);
  5727. Emit(Push(position,len));
  5728. ReleaseIntermediateOperand(len);
  5729. Emit(Push(position,op.op));
  5730. CallProcedure;
  5731. END;
  5732. END StringOperand;
  5733. PROCEDURE Ln;
  5734. BEGIN
  5735. IF GetProcedure("Ln") THEN
  5736. CallProcedure;
  5737. END;
  5738. END Ln;
  5739. BEGIN
  5740. IF backend.traceModuleName = "" THEN RETURN END;
  5741. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5742. IF GetProcedure("Enter") THEN
  5743. CallProcedure
  5744. END;
  5745. NEW(stringWriter,LEN(s));
  5746. FOR i := 0 TO x.Length()-1 DO
  5747. msg := "";
  5748. expression := x.GetExpression(i);
  5749. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5750. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5751. ELSE
  5752. Global.GetModuleName(module.module, s);
  5753. END;
  5754. IF i = 0 THEN
  5755. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5756. stringWriter.String(":");
  5757. END;
  5758. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5759. IF ~(expression IS SyntaxTree.StringValue) THEN
  5760. printout.Expression(expression);
  5761. stringWriter.Get(s);
  5762. Strings.Append(msg,s);
  5763. Strings.Append(msg,"= ");
  5764. ELSE stringWriter.Get(s); (* remove from string writer *)
  5765. Strings.Append(msg, s);
  5766. END;
  5767. String(msg);
  5768. IF SemanticChecker.IsStringType(expression.type) THEN
  5769. Designate(expression,res);
  5770. StringOperand(res, expression.type);
  5771. ELSE
  5772. Evaluate(expression,res);
  5773. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5774. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5775. Convert(res.op,int64);
  5776. END;
  5777. Integer(res.op);
  5778. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5779. Boolean(res.op);
  5780. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5781. Set(res.op);
  5782. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5783. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5784. Convert(res.op,float64);
  5785. END;
  5786. Float(res.op);
  5787. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5788. Char(res.op);
  5789. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5790. Address(res.op);String("H");
  5791. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5792. Address(res.op);String("H");
  5793. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5794. Address(res.op);String("H");
  5795. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5796. Address(res.op);String("H");
  5797. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5798. String("NIL");
  5799. ELSE HALT(200);
  5800. END;
  5801. END;
  5802. ReleaseOperand(res);
  5803. String("; ");
  5804. END;
  5805. IF GetProcedure("Exit") THEN
  5806. CallProcedure
  5807. ELSE
  5808. Ln;
  5809. END;
  5810. END;
  5811. END SystemTrace;
  5812. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5813. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5814. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5815. BEGIN
  5816. type := type.resolved;
  5817. IF type IS SyntaxTree.RecordType THEN
  5818. WITH type: SyntaxTree.RecordType DO
  5819. baseType := type.baseType;
  5820. IF baseType # NIL THEN
  5821. baseType := baseType.resolved;
  5822. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5823. InitFields(baseType,adr,offset);
  5824. END;
  5825. variable := type.recordScope.firstVariable;
  5826. WHILE variable # NIL DO
  5827. IF variable.initializer # NIL THEN
  5828. Evaluate(variable.initializer,initializerOp);
  5829. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5830. Emit(Mov(position,mem,initializerOp.op));
  5831. ReleaseOperand(initializerOp);
  5832. ReleaseIntermediateOperand(mem);
  5833. END;
  5834. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5835. variable := variable.nextVariable
  5836. END;
  5837. END;
  5838. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5839. WITH type: SyntaxTree.ArrayType DO
  5840. IF type.form = SyntaxTree.Static THEN
  5841. baseType := type.arrayBase;
  5842. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5843. FOR i := 0 TO type.staticLength-1 DO
  5844. InitFields(baseType,adr,offset+i*size);
  5845. END;
  5846. END;
  5847. END;
  5848. ELSIF type IS SyntaxTree.MathArrayType THEN
  5849. WITH type: SyntaxTree.MathArrayType DO
  5850. IF type.form = SyntaxTree.Open THEN
  5851. dim := DynamicDim(type);
  5852. imm := IntermediateCode.Immediate(addressType,dim);
  5853. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5854. baseType := SemanticChecker.ArrayBase(type,dim);
  5855. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5856. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5857. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5858. ReleaseIntermediateOperand(imm);
  5859. (* flags remain empty (=0) for open array *)
  5860. ELSIF type.form = SyntaxTree.Static THEN
  5861. baseType := type.arrayBase;
  5862. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5863. ASSERT(type.staticLength < 1024*1024*1024);
  5864. FOR i := 0 TO type.staticLength-1 DO
  5865. InitFields(baseType,adr,offset+i*size);
  5866. END;
  5867. END;
  5868. END;
  5869. END;
  5870. END InitFields;
  5871. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5872. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5873. BEGIN
  5874. type := variable.type.resolved;
  5875. IF (type IS SyntaxTree.MathArrayType) THEN
  5876. WITH type: SyntaxTree.MathArrayType DO
  5877. IF type.form = SyntaxTree.Open THEN
  5878. Symbol(variable,operand);
  5879. InitFields(type, operand.tag,0);
  5880. ELSIF type.form = SyntaxTree.Tensor THEN
  5881. Symbol(variable, operand);
  5882. MakeMemory(tmp,operand.op,addressType,0);
  5883. ReleaseOperand(operand);
  5884. Emit(Mov(position,tmp, nil ) );
  5885. ReleaseIntermediateOperand(tmp);
  5886. END;
  5887. END;
  5888. ELSE
  5889. Symbol(variable,operand);
  5890. IF variable.initializer # NIL THEN
  5891. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5892. reference.SetType(variable.type.resolved);
  5893. reference.SetAssignable(TRUE);
  5894. Assign(reference,variable.initializer);
  5895. END;
  5896. InitFields(type, operand.op,0);
  5897. ReleaseOperand(operand);
  5898. END;
  5899. END InitVariable;
  5900. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5901. VAR end: Label;
  5902. BEGIN
  5903. IF type.form = SyntaxTree.Tensor THEN
  5904. InitOperand(result,ModeValue);
  5905. ReuseCopy(result.op,base);
  5906. end := NewLabel();
  5907. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5908. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5909. SetLabel(end);
  5910. Convert(result.op,int32);
  5911. ELSE
  5912. InitOperand(result,ModeValue);
  5913. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5914. END
  5915. END MathArrayDim;
  5916. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5917. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5918. BEGIN
  5919. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5920. MakeMemory(mem,base,addressType,offset);
  5921. Emit(Mov(position,mem,value));
  5922. ReleaseIntermediateOperand(mem);
  5923. END PutMathArrayField;
  5924. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5925. VAR mem: IntermediateCode.Operand;
  5926. BEGIN
  5927. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5928. MakeMemory(mem,base,addressType,offset);
  5929. Emit(Mov(position,mem,value));
  5930. ReleaseIntermediateOperand(mem);
  5931. END PutMathArrayFieldOffset;
  5932. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5933. BEGIN
  5934. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5935. MakeMemory(value,base,addressType,offset);
  5936. END GetMathArrayField;
  5937. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5938. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5939. BEGIN
  5940. IF incr THEN
  5941. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5942. ELSE
  5943. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5944. END;
  5945. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5946. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5947. ELSE
  5948. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5949. Emit(Mov(position,reg,dim));
  5950. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5951. Emit(Add(position,reg,reg,base));
  5952. MakeMemory(mem, reg, addressType, offset);
  5953. ReleaseIntermediateOperand(reg);
  5954. Emit(Mov(position,mem,value));
  5955. ReleaseIntermediateOperand(mem);
  5956. END;
  5957. END PutMathArrayLenOrIncr;
  5958. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5959. BEGIN
  5960. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5961. END PutMathArrayLength;
  5962. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5963. BEGIN
  5964. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5965. END PutMathArrayIncrement;
  5966. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5967. BEGIN
  5968. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5969. END GetMathArrayIncrement;
  5970. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5971. BEGIN
  5972. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5973. END GetMathArrayLength;
  5974. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5975. VAR dimOp: IntermediateCode.Operand;
  5976. BEGIN
  5977. dimOp := IntermediateCode.Immediate(int32, dim);
  5978. GetMathArrayLength(type, operand, dimOp, check, result);
  5979. END GetMathArrayLengthAt;
  5980. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5981. VAR dimOp: IntermediateCode.Operand;
  5982. BEGIN
  5983. dimOp := IntermediateCode.Immediate(int32, dim);
  5984. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5985. END GetMathArrayIncrementAt;
  5986. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5987. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5988. offset: LONGINT;
  5989. BEGIN
  5990. IF increment THEN
  5991. offset := MathIncrOffset;
  5992. ELSE
  5993. offset := MathLenOffset;
  5994. END;
  5995. INC(offset,operand.dimOffset*2);
  5996. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5997. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5998. END;
  5999. (* static dimension *)
  6000. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6001. IF check & (type.form = SyntaxTree.Tensor) THEN
  6002. DimensionCheck(operand.tag,dim,BrltL);
  6003. END;
  6004. val := SHORT(dim.intValue);
  6005. IF type.form # SyntaxTree.Tensor THEN
  6006. t := SemanticChecker.ArrayBase(type,val);
  6007. type := t.resolved(SyntaxTree.MathArrayType);
  6008. IF type.form = SyntaxTree.Static THEN
  6009. IF increment THEN
  6010. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6011. ELSE
  6012. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6013. END;
  6014. InitOperand(result,ModeValue);
  6015. result.op := res;
  6016. RETURN;
  6017. END;
  6018. END;
  6019. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6020. MakeMemory(res,operand.tag,addressType,offset);
  6021. (*
  6022. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6023. *)
  6024. InitOperand(result,ModeValue);
  6025. result.op := res;
  6026. ELSE
  6027. Convert(dim,addressType);
  6028. IF check THEN
  6029. IF type.form = SyntaxTree.Tensor THEN
  6030. DimensionCheck(operand.tag,dim,BrltL);
  6031. ELSIF isUnchecked THEN (* do nothing *)
  6032. ELSE
  6033. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6034. END;
  6035. END;
  6036. end := NewLabel(); next := NIL;
  6037. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6038. Emit(Mov(position,res,dim));
  6039. Convert(res,int32);
  6040. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6041. WHILE t IS SyntaxTree.MathArrayType DO
  6042. type := t(SyntaxTree.MathArrayType);
  6043. IF type.form = SyntaxTree.Static THEN
  6044. imm := IntermediateCode.Immediate(int32,val);
  6045. next := NewLabel();
  6046. BrneL(next,imm,res);
  6047. IF increment THEN
  6048. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6049. ELSE
  6050. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6051. END;
  6052. Emit(MovReplace(position,res,imm));
  6053. BrL(end);
  6054. ELSE hasDynamicPart := TRUE;
  6055. END;
  6056. t := type.arrayBase.resolved;
  6057. val := val + 1;
  6058. IF next # NIL THEN SetLabel(next) END;
  6059. END;
  6060. IF hasDynamicPart THEN
  6061. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6062. Emit(Mov(position,res2,dim));
  6063. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6064. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6065. Emit(Add(position,res2,res2,imm));
  6066. Emit(Add(position,res2,res2,operand.tag));
  6067. IntermediateCode.MakeMemory(res2,int32);
  6068. Emit(MovReplace(position,res,res2));
  6069. ReleaseIntermediateOperand(res2);
  6070. END;
  6071. SetLabel(end);
  6072. Convert(res,int32);
  6073. InitOperand(result,ModeValue);
  6074. result.op := res;
  6075. END;
  6076. END MathArrayLenOrIncr;
  6077. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6078. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6079. offset: LONGINT;
  6080. BEGIN
  6081. offset := operand.dimOffset+DynamicDim(type)-1;
  6082. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6083. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6084. val := SHORT(dim.intValue);
  6085. t := SemanticChecker.ArrayBase(type,val);
  6086. type := t.resolved(SyntaxTree.ArrayType);
  6087. IF type.form = SyntaxTree.Static THEN
  6088. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6089. ELSE
  6090. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6091. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6092. END;
  6093. UseIntermediateOperand(res);
  6094. InitOperand(result,ModeValue);
  6095. result.op := res;
  6096. ELSE
  6097. Convert(dim,addressType);
  6098. IF ~isUnchecked THEN
  6099. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6100. END;
  6101. end := NewLabel(); next := NIL;
  6102. (* ReuseCopy(dim,res); *)
  6103. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6104. Emit(Mov(position,res,dim));
  6105. Convert(res,int32);
  6106. Convert(res,int32);
  6107. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6108. WHILE t IS SyntaxTree.ArrayType DO
  6109. type := t(SyntaxTree.ArrayType);
  6110. IF type.form = SyntaxTree.Static THEN
  6111. imm := IntermediateCode.Immediate(int32,val);
  6112. next := NewLabel();
  6113. BrneL(next,imm,res);
  6114. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6115. Emit(MovReplace(position,res,imm));
  6116. BrL(end);
  6117. ELSE hasDynamicPart := TRUE;
  6118. END;
  6119. t := type.arrayBase.resolved;
  6120. val := val + 1;
  6121. IF next # NIL THEN SetLabel(next) END;
  6122. END;
  6123. IF hasDynamicPart THEN
  6124. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6125. Convert(res2,addressType);
  6126. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6127. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6128. Emit(Sub(position,res2,imm,res2));
  6129. Emit(Add(position,res2,res2,operand.tag));
  6130. IntermediateCode.MakeMemory(res2,int32);
  6131. Emit(MovReplace(position,res,res2));
  6132. ReleaseIntermediateOperand(res2);
  6133. END;
  6134. SetLabel(end);
  6135. Convert(res,int32);
  6136. InitOperand(result,ModeValue);
  6137. result.op := res;
  6138. END;
  6139. END ArrayLen;
  6140. (**
  6141. create a temporary variable in current scope
  6142. **)
  6143. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6144. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6145. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6146. BEGIN
  6147. IF ~register THEN
  6148. variable := temporaries.GetFreeVariable(type, index);
  6149. ELSE
  6150. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6151. END;
  6152. scope := currentScope;
  6153. IF variable = NIL THEN
  6154. COPY("@hiddenIRVar",string);
  6155. Basic.AppendNumber(string,index);
  6156. name := SyntaxTree.NewIdentifier(string);
  6157. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6158. variable.SetType(type);
  6159. variable.SetAccess(SyntaxTree.Hidden);
  6160. IF ~register THEN
  6161. temporaries.AddVariable(variable);
  6162. IF scope.lastVariable # NIL THEN
  6163. offset := scope.lastVariable.offsetInBits;
  6164. ELSE
  6165. offset := 0;
  6166. END;
  6167. DEC(offset,system.SizeOf(variable.type));
  6168. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6169. variable(SyntaxTree.Variable).SetOffset(offset);
  6170. scope.AddVariable(variable(SyntaxTree.Variable));
  6171. scope.EnterSymbol(variable, duplicate);
  6172. ASSERT(~duplicate);
  6173. InitVariable(variable(SyntaxTree.Variable));
  6174. ELSE
  6175. variable.SetUseRegister(TRUE);
  6176. variable(SyntaxTree.Variable).SetOffset(0);
  6177. END;
  6178. ELSE
  6179. InitVariable(variable(SyntaxTree.Variable));
  6180. (*
  6181. ASSERT(variable.type.resolved = type.resolved)
  6182. *)
  6183. END;
  6184. RETURN variable(SyntaxTree.Variable)
  6185. END GetTemporaryVariable;
  6186. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6187. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6188. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6189. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6190. i: LONGINT; duplicate: BOOLEAN;
  6191. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6192. VAR variable: SyntaxTree.Variable;
  6193. BEGIN
  6194. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6195. variable.SetType(type);
  6196. recordScope.AddVariable(variable);
  6197. END AddVariable;
  6198. BEGIN
  6199. name := "@ArrayDescriptor";
  6200. Basic.AppendNumber(name,dimensions);
  6201. identifier := SyntaxTree.NewIdentifier(name);
  6202. parentScope := module.module.moduleScope;
  6203. symbol := parentScope.FindSymbol(identifier);
  6204. IF symbol # NIL THEN
  6205. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6206. type := typeDeclaration.declaredType;
  6207. ELSE
  6208. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6209. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6210. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6211. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6212. recordType.SetTypeDeclaration(typeDeclaration);
  6213. recordType.SetState(SyntaxTree.Resolved);
  6214. typeDeclaration.SetDeclaredType(recordType);
  6215. AddVariable("@ptr",system.anyType);
  6216. AddVariable("@adr",system.addressType);
  6217. AddVariable("@flags",system.addressType);
  6218. AddVariable("@dim",system.addressType);
  6219. AddVariable("@elementSize",system.addressType);
  6220. FOR i := 0 TO dimensions-1 DO
  6221. name := "@len";
  6222. Basic.AppendNumber(name,i);
  6223. AddVariable(name,system.addressType);
  6224. name := "@incr";
  6225. Basic.AppendNumber(name,i);
  6226. AddVariable(name,system.addressType);
  6227. END;
  6228. parentScope.AddTypeDeclaration(typeDeclaration);
  6229. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6230. ASSERT(~duplicate);
  6231. type := recordType;
  6232. END;
  6233. RETURN type
  6234. END GetMathArrayDescriptorType;
  6235. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6236. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6237. BEGIN
  6238. type := GetMathArrayDescriptorType(dimensions);
  6239. Emit(Push(position,op.op));
  6240. (* push type descriptor *)
  6241. reg := TypeDescriptorAdr(type);
  6242. IF ~newObjectFile THEN
  6243. IntermediateCode.MakeMemory(reg,addressType);
  6244. END;
  6245. Emit(Push(position,reg));
  6246. ReleaseIntermediateOperand(reg);
  6247. (* push realtime flag: false by default *)
  6248. Emit(Push(position,false));
  6249. CallThis(position,"Heaps","NewRec",3);
  6250. END NewMathArrayDescriptor;
  6251. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6252. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6253. BEGIN
  6254. NEW(string, LEN(s)); COPY(s, string^);
  6255. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6256. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6257. sv.SetType(type);
  6258. Designate(sv,res);
  6259. Emit(Push(position,res.tag));
  6260. Emit(Push(position,res.op));
  6261. ReleaseOperand(res);
  6262. END PushConstString;
  6263. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6264. BEGIN
  6265. IF b THEN
  6266. Emit(Push(Basic.invalidPosition, true));
  6267. ELSE
  6268. Emit(Push(Basic.invalidPosition, false));
  6269. END;
  6270. END PushConstBoolean;
  6271. PROCEDURE PushConstSet(v: SET);
  6272. VAR value: SyntaxTree.Value; op: Operand;
  6273. BEGIN
  6274. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6275. value.SetType(system.setType);
  6276. Evaluate(value, op);
  6277. Emit(Push(Basic.invalidPosition, op.op));
  6278. ReleaseOperand(op);
  6279. END PushConstSet;
  6280. PROCEDURE PushConstInteger(v: LONGINT);
  6281. VAR value: SyntaxTree.Value; op: Operand;
  6282. BEGIN
  6283. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6284. value.SetType(system.longintType);
  6285. Evaluate(value, op);
  6286. Emit(Push(Basic.invalidPosition, op.op));
  6287. ReleaseOperand(op);
  6288. END PushConstInteger;
  6289. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6290. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6291. section: IntermediateCode.Section;
  6292. BEGIN
  6293. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6294. Global.GetSymbolSegmentedName(symbol, name);
  6295. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6296. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6297. procedure.SetScope(moduleScope);
  6298. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6299. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6300. procedure.SetAccess(SyntaxTree.Hidden);
  6301. currentScope := procedureScope;
  6302. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6303. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6304. RETURN section;
  6305. END OpenInitializer;
  6306. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6307. BEGIN
  6308. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6309. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6310. section := prev;
  6311. END CloseInitializer;
  6312. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6313. VAR name: SyntaxTree.IdentifierString;
  6314. variable: SyntaxTree.Variable;
  6315. parameter: SyntaxTree.Parameter;
  6316. type: SyntaxTree.Type;
  6317. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6318. BEGIN
  6319. InitOperand(op,ModeReference);
  6320. op.op := fp;
  6321. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6322. Dereference(op, x, FALSE);
  6323. result := op;
  6324. Symbol(symbol, op);
  6325. END Field;
  6326. PROCEDURE Direction(direction: LONGINT): SET;
  6327. BEGIN
  6328. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6329. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6330. ELSE HALT(100);
  6331. END;
  6332. END Direction;
  6333. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6334. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6335. BEGIN
  6336. Field(port, op);
  6337. ToMemory(op.op,addressType,0);
  6338. Emit(Push(Basic.invalidPosition, op.op));
  6339. ReleaseOperand(op);
  6340. Basic.GetString(modifier.identifier, name);
  6341. PushConstString(name);
  6342. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6343. ASSERT(SemanticChecker.IsStringType(value.type));
  6344. Designate(value, op);
  6345. Emit(Push(modifier.position, op.tag));
  6346. Emit(Push(modifier.position, op.op));
  6347. ReleaseOperand(op);
  6348. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6349. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6350. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6351. Evaluate(value, op);
  6352. Emit(Push(modifier.position, op.op));
  6353. ReleaseOperand(op);
  6354. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6355. ELSE
  6356. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6357. END;
  6358. END AddPortProperty;
  6359. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6360. VAR modifier: SyntaxTree.Modifier;
  6361. BEGIN
  6362. modifier := parameter.modifiers;
  6363. WHILE modifier # NIL DO
  6364. AddPortProperty(parameter,modifier, modifier.expression);
  6365. modifier := modifier.nextModifier;
  6366. END;
  6367. END AddPortProperties;
  6368. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6369. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6370. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6371. PROCEDURE PushLens(type: SyntaxTree.Type);
  6372. BEGIN
  6373. IF IsSemiDynamicArray(type) THEN
  6374. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6375. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6376. Emit(Push(Basic.invalidPosition, op.op));
  6377. ReleaseOperand(op);
  6378. INC(dim);
  6379. ELSIF IsStaticArray(type) THEN
  6380. len := len * type(SyntaxTree.ArrayType).staticLength;
  6381. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6382. INC(dim);
  6383. ELSE
  6384. baseType := type;
  6385. END;
  6386. END PushLens;
  6387. BEGIN
  6388. (* cell *)
  6389. IF parameter.type IS SyntaxTree.ArrayType THEN
  6390. type := parameter.type;
  6391. dim := 0;
  6392. len := 1;
  6393. PushLens(type);
  6394. portType := baseType.resolved(SyntaxTree.PortType);
  6395. ELSE
  6396. portType := parameter.type(SyntaxTree.PortType);
  6397. END;
  6398. PushSelfPointer();
  6399. (* port / array of ports *)
  6400. IF IsStaticArray(type) THEN
  6401. PushConstInteger(len);
  6402. END;
  6403. Field(parameter, op);
  6404. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6405. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6406. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6407. Designate(d, op);*)
  6408. Emit(Push(Basic.invalidPosition, op.op));
  6409. ReleaseOperand(op);
  6410. (* name *)
  6411. PushConstString(name);
  6412. (* inout *)
  6413. PushConstSet(Direction(portType.direction));
  6414. (* width *)
  6415. PushConstInteger(portType.sizeInBits);
  6416. IF parameter.type IS SyntaxTree.PortType THEN
  6417. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6418. AddPortProperties(parameter);
  6419. ELSIF IsStaticArray(type)THEN
  6420. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6421. ELSIF IsSemiDynamicArray(type) THEN
  6422. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6423. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6424. Emit(Add(position,reg, sp, size));
  6425. (* dim *)
  6426. PushConstInteger(dim);
  6427. (* len array *)
  6428. Emit(Push(position, reg));
  6429. ReleaseIntermediateOperand(reg);
  6430. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6431. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6432. Emit(Add(position, sp,sp, size));
  6433. ELSE
  6434. HALT(100);
  6435. END;
  6436. END Parameter;
  6437. BEGIN
  6438. IF backend.cellsAreObjects THEN
  6439. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6440. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6441. END;
  6442. parameter := x.firstParameter;
  6443. WHILE (parameter # NIL) DO
  6444. type := parameter.type.resolved;
  6445. WHILE (type IS SyntaxTree.ArrayType) DO
  6446. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6447. END;
  6448. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6449. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6450. Parameter(name,parameter);
  6451. END;
  6452. parameter := parameter.nextParameter;
  6453. END;
  6454. ELSE HALT(200)
  6455. END;
  6456. END AddPorts;
  6457. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6458. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6459. BEGIN
  6460. Symbol(cell,op);
  6461. ToMemory(op.op,addressType,0);
  6462. Emit(Push(position,op.op));
  6463. ReleaseOperand(op);
  6464. property.GetName(name);
  6465. (* does not work when inheritance is used:
  6466. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6467. *)
  6468. PushConstString(name);
  6469. IF (value # NIL) THEN
  6470. ASSERT(
  6471. SemanticChecker.IsStringType(property.type)
  6472. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6473. OR (property.type.resolved IS SyntaxTree.FloatType)
  6474. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6475. OR (property.type.resolved IS SyntaxTree.SetType)
  6476. );
  6477. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6478. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6479. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6480. Designate(d, op);
  6481. IF SemanticChecker.IsStringType(property.type) THEN
  6482. Emit(Push(Basic.invalidPosition, op.tag))
  6483. END;
  6484. Emit(Push(Basic.invalidPosition, op.op));
  6485. ReleaseOperand(op);
  6486. END;
  6487. IF value = NIL THEN
  6488. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6489. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6490. ASSERT(SemanticChecker.IsStringType(value.type));
  6491. Designate(value, op);
  6492. PushString(op, value.type);
  6493. ReleaseOperand(op);
  6494. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6495. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6496. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6497. Evaluate(value, op);
  6498. Emit(Push(property.position, op.op));
  6499. ReleaseOperand(op);
  6500. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6501. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6502. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6503. Evaluate(value, op);
  6504. Emit(Push(property.position, op.op));
  6505. ReleaseOperand(op);
  6506. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6507. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6508. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6509. Evaluate(value, op);
  6510. Emit(Push(property.position, op.op));
  6511. ReleaseOperand(op);
  6512. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6513. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6514. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6515. Evaluate(value, op);
  6516. Emit(Push(property.position, op.op));
  6517. ReleaseOperand(op);
  6518. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6519. ELSE
  6520. HALT(200);
  6521. END;
  6522. END AddProperty;
  6523. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6524. VAR symbol: SyntaxTree.Symbol;
  6525. BEGIN
  6526. WHILE modifier # NIL DO
  6527. symbol := cellType.FindProperty(modifier.identifier);
  6528. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6529. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6530. modifier := modifier.nextModifier;
  6531. END;
  6532. END AddModifiers;
  6533. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6534. VAR last: SyntaxTree.Modifier;
  6535. BEGIN
  6536. IF to = NIL THEN
  6537. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6538. ELSE
  6539. last := to;
  6540. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6541. last := last.nextModifier;
  6542. END;
  6543. IF last.identifier # this.identifier THEN
  6544. ASSERT(last.nextModifier = NIL);
  6545. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6546. END;
  6547. END;
  6548. END AppendModifier;
  6549. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6550. BEGIN
  6551. WHILE this # NIL DO
  6552. AppendModifier(to, this);
  6553. this := this.nextModifier;
  6554. END;
  6555. END AppendModifiers;
  6556. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6557. VAR base: SyntaxTree.Type;
  6558. BEGIN
  6559. AppendModifiers(to, c.modifiers);
  6560. base := c.GetBaseValueType();
  6561. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6562. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6563. END;
  6564. END AppendCellTypeModifiers;
  6565. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6566. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6567. BEGIN
  6568. Basic.GetString(modifier.identifier, name);
  6569. PushConstString(name);
  6570. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6571. ASSERT(SemanticChecker.IsStringType(value.type));
  6572. Designate(value, op);
  6573. PushString(op, value.type);
  6574. ReleaseOperand(op);
  6575. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6576. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6577. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6578. Evaluate(value, op);
  6579. Emit(Push(modifier.position, op.op));
  6580. ReleaseOperand(op);
  6581. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6582. ELSE
  6583. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6584. END;
  6585. END AddPortProperty;
  6586. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6587. BEGIN
  6588. WHILE modifier # NIL DO
  6589. AddPortProperty(modifier, modifier.expression);
  6590. modifier := modifier.nextModifier;
  6591. END;
  6592. END AddPortProperties;
  6593. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6594. VAR op: Operand;
  6595. BEGIN
  6596. Evaluate(p, op);
  6597. Emit(Push(p.position, op.op));
  6598. ReleaseOperand(op);
  6599. IF p IS SyntaxTree.Designator THEN
  6600. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6601. END;
  6602. END PushPort;
  6603. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6604. VAR tmp: IntermediateCode.Operand;
  6605. BEGIN
  6606. actualType := actualType.resolved;
  6607. IF actualType IS SyntaxTree.StringType THEN
  6608. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6609. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6610. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6611. ELSE
  6612. tmp := op.tag;
  6613. IntermediateCode.MakeMemory(tmp,addressType);
  6614. Emit(Push(position,tmp));
  6615. END;
  6616. Emit(Push(position,op.op))
  6617. END PushString;
  6618. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6619. VAR
  6620. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6621. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6622. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6623. tmp:IntermediateCode.Operand;
  6624. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6625. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6626. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6627. dest: IntermediateCode.Operand;
  6628. staticLength: LONGINT; itype: IntermediateCode.Type;
  6629. convert,isTensor: BOOLEAN;
  6630. recordType: SyntaxTree.RecordType;
  6631. baseType: SyntaxTree.Type;
  6632. flags: SET;
  6633. left: SyntaxTree.Expression;
  6634. call: SyntaxTree.Designator;
  6635. procedure: SyntaxTree.Procedure;
  6636. temporaryVariable: SyntaxTree.Variable;
  6637. dummy: IntermediateCode.Operand;
  6638. customBuiltin: SyntaxTree.CustomBuiltin;
  6639. isVarPar: ARRAY 3 OF BOOLEAN;
  6640. callsection: Sections.Section;
  6641. segmentedName: Basic.SegmentedName;
  6642. needsTrace: BOOLEAN;
  6643. n: ARRAY 256 OF CHAR;
  6644. modifier: SyntaxTree.Modifier;
  6645. previous, init: IntermediateCode.Section;
  6646. prevScope: SyntaxTree.Scope;
  6647. firstPar: LONGINT;
  6648. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6649. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6650. priority: IntermediateCode.Operand;
  6651. op,callop: Operand;
  6652. BEGIN
  6653. IF type = NIL THEN RETURN END;
  6654. type := type.resolved;
  6655. IF type IS SyntaxTree.PointerType THEN
  6656. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6657. END;
  6658. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6659. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6660. recordScope := type(SyntaxTree.RecordType).recordScope;
  6661. IF recordScope.bodyProcedure # NIL THEN
  6662. procedure := recordScope.bodyProcedure;
  6663. body := procedure.procedureScope.body;
  6664. Emit(Push(position,self));
  6665. IF body.isActive THEN
  6666. StaticCallOperand(callop,procedure);
  6667. Emit(Push(position,callop.op));
  6668. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6669. Convert(priority,sizeType);
  6670. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6671. END;
  6672. Emit(Push(position,priority));
  6673. ReleaseIntermediateOperand(priority);
  6674. IF backend.cooperative THEN
  6675. Emit(Push(position,self));
  6676. CallThis(position,"Activities","Create",3)
  6677. ELSE
  6678. flags := 0;
  6679. IF body.isSafe THEN
  6680. flags := 1;
  6681. END;
  6682. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6683. Emit(Push(position,self));
  6684. CallThis(position,"Objects","CreateProcess",4)
  6685. END;
  6686. ELSE
  6687. Emit(Push(position,self));
  6688. StaticCallOperand(callop,procedure);
  6689. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6690. END;
  6691. Emit(Pop(position,self));
  6692. END;
  6693. END CallBodies;
  6694. PROCEDURE PushTD(type: SyntaxTree.Type);
  6695. VAR op: IntermediateCode.Operand;
  6696. BEGIN
  6697. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6698. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6699. ELSE
  6700. IF type.resolved IS SyntaxTree.PointerType THEN
  6701. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6702. END;
  6703. op := TypeDescriptorAdr(type.resolved);
  6704. IF ~newObjectFile THEN
  6705. IntermediateCode.MakeMemory(op,addressType);
  6706. END;
  6707. Emit(Push(position,op));
  6708. END
  6709. END PushTD;
  6710. BEGIN
  6711. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6712. dest := destination; destination := emptyOperand;
  6713. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6714. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6715. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6716. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6717. CASE x.id OF
  6718. (* ---- COPY ----- *)
  6719. |Global.Copy:
  6720. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6721. (* ---- EXCL, INCL----- *)
  6722. |Global.Excl,Global.Incl:
  6723. Evaluate(p0,s0);
  6724. Evaluate(p1,s1);
  6725. Convert(s1.op,setType);
  6726. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6727. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6728. END;
  6729. ReuseCopy(res,s0.op);
  6730. ReleaseOperand(s0);
  6731. Reuse1(tmp,s1.op);
  6732. ReleaseOperand(s1);
  6733. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6734. IF x.id = Global.Excl THEN
  6735. Emit(Not(position,tmp,tmp));
  6736. Emit(And(position,res,res,tmp));
  6737. ELSE
  6738. Emit(Or(position,res,res,tmp));
  6739. END;
  6740. ReleaseIntermediateOperand(tmp);
  6741. Designate(p0,s0);
  6742. ToMemory(s0.op,setType,0);
  6743. Emit(Mov(position,s0.op,res));
  6744. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6745. (* ---- DISPOSE ----- *)
  6746. |Global.Dispose:
  6747. Designate(p0,s0);
  6748. Emit(Push(position,s0.op));
  6749. ReleaseOperand(s0);
  6750. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6751. (* ---- GETPROCEDURE ----- *)
  6752. |Global.GetProcedure:
  6753. Designate(p0,s0);
  6754. PushString(s0,p0.type);
  6755. Designate(p1,s1);
  6756. PushString(s1,p1.type);
  6757. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6758. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6759. ELSE PushTD(procedureType.firstParameter.type)
  6760. END;
  6761. PushTD(procedureType.returnType);
  6762. Designate(p2,s2);
  6763. Emit(Push(position,s2.op));
  6764. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6765. CallThis(position,"Modules","GetProcedure", 7);
  6766. (* ---- ASH, LSH, ROT ----- *)
  6767. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6768. Evaluate(p0,s0);
  6769. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6770. (* make unsigned arguments in order to produced a logical shift *)
  6771. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6772. convert:= TRUE;
  6773. itype := s0.op.type;
  6774. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6775. Convert(s0.op,itype);
  6776. ELSE
  6777. convert := FALSE;
  6778. END;
  6779. END;
  6780. Evaluate(p1,s1);
  6781. IF IsIntegerConstant(p1,hint) THEN
  6782. ReuseCopy(reg,s0.op);
  6783. IF hint > 0 THEN
  6784. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6785. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6786. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6787. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6788. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6789. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6790. END;
  6791. ELSIF hint < 0 THEN
  6792. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6793. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6794. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6795. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6796. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6797. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6798. END;
  6799. END;
  6800. ReleaseOperand(s0); ReleaseOperand(s1);
  6801. ELSE
  6802. exit := NewLabel();
  6803. end := NewLabel();
  6804. ReuseCopy(reg,s0.op);
  6805. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6806. Reuse1(tmp,s1.op);
  6807. Emit(Neg(position,tmp,s1.op));
  6808. Convert(tmp,s1.op.type);
  6809. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6810. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6811. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6812. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6813. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6814. END;
  6815. ReleaseIntermediateOperand(tmp);
  6816. BrL(end);
  6817. SetLabel(exit);
  6818. ReuseCopy(tmp,s1.op);
  6819. Convert(tmp,s1.op.type);
  6820. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6821. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6822. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6823. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6824. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6825. END;
  6826. ReleaseIntermediateOperand(tmp);
  6827. SetLabel(end);
  6828. ReleaseOperand(s0); ReleaseOperand(s1);
  6829. END;
  6830. InitOperand(result,ModeValue);
  6831. IF convert THEN
  6832. itype := reg.type;
  6833. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6834. Convert(reg,itype);
  6835. END;
  6836. result.op := reg;
  6837. (* ---- CAP ----- *)
  6838. |Global.Cap:
  6839. Evaluate(p0,result);
  6840. ReuseCopy(reg,result.op);
  6841. ReleaseIntermediateOperand(result.op);
  6842. ignore := NewLabel();
  6843. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6844. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6845. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6846. SetLabel(ignore);
  6847. result.op := reg;
  6848. (* ---- CHR ----- *)
  6849. |Global.Chr, Global.Chr32:
  6850. Evaluate(p0,result);
  6851. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6852. |Global.Entier, Global.EntierH:
  6853. Evaluate(p0,result);
  6854. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6855. (* ---- MIN and MAX ----- *)
  6856. |Global.Max,Global.Min:
  6857. Evaluate(p0,s0);
  6858. Evaluate(p1,s1);
  6859. Reuse2(res,s0.op,s1.op);
  6860. else := NewLabel();
  6861. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6862. ELSE BrltL(else,s1.op,s0.op) END;
  6863. Emit(Mov(position,res,s0.op));
  6864. ReleaseOperand(s0);
  6865. end := NewLabel();
  6866. BrL(end);
  6867. SetLabel(else);
  6868. Emit(MovReplace(position,res,s1.op));
  6869. SetLabel(end);
  6870. ReleaseOperand(s1);
  6871. InitOperand(result,ModeValue);
  6872. result.op := res;
  6873. (* ---- ODD ----- *)
  6874. |Global.Odd:
  6875. IF ~conditional THEN
  6876. ConditionToValue(x)
  6877. ELSE
  6878. Evaluate(p0,result);
  6879. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6880. Reuse1(res,result.op);
  6881. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6882. ReleaseIntermediateOperand(result.op);
  6883. result.op := res;
  6884. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6885. ReleaseOperand(result);
  6886. BrL(falseLabel);
  6887. END;
  6888. (* ---- ORD ----- *)
  6889. |Global.Ord, Global.Ord32:
  6890. Evaluate(p0,result);
  6891. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6892. (* ---- SHORT, LONG ----- *)
  6893. |Global.Short, Global.Long:
  6894. Evaluate(p0,result);
  6895. IF x.type IS SyntaxTree.ComplexType THEN
  6896. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6897. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6898. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6899. ELSE
  6900. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6901. END
  6902. (* ---- HALT, SYSTEM.HALT----- *)
  6903. |Global.Halt, Global.systemHalt:
  6904. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6905. EmitTrap (position, val);
  6906. (* ---- ASSERT ----- *)
  6907. |Global.Assert:
  6908. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6909. trueL := NewLabel();
  6910. falseL := NewLabel();
  6911. Condition(p0,trueL,falseL);
  6912. IF p1 = NIL THEN val := AssertTrap
  6913. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6914. END;
  6915. SetLabel(falseL);
  6916. EmitTrap(position,val);
  6917. SetLabel(trueL);
  6918. END;
  6919. (*
  6920. Emit(TrapC(result.op,val);
  6921. *)
  6922. (* ---- INC, DEC----- *)
  6923. |Global.Inc,Global.Dec:
  6924. Expression(p0); adr := result.op;
  6925. LoadValue(result,p0.type); l := result;
  6926. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6927. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6928. END;
  6929. IF x.id = Global.Inc THEN
  6930. Emit(Add(position,l.op,l.op,r.op));
  6931. ELSE
  6932. Emit(Sub(position,l.op,l.op,r.op));
  6933. END;
  6934. ReleaseOperand(l); ReleaseOperand(r);
  6935. (* ---- LEN ----- *)
  6936. |Global.Len: (* dynamic length, static length done by checker *)
  6937. Designate(p0,operand);
  6938. IF p1 = NIL THEN
  6939. InitOperand(l,ModeValue);
  6940. l.op := IntermediateCode.Immediate(int32,0);
  6941. ELSE
  6942. Evaluate(p1,l);
  6943. END;
  6944. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6945. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6946. Dereference(operand, p0.type.resolved, FALSE);
  6947. END;
  6948. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6949. ReleaseOperand(operand); ReleaseOperand(l);
  6950. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6951. ASSERT(p1 # NIL);
  6952. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6953. Dereference(operand,p0.type.resolved,FALSE);
  6954. END;
  6955. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6956. ReleaseOperand(operand); ReleaseOperand(l);
  6957. ELSE HALT(100);
  6958. END;
  6959. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6960. (* ---- FIRST ---- *)
  6961. |Global.First:
  6962. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6963. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6964. ELSE
  6965. Designate(p0, result)
  6966. END
  6967. (* ---- LAST ---- *)
  6968. |Global.Last:
  6969. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6970. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6971. ELSE
  6972. Designate(p0, result);
  6973. (* make sure result.op is a register *)
  6974. tmp := result.op;
  6975. ReuseCopy(result.op, result.op);
  6976. ReleaseIntermediateOperand(tmp);
  6977. (* add offset to result.op *)
  6978. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6979. END
  6980. (* ---- STEP ---- *)
  6981. |Global.Step:
  6982. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6983. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6984. ELSE
  6985. Designate(p0, result);
  6986. (* make sure result.op is a register *)
  6987. tmp := result.op;
  6988. ReuseCopy(result.op, result.op);
  6989. ReleaseIntermediateOperand(tmp);
  6990. (* add offset to result.op *)
  6991. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6992. END
  6993. (* ---- RE ---- *)
  6994. |Global.Re:
  6995. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6996. Designate(p0, result)
  6997. ELSE
  6998. Evaluate(p0, result)
  6999. END
  7000. (* ---- IM ---- *)
  7001. |Global.Im:
  7002. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  7003. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  7004. Designate(p0, result);
  7005. (* make sure result.op is a register *)
  7006. tmp := result.op;
  7007. ReuseCopy(result.op, result.op);
  7008. ReleaseIntermediateOperand(tmp);
  7009. (* add offset to result.op *)
  7010. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7011. (* ---- ABS ----- *)
  7012. |Global.Abs:
  7013. Evaluate(p0,operand);
  7014. type := p0.type.resolved;
  7015. InitOperand(result,ModeValue);
  7016. Reuse1a(result.op,operand.op,dest);
  7017. Emit(Abs(position,result.op,operand.op));
  7018. ReleaseOperand(operand);
  7019. (* ---- WAIT ----- *)
  7020. |Global.Wait:
  7021. Evaluate(p0,operand);
  7022. Emit(Push(position,operand.op));
  7023. ReleaseOperand(operand);
  7024. CallThis(position,"Activities","Wait", 1);
  7025. (* ---- NEW ----- *)
  7026. |Global.New:
  7027. (*! the following code is only correct for "standard" Oberon calling convention *)
  7028. IF x.type # NIL THEN
  7029. type := x.type.resolved;
  7030. firstPar := 0;
  7031. ELSE
  7032. type := p0.type.resolved;
  7033. firstPar := 1;
  7034. END;
  7035. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7036. THEN
  7037. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7038. IF backend.cooperative THEN
  7039. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7040. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7041. IF recordType.isObject THEN
  7042. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7043. IF recordType.IsActive() THEN
  7044. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7045. END;
  7046. IF recordType.IsProtected() THEN
  7047. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7048. END;
  7049. ELSE
  7050. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7051. END;
  7052. END;
  7053. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7054. CallThis(position,"Runtime","New", 1);
  7055. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7056. Emit(Result(position, pointer));
  7057. exit := NewLabel();
  7058. BreqL(exit,pointer,nil);
  7059. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7060. GetRecordTypeName (recordType,name);
  7061. IF ~recordType.isObject THEN
  7062. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7063. END;
  7064. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7065. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7066. IF recordType.isObject THEN
  7067. IF recordType.IsProtected() THEN
  7068. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7069. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7070. END;
  7071. IF recordType.IsActive() THEN
  7072. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7073. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7074. END;
  7075. END;
  7076. END;
  7077. (* initialize fields *)
  7078. IF type(SyntaxTree.PointerType).isPlain THEN
  7079. size := 0;
  7080. ELSIF recordType.isObject THEN
  7081. size := BaseObjectTypeSize;
  7082. ELSE
  7083. size := BaseRecordTypeSize;
  7084. END;
  7085. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7086. (* call initializer *)
  7087. constructor := GetConstructor(recordType);
  7088. IF constructor # NIL THEN
  7089. (*! should be unified with ProcedureCallDesignator *)
  7090. Emit(Push(position,pointer));
  7091. ReleaseIntermediateOperand(pointer);
  7092. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7093. FOR i := firstPar TO x.parameters.Length()-1 DO
  7094. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7095. formalParameter := formalParameter.nextParameter;
  7096. END;
  7097. (* static call of the constructor *)
  7098. GetCodeSectionNameForSymbol(constructor,name);
  7099. ASSERT(~constructor.isInline);
  7100. IF constructor.scope.ownerModule # module.module THEN
  7101. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7102. ELSE
  7103. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7104. END;
  7105. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7106. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7107. Emit(Pop(position,pointer));
  7108. END;
  7109. (* call bodies *)
  7110. CallBodies(pointer,type);
  7111. SetLabel(exit);
  7112. needsTrace := p0.NeedsTrace();
  7113. IF needsTrace THEN ModifyAssignments(true) END;
  7114. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7115. Emit(Push(position, pointer));
  7116. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7117. Emit(Pop(position, pointer));
  7118. END;
  7119. Designate(p0,l);
  7120. IF needsTrace THEN
  7121. CallAssignPointer(l.op, pointer);
  7122. ELSE
  7123. ToMemory(l.op,addressType,0);
  7124. Emit(Mov(position,l.op,pointer));
  7125. END;
  7126. ReleaseIntermediateOperand(pointer);
  7127. ReleaseOperand(l);
  7128. IF needsTrace THEN ModifyAssignments(false) END;
  7129. ELSE (* not cooperative backend *)
  7130. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7131. IF temporaryVariable # NIL THEN
  7132. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7133. ELSE
  7134. Designate(p0,l);
  7135. END;
  7136. (* l.op contains address of pointer to record *)
  7137. Emit(Push(position,l.op)); (* address for use after syscall *)
  7138. Emit(Push(position,l.op));
  7139. ReleaseOperand(l);
  7140. (* push type descriptor *)
  7141. reg := TypeDescriptorAdr(recordType);
  7142. IF ~newObjectFile THEN
  7143. IntermediateCode.MakeMemory(reg,addressType);
  7144. END;
  7145. Emit(Push(position,reg));
  7146. ReleaseIntermediateOperand(reg);
  7147. (* push realtime flag *)
  7148. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7149. ELSE Emit(Push(position,false));
  7150. END;
  7151. CallThis(position,"Heaps","NewRec", 3);
  7152. (* check allocation success, if not successful then do not call initializers and bodies *)
  7153. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7154. Emit(Pop(position,pointer));
  7155. MakeMemory(reg,pointer,addressType,0);
  7156. ReleaseIntermediateOperand(pointer);
  7157. pointer := reg;
  7158. exit := NewLabel();
  7159. BreqL(exit,pointer,nil);
  7160. Emit(Push(position,pointer));
  7161. (* initialize fields *)
  7162. InitFields(recordType, pointer,0);
  7163. (* call initializer *)
  7164. constructor := GetConstructor(recordType);
  7165. IF constructor # NIL THEN
  7166. (*! should be unified with ProcedureCallDesignator *)
  7167. Emit(Push(position,pointer));
  7168. ReleaseIntermediateOperand(pointer);
  7169. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7170. FOR i := firstPar TO x.parameters.Length()-1 DO
  7171. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7172. formalParameter := formalParameter.nextParameter;
  7173. END;
  7174. (* static call of the constructor *)
  7175. GetCodeSectionNameForSymbol(constructor,name);
  7176. ASSERT(~constructor.isInline);
  7177. IF constructor.scope.ownerModule # module.module THEN
  7178. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7179. ELSE
  7180. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7181. END;
  7182. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7183. ELSE
  7184. ReleaseIntermediateOperand(pointer);
  7185. END;
  7186. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7187. Emit(Pop(position,pointer));
  7188. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7189. Designate(p0,l);
  7190. ToMemory(l.op,addressType,0);
  7191. Emit(Mov(position,l.op,pointer));
  7192. ReleaseOperand(l);
  7193. result.tag := emptyOperand;
  7194. ELSIF (x.type # NIL) THEN
  7195. result := l; (* temporary variable is the result of NEW Type() *)
  7196. END;
  7197. (* call bodies *)
  7198. CallBodies(pointer,type);
  7199. ReleaseIntermediateOperand(pointer);
  7200. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7201. end := NewLabel();
  7202. BrL(end);
  7203. SetLabel(exit);
  7204. Designate(p0,l);
  7205. ToMemory(l.op,addressType,0);
  7206. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7207. ReleaseOperand(l);
  7208. SetLabel(end);
  7209. ELSE
  7210. SetLabel(exit);
  7211. END;
  7212. END;
  7213. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7214. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7215. dim := 0;
  7216. IntermediateCode.InitOperand(reg);
  7217. IF p1 # NIL THEN
  7218. FOR i := firstPar TO x.parameters.Length()-1 DO
  7219. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7220. parameter := x.parameters.GetExpression(i);
  7221. Evaluate(parameter,r);
  7222. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7223. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7224. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7225. END;
  7226. Emit(Push(position,r.op));
  7227. IF i=1 THEN
  7228. CopyInt(reg,r.op);
  7229. ELSE
  7230. MulInt(reg, reg, r.op);
  7231. END;
  7232. ReleaseOperand(r);
  7233. INC(dim);
  7234. END;
  7235. Convert(reg,addressType);
  7236. ELSE
  7237. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7238. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7239. END;
  7240. openDim := dim;
  7241. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7242. IF backend.cooperative THEN
  7243. size := ToMemoryUnits(system,system.SizeOf(type));
  7244. WHILE type IS SyntaxTree.ArrayType DO
  7245. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7246. END;
  7247. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7248. IF (size # 1) THEN
  7249. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7250. END;
  7251. Emit(Push(position,reg));
  7252. size := ToMemoryUnits(system,system.SizeOf(type));
  7253. IF (size # 1) THEN
  7254. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7255. END;
  7256. AddInt(reg, reg, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize)));
  7257. (*Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));*)
  7258. Emit(Push(position,reg));
  7259. ReleaseIntermediateOperand(reg);
  7260. CallThis(position,"Runtime","New", 1);
  7261. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7262. Emit(Result(position, pointer));
  7263. exit := NewLabel();
  7264. else := NewLabel();
  7265. BreqL(else,pointer,nil);
  7266. IF ~type.hasPointers THEN
  7267. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7268. ELSIF type IS SyntaxTree.RecordType THEN
  7269. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7270. ELSIF type IS SyntaxTree.ProcedureType THEN
  7271. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7272. ELSE
  7273. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7274. END;
  7275. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7276. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7277. 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))));
  7278. IF type IS SyntaxTree.RecordType THEN
  7279. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7280. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7281. ELSE
  7282. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7283. END;
  7284. i := openDim;
  7285. WHILE i > 0 DO
  7286. DEC (i);
  7287. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7288. END;
  7289. needsTrace := p0.NeedsTrace();
  7290. IF needsTrace THEN ModifyAssignments(true) END;
  7291. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7292. Emit(Push(position, pointer));
  7293. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7294. Emit(Pop(position, pointer));
  7295. END;
  7296. Designate(p0,l);
  7297. IF needsTrace THEN
  7298. CallAssignPointer(l.op, pointer);
  7299. ModifyAssignments(false);
  7300. ELSE
  7301. ToMemory(l.op,addressType,0);
  7302. Emit(Mov(position,l.op,pointer));
  7303. END;
  7304. ReleaseIntermediateOperand(pointer);
  7305. ReleaseOperand(l);
  7306. BrL(exit);
  7307. SetLabel(else);
  7308. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7309. Designate(p0,l);
  7310. IF needsTrace THEN
  7311. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7312. ELSE
  7313. ToMemory(l.op,addressType,0);
  7314. Emit(Mov(position,l.op,pointer));
  7315. END;
  7316. ReleaseOperand(l);
  7317. SetLabel(exit);
  7318. ELSE
  7319. (*! the following code is only correct for "standard" Oberon calling convention *)
  7320. IF SemanticChecker.ContainsPointer(type) THEN
  7321. IF type IS SyntaxTree.ArrayType THEN
  7322. staticLength := 1;
  7323. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7324. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7325. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7326. END;
  7327. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7328. MulInt(reg,reg,tmp);
  7329. END;
  7330. Designate(p0,l);
  7331. IF openDim > 0 THEN
  7332. Emit(Push(position,l.op)); (* address for use after syscall *)
  7333. END;
  7334. Emit(Push(position,l.op)); (* address *)
  7335. ReleaseOperand(l);
  7336. tmp := TypeDescriptorAdr(type);
  7337. IF ~newObjectFile THEN
  7338. IntermediateCode.MakeMemory(tmp,addressType);
  7339. END;
  7340. Emit(Push(position,tmp)); (* type descriptor *)
  7341. ReleaseIntermediateOperand(tmp);
  7342. Emit(Push(position,reg)); (* number Elements *)
  7343. ReleaseIntermediateOperand(reg);
  7344. tmp := IntermediateCode.Immediate(addressType,dim);
  7345. Emit(Push(position,tmp)); (* dimensions *)
  7346. (* push realtime flag *)
  7347. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7348. ELSE Emit(Push(position,false));
  7349. END;
  7350. CallThis(position,"Heaps","NewArr",5)
  7351. ELSE
  7352. size := ToMemoryUnits(system,system.SizeOf(type));
  7353. IF (size # 1) THEN
  7354. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7355. (*
  7356. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7357. *)
  7358. END;
  7359. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7360. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7361. AddInt(reg, reg, tmp);
  7362. (*
  7363. Emit(Add(position,reg,reg,tmp));
  7364. *)
  7365. Designate(p0,l);
  7366. IF openDim >0 THEN
  7367. Emit(Push(position,l.op)); (* address for use after syscall *)
  7368. END;
  7369. Emit(Push(position,l.op)); (* address for syscall *)
  7370. ReleaseOperand(l); (* pointer address *)
  7371. Emit(Push(position,reg)); (* size *)
  7372. ReleaseIntermediateOperand(reg);
  7373. (* push realtime flag *)
  7374. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7375. ELSE Emit(Push(position,false));
  7376. END;
  7377. CallThis(position,"Heaps","NewSys", 3)
  7378. END;
  7379. IF openDim > 0 THEN
  7380. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7381. Emit(Pop(position,adr));
  7382. ToMemory(adr,addressType,0);
  7383. ReuseCopy(tmp,adr);
  7384. ReleaseIntermediateOperand(adr);
  7385. adr := tmp;
  7386. else := NewLabel();
  7387. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7388. i := openDim-1;
  7389. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7390. WHILE (i >= 0) DO
  7391. Emit(Pop(position,reg));
  7392. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7393. Emit(Mov(position,res,reg));
  7394. DEC(i);
  7395. END;
  7396. ReleaseIntermediateOperand(adr);
  7397. ReleaseIntermediateOperand(reg);
  7398. exit := NewLabel();
  7399. BrL(exit);
  7400. SetLabel(else);
  7401. (* else part: array could not be allocated *)
  7402. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7403. Emit(Add(position,sp,sp,tmp));
  7404. SetLabel(exit);
  7405. END;
  7406. END;
  7407. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7408. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7409. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7410. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7411. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7412. left.SetType(procedure.type);
  7413. formalParameter := procedureType.firstParameter;
  7414. (* push array to allocate *)
  7415. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7416. formalParameter :=formalParameter.nextParameter;
  7417. (* push length array *)
  7418. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7419. (* push size *)
  7420. type := t0;
  7421. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7422. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7423. END;
  7424. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7425. Emit(Push(position,tmp));
  7426. (* *)
  7427. IF SemanticChecker.ContainsPointer(type) THEN
  7428. tmp := TypeDescriptorAdr(type);
  7429. IF ~newObjectFile THEN
  7430. IntermediateCode.MakeMemory(tmp,addressType);
  7431. END;
  7432. ELSE
  7433. tmp := IntermediateCode.Immediate(addressType, 0);
  7434. END;
  7435. Emit(Push(position,tmp)); (* type descriptor *)
  7436. StaticCallOperand(result,procedure);
  7437. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7438. ReleaseOperand(result);
  7439. END;
  7440. (*
  7441. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7442. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7443. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7444. *)
  7445. ELSE
  7446. dim := 0;
  7447. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7448. (* generate geometry descriptor *)
  7449. Designate(p0,l);
  7450. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7451. ReleaseOperand(l);
  7452. isTensor := TRUE;
  7453. ELSE
  7454. isTensor := FALSE;
  7455. END;
  7456. FOR i := firstPar TO x.parameters.Length()-1 DO
  7457. IF ~isTensor THEN
  7458. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7459. END;
  7460. parameter := x.parameters.GetExpression(i);
  7461. Evaluate(parameter,r);
  7462. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7463. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7464. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7465. END;
  7466. Emit(Push(position,r.op));
  7467. IF i=1 THEN
  7468. CopyInt(reg, r.op);
  7469. ELSE
  7470. MulInt(reg, reg, r.op);
  7471. END;
  7472. ReleaseOperand(r);
  7473. INC(dim);
  7474. END;
  7475. Convert(reg,addressType);
  7476. openDim := dim;
  7477. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7478. (*! the following code is only correct for "standard" Oberon calling convention *)
  7479. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7480. t := type;
  7481. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7482. staticLength := 1;
  7483. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7484. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7485. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7486. END;
  7487. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7488. MulInt(reg,reg,tmp);
  7489. END;
  7490. Designate(p0,l);
  7491. IF isTensor THEN
  7492. Dereference(l,type,FALSE);
  7493. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7494. END;
  7495. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7496. Emit(Push(position,l.tag)); (* address *)
  7497. ReleaseOperand(l);
  7498. tmp := TypeDescriptorAdr(t);
  7499. IF ~newObjectFile THEN
  7500. IntermediateCode.MakeMemory(tmp,addressType);
  7501. END;
  7502. Emit(Push(position,tmp)); (* type descriptor *)
  7503. ReleaseIntermediateOperand(tmp);
  7504. Emit(Push(position,reg)); (* number Elements *)
  7505. ReleaseIntermediateOperand(reg);
  7506. tmp := IntermediateCode.Immediate(addressType,0);
  7507. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7508. (* push realtime flag: false by default *)
  7509. Emit(Push(position,false));
  7510. CallThis(position,"Heaps","NewArr",5);
  7511. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7512. Emit(Pop(position,adr));
  7513. GetMathArrayField(tmp,adr,MathPtrOffset);
  7514. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7515. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7516. PutMathArrayField(adr,reg,MathAdrOffset);
  7517. ReleaseIntermediateOperand(tmp);
  7518. ReleaseIntermediateOperand(reg);
  7519. ELSE
  7520. IF isTensor THEN
  7521. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7522. ELSE
  7523. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7524. END;
  7525. IF (size # 1) THEN
  7526. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7527. END;
  7528. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7529. AddInt(reg,reg,tmp);
  7530. Designate(p0,l);
  7531. IF isTensor THEN
  7532. Dereference(l,type,FALSE);
  7533. END;
  7534. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7535. Emit(Push(position,l.tag)); (* address for syscall *)
  7536. ReleaseOperand(l); (* pointer address *)
  7537. Emit(Push(position,reg)); (* size *)
  7538. ReleaseIntermediateOperand(reg);
  7539. (* push realtime flag: false by default *)
  7540. Emit(Push(position,false));
  7541. CallThis(position,"Heaps","NewSys",3);
  7542. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7543. Emit(Pop(position,adr));
  7544. GetMathArrayField(tmp,adr,MathPtrOffset);
  7545. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7546. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7547. PutMathArrayField(adr,reg,MathAdrOffset);
  7548. ReleaseIntermediateOperand(tmp);
  7549. ReleaseIntermediateOperand(reg);
  7550. END;
  7551. flags := {};
  7552. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7553. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7554. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7555. PutMathArrayField(adr,tmp,MathDimOffset);
  7556. else := NewLabel();
  7557. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7558. i := openDim-1;
  7559. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7560. IF isTensor THEN
  7561. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7562. ELSE
  7563. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7564. END;
  7565. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7566. WHILE (i >= 0) DO
  7567. Emit(Pop(position,reg));
  7568. PutMathArrayLength(adr,reg,i);
  7569. PutMathArrayIncrement(adr,tmp,i);
  7570. IF i > 0 THEN
  7571. IF i=openDim-1 THEN
  7572. CopyInt(tmp,tmp);
  7573. END;
  7574. MulInt(tmp,tmp,reg);
  7575. END;
  7576. DEC(i);
  7577. END;
  7578. ReleaseIntermediateOperand(adr);
  7579. ReleaseIntermediateOperand(reg);
  7580. ReleaseIntermediateOperand(tmp);
  7581. exit := NewLabel();
  7582. BrL(exit);
  7583. SetLabel(else);
  7584. (* else part: array could not be allocated *)
  7585. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7586. Emit(Add(position,sp,sp,tmp));
  7587. SetLabel(exit);
  7588. END;
  7589. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7590. THEN
  7591. IF ~backend.cellsAreObjects THEN RETURN END;
  7592. IF InCellScope(currentScope) THEN
  7593. PushSelfPointer()
  7594. ELSE
  7595. Emit(Push(position, nil));
  7596. END;
  7597. (* push temp address *)
  7598. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7599. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7600. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7601. (* l.op contains address of pointer to record *)
  7602. Emit(Push(position,l.op)); (* address for use after syscall *)
  7603. ReleaseOperand(l);
  7604. (* push type descriptor *)
  7605. reg := TypeDescriptorAdr(baseType);
  7606. IF ~newObjectFile THEN
  7607. IntermediateCode.MakeMemory(reg,addressType);
  7608. END;
  7609. Emit(Push(position,reg));
  7610. ReleaseIntermediateOperand(reg);
  7611. (* push name *)
  7612. (*Global.GetSymbolName(p0, n);*)
  7613. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7614. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7615. ELSE
  7616. Global.GetModuleName(module.module, n);
  7617. END;
  7618. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7619. (*type.typeDeclaration.GetName(n);*)
  7620. PushConstString(n);
  7621. (* push cellnet boolean *)
  7622. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7623. (* push engine boolean *)
  7624. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7625. (* allocate *)
  7626. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7627. (* add capabilities *)
  7628. modifier := p0(SyntaxTree.Designator).modifiers;
  7629. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7630. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7631. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7632. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7633. END;
  7634. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7635. (*
  7636. modifier := baseType(SyntaxTree.CellType).modifiers;
  7637. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7638. modifier := p0(SyntaxTree.Designator).modifiers;
  7639. *)
  7640. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7641. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7642. (* l.op contains address of pointer to record *)
  7643. ToMemory(l.op,addressType,0);
  7644. (* l.op contains value of pointer to record *)
  7645. Emit(Push(position,l.op)); (* address for use after syscall *)
  7646. ReleaseOperand(l);
  7647. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7648. prevScope := currentScope;
  7649. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7650. previous := section;
  7651. section := init;
  7652. (* add ports *)
  7653. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7654. CloseInitializer(previous);
  7655. currentScope := prevScope;
  7656. Symbol(temporaryVariable,l);
  7657. ToMemory(l.op,addressType,0);
  7658. Emit(Push(position,l.op));
  7659. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7660. (*
  7661. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7662. IF constructor # NIL THEN
  7663. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7664. FOR i := 1 TO x.parameters.Length()-1 DO
  7665. p := x.parameters.GetExpression(i);
  7666. Global.GetSymbolName(parameter,name);
  7667. Evaluate(p, value);
  7668. ASSERT(value.type # NIL);
  7669. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7670. par := instance.AddParameter(name);
  7671. par.SetInteger(value.integer);
  7672. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7673. par := instance.AddParameter(name);
  7674. par.SetBoolean(value.boolean);
  7675. ELSE Error(x.position,NotYetImplemented)
  7676. END;
  7677. parameter := parameter.nextParameter
  7678. END;
  7679. END;
  7680. *)
  7681. (* call initializer *)
  7682. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7683. IF constructor # NIL THEN
  7684. (*! should be unified with ProcedureCallDesignator *)
  7685. IF backend.cellsAreObjects THEN
  7686. Symbol(temporaryVariable,l);
  7687. ToMemory(l.op,addressType,0);
  7688. Emit(Push(position,l.op));
  7689. ReleaseOperand(l);
  7690. END;
  7691. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7692. FOR i := firstPar TO x.parameters.Length()-1 DO
  7693. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7694. formalParameter := formalParameter.nextParameter;
  7695. END;
  7696. (* static call of the constructor *)
  7697. Global.GetSymbolSegmentedName(constructor,name);
  7698. ASSERT(~constructor.isInline);
  7699. IF constructor.scope.ownerModule # module.module THEN
  7700. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7701. ELSE
  7702. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7703. END;
  7704. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7705. (*ELSE
  7706. ReleaseIntermediateOperand(pointer);*)
  7707. END;
  7708. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7709. ToMemory(l.op, addressType, 0);
  7710. Designate(p0,s0);
  7711. ToMemory(s0.op,addressType,0);
  7712. Emit(Mov(position,s0.op,l.op));
  7713. ReleaseOperand(l);
  7714. ReleaseOperand(s0);
  7715. result.tag := emptyOperand;
  7716. (* start *)
  7717. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7718. (* push cell *)
  7719. Symbol(temporaryVariable, l);
  7720. ToMemory(l.op,addressType,0);
  7721. Emit(Push(Basic.invalidPosition,l.op));
  7722. (* push delegate *)
  7723. Emit(Push(Basic.invalidPosition,l.op));
  7724. ReleaseOperand(l);
  7725. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7726. Emit(Push(position, s1.op));
  7727. ReleaseOperand(s1);
  7728. CallThis(position,"ActiveCellsRuntime","Start",3);
  7729. END;
  7730. (*IF temporaryVariable # NIL THEN
  7731. end := NewLabel();
  7732. BrL(end);
  7733. SetLabel(exit);
  7734. Designate(p0,l);
  7735. ToMemory(l.op,addressType,0);
  7736. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7737. ReleaseOperand(l);
  7738. SetLabel(end);
  7739. ELSE
  7740. SetLabel(exit);
  7741. END;
  7742. *)
  7743. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7744. ELSE (* no pointer to record, no pointer to array *)
  7745. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7746. (* ignore new statement *)
  7747. Warning(p0.position, "cannot run on final hardware");
  7748. ELSE
  7749. HALT(200);
  7750. END;
  7751. END;
  7752. (* ---- ADDRESSOF----- *)
  7753. |Global.systemAdr:
  7754. Designate(p0,s0);
  7755. s0.mode := ModeValue;
  7756. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7757. ReleaseIntermediateOperand(s0.op);
  7758. s0.op := s0.tag;
  7759. IntermediateCode.InitOperand(s0.tag);
  7760. END;
  7761. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7762. result := s0;
  7763. (* ---- BIT ----- *)
  7764. |Global.systemBit:
  7765. Evaluate(p0,s0);
  7766. ToMemory(s0.op,addressType,0);
  7767. ReuseCopy(res,s0.op);
  7768. ReleaseOperand(s0);
  7769. Evaluate(p1,s1);
  7770. Emit(Ror(position,res,res,s1.op));
  7771. ReleaseOperand(s1);
  7772. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7773. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7774. IF ~conditional THEN
  7775. InitOperand(result,ModeValue); result.op := res;
  7776. ELSE
  7777. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7778. BrL(falseLabel);
  7779. ReleaseIntermediateOperand(res);
  7780. END;
  7781. (* --- MSK ----*)
  7782. |Global.systemMsk:
  7783. Evaluate(p0,s0);
  7784. Evaluate(p1,s1);
  7785. ReuseCopy(res,s0.op);
  7786. ReleaseOperand(s0);
  7787. Emit(And(position,res,res,s1.op));
  7788. ReleaseOperand(s1);
  7789. InitOperand(result,ModeValue);
  7790. result.op := res;
  7791. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7792. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7793. Evaluate(p0,s0);
  7794. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7795. ReleaseOperand(s0);
  7796. InitOperand(result,ModeValue);
  7797. result.op := res;
  7798. (* ---- SYSTEM.GetStackPointer ----- *)
  7799. |Global.systemGetStackPointer:
  7800. InitOperand(result,ModeValue);
  7801. result.op := sp;
  7802. (* ---- SYSTEM.GetFramePointer ----- *)
  7803. |Global.systemGetFramePointer:
  7804. InitOperand(result,ModeValue);
  7805. result.op := fp;
  7806. (* ---- SYSTEM.GetActivity ----- *)
  7807. |Global.systemGetActivity:
  7808. ASSERT(backend.cooperative);
  7809. InitOperand(result,ModeValue);
  7810. result.op := ap;
  7811. (* ---- SYSTEM.SetStackPointer ----- *)
  7812. |Global.systemSetStackPointer:
  7813. Evaluate(p0,s0); (* *)
  7814. Emit(Mov(position,sp,s0.op));
  7815. ReleaseOperand(s0);
  7816. (* ---- SYSTEM.SetFramePointer ----- *)
  7817. |Global.systemSetFramePointer:
  7818. Evaluate(p0,s0); (* *)
  7819. Emit(Mov(position,fp,s0.op));
  7820. ReleaseOperand(s0);
  7821. (* ---- SYSTEM.Activity ----- *)
  7822. |Global.systemSetActivity:
  7823. ASSERT(backend.cooperative);
  7824. Evaluate(p0,s0); (* *)
  7825. Emit(Mov(position,ap,s0.op));
  7826. ReleaseOperand(s0);
  7827. (* ---- SYSTEM.VAL ----- *)
  7828. |Global.systemVal:
  7829. Expression(p1);
  7830. s1 := result;
  7831. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7832. IF s1.mode = ModeReference THEN
  7833. (* nothing to be done if not record type, just take over new type *)
  7834. IF (type IS SyntaxTree.RecordType) THEN
  7835. ReleaseIntermediateOperand(s1.tag);
  7836. s1.tag := TypeDescriptorAdr(type);
  7837. IF ~newObjectFile THEN
  7838. IntermediateCode.MakeMemory(s1.tag,addressType);
  7839. END;
  7840. UseIntermediateOperand(s1.tag);
  7841. END;
  7842. result := s1;
  7843. ELSE (* copy over result to different type, may not use convert *)
  7844. itype := IntermediateCode.GetType(system,type);
  7845. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7846. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7847. Emit(Mov(position,s0.op,s1.op));
  7848. ReleaseOperand(s1);
  7849. InitOperand(result,ModeValue);
  7850. result.op := s0.op;
  7851. ELSE (* different size, must convert *)
  7852. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7853. Convert(s1.op, IntermediateCode.GetType(system,type));
  7854. result := s1;
  7855. END;
  7856. END;
  7857. (* ---- SYSTEM.GET ----- *)
  7858. |Global.systemGet:
  7859. Evaluate(p0,s0); (* adr *)
  7860. Designate(p1,s1); (* variable *)
  7861. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7862. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7863. Emit(Mov(position,s1.op,s0.op));
  7864. ReleaseOperand(s1);
  7865. ReleaseOperand(s0);
  7866. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7867. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7868. Evaluate(p0,s0); (* *)
  7869. Evaluate(p1,s1); (* variable *)
  7870. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7871. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7872. (* real part *)
  7873. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7874. Emit(Mov(position,res, s1.op));
  7875. ReleaseIntermediateOperand(res);
  7876. (* imaginary part *)
  7877. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7878. Emit(Mov(position,res, s1.tag));
  7879. ReleaseIntermediateOperand(res);
  7880. ReleaseOperand(s1);
  7881. ReleaseOperand(s0);
  7882. ELSE
  7883. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7884. ReleaseOperand(s0);
  7885. Emit(Mov(position,res,s1.op));
  7886. ReleaseIntermediateOperand(res);
  7887. ReleaseOperand(s1);
  7888. END;
  7889. (* ---- SYSTEM.MOVE ----- *)
  7890. |Global.systemMove:
  7891. Evaluate(p0,s0);
  7892. Evaluate(p1,s1);
  7893. Evaluate(p2,s2);
  7894. Emit(Copy(position,s1.op,s0.op,s2.op));
  7895. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7896. (* ---- SYSTEM.NEW ----- *)
  7897. |Global.systemNew:
  7898. Designate(p0,s0);
  7899. Emit(Push(position,s0.op));
  7900. ReleaseOperand(s0);
  7901. Evaluate(p1,s1);
  7902. Emit(Push(position,s1.op));
  7903. ReleaseOperand(s1);
  7904. (* push realtime flag: false by default *)
  7905. Emit(Push(position,false));
  7906. CallThis(position,"Heaps","NewSys",3);
  7907. (* ---- SYSTEM.CALL ----- *)
  7908. |Global.systemRef:
  7909. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7910. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7911. s0.mode := ModeValue;
  7912. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7913. result := s0
  7914. (* ---- INCR ----- *)
  7915. |Global.Incr:
  7916. Designate(p0,operand);
  7917. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7918. Dereference(operand,p0.type.resolved,FALSE);
  7919. END;
  7920. ASSERT(p1 # NIL);
  7921. Evaluate(p1,l);
  7922. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7923. ReleaseOperand(operand); ReleaseOperand(l);
  7924. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7925. (* ---- SUM ----- *)
  7926. |Global.Sum: HALT(200);
  7927. (* ---- ALL ----- *)
  7928. |Global.All: HALT(200);
  7929. (* ---- CAS ----- *)
  7930. |Global.Cas:
  7931. needsTrace := p0.NeedsTrace();
  7932. IF needsTrace THEN ModifyAssignments(true) END;
  7933. Designate(p0,s0);
  7934. Evaluate(p1,s1);
  7935. Evaluate(p2,s2);
  7936. IF needsTrace THEN
  7937. Emit(Push(position, s0.op));
  7938. Emit(Push(position, s1.op));
  7939. Emit(Push(position, s2.op));
  7940. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7941. ELSE
  7942. Emit(Cas(position,s0.op,s1.op,s2.op));
  7943. END;
  7944. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7945. IF needsTrace THEN ModifyAssignments(false) END;
  7946. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7947. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7948. IF conditional THEN
  7949. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7950. BrL(falseLabel);
  7951. ReleaseIntermediateOperand(res);
  7952. END;
  7953. (* ---- DIM ----- *)
  7954. |Global.Dim:
  7955. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7956. Designate(p0,s0);
  7957. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7958. Dereference(s0,p0.type.resolved,FALSE);
  7959. END;
  7960. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7961. ReleaseOperand(s0);
  7962. (* ---- RESHAPE ----- *)
  7963. |Global.Reshape:
  7964. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7965. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7966. left.SetType(procedure.type);
  7967. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7968. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7969. END;
  7970. (* ---- SYSTEM.TYPECODE ----- *)
  7971. |Global.systemTypeCode:
  7972. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7973. IF type.resolved IS SyntaxTree.PointerType THEN
  7974. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7975. END;
  7976. result.op := TypeDescriptorAdr(type);
  7977. IF ~newObjectFile THEN
  7978. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7979. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7980. END;
  7981. result.mode := ModeValue;
  7982. (* ---- SYSTEM.TRACE ----- *)
  7983. |Global.systemTrace:
  7984. SystemTrace(x.parameters, x.position);
  7985. (* ----- CONNECT ------*)
  7986. |Global.Connect:
  7987. IF backend.cellsAreObjects THEN
  7988. PushPort(p0);
  7989. PushPort(p1);
  7990. IF p2 # NIL THEN
  7991. Evaluate(p2, s2);
  7992. Emit(Push(p2.position, s2.op));
  7993. ReleaseOperand(s2);
  7994. ELSE
  7995. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7996. END;
  7997. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7998. ELSE
  7999. Warning(x.position, "cannot run on final hardware");
  8000. END;
  8001. (* ----- DELEGATE ------*)
  8002. |Global.Delegate:
  8003. IF backend.cellsAreObjects THEN
  8004. PushPort(p0);
  8005. PushPort(p1);
  8006. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  8007. ELSE
  8008. Warning(x.position, "cannot run on final hardware");
  8009. END;
  8010. (* ----- SEND ------*)
  8011. |Global.Send:
  8012. Evaluate(p0,s0);
  8013. Evaluate(p1,s1);
  8014. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8015. Emit(Push(position,s0.op));
  8016. Emit(Push(position,s1.op));
  8017. (*
  8018. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8019. *)
  8020. IF ~backend.cellsAreObjects THEN
  8021. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8022. END;
  8023. ReleaseOperand(s0);
  8024. ReleaseOperand(s1);
  8025. IF backend.cellsAreObjects THEN
  8026. CallThis(position,"ActiveCellsRuntime","Send",2);
  8027. ELSE
  8028. CallThis(position,ChannelModuleName,"Send",2);
  8029. END;
  8030. (* ----- RECEIVE ------*)
  8031. |Global.Receive:
  8032. Evaluate(p0,s0);
  8033. Emit(Push(position,s0.op));
  8034. Designate(p1,s1);
  8035. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8036. Emit(Push(position,s1.op));
  8037. IF p2 # NIL THEN
  8038. Designate(p2,s2);
  8039. Emit(Push(position,s2.op));
  8040. END;
  8041. (*
  8042. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8043. *)
  8044. IF ~backend.cellsAreObjects THEN
  8045. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8046. END;
  8047. ReleaseOperand(s0);
  8048. ReleaseOperand(s1);
  8049. ReleaseOperand(s2);
  8050. IF backend.cellsAreObjects THEN
  8051. IF p2 = NIL THEN
  8052. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8053. ELSE
  8054. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8055. END;
  8056. ELSE
  8057. IF p2 = NIL THEN
  8058. CallThis(position,ChannelModuleName,"Receive",2)
  8059. ELSE
  8060. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8061. END;
  8062. END;
  8063. | Global.systemSpecial:
  8064. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8065. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8066. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8067. (* determine if parameters are of the VAR kind *)
  8068. ASSERT(x.parameters.Length() <= 3);
  8069. formalParameter := procedureType.firstParameter;
  8070. FOR i := 0 TO x.parameters.Length() - 1 DO
  8071. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8072. formalParameter := formalParameter.nextParameter
  8073. END;
  8074. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8075. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8076. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8077. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8078. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8079. IF procedureType.returnType # NIL THEN
  8080. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8081. Emit(Result(position, res));
  8082. (*InitOperand(result,ModeValue);
  8083. result.op := res;
  8084. *)
  8085. IF ~conditional THEN
  8086. InitOperand(result,ModeValue); result.op := res;
  8087. ELSE
  8088. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8089. BrL(falseLabel);
  8090. ReleaseIntermediateOperand(res);
  8091. END;
  8092. END
  8093. ELSE (* function not yet implemented *)
  8094. Error(position,"not yet implemented");
  8095. END;
  8096. destination := dest;
  8097. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8098. END VisitBuiltinCallDesignator;
  8099. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8100. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8101. BEGIN
  8102. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8103. dest := destination; destination := emptyOperand;
  8104. Expression(x.left);
  8105. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8106. ELSIF isUnchecked THEN (* no check *)
  8107. ELSE
  8108. trueL := NewLabel();
  8109. falseL := NewLabel();
  8110. IF IsPointerToRecord(x.left.type,recordType) THEN
  8111. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8112. Emit(Mov(position,tag, result.op));
  8113. IF result.mode # ModeValue THEN
  8114. ptr := tag;
  8115. IntermediateCode.MakeMemory(ptr,addressType);
  8116. Emit(Mov(position,tag, ptr));
  8117. END;
  8118. IF ~backend.cooperative THEN
  8119. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8120. END;
  8121. IntermediateCode.MakeMemory(tag,addressType);
  8122. ELSE
  8123. tag := result.tag;
  8124. UseIntermediateOperand(tag);
  8125. END;
  8126. TypeTest(tag,x.type,trueL,falseL);
  8127. ReleaseIntermediateOperand(tag);
  8128. SetLabel(falseL);
  8129. EmitTrap(position,TypeCheckTrap);
  8130. SetLabel(trueL);
  8131. END;
  8132. destination := dest;
  8133. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8134. END VisitTypeGuardDesignator;
  8135. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8136. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8137. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8138. VAR label: Label; pc: LONGINT;
  8139. BEGIN
  8140. IF backend.cooperative & ~isUnchecked THEN
  8141. pc := section.pc;
  8142. label := NewLabel();
  8143. BrneL(label, operand.op, nil);
  8144. EmitTrap(position, NilPointerTrap);
  8145. SetLabel(label);
  8146. INC(statCoopNilCheck, section.pc - pc);
  8147. END;
  8148. END NilCheck;
  8149. BEGIN
  8150. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8151. ReuseCopy(dereferenced,operand.op);
  8152. ReleaseOperand(operand);
  8153. operand.mode := ModeReference;
  8154. operand.op := dereferenced;
  8155. operand.tag := dereferenced;
  8156. UseIntermediateOperand(operand.tag);
  8157. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8158. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8159. ReleaseIntermediateOperand(operand.tag);
  8160. operand.tag := TypeDescriptorAdr(type);
  8161. IF ~newObjectFile THEN
  8162. IntermediateCode.MakeMemory(operand.tag,addressType);
  8163. END;
  8164. ELSE
  8165. IF ~backend.cooperative THEN
  8166. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8167. END;
  8168. IntermediateCode.MakeMemory(operand.tag,addressType);
  8169. END;
  8170. NilCheck(operand.op);
  8171. ELSIF type IS SyntaxTree.ArrayType THEN
  8172. IF isUnsafe THEN
  8173. NilCheck(operand.op);
  8174. ReleaseIntermediateOperand(operand.tag);
  8175. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8176. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8177. ELSE
  8178. operand.tag := emptyOperand;
  8179. END;
  8180. ELSE
  8181. NilCheck(operand.op);
  8182. IF backend.cooperative THEN
  8183. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8184. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8185. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8186. ELSE
  8187. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8188. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8189. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8190. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8191. END;
  8192. END;
  8193. ELSIF type IS SyntaxTree.MathArrayType THEN
  8194. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8195. IntermediateCode.MakeMemory(operand.op,addressType);
  8196. ELSE HALT(100);
  8197. END;
  8198. END Dereference;
  8199. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8200. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8201. BEGIN
  8202. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8203. dest := destination; destination := emptyOperand;
  8204. Evaluate(x.left,d);
  8205. type := x.type.resolved;
  8206. prevIsUnchecked := isUnchecked;
  8207. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8208. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8209. END;
  8210. Dereference(d,type,IsUnsafePointer(x.left.type));
  8211. isUnchecked := prevIsUnchecked;
  8212. result := d;
  8213. 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
  8214. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8215. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8216. END;
  8217. END;
  8218. destination := dest;
  8219. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8220. END VisitDereferenceDesignator;
  8221. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8222. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8223. BEGIN
  8224. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8225. dest := destination; destination := emptyOperand;
  8226. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8227. tag := result.op;
  8228. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8229. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8230. StaticCallOperand(result,procedure.super);
  8231. ReleaseIntermediateOperand(result.tag);
  8232. UseIntermediateOperand(tag); (* necessary ? *)
  8233. result.tag := tag;
  8234. destination := dest;
  8235. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8236. END VisitSupercallDesignator;
  8237. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8238. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8239. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8240. name: Basic.SegmentedName;
  8241. procedure: SyntaxTree.Procedure;
  8242. procedureType: SyntaxTree.ProcedureType;
  8243. BEGIN
  8244. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8245. dest := destination; destination := emptyOperand;
  8246. scope := currentScope;
  8247. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8248. scope := scope.outerScope;
  8249. END;
  8250. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8251. IF newObjectFile THEN
  8252. moduleSection := meta.ModuleSection();
  8253. IF backend.cooperative THEN
  8254. moduleOffset := 0;
  8255. ELSE
  8256. moduleOffset := moduleSection.pc;
  8257. END;
  8258. result.mode := ModeValue;
  8259. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8260. ELSE
  8261. Symbol(moduleSelf,result);
  8262. IntermediateCode.MakeMemory(result.op,addressType);
  8263. END
  8264. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8265. result.mode := ModeValue;
  8266. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8267. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8268. ELSE
  8269. GetBaseRegister(basereg,currentScope,scope);
  8270. InitOperand(result,ModeReference);
  8271. result.op := basereg;
  8272. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8273. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8274. parametersSize := ProcedureParametersSize(system,procedure);
  8275. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8276. IF backend.cooperative THEN
  8277. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8278. END;
  8279. (* tag must be loaded when dereferencing SELF pointer *)
  8280. END;
  8281. destination := dest;
  8282. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8283. END VisitSelfDesignator;
  8284. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8285. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; map: SymbolMap;
  8286. BEGIN
  8287. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8288. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8289. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8290. parameter := procedureType.returnParameter;
  8291. IF currentIsInline THEN
  8292. map := currentMapper.Get(NIL);
  8293. IF map # NIL THEN
  8294. Designate(map.to, result);
  8295. ELSE
  8296. HALT(200);
  8297. END;
  8298. RETURN;
  8299. END;
  8300. VisitParameter(parameter);
  8301. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8302. END VisitResultDesignator;
  8303. (** values *)
  8304. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8305. BEGIN
  8306. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8307. IF conditional THEN
  8308. IF x.value THEN BrL(trueLabel)
  8309. ELSE BrL(falseLabel)
  8310. END;
  8311. ELSE
  8312. InitOperand(result,ModeValue);
  8313. IF x.value THEN result.op := true ELSE result.op := false END;
  8314. END;
  8315. END VisitBooleanValue;
  8316. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8317. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8318. BEGIN
  8319. Global.GetModuleSegmentedName(module.module, name);
  8320. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8321. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8322. RETURN section
  8323. END GetDataSection;
  8324. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8325. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8326. BEGIN
  8327. type := vop.type;
  8328. data := GetDataSection();
  8329. pc := EnterImmediate(data,vop);
  8330. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8331. IntermediateCode.MakeMemory(vop, type);
  8332. END GetImmediateMem;
  8333. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8334. BEGIN
  8335. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8336. InitOperand(result,ModeValue);
  8337. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8338. IF ~supportedImmediate(result.op) &~inData THEN
  8339. GetImmediateMem(result.op)
  8340. END;
  8341. END VisitIntegerValue;
  8342. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8343. BEGIN
  8344. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8345. InitOperand(result,ModeValue);
  8346. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8347. END VisitCharacterValue;
  8348. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8349. BEGIN
  8350. IF Trace THEN TraceEnter("VisitSetValue") END;
  8351. InitOperand(result,ModeValue);
  8352. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8353. END VisitSetValue;
  8354. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8355. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8356. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8357. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8358. BEGIN
  8359. numberElements := x.elements.Length();
  8360. FOR i := 0 TO numberElements-1 DO
  8361. expression := x.elements.GetExpression(i);
  8362. IF expression IS SyntaxTree.MathArrayExpression THEN
  8363. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8364. ELSE
  8365. inData := TRUE;
  8366. Evaluate(expression,op);
  8367. irv.Emit(Data(position,op.op));
  8368. inData := FALSE;
  8369. ReleaseOperand(op);
  8370. END;
  8371. END;
  8372. END RecursiveData;
  8373. BEGIN
  8374. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8375. IF ~TryConstantDeclaration() THEN
  8376. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8377. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8378. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8379. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8380. ELSE
  8381. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8382. END;
  8383. RecursiveData(x.array);
  8384. InitOperand(result,ModeReference);
  8385. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8386. END
  8387. END VisitMathArrayValue;
  8388. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8389. VAR constant: Sections.Section;
  8390. BEGIN
  8391. IF constantDeclaration = NIL THEN
  8392. RETURN FALSE
  8393. ELSE
  8394. (* Is a constant in this module: did we generate it already? *)
  8395. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8396. IF constant # NIL THEN
  8397. InitOperand(result,ModeReference);
  8398. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8399. RETURN TRUE;
  8400. END;
  8401. END;
  8402. RETURN FALSE
  8403. END TryConstantDeclaration;
  8404. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8405. BEGIN
  8406. constantDeclaration := x;
  8407. x.value.resolved.Accept(SELF);
  8408. END VisitConstant;
  8409. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8410. BEGIN
  8411. IF Trace THEN TraceEnter("VisitRealValue") END;
  8412. InitOperand(result,ModeValue);
  8413. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8414. END VisitRealValue;
  8415. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8416. VAR
  8417. componentType: SyntaxTree.Type;
  8418. BEGIN
  8419. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8420. ASSERT(x.type IS SyntaxTree.ComplexType);
  8421. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8422. InitOperand(result,ModeValue);
  8423. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8424. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8425. END VisitComplexValue;
  8426. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8427. VAR i: LONGINT; name: Basic.SegmentedName;
  8428. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8429. BEGIN
  8430. IF Trace THEN TraceEnter("VisitStringValue") END;
  8431. IF ~TryConstantDeclaration() THEN
  8432. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8433. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8434. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8435. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8436. ELSE
  8437. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8438. END;
  8439. FOR i := 0 TO x.length-1 DO
  8440. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8441. irv.Emit(Data(position,op));
  8442. END;
  8443. InitOperand(result,ModeReference);
  8444. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8445. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8446. END
  8447. END VisitStringValue;
  8448. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8449. BEGIN
  8450. IF Trace THEN TraceEnter("VisitNilValue") END;
  8451. InitOperand(result,ModeValue);
  8452. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8453. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8454. END VisitNilValue;
  8455. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8456. BEGIN
  8457. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8458. InitOperand(result,ModeValue);
  8459. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8460. END VisitEnumerationValue;
  8461. (** symbols *)
  8462. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8463. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8464. END VisitImport;
  8465. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8466. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8467. BEGIN
  8468. IF scope # baseScope THEN
  8469. (* left := [fp+8] *)
  8470. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8471. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8472. ReuseCopy(left,right);
  8473. ReleaseIntermediateOperand(right);
  8474. scope := scope.outerScope; DEC(level);
  8475. (* { left := [left+8] } *)
  8476. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8477. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8478. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8479. Emit(Mov(position,left,right));
  8480. scope := scope.outerScope; DEC(level);
  8481. END;
  8482. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8483. result := left;
  8484. ELSE
  8485. result := fp;
  8486. END;
  8487. END GetBaseRegister;
  8488. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8489. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8490. BEGIN
  8491. IF Trace THEN TraceEnter("VisitVariable"); END;
  8492. type := x.type.resolved;
  8493. IF (x.useRegister) THEN
  8494. InitOperand(result, ModeValue);
  8495. IF x.registerNumber < 0 THEN
  8496. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8497. reg := x.registerNumber;
  8498. ELSE
  8499. reg := registerUsageCount.Map(x.registerNumber);
  8500. UseRegister(reg);
  8501. END;
  8502. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8503. ELSIF x.externalName # NIL THEN
  8504. InitOperand(result,ModeReference);
  8505. Basic.ToSegmentedName(x.externalName^, name);
  8506. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8507. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8508. InitOperand(result,ModeReference);
  8509. GetBaseRegister(result.op,currentScope,x.scope);
  8510. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8511. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8512. InitOperand(result,ModeReference);
  8513. GetCodeSectionNameForSymbol(x,name);
  8514. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8515. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8516. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8517. InitOperand(result,ModeReference);
  8518. GetCodeSectionNameForSymbol(x,name);
  8519. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8520. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8521. ELSE (* field, left designator must have been emitted *)
  8522. ASSERT(result.mode = ModeReference);
  8523. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8524. ReuseCopy(temp,result.op);
  8525. ReleaseIntermediateOperand(result.op);
  8526. result.op := temp;
  8527. END;
  8528. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8529. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8530. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8531. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8532. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8533. END;
  8534. END;
  8535. END;
  8536. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8537. ValueToCondition(result);
  8538. ELSIF type IS SyntaxTree.ProcedureType THEN
  8539. ReleaseIntermediateOperand(result.tag);
  8540. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8541. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8542. UseIntermediateOperand(result.tag);
  8543. ELSE
  8544. result.tag := nil; (* nil *)
  8545. END;
  8546. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8547. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8548. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8549. ReleaseIntermediateOperand(result.tag);
  8550. Global.GetSymbolSegmentedName(x,name);
  8551. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8552. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8553. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8554. ELSE
  8555. END;
  8556. ELSE
  8557. ReleaseIntermediateOperand(result.tag);
  8558. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8559. END;
  8560. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8561. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8562. ReleaseIntermediateOperand(result.tag);
  8563. result.tag := result.op;
  8564. UseIntermediateOperand(result.tag);
  8565. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8566. IntermediateCode.MakeMemory(result.op,addressType);
  8567. END;
  8568. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8569. ReleaseIntermediateOperand(result.tag);
  8570. result.tag := TypeDescriptorAdr(type);
  8571. IF ~newObjectFile THEN
  8572. IntermediateCode.MakeMemory(result.tag,addressType);
  8573. END;
  8574. UseIntermediateOperand(result.tag);
  8575. (* tag for pointer type computed not here but during dereferencing *)
  8576. END;
  8577. IF Trace THEN TraceExit("VisitVariable") END;
  8578. END VisitVariable;
  8579. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8580. BEGIN
  8581. VisitVariable(property);
  8582. END VisitProperty;
  8583. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8584. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8585. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8586. BEGIN
  8587. type := x.type.resolved;
  8588. IF Trace THEN TraceEnter("VisitParameter") END;
  8589. IF x.ownerType IS SyntaxTree.CellType THEN
  8590. ptype := x.type.resolved;
  8591. IF backend.cellsAreObjects THEN
  8592. ASSERT(result.mode = ModeReference);
  8593. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8594. ReuseCopy(temp,result.op);
  8595. ReleaseIntermediateOperand(result.op);
  8596. result.op := temp;
  8597. END;
  8598. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8599. RETURN;
  8600. ELSE
  8601. InitOperand(result,ModeReference);
  8602. GetCodeSectionNameForSymbol(x,name);
  8603. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8604. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8605. RETURN;
  8606. END;
  8607. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8608. InitOperand(result,ModeReference);
  8609. GetCodeSectionNameForSymbol(x,name);
  8610. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8611. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8612. RETURN
  8613. ELSE
  8614. GetBaseRegister(basereg,currentScope,x.scope);
  8615. InitOperand(result,ModeReference);
  8616. result.op := basereg;
  8617. END;
  8618. IF IsOpenArray(type) THEN
  8619. result.tag := basereg;
  8620. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8621. IntermediateCode.MakeMemory(result.op,addressType);
  8622. IF Global.IsOberonProcedure(x.ownerType) THEN
  8623. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8624. UseIntermediateOperand(result.tag);
  8625. ELSE
  8626. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8627. END;
  8628. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8629. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8630. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8631. ELSIF IsStaticArray(type) THEN
  8632. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8633. IntermediateCode.MakeMemory(result.op,addressType);
  8634. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8635. ELSIF type IS SyntaxTree.MathArrayType THEN
  8636. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8637. WITH type: SyntaxTree.MathArrayType DO
  8638. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8639. IF type.form = SyntaxTree.Tensor THEN
  8640. ELSIF type.form = SyntaxTree.Open THEN
  8641. result.tag := result.op;
  8642. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8643. IntermediateCode.MakeMemory(result.op,addressType);
  8644. UseIntermediateOperand(result.tag);
  8645. ELSIF type.form = SyntaxTree.Static THEN
  8646. IF x.kind = SyntaxTree.ConstParameter THEN
  8647. IntermediateCode.MakeMemory(result.op,addressType);
  8648. END;
  8649. ELSE HALT(100)
  8650. END;
  8651. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8652. IF type.form = SyntaxTree.Tensor THEN
  8653. ToMemory(result.op,addressType,0);
  8654. ELSIF type.form = SyntaxTree.Open THEN
  8655. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8656. ReuseCopy(result.tag, mem);
  8657. ReleaseIntermediateOperand(mem);
  8658. ReleaseIntermediateOperand(result.op);
  8659. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8660. ELSIF type.form = SyntaxTree.Static THEN
  8661. IntermediateCode.MakeMemory(result.op,addressType);
  8662. ELSE HALT(100)
  8663. END;
  8664. ELSE HALT(100)
  8665. END;
  8666. END;
  8667. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8668. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8669. IntermediateCode.MakeMemory(result.op,addressType);
  8670. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8671. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8672. END;
  8673. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8674. ValueToCondition(result);
  8675. ELSIF type IS SyntaxTree.ProcedureType THEN
  8676. ReleaseIntermediateOperand(result.tag);
  8677. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8678. IF x.kind = SyntaxTree.VarParameter THEN
  8679. ReuseCopy(result.tag,result.op);
  8680. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8681. IntermediateCode.MakeMemory(result.tag,addressType);
  8682. ELSE
  8683. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8684. UseIntermediateOperand(result.tag);
  8685. END;
  8686. ELSE
  8687. result.tag := nil;
  8688. END;
  8689. (* tag for pointer type computed not here but during dereferencing *)
  8690. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) & ~(x.selfParameter) THEN
  8691. ReleaseIntermediateOperand(result.tag);
  8692. result.tag := basereg;
  8693. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8694. IntermediateCode.MakeMemory(result.tag,addressType);
  8695. UseIntermediateOperand(result.tag);
  8696. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & ((x.kind = SyntaxTree.ValueParameter) OR x.selfParameter) THEN
  8697. ReleaseIntermediateOperand(result.tag);
  8698. result.tag := TypeDescriptorAdr(type);
  8699. IF ~newObjectFile THEN
  8700. IntermediateCode.MakeMemory(result.tag,addressType);
  8701. END;
  8702. UseIntermediateOperand(result.tag);
  8703. END;
  8704. IF Trace THEN TraceExit("VisitParameter") END;
  8705. END VisitParameter;
  8706. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8707. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8708. BEGIN
  8709. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8710. (* left.p: left already emitted *)
  8711. tag := result.op; (* value of pointer to left *)
  8712. (* get type desc *)
  8713. tmp := result.tag;
  8714. IntermediateCode.MakeMemory(tmp,addressType);
  8715. (* get method adr *)
  8716. Reuse1(reg,tmp);
  8717. ReleaseIntermediateOperand(tmp);
  8718. IF backend.cooperative THEN
  8719. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8720. WHILE recordType.baseType # NIL DO
  8721. recordType := recordType.GetBaseRecord ();
  8722. END;
  8723. GetRecordTypeName (recordType,name);
  8724. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8725. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8726. offset := 0;
  8727. ELSE
  8728. offset := 2;
  8729. END;
  8730. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8731. ELSE
  8732. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8733. END;
  8734. InitOperand(operand,ModeReference);
  8735. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8736. operand.op := reg;
  8737. operand.tag := tag;
  8738. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8739. END DynamicCallOperand;
  8740. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8741. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8742. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8743. BEGIN
  8744. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8745. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8746. tag := operand.op;
  8747. ReleaseIntermediateOperand(operand.tag);
  8748. ELSE tag := nil
  8749. END;
  8750. IF x.isInline THEN
  8751. sectionType := Sections.InlineCodeSection;
  8752. ELSE
  8753. sectionType := Sections.CodeSection;
  8754. END;
  8755. IF x.externalName # NIL THEN
  8756. Basic.ToSegmentedName(x.externalName^, name);
  8757. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8758. ELSE
  8759. GetCodeSectionNameForSymbol(x, name);
  8760. IF (x.scope.ownerModule = module.module) THEN
  8761. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8762. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8763. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8764. IF source.pc = 0 THEN (* no code yet *)
  8765. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8766. END;
  8767. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8768. bits := x.procedureScope.body.code.inlineCode;
  8769. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8770. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8771. binary.CopyBits(bits, 0, bits.GetSize());
  8772. source.SetResolved(binary);
  8773. ELSE
  8774. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8775. END;
  8776. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8777. END;
  8778. InitOperand(operand,ModeValue);
  8779. operand.op := reg;
  8780. operand.tag := tag;
  8781. IF Trace THEN TraceExit("StaticCallOperand") END;
  8782. END StaticCallOperand;
  8783. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8784. (* handle expressions of the form designator.procedure or procedure *)
  8785. BEGIN
  8786. IF Trace THEN TraceEnter("VisitProcedure") END;
  8787. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) & ~(result.tag.mode = IntermediateCode.ModeImmediate) THEN
  8788. DynamicCallOperand(result,x);
  8789. ELSIF x.isInline THEN
  8790. StaticCallOperand(result,x);
  8791. ELSE
  8792. StaticCallOperand(result,x);
  8793. END;
  8794. IF Trace THEN TraceExit("VisitProcedure") END;
  8795. END VisitProcedure;
  8796. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8797. BEGIN
  8798. VisitProcedure(x);
  8799. END VisitOperator;
  8800. (** statements *)
  8801. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8802. BEGIN
  8803. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8804. Expression(x.call);
  8805. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8806. ReleaseOperand(result)
  8807. END;
  8808. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8809. END VisitProcedureCallStatement;
  8810. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8811. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8812. leftBase, rightBase: SyntaxTree.Type;
  8813. procedureName,s: SyntaxTree.IdentifierString;
  8814. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8815. size: LONGINT; rightKind: LONGINT;
  8816. dummy: IntermediateCode.Operand;
  8817. CONST moduleName = "FoxArrayBase";
  8818. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8819. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8820. BEGIN
  8821. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8822. IF base IS SyntaxTree.MathArrayType THEN
  8823. base := OpenArray(base(SyntaxTree.MathArrayType));
  8824. END;
  8825. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8826. result.SetArrayBase(base);
  8827. RETURN result
  8828. END OpenArray;
  8829. BEGIN
  8830. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8831. SaveRegisters();ReleaseUsedRegisters(saved);
  8832. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8833. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8834. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8835. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8836. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8837. IF leftType.form = SyntaxTree.Tensor THEN
  8838. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8839. ELSIF leftType.form = SyntaxTree.Open THEN
  8840. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8841. ELSIF leftType.form = SyntaxTree.Static THEN
  8842. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8843. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8844. END;
  8845. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8846. IF procedure = NIL THEN
  8847. s := "Instruction not supported on target, emulation procedure ";
  8848. Strings.Append(s,moduleName);
  8849. Strings.Append(s,".");
  8850. Strings.Append(s,procedureName);
  8851. Strings.Append(s," not present");
  8852. Error(position,s);
  8853. ELSE
  8854. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8855. parameter.SetType(leftType);
  8856. parameter.SetAccess(SyntaxTree.Internal);
  8857. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8858. parameter.SetKind(rightKind);
  8859. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8860. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8861. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8862. StaticCallOperand(result,procedure);
  8863. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8864. ReleaseOperand(result);
  8865. END;
  8866. RestoreRegisters(saved);
  8867. END;
  8868. END AssignMathArray;
  8869. VAR modifyAssignmentCounter := 0: LONGINT;
  8870. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8871. VAR processor,mem,dst: IntermediateCode.Operand;
  8872. BEGIN
  8873. IF value.intValue = true.intValue THEN
  8874. INC(modifyAssignmentCounter);
  8875. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8876. modifyAssignmentsPC := section.pc;
  8877. ELSE
  8878. DEC(modifyAssignmentCounter);
  8879. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8880. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8881. END;
  8882. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8883. dst := NewRegisterOperand (addressType);
  8884. Emit(Mov(position,dst, processor));
  8885. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8886. Emit(Mov(position,mem, value));
  8887. ReleaseIntermediateOperand(dst);
  8888. END ModifyAssignments;
  8889. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8890. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8891. BEGIN
  8892. type := left.type.resolved;
  8893. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8894. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8895. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8896. IF procedureType.returnParameter = parameter THEN
  8897. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8898. END;
  8899. END;
  8900. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8901. END CopySize;
  8902. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8903. VAR
  8904. leftO, rightO: Operand;
  8905. mem, sizeOp: IntermediateCode.Operand;
  8906. leftType, rightType, componentType: SyntaxTree.Type;
  8907. size: LONGINT;
  8908. parameters: SyntaxTree.ExpressionList;
  8909. procedure: SyntaxTree.Procedure;
  8910. call: SyntaxTree.ProcedureCallDesignator;
  8911. designator: SyntaxTree.Designator;
  8912. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8913. VAR procedureType: SyntaxTree.ProcedureType;
  8914. BEGIN
  8915. IF SemanticChecker.ReturnedAsParameter(right.type) THEN
  8916. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8917. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8918. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8919. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8920. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8921. IF right.id = Global.Reshape THEN RETURN TRUE
  8922. END;
  8923. END;
  8924. END;
  8925. END;
  8926. RETURN FALSE
  8927. END CanPassAsResultParameter;
  8928. BEGIN
  8929. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8930. leftType := left.type.resolved; rightType:= right.type.resolved;
  8931. IF backend.cooperative & left.NeedsTrace() THEN
  8932. ModifyAssignments(true);
  8933. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8934. Designate(right, rightO);
  8935. Designate(left, leftO);
  8936. ASSERT(leftO.mode = ModeReference);
  8937. TransferToRegister(leftO.op, leftO.op);
  8938. TransferToRegister(rightO.op, rightO.op);
  8939. Emit(Push(position, leftO.op));
  8940. Emit(Push(position, rightO.op));
  8941. CallAssignMethod(leftO.op, rightO.op, left.type);
  8942. Emit(Pop(position, rightO.op));
  8943. Emit(Pop(position, leftO.op));
  8944. sizeOp := CopySize(left);
  8945. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8946. ReleaseOperand(leftO);
  8947. ReleaseOperand(rightO);
  8948. ELSE
  8949. Evaluate(right,rightO);
  8950. Designate(left,leftO);
  8951. ASSERT(leftO.mode = ModeReference);
  8952. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8953. (* copy procedure address first *)
  8954. MakeMemory(mem,leftO.op,addressType,0);
  8955. Emit(Mov(position,mem,rightO.op));
  8956. ReleaseIntermediateOperand(mem);
  8957. (* copy pointer address *)
  8958. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8959. CallAssignPointer(leftO.tag, rightO.tag);
  8960. ELSE
  8961. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8962. CallAssignPointer(leftO.op, rightO.op);
  8963. END;
  8964. ReleaseOperand(leftO);
  8965. ReleaseOperand(rightO);
  8966. END;
  8967. ModifyAssignments(false);
  8968. RETURN;
  8969. END;
  8970. IF CanPassAsResultParameter(right) THEN
  8971. procedureResultDesignator := left(SyntaxTree.Designator);
  8972. Expression(right);
  8973. procedureResultDesignator := NIL;
  8974. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8975. (* left <-- ALIAS OF right: zerocopy *)
  8976. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8977. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8978. designator.SetType(procedure.type);
  8979. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8980. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8981. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8982. END;
  8983. ELSIF leftType IS SyntaxTree.RangeType THEN
  8984. (* LHS is of array range type *)
  8985. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8986. Evaluate(right, rightO);
  8987. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8988. (* first *)
  8989. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8990. Emit(Mov(position,mem, rightO.op));
  8991. ReleaseIntermediateOperand(mem);
  8992. (* last *)
  8993. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8994. Emit(Mov(position,mem, rightO.tag));
  8995. ReleaseIntermediateOperand(mem);
  8996. (* step *)
  8997. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8998. Emit(Mov(position,mem, rightO.extra));
  8999. ReleaseIntermediateOperand(mem);
  9000. ReleaseOperand(rightO);
  9001. ReleaseOperand(leftO)
  9002. ELSIF leftType IS SyntaxTree.ComplexType THEN
  9003. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  9004. Evaluate(right, rightO);
  9005. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  9006. componentType := leftType(SyntaxTree.ComplexType).componentType;
  9007. (* real part *)
  9008. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  9009. Emit(Mov(position,mem, rightO.op));
  9010. ReleaseIntermediateOperand(mem);
  9011. (* imaginary part *)
  9012. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9013. Emit(Mov(position,mem, rightO.tag));
  9014. ReleaseIntermediateOperand(mem);
  9015. ReleaseOperand(rightO);
  9016. ReleaseOperand(leftO)
  9017. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9018. OR (leftType IS SyntaxTree.PortType) THEN
  9019. (* rightO := leftO;*)
  9020. Evaluate(right,rightO);
  9021. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9022. Designate(left,leftO);
  9023. IF leftO.mode = ModeReference THEN
  9024. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9025. destination := mem;
  9026. ELSE
  9027. destination := leftO.op;
  9028. END;
  9029. ReleaseOperand(leftO);
  9030. IF destination.mode # IntermediateCode.Undefined THEN
  9031. Emit(Mov(position,destination,rightO.op));
  9032. END;
  9033. ReleaseOperand(rightO);
  9034. ReleaseIntermediateOperand(mem);
  9035. IntermediateCode.InitOperand(destination);
  9036. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9037. Evaluate(right,rightO);
  9038. Designate(left,leftO);
  9039. MakeMemory(mem,leftO.op,addressType,0);
  9040. Emit(Mov(position,mem,rightO.op));
  9041. ReleaseIntermediateOperand(mem);
  9042. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9043. (* delegate *)
  9044. (*
  9045. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9046. *)
  9047. Emit(Mov(position,leftO.tag,rightO.tag));
  9048. END;
  9049. ReleaseOperand(leftO);
  9050. ReleaseOperand(rightO);
  9051. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9052. Designate(right,rightO);
  9053. Designate(left,leftO);
  9054. sizeOp := CopySize(left);
  9055. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9056. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9057. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9058. IF (rightType IS SyntaxTree.StringType) THEN
  9059. CopyString(left,right);
  9060. 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
  9061. Designate(right,rightO);
  9062. Designate(left,leftO);
  9063. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9064. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9065. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9066. ELSE
  9067. HALT(201)
  9068. END;
  9069. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9070. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9071. Designate(right,rightO);
  9072. Designate(left,leftO);
  9073. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9074. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9075. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9076. END;
  9077. AssignMathArray(left,right);
  9078. ELSE
  9079. HALT(200);
  9080. END;
  9081. END Assign;
  9082. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9083. BEGIN
  9084. IF Trace THEN TraceEnter("VisitAssignment") END;
  9085. Assign(x.left,x.right);
  9086. IF Trace THEN TraceExit("VisitAssignment") END;
  9087. END VisitAssignment;
  9088. PROCEDURE EmitCooperativeSwitch;
  9089. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9090. BEGIN
  9091. ASSERT (cooperativeSwitches);
  9092. pc := section.pc;
  9093. IF lastSwitchPC = section.pc THEN RETURN END;
  9094. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9095. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9096. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9097. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9098. INC(statCoopSwitch, section.pc - pc);
  9099. END EmitCooperativeSwitch;
  9100. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9101. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9102. BEGIN
  9103. p0 := communication.left; p1 := communication.right;
  9104. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9105. Evaluate(p0,s0);
  9106. Evaluate(p1,s1);
  9107. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9108. Emit(Push(position,s0.op));
  9109. Emit(Push(position,s1.op));
  9110. (*
  9111. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9112. *)
  9113. IF ~backend.cellsAreObjects THEN
  9114. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9115. END;
  9116. ReleaseOperand(s0);
  9117. ReleaseOperand(s1);
  9118. IF backend.cellsAreObjects THEN
  9119. CallThis(position,"ActiveCellsRuntime","Send",2);
  9120. ELSE
  9121. CallThis(position,ChannelModuleName,"Send",2);
  9122. END;
  9123. (* ----- RECEIVE ------*)
  9124. ELSE
  9125. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9126. tmp := p0; p0 := p1; p1 := tmp;
  9127. END;
  9128. Evaluate(p0,s0);
  9129. Emit(Push(position,s0.op));
  9130. Designate(p1,s1);
  9131. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9132. Emit(Push(position,s1.op));
  9133. (*
  9134. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9135. *)
  9136. IF ~backend.cellsAreObjects THEN
  9137. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9138. END;
  9139. ReleaseOperand(s0);
  9140. ReleaseOperand(s1);
  9141. IF backend.cellsAreObjects THEN
  9142. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9143. ELSE
  9144. CallThis(position,ChannelModuleName,"Receive",2)
  9145. END;
  9146. END;
  9147. END VisitCommunicationStatement;
  9148. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9149. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9150. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9151. VAR true, false: Label; condition, value: BOOLEAN;
  9152. BEGIN
  9153. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9154. IF condition THEN
  9155. true := NewLabel();
  9156. false := NewLabel();
  9157. Condition(if.condition,true,false);
  9158. SetLabel(true);
  9159. StatementSequence(if.statements);
  9160. BrL(end);
  9161. SetLabel(false);
  9162. ELSE
  9163. IF value THEN (* always true *)
  9164. escape := TRUE;
  9165. StatementSequence(if.statements);
  9166. (* no branch necessary -- rest skipped *)
  9167. END;
  9168. END;
  9169. END IfPart;
  9170. BEGIN
  9171. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9172. end := NewLabel();
  9173. elsifs := x.ElsifParts();
  9174. IfPart(x.ifPart);
  9175. FOR i := 0 TO elsifs-1 DO
  9176. IF ~escape THEN
  9177. elsif := x.GetElsifPart(i);
  9178. IfPart(elsif);
  9179. END;
  9180. END;
  9181. IF (x.elsePart # NIL) & ~escape THEN
  9182. StatementSequence(x.elsePart);
  9183. END;
  9184. SetLabel(end);
  9185. IF Trace THEN TraceExit("VisitIfStatement") END;
  9186. END VisitIfStatement;
  9187. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9188. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9189. BEGIN
  9190. (*IF x.variable.type.resolved = x.type.resolved THEN
  9191. (* always true, do nothing *)
  9192. ELSE*)
  9193. Designate(x.variable,res);
  9194. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9195. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9196. END;
  9197. trueL := NewLabel();
  9198. TypeTest(res.tag,x.type,trueL,falseL);
  9199. ReleaseOperand(res);
  9200. SetLabel(trueL);
  9201. StatementSequence(x.statements);
  9202. BrL(endL);
  9203. END WithPart;
  9204. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9205. VAR endL,falseL: Label;i: LONGINT;
  9206. BEGIN
  9207. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9208. endL := NewLabel();
  9209. FOR i := 0 TO x.WithParts()-1 DO
  9210. falseL := NewLabel();
  9211. WithPart(x.GetWithPart(i),falseL,endL);
  9212. SetLabel(falseL);
  9213. END;
  9214. IF x.elsePart = NIL THEN
  9215. IF ~isUnchecked THEN
  9216. EmitTrap(position,WithTrap);
  9217. END;
  9218. ELSE
  9219. StatementSequence(x.elsePart)
  9220. END;
  9221. SetLabel(endL);
  9222. IF Trace THEN TraceExit("VisitWithStatement") END;
  9223. END VisitWithStatement;
  9224. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9225. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9226. out,else: Label; label: Label;
  9227. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9228. symbol: SyntaxTree.Symbol;
  9229. BEGIN
  9230. (*! split case statement into if-elsif statements for large case label lists *)
  9231. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9232. size := x.max-x.min+1;
  9233. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9234. END;
  9235. Evaluate(x.variable,var);
  9236. ReuseCopy(tmp,var.op);
  9237. ReleaseIntermediateOperand(var.op);
  9238. var.op := tmp;
  9239. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9240. Convert(var.op,addressType);
  9241. size := x.max-x.min+1;
  9242. else := NewLabel();
  9243. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9244. (*
  9245. UniqueId(name,module.module,"case",caseId);
  9246. *)
  9247. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9248. Global.GetModuleSegmentedName(module.module, name);
  9249. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9250. symbol := SyntaxTree.NewSymbol(name[1]);
  9251. symbol.SetScope(moduleScope);
  9252. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9253. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9254. section.isCaseTable := TRUE;
  9255. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9256. ReuseCopy(res,var.op);
  9257. ReleaseOperand(var);
  9258. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9259. Emit(Add(position,res,res,jmp));
  9260. IntermediateCode.MakeMemory(res,addressType);
  9261. Emit(Br(position,res));
  9262. ReleaseIntermediateOperand(res);
  9263. out := NewLabel();
  9264. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9265. part := x.GetCasePart(i);
  9266. constant := part.firstConstant;
  9267. label := NewLabel();
  9268. SetLabel(label);
  9269. WHILE(constant # NIL) DO (* case labels for this case part *)
  9270. FOR j := constant.min TO constant.max DO
  9271. fixups[j-x.min] := label;
  9272. END;
  9273. constant := constant.next;
  9274. END;
  9275. StatementSequence(part.statements);
  9276. BrL(out);
  9277. END;
  9278. SetLabel(else);
  9279. FOR i := 0 TO size-1 DO
  9280. IF fixups[i] = NIL THEN
  9281. fixups[i] := else;
  9282. END;
  9283. END;
  9284. IF x.elsePart # NIL THEN
  9285. StatementSequence(x.elsePart);
  9286. ELSIF ~isUnchecked THEN
  9287. EmitTrap(position,CaseTrap);
  9288. END;
  9289. SetLabel(out);
  9290. FOR i := 0 TO size-1 DO
  9291. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9292. section.Emit(Data(position,op));
  9293. END;
  9294. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9295. END VisitCaseStatement;
  9296. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9297. VAR start: Label; true,false: Label;
  9298. BEGIN
  9299. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9300. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9301. start := NewLabel();
  9302. true := NewLabel();
  9303. false := NewLabel();
  9304. SetLabel(start);
  9305. Condition(x.condition,true,false);
  9306. SetLabel(true);
  9307. StatementSequence(x.statements);
  9308. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9309. BrL(start);
  9310. SetLabel(false);
  9311. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9312. END VisitWhileStatement;
  9313. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9314. VAR false,true: Label;
  9315. BEGIN
  9316. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9317. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9318. true := NewLabel();
  9319. false := NewLabel();
  9320. SetLabel(false);
  9321. StatementSequence(x.statements);
  9322. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9323. Condition(x.condition,true,false);
  9324. SetLabel(true);
  9325. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9326. END VisitRepeatStatement;
  9327. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9328. VAR
  9329. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9330. temporaryVariable: SyntaxTree.Variable;
  9331. temporaryVariableDesignator : SyntaxTree.Designator;
  9332. BEGIN
  9333. IF Trace THEN TraceEnter("VisitForStatement") END;
  9334. true := NewLabel();
  9335. false := NewLabel();
  9336. start := NewLabel();
  9337. Assign(x.variable,x.from);
  9338. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9339. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9340. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9341. Assign(temporaryVariableDesignator,x.to);
  9342. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9343. IF by > 0 THEN
  9344. cmp := Scanner.LessEqual
  9345. ELSE
  9346. cmp := Scanner.GreaterEqual
  9347. END;
  9348. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9349. binary.SetType(system.booleanType);
  9350. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9351. SetLabel(start);
  9352. Condition(binary,true,false);
  9353. SetLabel(true);
  9354. StatementSequence(x.statements);
  9355. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9356. binary.SetType(x.variable.type);
  9357. Assign(x.variable,binary);
  9358. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9359. BrL(start);
  9360. SetLabel(false);
  9361. IF Trace THEN TraceExit("VisitForStatement") END;
  9362. END VisitForStatement;
  9363. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9364. VAR prevLoop: Label;
  9365. BEGIN
  9366. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9367. prevLoop := currentLoop;
  9368. currentLoop := NewLabel();
  9369. StatementSequence(x.statements);
  9370. SetLabel(currentLoop);
  9371. currentLoop := prevLoop;
  9372. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9373. END VisitExitableBlock;
  9374. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9375. VAR prevLoop,start: Label;
  9376. BEGIN
  9377. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9378. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9379. start := NewLabel();
  9380. prevLoop := currentLoop;
  9381. SetLabel(start);
  9382. currentLoop := NewLabel();
  9383. StatementSequence(x.statements);
  9384. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9385. BrL(start);
  9386. SetLabel(currentLoop);
  9387. currentLoop := prevLoop;
  9388. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9389. END VisitLoopStatement;
  9390. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9391. VAR outer: SyntaxTree.Statement;
  9392. BEGIN
  9393. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9394. IF locked THEN (* r if we jump out of an exclusive block *)
  9395. outer := x.outer;
  9396. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9397. outer := outer.outer;
  9398. END;
  9399. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9400. Lock(FALSE);
  9401. END;
  9402. END;
  9403. BrL(currentLoop);
  9404. IF Trace THEN TraceExit("VisitExitStatement") END;
  9405. END VisitExitStatement;
  9406. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9407. VAR
  9408. expression, parameterDesignator: SyntaxTree.Expression;
  9409. type, componentType: SyntaxTree.Type;
  9410. res, right: Operand;
  9411. left, mem, reg: IntermediateCode.Operand;
  9412. parameter: SyntaxTree.Parameter;
  9413. procedure: SyntaxTree.Procedure;
  9414. procedureType: SyntaxTree.ProcedureType;
  9415. returnTypeOffset: LONGINT;
  9416. delegate: BOOLEAN;
  9417. map: SymbolMap;
  9418. cc, parametersSize: LONGINT;
  9419. BEGIN
  9420. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9421. expression := x.returnValue;
  9422. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9423. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9424. IF currentIsInline THEN
  9425. IF expression # NIL THEN
  9426. map := currentMapper.Get(NIL);
  9427. IF map # NIL THEN
  9428. Assign(map.to, expression);
  9429. ELSE
  9430. Evaluate(expression,res);
  9431. Emit(Return(position,res.op));
  9432. ReleaseOperand(res);
  9433. END;
  9434. END;
  9435. BrL(currentInlineExit);
  9436. RETURN;
  9437. END;
  9438. IF expression # NIL THEN
  9439. type := expression.type.resolved;
  9440. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9441. IF locked THEN Lock(FALSE) END;
  9442. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9443. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9444. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9445. (* return without structured return parameter *)
  9446. Evaluate(expression,res);
  9447. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9448. IF locked OR profile THEN
  9449. Emit(Push(position,res.op));
  9450. IF delegate THEN HALT(200) END;
  9451. ReleaseOperand(res);
  9452. IF locked THEN Lock(FALSE) END;
  9453. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9454. reg := NewRegisterOperand(res.op.type);
  9455. Emit(Pop(position,reg));
  9456. Emit(Return(position,reg));
  9457. ReleaseIntermediateOperand(reg);
  9458. ELSE
  9459. Emit(Return(position,res.op));
  9460. ReleaseOperand(res);
  9461. END;
  9462. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9463. THEN
  9464. (* return using structured return parameter *)
  9465. 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)
  9466. OR SemanticChecker.IsPointerType(type));
  9467. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9468. parameter :=procedureType.returnParameter;
  9469. ASSERT(parameter # NIL);
  9470. returnTypeOffset := parameter.offsetInBits;
  9471. (*
  9472. IF parameter# NIL THEN
  9473. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9474. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9475. ELSE
  9476. returnTypeOffset := system.offsetFirstParameter
  9477. END;
  9478. *)
  9479. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9480. IF type IS SyntaxTree.RangeType THEN
  9481. (* array range type *)
  9482. Evaluate(expression, right);
  9483. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9484. Emit(Mov(position,mem, right.op)); (* first *)
  9485. ReleaseIntermediateOperand(mem);
  9486. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9487. Emit(Mov(position,mem, right.tag)); (* last *)
  9488. ReleaseIntermediateOperand(mem);
  9489. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9490. Emit(Mov(position,mem, right.extra)); (* step *)
  9491. ReleaseIntermediateOperand(mem);
  9492. ReleaseOperand(right);
  9493. ELSIF type IS SyntaxTree.ComplexType THEN
  9494. Evaluate(expression, right);
  9495. componentType := type(SyntaxTree.ComplexType).componentType;
  9496. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9497. Emit(Mov(position,mem, right.op)); (* real part *)
  9498. ReleaseIntermediateOperand(mem);
  9499. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9500. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9501. ReleaseIntermediateOperand(mem);
  9502. ReleaseOperand(right);
  9503. ELSE (* covers cases: pointer / record / array *)
  9504. parameter := procedureType.returnParameter;
  9505. checker.SetCurrentScope(currentScope);
  9506. ASSERT(parameter # NIL);
  9507. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9508. Assign(parameterDesignator,expression);
  9509. END;
  9510. ReleaseIntermediateOperand(left);
  9511. IF locked THEN Lock(FALSE) END;
  9512. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9513. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9514. parameter := procedureType.returnParameter;
  9515. checker.SetCurrentScope(currentScope);
  9516. IF parameter = NIL THEN
  9517. Error(procedure.position, "structured return of parameter of procedure not found");
  9518. ELSE
  9519. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9520. Assign(parameterDesignator,expression);
  9521. END;
  9522. IF locked THEN Lock(FALSE) END;
  9523. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9524. ELSE
  9525. HALT(200);
  9526. END;
  9527. ELSE
  9528. IF locked THEN Lock(FALSE) END;
  9529. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9530. END;
  9531. IF backend.cooperative THEN
  9532. BrL(exitLabel);
  9533. ELSE
  9534. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9535. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9536. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9537. ELSE
  9538. parametersSize := 0;
  9539. END;
  9540. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9541. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9542. END;
  9543. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9544. END VisitReturnStatement;
  9545. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9546. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9547. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9548. statements: SyntaxTree.StatementSequence;
  9549. name, suffix: SyntaxTree.IdentifierString;
  9550. BEGIN
  9551. Strings.IntToStr(awaitProcCounter,suffix);
  9552. Strings.Concat("@AwaitProcedure",suffix,name);
  9553. identifier := SyntaxTree.NewIdentifier(name);
  9554. INC(awaitProcCounter);
  9555. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9556. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9557. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9558. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9559. procedure.SetAccess(SyntaxTree.Hidden);
  9560. procedure.SetScope(currentScope);
  9561. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9562. procedureType.SetReturnType(system.booleanType);
  9563. procedure.SetType(procedureType);
  9564. body := SyntaxTree.NewBody(x.position,procedureScope);
  9565. procedureScope.SetBody(body);
  9566. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9567. returnStatement.SetReturnValue(x.condition);
  9568. statements := SyntaxTree.NewStatementSequence();
  9569. statements.AddStatement(returnStatement);
  9570. body.SetStatementSequence(statements);
  9571. currentScope.AddProcedure(procedure);
  9572. RETURN procedure
  9573. END MakeAwaitProcedure;
  9574. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9575. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9576. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9577. BEGIN
  9578. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9579. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9580. IF backend.cooperative THEN
  9581. start := NewLabel();
  9582. true := NewLabel();
  9583. false := NewLabel();
  9584. SetLabel(start);
  9585. Condition(x.condition,true,false);
  9586. SetLabel(false);
  9587. PushSelfPointer();
  9588. CallThis(position,"ExclusiveBlocks","Await",1);
  9589. BrL(start);
  9590. SetLabel(true);
  9591. PushSelfPointer();
  9592. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9593. ELSE
  9594. proc := MakeAwaitProcedure(x);
  9595. Emit(Push(position,fp));
  9596. GetCodeSectionNameForSymbol(proc,name);
  9597. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9598. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9599. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9600. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9601. Emit(Result(position,res));
  9602. (*
  9603. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9604. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9605. *)
  9606. InitOperand(result,ModeValue);
  9607. result.op := res;
  9608. label := NewLabel();
  9609. BreqL(label, result.op, SELF.true);
  9610. ReleaseOperand(result);
  9611. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9612. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9613. Emit(Push(position,res));
  9614. Emit(Push(position,fp));
  9615. PushSelfPointer();
  9616. Emit(Push(position,nil));
  9617. CallThis(position,"Objects","Await",4);
  9618. SetLabel(label);
  9619. END;
  9620. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9621. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9622. END VisitAwaitStatement;
  9623. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9624. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9625. BEGIN
  9626. FOR i := 0 TO x.Length() - 1 DO
  9627. statement := x.GetStatement( i );
  9628. Statement(statement);
  9629. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9630. END;
  9631. END StatementSequence;
  9632. PROCEDURE PushSelfPointer;
  9633. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9634. procedureType: SyntaxTree.ProcedureType;
  9635. BEGIN
  9636. scope := currentScope;
  9637. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9638. scope := scope.outerScope;
  9639. END;
  9640. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9641. IF ~newObjectFile THEN
  9642. Symbol(moduleSelf,op);
  9643. IntermediateCode.MakeMemory(op.op,addressType);
  9644. ELSE
  9645. moduleSection := meta.ModuleSection();
  9646. IF backend.cooperative THEN
  9647. moduleOffset := 0;
  9648. ELSE
  9649. moduleOffset := moduleSection.pc;
  9650. END;
  9651. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9652. END;
  9653. ELSE
  9654. GetBaseRegister(op.op,currentScope,scope);
  9655. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9656. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9657. parametersSize := ProcedureParametersSize(system,procedure);
  9658. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9659. IF backend.cooperative THEN
  9660. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9661. END;
  9662. IntermediateCode.MakeMemory(op.op,addressType);
  9663. END;
  9664. Emit(Push(position,op.op));
  9665. ReleaseOperand(op);
  9666. END PushSelfPointer;
  9667. PROCEDURE Lock(lock: BOOLEAN);
  9668. BEGIN
  9669. IF Trace THEN TraceEnter("Lock") END;
  9670. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9671. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9672. ASSERT(modifyAssignmentCounter = 0);
  9673. IF dump # NIL THEN
  9674. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9675. dump.Ln;dump.Update;
  9676. END;
  9677. PushSelfPointer;
  9678. IF backend.cooperative THEN
  9679. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9680. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9681. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9682. END;
  9683. ELSE
  9684. Emit(Push(position,true));
  9685. IF lock THEN CallThis(position,"Objects","Lock",2)
  9686. ELSE CallThis(position,"Objects","Unlock",2);
  9687. END;
  9688. END;
  9689. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9690. IF Trace THEN TraceExit("Lock") END;
  9691. END Lock;
  9692. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9693. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9694. BEGIN
  9695. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9696. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9697. previouslyUnchecked := isUnchecked;
  9698. isUnchecked := isUnchecked OR x.isUnchecked;
  9699. previouslyCooperativeSwitches := cooperativeSwitches;
  9700. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9701. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9702. IF x.statements # NIL THEN
  9703. StatementSequence(x.statements);
  9704. END;
  9705. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9706. isUnchecked := previouslyUnchecked;
  9707. cooperativeSwitches := previouslyCooperativeSwitches;
  9708. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9709. END VisitStatementBlock;
  9710. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9711. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9712. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9713. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9714. procedure: SyntaxTree.Procedure;
  9715. map: SymbolMap;
  9716. cc, parametersSize: LONGINT;
  9717. BEGIN
  9718. scope := currentScope;
  9719. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9720. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9721. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9722. return := emptyOperand;
  9723. IF Trace THEN TraceEnter("VisitCode") END;
  9724. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9725. NEW(in, x.inRules.Length());
  9726. FOR i := 0 TO LEN(in)-1 DO
  9727. statement := x.inRules.GetStatement(i);
  9728. WITH statement: SyntaxTree.Assignment DO
  9729. Evaluate(statement.right, operand);
  9730. result := operand.op;
  9731. NEW(str, 64);
  9732. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9733. in[i] := result; IntermediateCode.SetString(in[i], str);
  9734. ReleaseIntermediateOperand(operand.tag);
  9735. END;
  9736. END;
  9737. ELSE in := NIL
  9738. END;
  9739. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9740. NEW(out, x.outRules.Length());
  9741. FOR i := 0 TO LEN(out)-1 DO
  9742. statement := x.outRules.GetStatement(i);
  9743. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9744. WITH statement: SyntaxTree.Assignment DO
  9745. Designate(statement.left, operand);
  9746. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9747. NEW(str, 64);
  9748. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9749. out[i] := result; IntermediateCode.SetString(out[i], str);
  9750. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9751. |statement: SyntaxTree.ReturnStatement DO
  9752. NEW(str, 64);
  9753. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9754. IF currentIsInline THEN
  9755. map := currentMapper.Get(NIL);
  9756. Designate(map.to, operand);
  9757. IF map.isAddress THEN
  9758. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9759. ELSE
  9760. result := operand.op;
  9761. END;
  9762. (*! only if it does not fit into register
  9763. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9764. *)
  9765. (*Evaluate(map.to, operand);*)
  9766. out[i] := result;
  9767. ELSE
  9768. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9769. END;
  9770. IntermediateCode.SetString(out[i], str);
  9771. ReleaseIntermediateOperand(operand.tag);
  9772. return := out[i];
  9773. ELSE
  9774. END;
  9775. END;
  9776. ELSE out := NIL
  9777. END;
  9778. Emit(Asm(x.position,x.sourceCode, in, out));
  9779. IF in # NIL THEN
  9780. FOR i := 0 TO LEN(in)-1 DO
  9781. ReleaseIntermediateOperand(in[i]);
  9782. END;
  9783. END;
  9784. IF out # NIL THEN
  9785. FOR i := 0 TO LEN(out)-1 DO
  9786. WITH statement: SyntaxTree.Assignment DO
  9787. ReleaseIntermediateOperand(out[i]);
  9788. |statement: SyntaxTree.ReturnStatement DO
  9789. (* release happens below *)
  9790. ELSE
  9791. END;
  9792. statement := x.outRules.GetStatement(i);
  9793. END;
  9794. END;
  9795. IF return.mode # IntermediateCode.Undefined THEN
  9796. IF currentIsInline THEN
  9797. ELSIF SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  9798. Symbol(procedureType.returnParameter, par);
  9799. MakeMemory(mem, par.op, return.type, 0);
  9800. ReleaseOperand(par);
  9801. Emit(Mov(position, mem, return));
  9802. ReleaseIntermediateOperand(mem);
  9803. ELSE
  9804. Emit(Return(position,return));
  9805. END;
  9806. ReleaseIntermediateOperand(return);
  9807. IF currentIsInline THEN RETURN END;
  9808. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9809. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9810. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9811. ELSE
  9812. parametersSize := 0;
  9813. END;
  9814. EmitLeave(section, position,NIL, cc);
  9815. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9816. END;
  9817. IF Trace THEN TraceExit("VisitCode") END;
  9818. END VisitCode;
  9819. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9820. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9821. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9822. const, call: IntermediateCode.Operand;
  9823. parameterDesignator: SyntaxTree.Expression;
  9824. saved: RegisterEntry;
  9825. name: Basic.SegmentedName;
  9826. BEGIN
  9827. IF Trace THEN TraceEnter("ParameterCopies") END;
  9828. parameter := x.firstParameter;
  9829. WHILE parameter # NIL DO
  9830. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9831. type := parameter.type.resolved;
  9832. IF IsOpenArray(type) THEN
  9833. VisitParameter(parameter);
  9834. op := result;
  9835. IF backend.cooperative & parameter.NeedsTrace() THEN
  9836. length := GetArrayLength(type, op.tag);
  9837. size := NewRegisterOperand(addressType);
  9838. base := ArrayBaseType(type);
  9839. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9840. Emit(Mul(position, size, length, const));
  9841. dst := NewRegisterOperand (addressType);
  9842. Emit(Mov(position, dst, size));
  9843. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9844. Emit(Sub(position,dst,sp,dst));
  9845. Emit(And(position,dst,dst,const));
  9846. Emit(Mov(position,sp,dst));
  9847. par := fp;
  9848. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9849. IntermediateCode.InitImmediate(null, byteType, 0);
  9850. Emit(Fill(position, dst, size, null));
  9851. ReleaseIntermediateOperand(dst);
  9852. ReleaseIntermediateOperand(length);
  9853. SaveRegisters();ReleaseUsedRegisters(saved);
  9854. (* register dst has been freed before SaveRegisters already *)
  9855. base := ArrayBaseType(type);
  9856. (* assign method of open array *)
  9857. IF base.IsRecordType() THEN
  9858. Emit (Push(position, length));
  9859. Emit (Push(position, dst));
  9860. Emit (Push(position, op.op));
  9861. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9862. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9863. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9864. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9865. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9866. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9867. Emit (Push(position,length));
  9868. Emit (Push(position, dst));
  9869. Emit (Push(position, length));
  9870. Emit (Push(position, op.op));
  9871. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9872. ELSE
  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","AssignPointerArray", 4);
  9878. ASSERT(ArrayBaseType(type).IsPointer());
  9879. END;
  9880. RestoreRegisters(saved);
  9881. ELSE
  9882. temp := GetDynamicSize(type,op.tag);
  9883. ReuseCopy(size,temp);
  9884. ReleaseIntermediateOperand(temp);
  9885. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9886. Emit(Sub(position,size,sp,size));
  9887. Emit(And(position,size,size,const));
  9888. Emit(Mov(position,sp,size));
  9889. par := fp;
  9890. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9891. ReleaseIntermediateOperand(size);
  9892. size := GetDynamicSize(type,op.tag);
  9893. END;
  9894. Emit(Copy(position,sp,op.op,size));
  9895. ReleaseIntermediateOperand(size);
  9896. ReleaseOperand(op);
  9897. IntermediateCode.MakeMemory(par,addressType);
  9898. Emit(Mov(position,par,sp));
  9899. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9900. checker.SetCurrentScope(currentScope);
  9901. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9902. Assign(parameterDesignator,parameterDesignator);
  9903. END;
  9904. END;
  9905. parameter := parameter.nextParameter;
  9906. END;
  9907. IF Trace THEN TraceExit("ParameterCopies") END;
  9908. END ParameterCopies;
  9909. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9910. VAR x: SyntaxTree.Variable;
  9911. BEGIN
  9912. x := scope.firstVariable;
  9913. WHILE x # NIL DO
  9914. InitVariable(x);
  9915. x := x.nextVariable;
  9916. END;
  9917. END InitVariables;
  9918. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9919. BEGIN
  9920. IF (symbol # NIL) THEN
  9921. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9922. ELSE
  9923. RETURN 0
  9924. END;
  9925. END GetFingerprint;
  9926. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9927. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9928. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9929. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9930. name: Basic.SegmentedName;
  9931. offset: LONGINT;
  9932. BEGIN
  9933. IF Trace THEN TraceEnter("Body") END;
  9934. ReleaseUsedRegisters(saved); (* just in case ... *)
  9935. section := ir;
  9936. exitLabel := NewLabel ();
  9937. IF moduleBody THEN moduleBodySection := section END;
  9938. IF ir.comments # NIL THEN
  9939. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9940. commentPrintout.SingleStatement(TRUE);
  9941. dump := ir.comments;
  9942. ELSE
  9943. commentPrintout := NIL;
  9944. dump := NIL;
  9945. END;
  9946. prevScope := currentScope;
  9947. currentScope := scope;
  9948. lastSwitchPC := 0;
  9949. cooperativeSwitches := backend.cooperative;
  9950. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9951. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9952. IF x # NIL THEN
  9953. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9954. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9955. IF moduleBody THEN
  9956. ProfilerInit();
  9957. ELSE
  9958. Basic.SegmentedNameToString(ir.name, string);
  9959. ProfilerAddProcedure(numberProcedures,string);
  9960. ProfilerEnterExit(numberProcedures,TRUE);
  9961. END;
  9962. END;
  9963. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9964. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9965. END;
  9966. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9967. IF moduleBody & ~newObjectFile THEN
  9968. InitVariables(moduleScope)
  9969. END;
  9970. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9971. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9972. IF procedure = cellScope.bodyProcedure THEN
  9973. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9974. StaticCallOperand(op, cellScope.constructor);
  9975. Emit(Call(position,op.op,0));
  9976. END;
  9977. END;
  9978. END;
  9979. ParameterCopies(procedureType);
  9980. InitVariables(scope);
  9981. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9982. GetCodeSectionNameForSymbol(procedure, name);
  9983. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9984. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9985. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9986. IntermediateCode.SetOffset(right,offset); (* tag *)
  9987. IntermediateCode.InitMemory(left,addressType,fp,0);
  9988. Emit(Mov(position, left, right));
  9989. END;
  9990. IF x.code = NIL THEN
  9991. VisitStatementBlock(x);
  9992. ELSE
  9993. VisitCode(x.code)
  9994. END;
  9995. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9996. ir.SetFinally(ir.pc);
  9997. StatementSequence(x.finally)
  9998. END;
  9999. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  10000. IF ~backend.cooperative THEN
  10001. ProfilerEnterExit(numberProcedures,FALSE);
  10002. END;
  10003. INC(numberProcedures);
  10004. END;
  10005. END;
  10006. IF backend.cooperative THEN
  10007. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  10008. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  10009. END;
  10010. IF x # NIL THEN
  10011. SELF.position := x.position;
  10012. END;
  10013. CheckRegistersFree();
  10014. ASSERT(modifyAssignmentCounter = 0);
  10015. currentScope := prevScope;
  10016. IF Trace THEN TraceExit("Body") END;
  10017. END Body;
  10018. END ImplementationVisitor;
  10019. MetaDataGenerator=OBJECT
  10020. VAR
  10021. implementationVisitor: ImplementationVisitor;
  10022. declarationVisitor: DeclarationVisitor;
  10023. module: Sections.Module;
  10024. moduleName: ARRAY 128 OF CHAR;
  10025. moduleNamePool: Basic.HashTableInt;
  10026. moduleNamePoolSection: IntermediateCode.Section;
  10027. modulePointerSection: IntermediateCode.Section;
  10028. modulePointerSizePC: LONGINT;
  10029. modulePointerSectionOffset: LONGINT;
  10030. modulePointers: LONGINT;
  10031. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10032. RecordBaseOffset: LONGINT;
  10033. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10034. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10035. TypeTags: LONGINT; (* type extension level support *)
  10036. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10037. patchInfoPC: LONGINT;
  10038. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10039. BEGIN
  10040. IF implementationVisitor.backend.cooperative THEN
  10041. TypeTags := MAX(LONGINT);
  10042. BaseTypesTableOffset := 0;
  10043. MethodTableOffset := 2;
  10044. TypeRecordBaseOffset := 0;
  10045. RecordBaseOffset := 0;
  10046. ELSIF simple THEN
  10047. TypeTags := 3; (* only 3 extensions allowed *)
  10048. BaseTypesTableOffset := 1;
  10049. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10050. TypeRecordBaseOffset := 0;
  10051. RecordBaseOffset := 1;
  10052. ELSE
  10053. TypeTags := 16;
  10054. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10055. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10056. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10057. RecordBaseOffset := 8;
  10058. END;
  10059. SELF.simple := simple;
  10060. SELF.implementationVisitor := implementationVisitor;
  10061. SELF.declarationVisitor := declarationVisitor;
  10062. implementationVisitor.meta := SELF;
  10063. declarationVisitor.meta := SELF;
  10064. END InitMetaDataGenerator;
  10065. PROCEDURE SetModule(module: Sections.Module);
  10066. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10067. BEGIN
  10068. SELF.module := module;
  10069. Global.GetModuleName(module.module,moduleName);
  10070. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10071. NEW(moduleNamePool, 32);
  10072. (*! require GC protection *)
  10073. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10074. name := "Heaps.AnyPtr";
  10075. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10076. (* set base pointer *)
  10077. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10078. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10079. modulePointers := 0;
  10080. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10081. AddPointer(moduleNamePoolSection, namePoolOffset);
  10082. END;
  10083. END SetModule;
  10084. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10085. BEGIN
  10086. IF ~implementationVisitor.backend.cooperative THEN
  10087. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10088. INC(modulePointers);
  10089. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10090. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10091. END;
  10092. END AddPointer;
  10093. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10094. BEGIN
  10095. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10096. END GetTypeRecordBaseOffset;
  10097. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10098. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10099. BEGIN
  10100. INC(dataAdrOffset,6);
  10101. Info(section,"headerAdr");
  10102. Address(section,0);
  10103. Info(section,"typeDesc");
  10104. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10105. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10106. NamedSymbol(section, name, symbol, 0, offset);
  10107. Info(section,"mark: LONGINT;");
  10108. Longint(section,-1);
  10109. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10110. Info(section,"dataAdr-: ADDRESS");
  10111. Symbol(section,section, dataAdrOffset,0);
  10112. Info(section,"size-: SIZE");
  10113. Address(section,0);
  10114. Info(section,"nextRealtime: HeapBlock;");
  10115. Address(section,0);
  10116. END HeapBlock;
  10117. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10118. VAR i: LONGINT;
  10119. BEGIN
  10120. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10121. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10122. Info(section,"count*: LONGINT");
  10123. Longint(section,0);
  10124. Info(section,"locked*: BOOLEAN");
  10125. Longint(section,0);
  10126. Info(section,"awaitingLock*: ProcessQueue");
  10127. Address(section,0);
  10128. Address(section,0);
  10129. Info(section,"awaitingCond*: ProcessQueue");
  10130. Address(section,0);
  10131. Address(section,0);
  10132. Info(section,"lockedBy*: ANY");
  10133. Address(section,0);
  10134. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10135. Longint(section,1);
  10136. FOR i := 2 TO 6 DO
  10137. Longint(section,0);
  10138. END;
  10139. Info(section,"lock*: ANY");
  10140. Address(section,0);
  10141. END ProtectedHeapBlock;
  10142. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10143. BEGIN
  10144. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10145. END Info;
  10146. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10147. VAR op: IntermediateCode.Operand;
  10148. BEGIN
  10149. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10150. section.Emit(Data(Basic.invalidPosition,op));
  10151. END Address;
  10152. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10153. VAR op: IntermediateCode.Operand;
  10154. BEGIN
  10155. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10156. section.Emit(Data(Basic.invalidPosition,op));
  10157. END Size;
  10158. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10159. VAR op: IntermediateCode.Operand;
  10160. BEGIN
  10161. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10162. section.Emit(Data(Basic.invalidPosition,op));
  10163. END Set;
  10164. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10165. VAR op: IntermediateCode.Operand;
  10166. BEGIN
  10167. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10168. section.Emit(Data(Basic.invalidPosition,op));
  10169. END Longint;
  10170. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10171. VAR op,noOperand: IntermediateCode.Operand;
  10172. BEGIN
  10173. IntermediateCode.InitOperand(noOperand);
  10174. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10175. section.PatchOperands(pc,op,noOperand,noOperand);
  10176. END PatchAddress;
  10177. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10178. VAR op,noOperand: IntermediateCode.Operand;
  10179. BEGIN
  10180. IntermediateCode.InitOperand(noOperand);
  10181. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10182. section.PatchOperands(pc,op,noOperand,noOperand);
  10183. END PatchSize;
  10184. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10185. VAR op,noOperand: IntermediateCode.Operand;
  10186. BEGIN
  10187. IntermediateCode.InitOperand(noOperand);
  10188. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10189. section.PatchOperands(pc,op,noOperand,noOperand);
  10190. END PatchLongint;
  10191. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10192. VAR op, noOperand: IntermediateCode.Operand;
  10193. BEGIN
  10194. IntermediateCode.InitOperand(noOperand);
  10195. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10196. section.PatchOperands(pc,op,noOperand,noOperand);
  10197. END PatchSymbol;
  10198. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10199. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10200. BEGIN
  10201. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10202. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10203. section.Emit(Data(Basic.invalidPosition,op));
  10204. END Boolean;
  10205. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10206. VAR op: IntermediateCode.Operand;
  10207. BEGIN
  10208. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10209. section.Emit(Data(Basic.invalidPosition,op));
  10210. END Char;
  10211. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10212. VAR op: IntermediateCode.Operand;
  10213. BEGIN
  10214. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10215. section.Emit(Data(Basic.invalidPosition,op));
  10216. END Integer;
  10217. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10218. VAR i: LONGINT;
  10219. BEGIN
  10220. Info(section,str);
  10221. i := 0;
  10222. WHILE(str[i] # 0X) DO
  10223. Char(section,str[i]);
  10224. INC(i);
  10225. END;
  10226. Char(section,0X);
  10227. END String;
  10228. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10229. VAR s: Basic.SectionName;
  10230. BEGIN
  10231. StringPool.GetString(str, s);
  10232. String(section, s);
  10233. END String0;
  10234. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10235. VAR op: IntermediateCode.Operand;
  10236. BEGIN
  10237. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10238. IntermediateCode.SetOffset(op,realOffset);
  10239. section.Emit(Data(Basic.invalidPosition,op));
  10240. END NamedSymbol;
  10241. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10242. VAR op: IntermediateCode.Operand;
  10243. BEGIN
  10244. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10245. IntermediateCode.SetOffset(op,realOffset);
  10246. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10247. END NamedSymbolAt;
  10248. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10249. BEGIN
  10250. IF symbol= NIL THEN
  10251. Address( section, realOffset);
  10252. ASSERT(virtualOffset = 0);
  10253. ELSE
  10254. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10255. END;
  10256. END Symbol;
  10257. (* OutPointers delivers
  10258. {pointerOffset}
  10259. *)
  10260. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10261. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10262. BEGIN
  10263. type := type.resolved;
  10264. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10265. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10266. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10267. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10268. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10269. ELSIF type IS SyntaxTree.PointerType THEN
  10270. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10271. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10272. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10273. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10274. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10275. ELSIF (type IS SyntaxTree.RecordType) THEN
  10276. (* never treat a record like a pointer, even if the pointer field is set! *)
  10277. WITH type: SyntaxTree.RecordType DO
  10278. base := type.GetBaseRecord();
  10279. IF base # NIL THEN
  10280. Pointers(offset,symbol,section, base,numberPointers);
  10281. END;
  10282. variable := type.recordScope.firstVariable;
  10283. WHILE(variable # NIL) DO
  10284. IF ~(variable.untraced) THEN
  10285. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10286. END;
  10287. variable := variable.nextVariable;
  10288. END;
  10289. END;
  10290. ELSIF (type IS SyntaxTree.CellType) THEN
  10291. WITH type: SyntaxTree.CellType DO
  10292. base := type.GetBaseRecord();
  10293. IF base # NIL THEN
  10294. Pointers(offset,symbol,section, base,numberPointers);
  10295. END;
  10296. variable := type.cellScope.firstVariable;
  10297. WHILE(variable # NIL) DO
  10298. IF ~(variable.untraced) THEN
  10299. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10300. END;
  10301. variable := variable.nextVariable;
  10302. END;
  10303. property := type.firstProperty;
  10304. WHILE(property # NIL) DO
  10305. IF ~(property.untraced) THEN
  10306. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10307. END;
  10308. property := property.nextProperty;
  10309. END;
  10310. parameter := type.firstParameter;
  10311. WHILE(parameter # NIL) DO
  10312. IF ~(parameter.untraced) THEN
  10313. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10314. END;
  10315. parameter := parameter.nextParameter;
  10316. END;
  10317. END;
  10318. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10319. WITH type: SyntaxTree.ArrayType DO
  10320. IF type.form= SyntaxTree.Static THEN
  10321. n := type.staticLength;
  10322. base := type.arrayBase.resolved;
  10323. WHILE(base IS SyntaxTree.ArrayType) DO
  10324. type := base(SyntaxTree.ArrayType);
  10325. n := n* type.staticLength;
  10326. base := type.arrayBase.resolved;
  10327. END;
  10328. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10329. IF SemanticChecker.ContainsPointer(base) THEN
  10330. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10331. FOR i := 0 TO n-1 DO
  10332. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10333. END;
  10334. END;
  10335. ELSE
  10336. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10337. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10338. END;
  10339. END;
  10340. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10341. WITH type: SyntaxTree.MathArrayType DO
  10342. IF type.form = SyntaxTree.Static THEN
  10343. n := type.staticLength;
  10344. base := type.arrayBase.resolved;
  10345. WHILE(base IS SyntaxTree.MathArrayType) DO
  10346. type := base(SyntaxTree.MathArrayType);
  10347. n := n* type.staticLength;
  10348. base := type.arrayBase.resolved;
  10349. END;
  10350. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10351. IF SemanticChecker.ContainsPointer(base) THEN
  10352. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10353. FOR i := 0 TO n-1 DO
  10354. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10355. END;
  10356. END;
  10357. ELSE
  10358. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10359. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10360. END
  10361. END;
  10362. (* ELSE no pointers in type *)
  10363. END;
  10364. END Pointers;
  10365. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10366. VAR position,i: LONGINT; ch: CHAR;
  10367. BEGIN
  10368. IF pool.Has(index) THEN
  10369. RETURN pool.GetInt(index)
  10370. ELSE
  10371. position := source.pc;
  10372. pool.PutInt(index, position);
  10373. Info(source, name);
  10374. i := 0;
  10375. REPEAT
  10376. ch := name[i]; INC(i);
  10377. Char( source, ch);
  10378. UNTIL ch = 0X;
  10379. END;
  10380. RETURN position;
  10381. END EnterDynamicName;
  10382. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10383. VAR name: Basic.SectionName; position: LONGINT;
  10384. BEGIN
  10385. IF pool.Has(index) THEN
  10386. RETURN pool.GetInt(index)
  10387. ELSE
  10388. StringPool.GetString(index, name);
  10389. position := EnterDynamicName(source,name,index, pool);
  10390. END;
  10391. RETURN position;
  10392. END DynamicName;
  10393. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10394. VAR section: IntermediateCode.Section;
  10395. BEGIN
  10396. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10397. IF implementationVisitor.backend.cooperative THEN
  10398. Info(section, "TypeDescriptor");
  10399. Basic.ToSegmentedName("BaseTypes.Array", name);
  10400. NamedSymbol(section, name,NIL, 0, 0);
  10401. BasePointer(section);
  10402. offset := 0;
  10403. ELSE
  10404. HeapBlock(mName,typeName,section,2);
  10405. Info(section, "HeapBlock");
  10406. (*
  10407. Symbol(section,section,2,0);
  10408. *)
  10409. Address(section,0); (* empty such that GC does not go on traversing *)
  10410. Info(section, "TypeDescriptor");
  10411. Address(section,0);
  10412. offset := section.pc;
  10413. END;
  10414. RETURN section
  10415. END NamedBlock;
  10416. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10417. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10418. BEGIN
  10419. COPY(moduleName,name);
  10420. Strings.Append(name,suffix);
  10421. Basic.ToSegmentedName(name, pooledName);
  10422. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10423. END Block;
  10424. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10425. VAR name: Basic.SegmentedName;
  10426. BEGIN
  10427. Info(source,"ArrayHeader");
  10428. IF implementationVisitor.backend.cooperative THEN
  10429. sizePC := source.pc;
  10430. Address(source,0);
  10431. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10432. IF baseType # "" THEN
  10433. Basic.ToSegmentedName(baseType, name);
  10434. NamedSymbol(source, name,NIL, 0, 0);
  10435. ELSE
  10436. Address(source,0);
  10437. END;
  10438. Address(source,0);
  10439. ELSE
  10440. Address(source,0);
  10441. Address(source,0);
  10442. (* first pointer for GC *)
  10443. IF hasPointer THEN
  10444. (* points to first element in the array, this is NOT the base type descriptor *)
  10445. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10446. ELSE
  10447. Address(source,0);
  10448. END;
  10449. sizePC := source.pc;
  10450. Address(source,0);
  10451. Info(source,"array data");
  10452. END;
  10453. END ArrayBlock;
  10454. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10455. BEGIN
  10456. IF implementationVisitor.backend.cooperative THEN
  10457. PatchSize(section, pc, size);
  10458. PatchSize(section, pc + 3, size);
  10459. ELSE
  10460. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10461. PatchSize(section, pc, size);
  10462. END;
  10463. END PatchArray;
  10464. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10465. VAR
  10466. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10467. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10468. poolMap: Basic.HashTableInt;
  10469. namePool: IntermediateCode.Section;
  10470. namePoolOffset: LONGINT;
  10471. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10472. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10473. BEGIN
  10474. Basic.SegmentedNameToString(s1.name,n1);
  10475. Basic.SegmentedNameToString(s2.name,n2);
  10476. index := 0;
  10477. ch1 := n1[index];
  10478. ch2 := n2[index];
  10479. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10480. INC(index);
  10481. ch1 := n1[index];
  10482. ch2 := n2[index];
  10483. END;
  10484. RETURN ch1 < ch2;
  10485. END Compare;
  10486. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10487. VAR
  10488. i, j: LONGINT;
  10489. x, t: Sections.Section;
  10490. BEGIN
  10491. IF lo < hi THEN
  10492. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10493. WHILE i <= j DO
  10494. WHILE Compare(list[i], x) DO INC(i) END;
  10495. WHILE Compare(x, list[j]) DO DEC(j) END;
  10496. IF i <= j THEN
  10497. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10498. INC(i); DEC(j)
  10499. END
  10500. END;
  10501. IF lo < j THEN QuickSort(list, lo, j) END;
  10502. IF i < hi THEN QuickSort(list, i, hi) END
  10503. END;
  10504. END QuickSort;
  10505. (*
  10506. ExportDesc* = RECORD
  10507. fp*: ADDRESS;
  10508. name* {UNTRACED}: DynamicName;
  10509. adr*: ADDRESS;
  10510. exports*: LONGINT;
  10511. dsc* {UNTRACED}: ExportArray
  10512. END;
  10513. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10514. *)
  10515. PROCEDURE ExportDesc2(
  10516. source: IntermediateCode.Section;
  10517. namePool: IntermediateCode.Section;
  10518. fingerPrinter: FingerPrinter.FingerPrinter;
  10519. symbol: Sections.Section;
  10520. name: StringPool.Index;
  10521. VAR patchAdr: LONGINT
  10522. ): BOOLEAN;
  10523. VAR fingerPrint: SyntaxTree.FingerPrint;
  10524. BEGIN
  10525. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10526. & (symbol.type # Sections.InlineCodeSection)
  10527. THEN
  10528. *)
  10529. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10530. & (symbol.type # Sections.InlineCodeSection))
  10531. THEN
  10532. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10533. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10534. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10535. Longint(source,fingerPrint.shallow);
  10536. ELSE
  10537. Longint(source, 0);
  10538. END;
  10539. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10540. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10541. Symbol(source, symbol,0,0);
  10542. patchAdr := source.pc;
  10543. Longint(source, 0);
  10544. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10545. Address(source,0);
  10546. END;
  10547. RETURN TRUE
  10548. ELSE
  10549. RETURN FALSE
  10550. END;
  10551. END ExportDesc2;
  10552. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10553. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10554. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10555. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10556. nextPatch: LONGINT;
  10557. TYPE
  10558. Scope = RECORD
  10559. elements: LONGINT;
  10560. gelements: LONGINT;
  10561. section: IntermediateCode.Section;
  10562. patchAdr: LONGINT;
  10563. arraySizePC: LONGINT;
  10564. beginPC: LONGINT; (* current scope start pc *)
  10565. END;
  10566. BEGIN
  10567. Basic.InitSegmentedName(prev);
  10568. olevel := -1;
  10569. scopes[0].section := source;
  10570. scopes[0].arraySizePC := MIN(LONGINT);
  10571. FOR s := 0 TO LEN(sections)-1 DO
  10572. symbol := sections[s];
  10573. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10574. this := sections[s].name;
  10575. level := 0;
  10576. WHILE (this[level] > 0) DO
  10577. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10578. INC(level);
  10579. END;
  10580. WHILE level < olevel DO
  10581. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10582. IF olevel > 0 THEN
  10583. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10584. nextPatch := scopes[olevel-1].patchAdr+1;
  10585. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10586. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10587. END;
  10588. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10589. DEC(olevel);
  10590. END;
  10591. IF (this[level] > 0) THEN
  10592. IF level > olevel THEN
  10593. (*TRACE("opening",level); *)
  10594. IF scopes[level].section = NIL THEN
  10595. arrayName := ".@ExportArray";
  10596. Strings.AppendInt(arrayName, level);
  10597. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10598. AddPointer(scopes[level].section,offset);
  10599. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10600. END;
  10601. scopes[level].beginPC := scopes[level].section.pc;
  10602. olevel := level;
  10603. scopes[olevel].elements := 0;
  10604. END;
  10605. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10606. sym := sections[s];
  10607. ELSE
  10608. sym := NIL;
  10609. END;
  10610. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10611. THEN
  10612. INC(scopes[olevel].elements);
  10613. END;
  10614. (* enter string in scope *)
  10615. INC(level);
  10616. END;
  10617. END;
  10618. Basic.SegmentedNameToString(this, name);
  10619. prev := this;
  10620. END;
  10621. END;
  10622. WHILE 0 <= olevel DO
  10623. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10624. IF olevel > 0 THEN
  10625. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10626. nextPatch := scopes[olevel-1].patchAdr+1;
  10627. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10628. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10629. END;
  10630. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10631. DEC(olevel);
  10632. END;
  10633. level := 0;
  10634. WHILE (level < LEN(scopes)) DO
  10635. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10636. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10637. END;
  10638. INC(level);
  10639. END;
  10640. END Export;
  10641. BEGIN
  10642. NEW(fingerPrinter);
  10643. NEW(poolMap, 64);
  10644. (* 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 *)
  10645. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10646. NEW(sectionArray, module.allSections.Length());
  10647. FOR i := 0 TO module.allSections.Length() - 1 DO
  10648. section := module.allSections.GetSection(i);
  10649. sectionArray[i] := section;
  10650. END;
  10651. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10652. Export(sectionArray^);
  10653. END ExportDesc;
  10654. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10655. VAR
  10656. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10657. BEGIN
  10658. Info(source, "exception table offsets array descriptor");
  10659. size := 0;
  10660. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10661. Info(source, "exception table content");
  10662. FOR i := 0 TO module.allSections.Length() - 1 DO
  10663. p := module.allSections.GetSection(i);
  10664. IF p.type = Sections.CodeSection THEN
  10665. finallyPC := p(IntermediateCode.Section).finally;
  10666. IF finallyPC>=0 THEN
  10667. Symbol( source, p, 0,0);
  10668. Symbol( source, p, finallyPC, 0);
  10669. Symbol( source, p, finallyPC,0);
  10670. INC(size);
  10671. END;
  10672. END
  10673. END;
  10674. PatchArray(source,sizePC,size);
  10675. END ExceptionArray;
  10676. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10677. VAR i: LONGINT; ch: CHAR;
  10678. BEGIN
  10679. i := 0;
  10680. REPEAT
  10681. ch := name[i]; INC(i);
  10682. Char( section, ch);
  10683. UNTIL ch = 0X;
  10684. WHILE i < 32 DO
  10685. Char( section, 0X); INC(i);
  10686. END;
  10687. END Name;
  10688. PROCEDURE References(section: IntermediateCode.Section);
  10689. CONST
  10690. sfTypeNone = 0X;
  10691. sfTypeCHAR = 01X;
  10692. sfTypeCHAR8 = 02X;
  10693. sfTypeCHAR16 = 03X;
  10694. sfTypeCHAR32 = 04X;
  10695. sfTypeRANGE = 05X;
  10696. sfTypeSHORTINT = 06X;
  10697. sfTypeINTEGER = 07X;
  10698. sfTypeLONGINT = 08X;
  10699. sfTypeHUGEINT = 09X;
  10700. sfTypeWORD = 0AX;
  10701. sfTypeLONGWORD = 0BX;
  10702. sfTypeSIGNED8 = 0CX;
  10703. sfTypeSIGNED16 = 0DX;
  10704. sfTypeSIGNED32 = 0EX;
  10705. sfTypeSIGNED64 = 0FX;
  10706. sfTypeUNSIGNED8 = 10X;
  10707. sfTypeUNSIGNED16 = 11X;
  10708. sfTypeUNSIGNED32 = 12X;
  10709. sfTypeUNSIGNED64 = 13X;
  10710. sfTypeREAL = 14X;
  10711. sfTypeLONGREAL = 15X;
  10712. sfTypeCOMPLEX = 16X;
  10713. sfTypeLONGCOMPLEX = 17X;
  10714. sfTypeBOOLEAN = 18X;
  10715. sfTypeSET = 19X;
  10716. sfTypeANY = 1AX;
  10717. sfTypeOBJECT = 1BX;
  10718. sfTypeBYTE = 1CX;
  10719. sfTypeADDRESS = 1DX;
  10720. sfTypeSIZE = 1EX;
  10721. sfTypeIndirect = 1FX;
  10722. sfTypeRecord = 20X;
  10723. sfTypePointerToRecord = 21X;
  10724. sfTypePointerToArray = 22X;
  10725. sfTypeOpenArray = 23X;
  10726. sfTypeStaticArray = 24X;
  10727. sfTypeDynamicArray = 25X;
  10728. sfTypeMathStaticArray = 26X;
  10729. sfTypeMathOpenArray = 27X;
  10730. sfTypeMathTensor = 28X;
  10731. sfTypeDelegate = 29X;
  10732. sfTypeENUM = 2AX;
  10733. sfTypeCELL = 2BX;
  10734. sfTypePORT = 2CX;
  10735. sfIN = 0X;
  10736. sfOUT = 1X;
  10737. flagDelegate = 0;
  10738. flagConstructor = 1;
  10739. (* variable / parameter addressing modes *)
  10740. sfAbsolute = 0X; (* global vars *)
  10741. sfRelative = 1X; (* variables, value parameters *)
  10742. sfIndirect = 2X; (* var parameters *)
  10743. sfScopeBegin = 0F0X;
  10744. sfScopeEnd = 0F1X;
  10745. sfProcedure = 0F2X;
  10746. sfVariable = 0F3X;
  10747. sfTypeDeclaration = 0F4X;
  10748. sfModule = 0FFX;
  10749. RefInfo = TRUE;
  10750. VAR
  10751. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10752. indirectTypes: Basic.HashTable;
  10753. PROCEDURE CurrentIndex(): SIZE;
  10754. VAR i: LONGINT;
  10755. BEGIN
  10756. FOR i := startPC TO section.pc -1 DO
  10757. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10758. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10759. END;
  10760. startPC := section.pc;
  10761. RETURN lastOffset;
  10762. END CurrentIndex;
  10763. (*
  10764. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10765. Module = sfModule prevSymbol:SIZE name:String Scope.
  10766. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10767. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10768. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10769. Type =
  10770. sfTypePointerToRecord
  10771. | sfTypePointerToArray Type
  10772. | sfTypeOpenArray Type
  10773. | sfTypeDynamicArray Type
  10774. | sfTypeStaticArray length:SIZE Type
  10775. | sfTypeMathOpenArray Type
  10776. | sfTypeMathStaticArray length:SIZE Type
  10777. | sfTypeMathTensor Type
  10778. | sfTypeRecord tdAdr:ADDRESS
  10779. | sfTypeDelegate {Parameter} return:Type
  10780. | sfTypePort (sfIN | sfOUT)
  10781. | sfTypeBOOLEAN
  10782. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10783. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10784. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10785. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10786. | sfTypeWORD | sfTypeLONGWORD
  10787. | sfTypeREAL | sfTypeLONGREAL
  10788. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10789. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10790. | sfTypeIndirect offset:SIZE.
  10791. *)
  10792. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10793. VAR offset: SIZE;
  10794. BEGIN
  10795. IF indirectTypes.Has(type) THEN
  10796. offset := indirectTypes.GetInt(type);
  10797. Char(section, sfTypeIndirect);
  10798. Size(section, offset);
  10799. RETURN TRUE;
  10800. ELSE
  10801. indirectTypes.PutInt(type, CurrentIndex());
  10802. RETURN FALSE;
  10803. END;
  10804. END Indirect;
  10805. PROCEDURE NType(type: SyntaxTree.Type);
  10806. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10807. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10808. BEGIN
  10809. IF type = NIL THEN
  10810. Char(section, sfTypeNone)
  10811. ELSE
  10812. type := type.resolved;
  10813. size := type.sizeInBits;
  10814. WITH type:SyntaxTree.PointerType DO
  10815. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10816. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10817. Char(section, sfTypePointerToRecord);
  10818. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10819. ELSE
  10820. IF RefInfo THEN Info(section,"PointerToArray") END;
  10821. Char(section, sfTypePointerToArray);
  10822. NType(type.pointerBase);
  10823. END;
  10824. | type: SyntaxTree.ArrayType DO
  10825. IF ~Indirect(type) THEN
  10826. IF type.form = SyntaxTree.Open THEN
  10827. IF RefInfo THEN Info(section,"OpenArray") END;
  10828. Char(section, sfTypeOpenArray);
  10829. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10830. IF RefInfo THEN Info(section,"DynamicArray") END;
  10831. Char(section, sfTypeDynamicArray);
  10832. ELSIF type.form = SyntaxTree.Static THEN
  10833. IF RefInfo THEN Info(section,"StaticArray") END;
  10834. Char(section, sfTypeStaticArray);
  10835. Size(section, type.staticLength);
  10836. ELSE
  10837. HALT(100);
  10838. END;
  10839. NType(type.arrayBase);
  10840. END;
  10841. | type: SyntaxTree.MathArrayType DO
  10842. IF ~Indirect(type) THEN
  10843. IF type.form = SyntaxTree.Open THEN
  10844. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10845. Char(section, sfTypeMathOpenArray);
  10846. ELSIF type.form = SyntaxTree.Static THEN
  10847. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10848. Char(section, sfTypeMathStaticArray);
  10849. Size(section, type.staticLength);
  10850. ELSIF type.form = SyntaxTree.Tensor THEN
  10851. IF RefInfo THEN Info(section,"MathTensor") END;
  10852. Char(section, sfTypeMathTensor);
  10853. ELSE
  10854. HALT(100);
  10855. END;
  10856. NType(type.arrayBase);
  10857. END;
  10858. | type: SyntaxTree.RecordType DO
  10859. IF ~Indirect(type) THEN
  10860. IF type.pointerType # NIL (* OBJECT *) THEN
  10861. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10862. Char(section, sfTypePointerToRecord)
  10863. ELSE
  10864. IF RefInfo THEN Info(section,"Record") END;
  10865. Char(section, sfTypeRecord);
  10866. td := type.typeDeclaration;
  10867. IF RefInfo THEN Info(section,"TD") END;
  10868. IF (td # NIL) THEN
  10869. Global.GetSymbolSegmentedName(td,segmentedName);
  10870. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10871. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10872. ELSE
  10873. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10874. END;
  10875. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10876. Symbol(section, tir, 0, offset);
  10877. ELSE
  10878. Address(section, 0);
  10879. END;
  10880. END;
  10881. END;
  10882. | type: SyntaxTree.CellType DO
  10883. IF ~Indirect(type) THEN
  10884. IF RefInfo THEN Info(section,"Record") END;
  10885. Char(section, sfTypeRecord);
  10886. td := type.typeDeclaration;
  10887. IF RefInfo THEN Info(section,"TD") END;
  10888. IF (td # NIL) THEN
  10889. Global.GetSymbolSegmentedName(td,segmentedName);
  10890. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10891. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10892. ELSE
  10893. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10894. END;
  10895. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10896. Symbol(section, tir, 0, offset);
  10897. ELSE
  10898. Address(section, 0);
  10899. END;
  10900. END;
  10901. | type: SyntaxTree.PortType DO
  10902. Char(section, sfTypePORT);
  10903. IF type.direction = SyntaxTree.OutPort THEN
  10904. Char(section, sfOUT)
  10905. ELSE
  10906. Char(section, sfIN)
  10907. END;
  10908. | type: SyntaxTree.ProcedureType DO
  10909. IF ~Indirect(type) THEN
  10910. Char(section, sfTypeDelegate);
  10911. parameter := type.firstParameter;
  10912. WHILE(parameter # NIL) DO
  10913. NParameter(parameter, -1);
  10914. parameter := parameter.nextParameter;
  10915. END;
  10916. NType(type.returnType);
  10917. END;
  10918. | type:SyntaxTree.EnumerationType DO
  10919. Char(section, sfTypeENUM);
  10920. | type: SyntaxTree.BasicType DO
  10921. WITH type: SyntaxTree.BooleanType DO
  10922. IF RefInfo THEN Info(section,"Boolean") END;
  10923. Char(section, sfTypeBOOLEAN);
  10924. | type: SyntaxTree.CharacterType DO
  10925. IF type = module.system.characterType THEN
  10926. IF RefInfo THEN Info(section,"CHAR") END;
  10927. Char(section, sfTypeCHAR);
  10928. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10929. IF RefInfo THEN Info(section,"CHAR8") END;
  10930. Char(section, sfTypeCHAR8)
  10931. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10932. IF RefInfo THEN Info(section,"CHAR16") END;
  10933. Char(section, sfTypeCHAR16);
  10934. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10935. IF RefInfo THEN Info(section,"CHAR32") END;
  10936. Char(section, sfTypeCHAR32);
  10937. ELSE
  10938. HALT(100);
  10939. END;
  10940. |type: SyntaxTree.IntegerType DO
  10941. IF type(SyntaxTree.IntegerType).signed THEN
  10942. IF (type = module.system.shortintType) THEN
  10943. IF RefInfo THEN Info(section,"SHORTINT") END;
  10944. Char(section, sfTypeSHORTINT)
  10945. ELSIF (type = module.system.integerType) THEN
  10946. IF RefInfo THEN Info(section,"INTEGER") END;
  10947. Char(section, sfTypeINTEGER)
  10948. ELSIF (type = module.system.longintType) THEN
  10949. IF RefInfo THEN Info(section,"LONGINT") END;
  10950. Char(section, sfTypeLONGINT)
  10951. ELSIF (type = module.system.hugeintType) THEN
  10952. IF RefInfo THEN Info(section,"HUGEINT") END;
  10953. Char(section, sfTypeHUGEINT)
  10954. ELSIF (type = module.system.wordType) THEN
  10955. IF RefInfo THEN Info(section,"WORD") END;
  10956. Char(section, sfTypeWORD)
  10957. ELSIF (type = module.system.longWordType) THEN
  10958. IF RefInfo THEN Info(section,"LONGWORD") END;
  10959. Char(section, sfTypeLONGWORD);
  10960. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10961. IF RefInfo THEN Info(section,"SIGNED8") END;
  10962. Char(section, sfTypeSIGNED8)
  10963. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10964. IF RefInfo THEN Info(section,"SIGNED16") END;
  10965. Char(section, sfTypeSIGNED16)
  10966. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10967. IF RefInfo THEN Info(section,"SIGNED32") END;
  10968. Char(section, sfTypeSIGNED32)
  10969. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10970. IF RefInfo THEN Info(section,"SIGNED64") END;
  10971. Char(section, sfTypeSIGNED64)
  10972. ELSE
  10973. HALT(100);
  10974. END
  10975. ELSE (* unsigned *)
  10976. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10977. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10978. Char(section, sfTypeUNSIGNED8)
  10979. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10980. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10981. Char(section, sfTypeUNSIGNED16)
  10982. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10983. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10984. Char(section, sfTypeUNSIGNED32)
  10985. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10986. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10987. Char(section, sfTypeUNSIGNED64)
  10988. ELSE
  10989. HALT(100)
  10990. END
  10991. END;
  10992. | type: SyntaxTree.FloatType DO
  10993. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10994. IF RefInfo THEN Info(section,"REAL") END;
  10995. Char(section, sfTypeREAL);
  10996. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  10997. IF RefInfo THEN Info(section,"LONGREAL") END;
  10998. Char(section, sfTypeLONGREAL);
  10999. ELSE
  11000. HALT(100);
  11001. END;
  11002. |type: SyntaxTree.ComplexType DO
  11003. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  11004. IF RefInfo THEN Info(section,"COMPLEX") END;
  11005. Char(section, sfTypeCOMPLEX);
  11006. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  11007. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  11008. Char(section, sfTypeLONGCOMPLEX);
  11009. ELSE
  11010. HALT(100);
  11011. END;
  11012. |type:SyntaxTree.SetType DO
  11013. IF RefInfo THEN Info(section,"SET") END;
  11014. Char(section, sfTypeSET);
  11015. |type:SyntaxTree.AnyType DO
  11016. IF RefInfo THEN Info(section,"ANY") END;
  11017. Char(section, sfTypeANY);
  11018. |type:SyntaxTree.ObjectType DO
  11019. IF RefInfo THEN Info(section,"OBJECT") END;
  11020. Char(section, sfTypeOBJECT);
  11021. |type:SyntaxTree.ByteType DO
  11022. IF RefInfo THEN Info(section,"BYTE") END;
  11023. Char(section, sfTypeBYTE);
  11024. |type:SyntaxTree.RangeType DO
  11025. IF RefInfo THEN Info(section,"RANGE") END;
  11026. Char(section, sfTypeRANGE)
  11027. |type:SyntaxTree.AddressType DO
  11028. IF RefInfo THEN Info(section,"ADDRESS") END;
  11029. Char(section, sfTypeADDRESS)
  11030. |type:SyntaxTree.SizeType DO
  11031. IF RefInfo THEN Info(section,"SIZE") END;
  11032. Char(section, sfTypeSIZE)
  11033. ELSE
  11034. HALT(100)
  11035. END;
  11036. ELSE HALT(101);
  11037. END;
  11038. END;
  11039. END NType;
  11040. (*
  11041. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11042. *)
  11043. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11044. VAR pos: LONGINT; type: SyntaxTree.Type;
  11045. BEGIN
  11046. IF RefInfo THEN Info(section, "Parameter") END;
  11047. Char(section, sfVariable);
  11048. Size(section, procOffset);
  11049. String0(section, parameter.name);
  11050. type := parameter.type.resolved;
  11051. IF parameter.kind = SyntaxTree.VarParameter THEN
  11052. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11053. ELSE Char(section, sfIndirect)
  11054. END;
  11055. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11056. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11057. Char(section, sfIndirect);
  11058. ELSE
  11059. Char(section, sfRelative);
  11060. END;
  11061. ELSE
  11062. Char(section, sfRelative);
  11063. END;
  11064. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11065. NType(parameter.type);
  11066. END NParameter;
  11067. (*
  11068. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11069. *)
  11070. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11071. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11072. name: Basic.SegmentedName; flags: SET;
  11073. BEGIN
  11074. IF RefInfo THEN Info(section, "Procedure") END;
  11075. pos := CurrentIndex();
  11076. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11077. Char(section, sfProcedure);
  11078. Size(section, scopeOffset);
  11079. String0(section,procedure.name);
  11080. s := module.allSections.FindBySymbol(procedure);
  11081. Symbol(section,s,0,0); (* start *)
  11082. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11083. flags := {};
  11084. IF procedureType.isDelegate THEN
  11085. INCL(flags, flagDelegate);
  11086. END;
  11087. IF procedure.isConstructor THEN
  11088. INCL(flags, flagConstructor);
  11089. END;
  11090. Set(section, flags);
  11091. Global.GetSymbolSegmentedName(procedure,name);
  11092. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11093. IF RefInfo THEN Info(section, "Parameters") END;
  11094. parameter := procedureType.firstParameter;
  11095. WHILE(parameter # NIL) DO
  11096. NParameter(parameter, pos);
  11097. parameter := parameter.nextParameter;
  11098. END;
  11099. IF RefInfo THEN Info(section, "ReturnType") END;
  11100. NType(procedureType.returnType);
  11101. NScope(procedure.procedureScope, pos);
  11102. END NProcedure;
  11103. (*
  11104. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11105. *)
  11106. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11107. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11108. BEGIN
  11109. IF RefInfo THEN Info(section, "Variable") END;
  11110. pos := CurrentIndex();
  11111. Char(section, sfVariable);
  11112. Size(section, scopeOffset);
  11113. String0(section, variable.name);
  11114. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11115. Char(section, sfAbsolute);
  11116. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11117. NamedSymbol(section, sn,variable, 0,0);
  11118. ELSE
  11119. Char(section, sfRelative);
  11120. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11121. END;
  11122. NType(variable.type);
  11123. s := module.allSections.FindBySymbol(variable);
  11124. END NVariable;
  11125. (*
  11126. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11127. *)
  11128. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11129. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11130. BEGIN
  11131. IF typeDeclaration = NIL THEN RETURN END;
  11132. pos := CurrentIndex();
  11133. s := module.allSections.FindBySymbol(typeDeclaration);
  11134. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11135. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11136. Char(section, sfTypeDeclaration);
  11137. Size(section, scopeOffset);
  11138. String0(section, typeDeclaration.name);
  11139. declared := typeDeclaration.declaredType.resolved;
  11140. IF (declared IS SyntaxTree.PointerType) THEN
  11141. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11142. END;
  11143. WITH declared: SyntaxTree.RecordType DO
  11144. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11145. Symbol(section, s, 0, offset);
  11146. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11147. Basic.AppendToSegmentedName(name,".@Info");
  11148. s := module.allSections.FindByName(name);
  11149. IF s # NIL THEN (* does not work for coop *)
  11150. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11151. NScope(declared.recordScope, pos);
  11152. END;
  11153. |declared: SyntaxTree.CellType DO
  11154. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11155. Symbol(section, s, 0, offset);
  11156. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11157. Basic.AppendToSegmentedName(name,".@Info");
  11158. s := module.allSections.FindByName(name);
  11159. IF s # NIL THEN
  11160. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11161. NScope(declared.cellScope, pos);
  11162. END;
  11163. ELSE
  11164. Address(section, 0);
  11165. END;
  11166. END NTypeDeclaration;
  11167. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11168. VAR pos: LONGINT;
  11169. BEGIN
  11170. pos := CurrentIndex();
  11171. Char(section,sfModule);
  11172. Size(section, prevSymbol);
  11173. String0(section, module.name);
  11174. NScope(module.moduleScope, pos);
  11175. END NModule;
  11176. (*
  11177. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11178. *)
  11179. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11180. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11181. BEGIN
  11182. IF scope = NIL THEN RETURN END;
  11183. IF RefInfo THEN Info(section, "Scope") END;
  11184. Char(section, sfScopeBegin);
  11185. variable := scope.firstVariable;
  11186. WHILE (variable # NIL) DO
  11187. NVariable(variable, prevSymbol);
  11188. variable := variable.nextVariable;
  11189. END;
  11190. WITH scope: SyntaxTree.ModuleScope DO
  11191. bodyProcedure := scope.bodyProcedure;
  11192. |scope: SyntaxTree.RecordScope DO
  11193. bodyProcedure := scope.bodyProcedure;
  11194. ELSE
  11195. bodyProcedure := NIL;
  11196. END;
  11197. IF bodyProcedure # NIL THEN
  11198. NProcedure(bodyProcedure, prevSymbol)
  11199. END;
  11200. procedure := scope.firstProcedure;
  11201. WHILE procedure # NIL DO
  11202. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11203. procedure := procedure.nextProcedure;
  11204. END;
  11205. typeDeclaration := scope.firstTypeDeclaration;
  11206. WHILE typeDeclaration # NIL DO
  11207. NTypeDeclaration(typeDeclaration, prevSymbol);
  11208. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11209. END;
  11210. Char(section, sfScopeEnd); (* scope ends *)
  11211. END NScope;
  11212. BEGIN
  11213. NEW(indirectTypes, 32);
  11214. ArrayBlock(section,sizePC,"", FALSE);
  11215. IF ~implementationVisitor.backend.cooperative THEN
  11216. startPC := section.pc;
  11217. NModule(module.module, -1);
  11218. PatchArray(section,sizePC,CurrentIndex());
  11219. END;
  11220. END References;
  11221. (*
  11222. Command* = RECORD
  11223. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11224. name*: Name; (* name of the procedure *)
  11225. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11226. entryAdr* : ADDRESS; (* entry address of procedure *)
  11227. END;
  11228. *)
  11229. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11230. VAR
  11231. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11232. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11233. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11234. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11235. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11236. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11237. BEGIN
  11238. RETURN
  11239. (type = NIL) OR
  11240. (type.resolved IS SyntaxTree.RecordType) OR
  11241. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11242. (type.resolved IS SyntaxTree.AnyType);
  11243. END TypeAllowed;
  11244. BEGIN
  11245. numberParameters := procedureType.numberParameters;
  11246. RETURN
  11247. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11248. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11249. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11250. END GetProcedureAllowed;
  11251. PROCEDURE WriteType(type : SyntaxTree.Type);
  11252. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11253. name: Basic.SegmentedName; offset: LONGINT;
  11254. BEGIN
  11255. IF type = NIL THEN
  11256. Address(source,0);
  11257. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11258. Address(source,1);
  11259. ELSE
  11260. type := type.resolved;
  11261. IF type IS SyntaxTree.PointerType THEN
  11262. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11263. END;
  11264. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11265. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11266. name[0] := typeDeclaration.name; name[1] := -1;
  11267. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11268. ASSERT(section # NIL);
  11269. ELSE
  11270. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11271. (* TODO *)
  11272. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11273. END;
  11274. IF implementationVisitor.backend.cooperative THEN
  11275. offset := 0;
  11276. ELSE
  11277. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11278. END;
  11279. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11280. END;
  11281. END WriteType;
  11282. BEGIN
  11283. Info(source, "command array descriptor");
  11284. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11285. numberCommands := 0;
  11286. Info(source, "command array content");
  11287. FOR i := 0 TO module.allSections.Length() - 1 DO
  11288. p := module.allSections.GetSection(i);
  11289. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11290. procedure := p.symbol(SyntaxTree.Procedure);
  11291. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11292. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11293. procedure.GetName(name);
  11294. Name(source,name);
  11295. numberParameters := procedureType.numberParameters;
  11296. (* offset of type of first parameter *)
  11297. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11298. ELSE WriteType(procedureType.firstParameter.type)
  11299. END;
  11300. (* offset of type of return parameter *)
  11301. WriteType(procedureType.returnType);
  11302. (* command name *)
  11303. (* command code offset *)
  11304. Symbol(source,p,0,0);
  11305. INC(numberCommands);
  11306. IF Trace THEN
  11307. D.Ln;
  11308. END;
  11309. END;
  11310. END
  11311. END;
  11312. PatchArray(source,sizePC,numberCommands);
  11313. END CommandArray;
  11314. (* to prevent from double import of different module aliases *)
  11315. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11316. VAR i: SyntaxTree.Import;
  11317. BEGIN
  11318. i := module.module.moduleScope.firstImport;
  11319. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11320. i := i.nextImport;
  11321. END;
  11322. RETURN i = import
  11323. END IsFirstDirectOccurence;
  11324. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11325. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11326. BEGIN
  11327. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11328. ArrayBlock(source,pc,"", FALSE);
  11329. Info(source, "import module array data");
  11330. IF implementationVisitor.backend.cooperative THEN
  11331. offset := 0;
  11332. ELSE
  11333. IF module.system.addressType.sizeInBits = 64 THEN
  11334. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11335. ELSE
  11336. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11337. END;
  11338. END;
  11339. import := module.module.moduleScope.firstImport;
  11340. numberImports := 0;
  11341. WHILE import # NIL DO
  11342. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11343. Global.GetModuleSegmentedName(import.module,name);
  11344. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11345. NamedSymbol(source, name, NIL, 0, offset);
  11346. INC(numberImports);
  11347. END;
  11348. import := import.nextImport
  11349. END;
  11350. PatchArray(source,pc,numberImports);
  11351. END ImportsArray;
  11352. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11353. VAR
  11354. p: Sections.Section; sizePC, size, i: LONGINT;
  11355. BEGIN
  11356. Info(source, "Type info section");
  11357. size := 0;
  11358. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11359. FOR i := 0 TO module.allSections.Length() - 1 DO
  11360. p := module.allSections.GetSection(i);
  11361. WITH p: IntermediateCode.Section DO
  11362. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11363. Symbol(source,p,0,0);
  11364. INC(size);
  11365. END;
  11366. END
  11367. END;
  11368. PatchArray(source,sizePC,size);
  11369. END TypeInfoSection;
  11370. (*
  11371. ProcTableEntry* = RECORD
  11372. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11373. noPtr*: LONGINT;
  11374. END;
  11375. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11376. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11377. *)
  11378. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11379. VAR
  11380. numberPointers: SIZE;
  11381. procedure: SyntaxTree.Procedure;
  11382. BEGIN
  11383. Info(section,"pcFrom");
  11384. Symbol(section,procedureSection,0,0);
  11385. Info(section,"pcTo");
  11386. Symbol(section, procedureSection, procedureSection.pc, 0);
  11387. Info(section,"pointer to offsets array");
  11388. Symbol(section, section,section.pc+1,0);
  11389. Info(section,"offsets array");
  11390. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11391. PointerArray(section, procedure.procedureScope, numberPointers);
  11392. END ProcedureDescriptor;
  11393. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11394. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11395. BEGIN
  11396. name := procedureSection.name;
  11397. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11398. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11399. ProcedureDescriptor(dest, procedureSection);
  11400. Symbol(section, dest, offset, 0);
  11401. END ProcedureDescriptorPointer;
  11402. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11403. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11404. BEGIN
  11405. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11406. numberProcs := 0;
  11407. FOR i := 0 TO module.allSections.Length() - 1 DO
  11408. destination := module.allSections.GetSection(i);
  11409. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11410. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11411. INC(numberProcs);
  11412. END
  11413. END;
  11414. PatchArray(section, sizePC, numberProcs);
  11415. END ProcedureDescriptorArray;
  11416. (*
  11417. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11418. VAR
  11419. next*: Module; (** once a module is published, all fields are read-only *)
  11420. name*: Name;
  11421. init, published: BOOLEAN;
  11422. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11423. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11424. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11425. command*: POINTER TO ARRAY OF Command;
  11426. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11427. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11428. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11429. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11430. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11431. data*, code*: Bytes;
  11432. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11433. export*: ExportDesc;
  11434. term*: TerminationHandler;
  11435. exTable*: ExceptionTable;
  11436. noProcs*: LONGINT;
  11437. firstProc*: ADDRESS; <- done by loader
  11438. maxPtrs*: LONGINT;
  11439. crc*: LONGINT;
  11440. *)
  11441. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11442. BEGIN
  11443. Info(section, "cycle");
  11444. Size(section,0);
  11445. Info(section, "references");
  11446. Size(section,0);
  11447. Info(section, "nextMarked");
  11448. Address(section,0);
  11449. Info(section, "nextWatched");
  11450. Address(section,0);
  11451. END BasePointer;
  11452. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11453. BEGIN
  11454. BasePointer(section);
  11455. Info(section, "action");
  11456. Address(section,0);
  11457. Info(section, "monitor");
  11458. Address(section,0);
  11459. END BaseObject;
  11460. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11461. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11462. pooledName: Basic.SegmentedName;
  11463. symbol: SyntaxTree.Symbol;
  11464. BEGIN
  11465. Global.GetModuleName(module.module,name);
  11466. Strings.Append(name,".@Module.@Descriptor");
  11467. Basic.ToSegmentedName(name, pooledName);
  11468. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11469. Symbol(section,descriptorSection,0,0);
  11470. Info(descriptorSection, "descriptor");
  11471. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11472. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11473. Address(descriptorSection,0);
  11474. Global.GetModuleName(module.module,name);
  11475. Strings.Append(name,".@Trace");
  11476. Basic.ToSegmentedName(name, pooledName);
  11477. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11478. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11479. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11480. END ModuleDescriptor;
  11481. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11482. VAR name: ARRAY 128 OF CHAR;
  11483. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11484. symbol: SyntaxTree.Symbol;
  11485. BEGIN
  11486. Global.GetModuleName(module.module,name);
  11487. Strings.Append(name,".@Module");
  11488. Basic.ToSegmentedName(name, pooledName);
  11489. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11490. moduleSection.SetExported(TRUE);
  11491. IF moduleSection.pc = 0 THEN
  11492. IF implementationVisitor.backend.cooperative THEN
  11493. Info(moduleSection, "descriptor");
  11494. ModuleDescriptor(moduleSection);
  11495. BaseObject(moduleSection);
  11496. implementationVisitor.CreateTraceModuleMethod(module.module);
  11497. ELSE
  11498. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11499. Info(moduleSection, "HeapBlock");
  11500. Symbol(moduleSection,moduleSection,2,0);
  11501. Info(moduleSection, "TypeDescriptor");
  11502. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11503. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11504. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11505. END;
  11506. END;
  11507. RETURN moduleSection;
  11508. END ModuleSection;
  11509. PROCEDURE NewModuleInfo();
  11510. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11511. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11512. sectionName: Basic.SectionName;
  11513. CONST MPO=-40000000H;
  11514. BEGIN
  11515. (*
  11516. TypeDesc* = POINTER TO RECORD
  11517. descSize: LONGINT;
  11518. sentinel: LONGINT; (* = MPO-4 *)
  11519. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11520. flags*: SET;
  11521. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11522. name*: Name;
  11523. END;
  11524. *)
  11525. Global.GetSymbolSegmentedName(module.module,name);
  11526. Basic.AppendToSegmentedName(name,".@Info");
  11527. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11528. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11529. Address(source,MPO-4);
  11530. Info(source, "type tag pointer");
  11531. Address( source,0);
  11532. Info(source, "type flags");
  11533. flags := {};
  11534. Set( source, flags);
  11535. Info(source, "pointer to module");
  11536. moduleSection := ModuleSection();
  11537. Symbol( source, moduleSection, moduleSection.pc,0);
  11538. Info(source, "type name");
  11539. i := 0;
  11540. sectionName := "@Self";
  11541. (*
  11542. Global.GetSymbolSegmentedName(td,name);
  11543. Basic.SegmentedNameToString(name, sectionName);
  11544. *)
  11545. Name(source,sectionName);
  11546. source.SetReferenced(FALSE);
  11547. patchInfoPC := source.pc;
  11548. Size(source, 0);
  11549. END NewModuleInfo;
  11550. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11551. VAR
  11552. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11553. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11554. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11555. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11556. referenceSectionOffset : LONGINT;
  11557. name: Basic.SegmentedName; offset: LONGINT;
  11558. BEGIN
  11559. NewModuleInfo();
  11560. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11561. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11562. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11563. ImportsArray(importSection);
  11564. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11565. CommandArray(commandsSection);
  11566. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11567. ExceptionArray(exceptionSection);
  11568. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11569. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11570. TypeInfoSection(typeInfoSection);
  11571. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11572. References(referenceSection);
  11573. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11574. IF ~implementationVisitor.backend.cooperative THEN
  11575. ProcedureDescriptorArray(procTableSection, numberProcs);
  11576. name := "Heaps.AnyPtr";
  11577. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11578. (* set base pointer *)
  11579. NamedSymbolAt(procTableSection, procTableSectionOffset -1 , name, NIL, 0, offset);
  11580. END;
  11581. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11582. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11583. moduleSection := ModuleSection();
  11584. Info(moduleSection, "nextRoot*: RootObject");
  11585. Address(moduleSection,0);
  11586. Info(moduleSection, "next*: Module");
  11587. Address(moduleSection,0);
  11588. Info(moduleSection, "name*: Name");
  11589. Name(moduleSection,moduleName);
  11590. Info(moduleSection, "init, published: BOOLEAN");
  11591. Boolean(moduleSection,FALSE);
  11592. Boolean(moduleSection,FALSE);
  11593. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11594. Boolean(moduleSection,FALSE);
  11595. Boolean(moduleSection,FALSE);
  11596. Info(moduleSection, "refcnt*: LONGINT");
  11597. Longint(moduleSection,0);
  11598. Info(moduleSection, "sb*: ADDRESS");
  11599. Address(moduleSection,0);
  11600. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11601. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11602. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11603. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11604. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11605. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11606. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11607. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11608. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11609. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11610. Info(moduleSection, "procTable*: ProcTable");
  11611. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11612. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11613. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11614. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11615. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11616. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11617. Info(moduleSection, "export*: ExportDesc");
  11618. ExportDesc(moduleSection);
  11619. Info(moduleSection, "term*: TerminationHandler");
  11620. Address(moduleSection,0);
  11621. Info(moduleSection, "exTable*: ExceptionTable");
  11622. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11623. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11624. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11625. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11626. Info(moduleSection, "crc*: LONGINT");
  11627. Longint(moduleSection, 0); (*! must be implemented *)
  11628. Info(moduleSection, "body*: ADDRESS");
  11629. Symbol(moduleSection, bodyProc, 0,0);
  11630. IF implementationVisitor.backend.cooperative THEN
  11631. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11632. Info(moduleSection, "monitor.owner");
  11633. Address(moduleSection,0);
  11634. Info(moduleSection, "monitor.nestingLevel");
  11635. Address(moduleSection,0);
  11636. Info(moduleSection, "monitor.blockedQueue");
  11637. Address(moduleSection,0); Address(moduleSection,0);
  11638. Info(moduleSection, "monitor.waitingQueue");
  11639. Address(moduleSection,0); Address(moduleSection,0);
  11640. Info(moduleSection, "monitor.waitingSentinel");
  11641. Address(moduleSection,0);
  11642. END;
  11643. END Module;
  11644. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11645. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11646. BEGIN
  11647. ArrayBlock(source,pc,"",FALSE);
  11648. Info(source, "pointer offsets array data");
  11649. IF scope IS SyntaxTree.RecordScope THEN
  11650. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11651. ELSIF scope IS SyntaxTree.CellScope THEN
  11652. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11653. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11654. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11655. WHILE variable # NIL DO
  11656. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11657. symbol := module.allSections.FindBySymbol(variable);
  11658. ASSERT(symbol # NIL);
  11659. Pointers(0,symbol, source,variable.type,numberPointers);
  11660. END;
  11661. variable := variable.nextVariable;
  11662. END;
  11663. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11664. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11665. WHILE parameter # NIL DO
  11666. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11667. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11668. END;
  11669. parameter := parameter.nextParameter;
  11670. END;
  11671. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11672. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11673. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11674. IF implementationVisitor.backend.preciseGC THEN
  11675. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11676. END;
  11677. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11678. END;
  11679. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11680. WHILE(variable # NIL) DO
  11681. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11682. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11683. END;
  11684. variable := variable.nextVariable
  11685. END;
  11686. END;
  11687. PatchArray(source,pc,numberPointers);
  11688. END PointerArray;
  11689. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11690. VAR
  11691. name: Basic.SegmentedName;
  11692. section: IntermediateCode.Section;
  11693. BEGIN
  11694. ASSERT(implementationVisitor.newObjectFile);
  11695. Global.GetSymbolSegmentedName(symbol,name);
  11696. Basic.AppendToSegmentedName(name,suffix);
  11697. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11698. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11699. Info(section, "HeapBlock");
  11700. Address(section,0); (* empty such that GC does not go on traversing *)
  11701. Info(section, suffix);
  11702. Address(section,0);
  11703. pc := section.pc;
  11704. RETURN section;
  11705. END SymbolSection;
  11706. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11707. VAR recordType: SyntaxTree.RecordType;
  11708. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11709. section: Sections.Section; cellType: SyntaxTree.CellType;
  11710. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11711. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11712. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11713. sectionName: Basic.SectionName;
  11714. CONST MPO=-40000000H;
  11715. BEGIN
  11716. (*
  11717. TypeDesc* = POINTER TO RECORD
  11718. descSize: LONGINT;
  11719. sentinel: LONGINT; (* = MPO-4 *)
  11720. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11721. flags*: SET;
  11722. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11723. name*: Name;
  11724. END;
  11725. *)
  11726. (* source := module.sections.FindByName(...) *)
  11727. Global.GetSymbolSegmentedName(td,name);
  11728. Basic.AppendToSegmentedName(name,".@Info");
  11729. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11730. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11731. Address(source,MPO-4);
  11732. Info(source, "type tag pointer");
  11733. Symbol( source, tag, offset, 0);
  11734. Info(source, "type flags");
  11735. flags := {};
  11736. IF isProtected THEN INCL(flags,31) END;
  11737. Set( source, flags);
  11738. Info(source, "pointer to module");
  11739. moduleSection := ModuleSection();
  11740. Symbol( source, moduleSection, moduleSection.pc,0);
  11741. Info(source, "type name");
  11742. i := 0;
  11743. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11744. (*
  11745. Global.GetSymbolSegmentedName(td,name);
  11746. Basic.SegmentedNameToString(name, sectionName);
  11747. *)
  11748. Name(source,sectionName);
  11749. source.SetReferenced(FALSE);
  11750. Size(source, 0);
  11751. RETURN source;
  11752. END NewTypeDescriptorInfo;
  11753. PROCEDURE NewTypeDescriptor;
  11754. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11755. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11756. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11757. numberPointers: LONGINT; padding, i: LONGINT;
  11758. CONST MPO=-40000000H;
  11759. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11760. VAR i: LONGINT;
  11761. PROCEDURE Td(record: SyntaxTree.RecordType);
  11762. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11763. BEGIN
  11764. IF record # NIL THEN
  11765. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11766. baseTD := record.typeDeclaration;
  11767. Global.GetSymbolSegmentedName(baseTD,name);
  11768. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11769. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11770. ELSE
  11771. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11772. END;
  11773. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11774. Symbol(source, tir, 0, offset);
  11775. IF reverse THEN Td(record.GetBaseRecord()) END;
  11776. END;
  11777. END Td;
  11778. BEGIN
  11779. Info(source, "tag table");
  11780. baseRecord := recordType;
  11781. i := 0;
  11782. WHILE baseRecord # NIL DO
  11783. INC(i);
  11784. baseRecord := baseRecord.GetBaseRecord();
  11785. END;
  11786. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11787. IF ~reverse THEN Td(recordType) END;
  11788. WHILE i < size DO
  11789. Address(source,0);
  11790. INC(i);
  11791. END;
  11792. IF reverse THEN Td(recordType) END;
  11793. END TdTable;
  11794. PROCEDURE MethodTable(reverse: BOOLEAN);
  11795. VAR i,methods: LONGINT;
  11796. BEGIN
  11797. Info(source, "method table");
  11798. IF recordType # NIL THEN
  11799. methods := recordType.recordScope.numberMethods;
  11800. IF reverse THEN
  11801. FOR i := methods-1 TO 0 BY -1 DO
  11802. procedure := recordType.recordScope.FindMethod(i);
  11803. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11804. NamedSymbol(source, name,procedure, 0,0);
  11805. END;
  11806. ELSE
  11807. FOR i := 0 TO methods-1 DO
  11808. procedure := recordType.recordScope.FindMethod(i);
  11809. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11810. NamedSymbol(source, name,procedure, 0,0);
  11811. END;
  11812. END;
  11813. END;
  11814. END MethodTable;
  11815. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11816. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11817. BEGIN
  11818. Info(source, "method table");
  11819. baseRecord := recordType;
  11820. WHILE baseRecord.baseType # NIL DO
  11821. baseRecord := baseRecord.GetBaseRecord ();
  11822. END;
  11823. GetRecordTypeName (baseRecord, name);
  11824. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11825. IF name = stackFrame THEN
  11826. start := 0;
  11827. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11828. baseRecord := recordType;
  11829. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11830. baseRecord := baseRecord.GetBaseRecord ();
  11831. END;
  11832. IF baseRecord # NIL THEN
  11833. GetRecordTypeName (baseRecord, name);
  11834. IF pointer & ~baseRecord.isObject THEN
  11835. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11836. END;
  11837. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11838. ELSIF recordType.isObject THEN
  11839. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11840. ELSIF pointer THEN
  11841. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11842. ELSE
  11843. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11844. END;
  11845. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11846. Symbol(source, tir, 0, 0);
  11847. start := 0;
  11848. baseRecord := recordType;
  11849. WHILE (baseRecord # NIL) DO
  11850. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11851. baseRecord := baseRecord.GetBaseRecord ();
  11852. END;
  11853. ELSE
  11854. (* explicit trace method: *)
  11855. procedure := recordType.recordScope.FindMethod(0);
  11856. IF ~procedure.isFinalizer THEN
  11857. Global.GetSymbolSegmentedName(procedure, name);
  11858. NamedSymbol(source, name,procedure, 0,0);
  11859. END;
  11860. start := 1;
  11861. END;
  11862. IF (name # stackFrame) & recordType.isObject THEN
  11863. baseRecord := recordType;
  11864. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11865. baseRecord := baseRecord.GetBaseRecord ();
  11866. END;
  11867. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11868. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11869. ELSE
  11870. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11871. END;
  11872. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11873. Symbol(source, tir, 0, 0);
  11874. END;
  11875. methods := recordType.recordScope.numberMethods;
  11876. FOR i := start TO methods-1 DO
  11877. procedure := recordType.recordScope.FindMethod(i);
  11878. IF ~procedure.isFinalizer THEN
  11879. Global.GetSymbolSegmentedName(procedure, name);
  11880. NamedSymbol(source, name,procedure, 0,0);
  11881. END;
  11882. END;
  11883. END CooperativeMethodTable;
  11884. BEGIN
  11885. Global.GetSymbolSegmentedName(td,name);
  11886. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11887. source.SetExported(IsExported(td));
  11888. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11889. IF implementationVisitor.backend.cooperative THEN
  11890. base := NIL;
  11891. baseRecord := recordType.GetBaseRecord();
  11892. IF baseRecord # NIL THEN
  11893. baseTD := baseRecord.typeDeclaration;
  11894. END;
  11895. IF ~recordType.isObject THEN
  11896. Info(source, "parent");
  11897. IF baseRecord # NIL THEN
  11898. Global.GetSymbolSegmentedName(baseTD,name);
  11899. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11900. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11901. ELSE
  11902. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11903. END;
  11904. Symbol(source, tir, 0, 0);
  11905. ELSE
  11906. Address(source,0);
  11907. END;
  11908. Info(source, "record size");
  11909. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11910. source.SetReferenced(FALSE);
  11911. CooperativeMethodTable(FALSE);
  11912. base := source;
  11913. Global.GetSymbolSegmentedName(td,name);
  11914. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11915. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11916. source.SetExported(IsExported(td));
  11917. source.SetReferenced(FALSE);
  11918. END;
  11919. Info(source, "parent");
  11920. IF baseRecord # NIL THEN
  11921. Global.GetSymbolSegmentedName(baseTD,name);
  11922. sym := baseTD;
  11923. IF ~recordType.isObject THEN
  11924. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11925. sym := NIL;
  11926. END;
  11927. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11928. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11929. ELSE
  11930. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11931. END;
  11932. Symbol(source, tir, 0, 0);
  11933. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11934. Address(source,0);
  11935. ELSE
  11936. IF recordType.isObject THEN
  11937. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11938. ELSE
  11939. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11940. END;
  11941. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11942. Symbol(source, tir, 0, 0);
  11943. END;
  11944. Info(source, "base record descriptor");
  11945. Symbol(source, base, 0, 0);
  11946. CooperativeMethodTable(TRUE);
  11947. source.SetReferenced(FALSE);
  11948. IF recordType.hasPointers THEN
  11949. IF ~HasExplicitTraceMethod (recordType) THEN
  11950. implementationVisitor.CreateTraceMethod(recordType);
  11951. END;
  11952. implementationVisitor.CreateResetProcedure(recordType);
  11953. implementationVisitor.CreateAssignProcedure(recordType);
  11954. END;
  11955. ELSIF ~simple THEN
  11956. (*
  11957. MethodEnd = MPO
  11958. ---
  11959. methods (# methods)
  11960. ---
  11961. tags (16)
  11962. ---
  11963. TypeDesc = TypeInfoAdr
  11964. ---
  11965. td adr ---> rec size
  11966. ----
  11967. pointer offsets
  11968. ----
  11969. (padding)
  11970. -----
  11971. empty [2 addresses aligned]
  11972. empty
  11973. empty
  11974. numPtrs
  11975. ---
  11976. pointer offsets
  11977. ---
  11978. *)
  11979. Info(source, "MethodEnd = MPO");
  11980. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11981. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11982. MethodTable(TRUE);
  11983. TdTable(TypeTags, TRUE);
  11984. Info(source, "type descriptor info pointer");
  11985. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11986. IF (cellType # NIL) THEN
  11987. IF cellType.sizeInBits < 0 THEN
  11988. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11989. END;
  11990. Info(source, "cell size");
  11991. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11992. ELSE
  11993. Info(source, "record size");
  11994. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11995. END;
  11996. Info(source, "pointer offsets pointer");
  11997. padding := 1- source.pc MOD 2;
  11998. Symbol(source, source, source.pc+1+padding,0);
  11999. IF padding >0 THEN
  12000. Info(source, "padding");
  12001. FOR i := 1 TO padding DO Address(source,0) END;
  12002. END;
  12003. IF cellType # NIL THEN
  12004. PointerArray(source, cellType.cellScope, numberPointers);
  12005. ELSE
  12006. PointerArray(source, recordType.recordScope, numberPointers);
  12007. END;
  12008. ELSE
  12009. (*
  12010. simple:
  12011. td adr --> size
  12012. tag(1)
  12013. tag(2)
  12014. tag(3)
  12015. methods ->
  12016. *)
  12017. Info(source, "record size");
  12018. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12019. TdTable(TypeTags, FALSE);
  12020. MethodTable(FALSE);
  12021. source.SetReferenced(FALSE);
  12022. END;
  12023. END NewTypeDescriptor;
  12024. BEGIN
  12025. x := x.resolved;
  12026. IF (x IS SyntaxTree.PointerType) THEN
  12027. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12028. END;
  12029. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12030. recordType := x(SyntaxTree.RecordType);
  12031. td := x.typeDeclaration;
  12032. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12033. ASSERT(td # NIL);
  12034. section := module.allSections.FindBySymbol(td); (* TODO *)
  12035. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12036. IF implementationVisitor.newObjectFile THEN
  12037. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12038. NewTypeDescriptor
  12039. END;
  12040. ELSE
  12041. (* data section in intermediate code *)
  12042. Global.GetSymbolSegmentedName(td,name);
  12043. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12044. Basic.SuffixSegmentedName (name, module.module.name);
  12045. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12046. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12047. tir.Emit(Data(Basic.invalidPosition,op));
  12048. END;
  12049. END;
  12050. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12051. cellType := x(SyntaxTree.CellType);
  12052. td := x.typeDeclaration;
  12053. section := module.allSections.FindBySymbol(td); (* TODO *)
  12054. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12055. IF implementationVisitor.newObjectFile THEN
  12056. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12057. NewTypeDescriptor
  12058. END;
  12059. END;
  12060. END;
  12061. END
  12062. END CheckTypeDeclaration
  12063. END MetaDataGenerator;
  12064. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12065. VAR
  12066. trace-: BOOLEAN;
  12067. traceString-: SyntaxTree.IdentifierString;
  12068. traceModuleName-: SyntaxTree.IdentifierString;
  12069. newObjectFile-: BOOLEAN;
  12070. profile-: BOOLEAN;
  12071. noRuntimeChecks: BOOLEAN;
  12072. simpleMetaData-: BOOLEAN;
  12073. noAsserts: BOOLEAN;
  12074. optimize-: BOOLEAN;
  12075. cooperative-: BOOLEAN;
  12076. preregisterStatic-: BOOLEAN;
  12077. dump-: Basic.Writer;
  12078. cellsAreObjects: BOOLEAN;
  12079. preciseGC: BOOLEAN;
  12080. PROCEDURE &InitIntermediateBackend*;
  12081. BEGIN
  12082. simpleMetaData := FALSE;
  12083. newObjectFile := FALSE;
  12084. InitBackend;
  12085. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12086. SetTraceModuleName(DefaultTraceModuleName);
  12087. END InitIntermediateBackend;
  12088. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12089. VAR
  12090. declarationVisitor: DeclarationVisitor;
  12091. implementationVisitor: ImplementationVisitor;
  12092. module: Sections.Module;
  12093. name, platformName: SyntaxTree.IdentifierString;
  12094. meta: MetaDataGenerator;
  12095. BEGIN
  12096. ResetError;
  12097. Global.GetSymbolName(x,name);
  12098. NEW(module,x,system); (* backend structures *)
  12099. Global.GetModuleName(x, name);
  12100. module.SetModuleName(name);
  12101. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12102. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12103. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12104. declarationVisitor.Module(x,module);
  12105. IF newObjectFile & ~meta.simple THEN
  12106. meta.Module(implementationVisitor.moduleBodySection);
  12107. END;
  12108. GetDescription(platformName);
  12109. module.SetPlatformName(platformName);
  12110. RETURN module
  12111. END GenerateIntermediate;
  12112. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12113. BEGIN RETURN TRUE
  12114. END SupportedImmediate;
  12115. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12116. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12117. END ProcessSyntaxTreeModule;
  12118. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12119. VAR
  12120. result: Sections.Module;
  12121. traceName: Basic.MessageString;
  12122. BEGIN
  12123. ASSERT(intermediateCodeModule IS Sections.Module);
  12124. result := intermediateCodeModule(Sections.Module);
  12125. IF trace THEN
  12126. traceName := "intermediate code trace: ";
  12127. Strings.Append(traceName,traceString);
  12128. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12129. IF (traceString="") OR (traceString="*") THEN
  12130. result.Dump(dump);
  12131. dump.Update
  12132. ELSE
  12133. Sections.DumpFiltered(dump, result, traceString);
  12134. END
  12135. END;
  12136. RETURN result
  12137. END ProcessIntermediateCodeModule;
  12138. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12139. BEGIN instructionSet := "Intermediate";
  12140. END GetDescription;
  12141. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12142. BEGIN
  12143. SELF.newObjectFile := newObjectFile;
  12144. SELF.simpleMetaData := simpleMetaData;
  12145. END SetNewObjectFile;
  12146. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12147. BEGIN COPY(name, traceModuleName)
  12148. END SetTraceModuleName;
  12149. PROCEDURE DefineOptions(options: Options.Options);
  12150. BEGIN
  12151. DefineOptions^(options);
  12152. options.Add(0X,"trace",Options.String);
  12153. options.Add(0X,"runtime",Options.String);
  12154. options.Add(0X,"newObjectFile",Options.Flag);
  12155. options.Add(0X,"traceModule",Options.String);
  12156. options.Add(0X,"profile",Options.Flag);
  12157. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12158. options.Add(0X,"noAsserts",Options.Flag);
  12159. options.Add(0X,"metaData",Options.String);
  12160. options.Add('o',"optimize", Options.Flag);
  12161. options.Add(0X,"preregisterStatic", Options.Flag);
  12162. options.Add(0X,"cellsAreObjects", Options.Flag);
  12163. options.Add(0X,"preciseGC", Options.Flag);
  12164. END DefineOptions;
  12165. PROCEDURE GetOptions(options: Options.Options);
  12166. VAR name,string: SyntaxTree.IdentifierString;
  12167. BEGIN
  12168. GetOptions^(options);
  12169. trace := options.GetString("trace",traceString);
  12170. profile := options.GetFlag("profile");
  12171. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12172. noAsserts := options.GetFlag("noAsserts");
  12173. cooperative := options.GetFlag("cooperative");
  12174. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12175. IF cooperative THEN
  12176. SetRuntimeModuleName("CPU") END
  12177. END;
  12178. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12179. simpleMetaData := TRUE
  12180. END;
  12181. IF options.GetString("metaData",string) THEN
  12182. IF string = "simple" THEN simpleMetaData := TRUE
  12183. ELSIF string ="full" THEN simpleMetaData := FALSE
  12184. END;
  12185. END;
  12186. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12187. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12188. optimize := options.GetFlag("optimize");
  12189. preregisterStatic := options.GetFlag("preregisterStatic");
  12190. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12191. preciseGC := options.GetFlag("preciseGC");
  12192. END GetOptions;
  12193. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12194. BEGIN RETURN SymbolFileFormat.Get()
  12195. END DefaultSymbolFileFormat;
  12196. END IntermediateBackend;
  12197. (* ----------------------------------- register allocation ------------------------------------- *)
  12198. (* register mapping scheme
  12199. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12200. spill offset
  12201. part(n) --> ticket <--> physical register
  12202. spill offset
  12203. *)
  12204. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12205. emptyOperand: IntermediateCode.Operand;
  12206. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12207. statCoopResetVariables: LONGINT;
  12208. statCoopModifyAssignments: LONGINT;
  12209. modifyAssignmentsPC : LONGINT;
  12210. statCoopNilCheck: LONGINT;
  12211. statCoopSwitch: LONGINT;
  12212. statCoopAssignProcedure: LONGINT;
  12213. statCoopTraceMethod: LONGINT;
  12214. statCoopResetProcedure: LONGINT;
  12215. statCoopTraceModule: LONGINT;
  12216. PROCEDURE ResetStatistics*;
  12217. BEGIN
  12218. statCoopResetVariables := 0;
  12219. statCoopModifyAssignments := 0;
  12220. statCoopNilCheck:= 0;
  12221. statCoopSwitch:= 0;
  12222. statCoopAssignProcedure:= 0;
  12223. statCoopTraceMethod:= 0;
  12224. statCoopResetProcedure:= 0;
  12225. statCoopTraceModule:= 0;
  12226. END ResetStatistics;
  12227. PROCEDURE Statistics*;
  12228. BEGIN
  12229. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12230. TRACE(statCoopNilCheck, statCoopSwitch);
  12231. TRACE(statCoopAssignProcedure,
  12232. statCoopTraceMethod,
  12233. statCoopResetProcedure,
  12234. statCoopTraceModule)
  12235. END Statistics;
  12236. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12237. VAR h: LONGINT;
  12238. BEGIN
  12239. WHILE b # 0 DO
  12240. h := a MOD b;
  12241. a := b;
  12242. b := h;
  12243. END;
  12244. RETURN a
  12245. END GCD;
  12246. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12247. BEGIN
  12248. RETURN a*b DIV GCD(a,b)
  12249. END SCM;
  12250. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12251. BEGIN
  12252. (*TRACE(a,b);*)
  12253. IF a = 0 THEN RETURN b
  12254. ELSIF b = 0 THEN RETURN a
  12255. ELSE RETURN SCM(a,b)
  12256. END;
  12257. END CommonAlignment;
  12258. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12259. BEGIN
  12260. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12261. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12262. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12263. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12264. 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)
  12265. END
  12266. END PassBySingleReference;
  12267. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12268. BEGIN
  12269. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12270. END PassInRegister;
  12271. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12272. VAR new: RegisterEntry;
  12273. BEGIN
  12274. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12275. IF queue = NIL THEN
  12276. queue := new
  12277. ELSE
  12278. new.next := queue;
  12279. IF queue#NIL THEN queue.prev := new END;
  12280. queue := new
  12281. END;
  12282. END AddRegisterEntry;
  12283. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12284. VAR this: RegisterEntry;
  12285. BEGIN
  12286. this := queue;
  12287. WHILE (this # NIL) & (this.register # register) DO
  12288. this := this.next;
  12289. END;
  12290. IF this = NIL THEN
  12291. RETURN FALSE
  12292. END;
  12293. ASSERT(this # NIL);
  12294. IF this = queue THEN queue := queue.next END;
  12295. IF this.prev # NIL THEN this.prev.next := this.next END;
  12296. IF this.next # NIL THEN this.next.prev := this.prev END;
  12297. RETURN TRUE
  12298. END RemoveRegisterEntry;
  12299. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12300. BEGIN ASSERT(cond);
  12301. END Assert;
  12302. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12303. BEGIN
  12304. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12305. END ReusableRegister;
  12306. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12307. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12308. BEGIN
  12309. procedure := moduleScope.bodyProcedure;
  12310. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12311. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12312. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12313. procedure.SetScope(moduleScope);
  12314. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12315. procedure.SetAccess(SyntaxTree.Hidden);
  12316. moduleScope.SetBodyProcedure(procedure);
  12317. moduleScope.AddProcedure(procedure);
  12318. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12319. END;
  12320. END EnsureBodyProcedure;
  12321. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12322. VAR import: SyntaxTree.Import;
  12323. selfName: SyntaxTree.IdentifierString;
  12324. module: SyntaxTree.Module;
  12325. BEGIN
  12326. scope.ownerModule.GetName(selfName);
  12327. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12328. module := scope.ownerModule
  12329. ELSE
  12330. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12331. IF import = NIL THEN
  12332. RETURN NIL
  12333. ELSIF import.module = NIL THEN
  12334. RETURN NIL
  12335. ELSE module := import.module
  12336. END;
  12337. END;
  12338. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12339. END GetSymbol;
  12340. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12341. BEGIN
  12342. op.mode := mode;
  12343. IntermediateCode.InitOperand(op.op);
  12344. IntermediateCode.InitOperand(op.tag);
  12345. IntermediateCode.InitOperand(op.extra);
  12346. op.dimOffset := 0;
  12347. END InitOperand;
  12348. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12349. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12350. BEGIN RETURN IntermediateCode.GetType(system, type)
  12351. END GetType;
  12352. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12353. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12354. BEGIN
  12355. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12356. (*
  12357. UniqueId(name,module,name,adr);
  12358. *)
  12359. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12360. constant.SetValue(value);
  12361. constant.SetAccess(SyntaxTree.Hidden);
  12362. module.moduleScope.AddConstant(constant);
  12363. constant.SetScope(module.moduleScope);
  12364. RETURN constant
  12365. END BuildConstant;
  12366. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12367. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12368. BEGIN
  12369. variable := scope.firstVariable;
  12370. WHILE variable # NIL DO
  12371. IF variable.NeedsTrace() THEN
  12372. RETURN TRUE;
  12373. END;
  12374. variable := variable.nextVariable;
  12375. END;
  12376. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12377. WHILE parameter # NIL DO
  12378. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12379. RETURN TRUE;
  12380. END;
  12381. parameter := parameter.nextParameter;
  12382. END;
  12383. RETURN FALSE;
  12384. END HasPointers;
  12385. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12386. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12387. END IsVariableParameter;
  12388. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12389. VAR parameter: SyntaxTree.Parameter;
  12390. BEGIN
  12391. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12392. WHILE parameter # NIL DO
  12393. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12394. IF parameter.movable THEN RETURN TRUE END;
  12395. parameter := parameter.nextParameter;
  12396. END;
  12397. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12398. END HasVariableParameters;
  12399. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12400. BEGIN
  12401. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12402. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12403. END HasExplicitTraceMethod;
  12404. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12405. BEGIN
  12406. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12407. IF (expression IS SyntaxTree.IntegerValue) THEN
  12408. val := expression(SyntaxTree.IntegerValue).value;
  12409. RETURN TRUE
  12410. ELSE
  12411. RETURN FALSE
  12412. END;
  12413. END IsIntegerConstant;
  12414. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12415. BEGIN
  12416. IF val <= 0 THEN RETURN FALSE END;
  12417. exp := 0;
  12418. WHILE ~ODD(val) DO
  12419. val := val DIV 2;
  12420. INC(exp)
  12421. END;
  12422. RETURN val = 1
  12423. END PowerOf2;
  12424. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12425. VAR procedure: SyntaxTree.Procedure;
  12426. BEGIN
  12427. procedure := record.recordScope.constructor;
  12428. IF procedure = NIL THEN
  12429. record := record.GetBaseRecord();
  12430. IF record # NIL THEN
  12431. procedure := GetConstructor(record)
  12432. END;
  12433. END;
  12434. RETURN procedure;
  12435. END GetConstructor;
  12436. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12437. BEGIN
  12438. value := SHORT(op.intValue);
  12439. RETURN op.mode = IntermediateCode.ModeImmediate;
  12440. END IsIntegerImmediate;
  12441. (** whether a type strictily is a pointer to record or object type
  12442. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12443. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12444. BEGIN
  12445. IF type = NIL THEN
  12446. RETURN FALSE
  12447. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12448. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12449. ELSE
  12450. RETURN FALSE
  12451. END
  12452. END IsStrictlyPointerToRecord;
  12453. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12454. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12455. END IsUnsafePointer;
  12456. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12457. BEGIN type := type.resolved;
  12458. IF type IS SyntaxTree.PointerType THEN
  12459. type := type(SyntaxTree.PointerType).pointerBase;
  12460. type := type.resolved;
  12461. IF type IS SyntaxTree.RecordType THEN
  12462. recordType := type(SyntaxTree.RecordType);
  12463. RETURN TRUE
  12464. ELSE
  12465. RETURN FALSE
  12466. END
  12467. ELSIF type IS SyntaxTree.RecordType THEN
  12468. recordType := type(SyntaxTree.RecordType);
  12469. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12470. ELSIF type IS SyntaxTree.ObjectType THEN
  12471. RETURN TRUE
  12472. ELSIF type IS SyntaxTree.AnyType THEN
  12473. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12474. ELSE
  12475. RETURN FALSE
  12476. END;
  12477. END IsPointerToRecord;
  12478. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12479. BEGIN
  12480. type := type.resolved;
  12481. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12482. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12483. END IsArrayOfSystemByte;
  12484. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12485. BEGIN
  12486. IF type = NIL THEN RETURN FALSE END;
  12487. type := type.resolved;
  12488. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12489. END IsOpenArray;
  12490. PROCEDURE IsSemiDynamicArray(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.SemiDynamic);
  12495. END IsSemiDynamicArray;
  12496. PROCEDURE IsStaticArray(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.Static);
  12501. END IsStaticArray;
  12502. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12503. VAR size: LONGINT;
  12504. BEGIN
  12505. size := 1;
  12506. WHILE (IsStaticArray(type)) DO
  12507. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12508. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12509. END;
  12510. RETURN size;
  12511. END StaticArrayNumElements;
  12512. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12513. BEGIN
  12514. WHILE (IsStaticArray(type)) DO
  12515. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12516. END;
  12517. RETURN type;
  12518. END StaticArrayBaseType;
  12519. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12520. BEGIN
  12521. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12522. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12523. END;
  12524. RETURN type;
  12525. END ArrayBaseType;
  12526. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12527. BEGIN
  12528. IF type = NIL THEN RETURN FALSE END;
  12529. type := type.resolved;
  12530. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12531. END IsDelegate;
  12532. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12533. VAR i: LONGINT;
  12534. BEGIN
  12535. i := 0; type := type.resolved;
  12536. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12537. INC(i);
  12538. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12539. END;
  12540. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12541. INC(i);
  12542. type := type(SyntaxTree.MathArrayType).arrayBase;
  12543. IF type # NIL THEN type := type.resolved END;
  12544. END;
  12545. RETURN i
  12546. END DynamicDim;
  12547. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12548. BEGIN
  12549. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12550. type := type(SyntaxTree.ArrayType).arrayBase;
  12551. END;
  12552. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12553. type := type(SyntaxTree.MathArrayType).arrayBase;
  12554. END;
  12555. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12556. END StaticSize;
  12557. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12558. BEGIN
  12559. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12560. END IsImmediate;
  12561. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12562. BEGIN
  12563. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12564. END IsAddress;
  12565. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12566. BEGIN
  12567. RETURN (x.mode = IntermediateCode.ModeRegister);
  12568. END IsRegister;
  12569. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12570. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12571. BEGIN
  12572. typeDeclaration := recordType.typeDeclaration;
  12573. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12574. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12575. END GetRecordTypeName;
  12576. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12577. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12578. BEGIN
  12579. parSize := 0;
  12580. IF SemanticChecker.StructuredReturnType(procedureType) THEN
  12581. parameter := procedureType.returnParameter;
  12582. INC(parSize,system.SizeOfParameter(parameter));
  12583. parSize := parSize + (-parSize) MOD system.addressSize;
  12584. END;
  12585. parameter :=procedureType.lastParameter;
  12586. WHILE (parameter # NIL) DO
  12587. INC(parSize,system.SizeOfParameter(parameter));
  12588. parSize := parSize + (-parSize) MOD system.addressSize;
  12589. parameter := parameter.prevParameter;
  12590. END;
  12591. IF procedureType.selfParameter # NIL THEN
  12592. parameter := procedureType.selfParameter;
  12593. INC(parSize,system.SizeOfParameter(parameter));
  12594. parSize := parSize + (-parSize) MOD system.addressSize;
  12595. ELSIF procedureType.isDelegate THEN INC(parSize,system.addressSize)
  12596. END; (* method => self pointer *)
  12597. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12598. RETURN ToMemoryUnits(system,parSize)
  12599. END ParametersSize;
  12600. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12601. BEGIN
  12602. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12603. END IsNested;
  12604. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12605. BEGIN
  12606. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12607. scope := scope.outerScope;
  12608. END;
  12609. RETURN scope # NIL;
  12610. END InCellScope;
  12611. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12612. BEGIN
  12613. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12614. RETURN 0
  12615. ELSE
  12616. *)
  12617. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12618. (*END;*)
  12619. END ProcedureParametersSize;
  12620. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12621. VAR dataUnit: LONGINT;
  12622. BEGIN dataUnit := system.dataUnit;
  12623. ASSERT(size MOD system.dataUnit = 0);
  12624. RETURN size DIV system.dataUnit
  12625. END ToMemoryUnits;
  12626. PROCEDURE Get*(): Backend.Backend;
  12627. VAR backend: IntermediateBackend;
  12628. BEGIN NEW(backend); RETURN backend
  12629. END Get;
  12630. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12631. VAR instruction: IntermediateCode.Instruction;
  12632. BEGIN
  12633. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12634. RETURN instruction
  12635. END Nop;
  12636. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12637. VAR instruction: IntermediateCode.Instruction;
  12638. BEGIN
  12639. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12640. RETURN instruction
  12641. END Mov;
  12642. (* like Mov but ensures that no new register will be allocated for dest *)
  12643. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12644. VAR instruction: IntermediateCode.Instruction;
  12645. BEGIN
  12646. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12647. RETURN instruction
  12648. END MovReplace;
  12649. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12650. VAR instruction: IntermediateCode.Instruction;
  12651. BEGIN
  12652. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12653. RETURN instruction
  12654. END Conv;
  12655. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12656. BEGIN
  12657. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12658. END SysvABI;
  12659. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12660. BEGIN
  12661. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12662. END SysvABIorWINAPI;
  12663. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12664. VAR instruction: IntermediateCode.Instruction;
  12665. BEGIN
  12666. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12667. RETURN instruction
  12668. END Call;
  12669. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12670. VAR op1, op2, op3: IntermediateCode.Operand;
  12671. VAR instruction: IntermediateCode.Instruction;
  12672. BEGIN
  12673. IntermediateCode.InitNumber(op1,pcOffset);
  12674. IntermediateCode.InitNumber(op2,callingConvention);
  12675. IntermediateCode.InitNumber(op3,unwind);
  12676. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12677. RETURN instruction
  12678. END Exit;
  12679. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12680. VAR instruction: IntermediateCode.Instruction;
  12681. BEGIN
  12682. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12683. RETURN instruction
  12684. END Return;
  12685. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12686. VAR instruction: IntermediateCode.Instruction;
  12687. BEGIN
  12688. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12689. RETURN instruction
  12690. END Result;
  12691. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12692. VAR op1: IntermediateCode.Operand;
  12693. VAR instruction: IntermediateCode.Instruction;
  12694. BEGIN
  12695. IntermediateCode.InitNumber(op1,nr);
  12696. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12697. RETURN instruction
  12698. END Trap;
  12699. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12700. VAR instruction: IntermediateCode.Instruction;
  12701. BEGIN
  12702. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12703. RETURN instruction
  12704. END Br;
  12705. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12706. VAR instruction: IntermediateCode.Instruction;
  12707. BEGIN
  12708. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12709. RETURN instruction
  12710. END Breq;
  12711. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12712. VAR instruction: IntermediateCode.Instruction;
  12713. BEGIN
  12714. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12715. RETURN instruction
  12716. END Brne;
  12717. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12718. VAR instruction: IntermediateCode.Instruction;
  12719. BEGIN
  12720. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12721. RETURN instruction
  12722. END Brge;
  12723. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12724. VAR instruction: IntermediateCode.Instruction;
  12725. BEGIN
  12726. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12727. RETURN instruction
  12728. END Brlt;
  12729. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12730. VAR instruction: IntermediateCode.Instruction;
  12731. BEGIN
  12732. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12733. RETURN instruction
  12734. END Pop;
  12735. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12736. VAR instruction: IntermediateCode.Instruction;
  12737. BEGIN
  12738. ASSERT(op.mode # IntermediateCode.Undefined);
  12739. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12740. RETURN instruction
  12741. END Push;
  12742. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12743. VAR instruction: IntermediateCode.Instruction;
  12744. BEGIN
  12745. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12746. RETURN instruction
  12747. END Neg;
  12748. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12749. VAR instruction: IntermediateCode.Instruction;
  12750. BEGIN
  12751. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12752. RETURN instruction
  12753. END Not;
  12754. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12755. VAR instruction: IntermediateCode.Instruction;
  12756. BEGIN
  12757. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12758. RETURN instruction
  12759. END Abs;
  12760. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12761. VAR instruction: IntermediateCode.Instruction;
  12762. BEGIN
  12763. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12764. ASSERT(~IsImmediate(instruction.op1));
  12765. RETURN instruction
  12766. END Mul;
  12767. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12768. VAR instruction: IntermediateCode.Instruction;
  12769. BEGIN
  12770. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12771. RETURN instruction
  12772. END Div;
  12773. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12774. VAR instruction: IntermediateCode.Instruction;
  12775. BEGIN
  12776. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12777. RETURN instruction
  12778. END Mod;
  12779. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12780. VAR instruction: IntermediateCode.Instruction;
  12781. BEGIN
  12782. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12783. RETURN instruction
  12784. END Sub;
  12785. PROCEDURE Add(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.add,dest,left,right);
  12789. RETURN instruction
  12790. END Add;
  12791. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12792. VAR instruction: IntermediateCode.Instruction;
  12793. BEGIN
  12794. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12795. RETURN instruction
  12796. END And;
  12797. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12798. VAR instruction: IntermediateCode.Instruction;
  12799. BEGIN
  12800. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12801. RETURN instruction
  12802. END Or;
  12803. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12804. VAR instruction: IntermediateCode.Instruction;
  12805. BEGIN
  12806. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12807. RETURN instruction
  12808. END Xor;
  12809. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12810. VAR instruction: IntermediateCode.Instruction;
  12811. BEGIN
  12812. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12813. RETURN instruction
  12814. END Shl;
  12815. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12816. VAR instruction: IntermediateCode.Instruction;
  12817. BEGIN
  12818. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12819. RETURN instruction
  12820. END Shr;
  12821. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12822. VAR instruction: IntermediateCode.Instruction;
  12823. BEGIN
  12824. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12825. RETURN instruction
  12826. END Rol;
  12827. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12828. VAR instruction: IntermediateCode.Instruction;
  12829. BEGIN
  12830. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12831. RETURN instruction
  12832. END Ror;
  12833. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12834. VAR instruction: IntermediateCode.Instruction;
  12835. BEGIN
  12836. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12837. RETURN instruction
  12838. END Cas;
  12839. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12840. VAR instruction: IntermediateCode.Instruction;
  12841. BEGIN
  12842. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12843. RETURN instruction
  12844. END Copy;
  12845. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12846. VAR instruction: IntermediateCode.Instruction;
  12847. BEGIN
  12848. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12849. RETURN instruction
  12850. END Fill;
  12851. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12852. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12853. BEGIN
  12854. string := IntermediateCode.String(s);
  12855. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12856. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12857. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12858. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12859. RETURN instruction
  12860. END Asm;
  12861. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12862. VAR instruction: IntermediateCode.Instruction;
  12863. BEGIN
  12864. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12865. RETURN instruction
  12866. END Data;
  12867. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12868. VAR instruction: IntermediateCode.Instruction;
  12869. BEGIN
  12870. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12871. IntermediateCode.SetSubType(instruction, subtype);
  12872. RETURN instruction
  12873. END SpecialInstruction;
  12874. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12875. VAR op1: IntermediateCode.Operand;
  12876. VAR instruction: IntermediateCode.Instruction;
  12877. BEGIN
  12878. (*! generate a warning if size exceeds a certain limit *)
  12879. (*
  12880. ASSERT(bytes < 1000000); (* sanity check *)
  12881. *)
  12882. ASSERT(0 <= units); (* sanity check *)
  12883. IntermediateCode.InitNumber(op1,units);
  12884. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12885. RETURN instruction
  12886. END Reserve;
  12887. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12888. VAR op1: IntermediateCode.Operand;
  12889. VAR instruction: IntermediateCode.Instruction;
  12890. BEGIN
  12891. IntermediateCode.InitNumber(op1,position.start);
  12892. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12893. RETURN instruction
  12894. END LabelInstruction;
  12895. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12896. VAR pc: LONGINT;
  12897. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12898. BEGIN
  12899. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12900. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12901. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12902. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12903. IF instr.op1.type.form = IntermediateCode.Float THEN
  12904. RETURN instr.op1.floatValue = op.floatValue
  12905. ELSE
  12906. RETURN instr.op1.intValue = op.intValue
  12907. END;
  12908. END ProvidesValue;
  12909. BEGIN
  12910. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12911. pc := 0;
  12912. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12913. INC(pc);
  12914. END;
  12915. IF pc = data.pc THEN
  12916. data.Emit(Data(Basic.invalidPosition,vop));
  12917. END;
  12918. RETURN pc
  12919. END EnterImmediate;
  12920. PROCEDURE Init;
  12921. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12922. BEGIN
  12923. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12924. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12925. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12926. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12927. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12928. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12929. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12930. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12931. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12932. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12933. IntermediateCode.InitOperand(emptyOperand);
  12934. FOR i := 0 TO NumberSystemCalls-1 DO
  12935. name := "@SystemCall";
  12936. Basic.AppendNumber(name,i);
  12937. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12938. END;
  12939. END Init;
  12940. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12941. BEGIN
  12942. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12943. END IsExported;
  12944. BEGIN
  12945. Init;
  12946. END FoxIntermediateBackend.
  12947. Compiler.Compile FoxIntermediateBackend.Mod ~
  12948. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12949. # 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 ~
  12950. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12951. # 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 ~
  12952. FSTools.CloseFiles A2Z.exe ~
  12953. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12954. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12955. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12956. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12957. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12958. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12959. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12960. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12961. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12962. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12963. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod